# -*- encoding : utf-8 -*-

class Admin::VolumePeriodiquesController < ApplicationController
  layout "admin"
  before_action :auth_admin, :except => :etat
  
  before_action :admin_space
  
  
  def index
    @v_contacts = VContact.limit(10).all
  end
  
  
  def admin_space
    @admin_space = "statistiques"
    
    
  end
  
  
  def import
    
    @sql = File.read("#{Rails.root}/import_csv/vulcov2.sql").encode('utf-8')
    
    @sql = @sql.gsub("`volumeperiodique`", "`volume_periodiques`")
    @sql = @sql.gsub("`contact`", "`v_contacts`")
    @sql = @sql.gsub("`societe`", "`societes`")
    @sql = @sql.gsub("`seuilsdeclencheur`", "`seuil_declencheurs`")
    
    
    vars = ["CodeManagInn","CodeIndicateur","DateDebut","DateFin","CodeIndicateur","Valeur","DateReception","Commentaire","CodeSociete"]
    vars.each do |var|
      @sql = @sql.gsub(var,var.downcase)
    end
    
    
    
    vars = ["typeUser", "CodeManagInn", "NomGroupe", "Societe", "Civilite", "Nom", "Prenom", "Titre", "MailTo", "MailCC", "MailBCC", "NumPortable", "NumTel", "NumFax", "Adresse1", "Adresse2", "CodePostal", "Ville", "Anniversaire", "Pays", "Photo", "Login", "Password", "typeUser", "DateCreation", "Commentaire", "CodeProprietaire", "TypePartage", "libre1", "libre2", "libre3", "libre4", "libre5", "AccesBloque", "Corbeille", "CodeCreateur", "PartageDefaut", "NPAI", "NpaiMail", "NpaiSms"]
   
    vars.each do |var|
      @sql = @sql.gsub(var,var.downcase)
    end
    
    
    
    vars = ["SocManagInn", "Nom", "Adresse1", "Adresse2", "CodePostal", "Ville", "Pays", "NumTel", "NumFax", "Mobile", "Mail", "MailCc", "MailBcc", "SiteWeb", "Statut", "RC", "NAF", "Type", "Enseigne", "DateCreation", "Activite1", "Activite2", "Activite3", "Activite4", "Activite5", "Login", "Password", "Photo", "TypeUser", "CodeProprietaire", "TypePartage", "Libre1", "Libre2", "Libre3", "Libre4", "Libre5", "AccesBloque", "Corbeille", "CodeTpe", "CodeCreateur", "PartageDefaut", "NPAI", "NpaiMail", "NpaiSms"]
    vars.each do |var|
      @sql = @sql.gsub(var,var.downcase)
    end
    
    vars = ["CodeDeclencheur", "SocManagInn", "ValSeuil", "Cadeau", "Media", "ParamMedia", "Parammedia", "DateModif", "Expression", "DateDeb", "DateFin", "EnvoiAuto", "CopieMail", "Copiemail", "ValMax", "SaisieCom", "ValBA", "UniteBA", "BonusActif"]
    vars.each do |var|
      @sql = @sql.gsub(var,var.downcase)
    end
    
    File.write("#{Rails.root}/import_csv/vulco-ok-#{Time.now.to_s.to_slug}.sql", @sql)
    
    
    #ActiveRecord::Base.connection.execute(@sql)
    
    if false
      
      @file = params[:csv_file]
    
      require 'csv'    

      csv_text = File.read("#{Rails.root}/import_csv/volumeperiodique.csv").encode('utf-8') #.force_encoding('ISO-8859-1')
      @csv_volumeperiodique = CSV.parse(csv_text, :headers => true, :col_sep => ";")
    end
    
  end
  
  
  
  
  
end
