mmsc_app/app/controllers/admin/p_customers_controller.rb
Nicolas Bally f4ccdc82dc suite
2020-07-23 11:17:11 +02:00

653 lines
17 KiB
Ruby

# -*- encoding : utf-8 -*-
class Admin::PCustomersController < ApplicationController
layout "admin"
before_action :auth_admin, :except => :etat
before_action :admin_space
def send_mail
@p_customer = PCustomer.find(params[:id])
if params[:slug] == "stickers-manque-adresse"
@p_customer.particular_mail_sended = true
@p_customer.save
end
mail_hist = MailHist.auto_generate_mail(:fr, params[:slug].to_s, @p_customer.email, {:arguments => {}, :p_customer => @p_customer, :element => @p_customer})
end
def archive_import
@p_customer = PCustomer.find(params[:id])
end
def import3
if false #params[:force]
require "csv"
csv_text = File.read("#{Rails.root}/import_csv/exportremises.csv", :encoding => 'UTF-8')
@csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
end
end
def import
if false #params[:force]
require "csv"
csv_text = File.read("#{Rails.root}/import_csv/export clients.csv", :encoding => 'UTF-8')
@csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
PCustomer.destroy_all
@csv.each do |row|
if row["Nom"].to_s != ""
p_customer_cat = PCustomerCat.where(:name => row["SEGMENTATION"]).first
p_customer = PCustomer.new(:enabled => true, :imported => true)
p_customer.code = row["NEW_CODE"]
p_customer.accounting_zone_id = 1
p_customer.p_customer_cat = p_customer_cat
if p_commercial = PCommercial.where(:code => row["Représentant 1 code"]).first
else
p_commercial = PCommercial.new(:code => row["Représentant 1 code"])
end
p_customer.p_commercial = p_commercial
particular = p_customer.particulars.new(:open_range_ids => OpenRange.all.ids)
particular.organisation = row["Nom"]
particular.com_name = row["Enseigne"]
particular.name = row["Contact"]
address_2 = ""
if row["N° voie"].to_s != ""
address_2 += row["N° voie"].to_s+" "
end
if row["BTQ"].to_s != ""
address_2 += row["BTQ"].to_s+" "
end
address_2 += row["Nom voie"].to_s
particular.address_2 = address_2
particular.address_3 = row["Complément"]
particular.cp = row["CP"]
particular.city = row["Nom ville"]
particular.country = "FR"
if row["Tél 1"].to_s != ""
p_customer.p_contacts << PContact.new(:comment => "Import tel 1", :tel => row["Tél 1"])
end
if row["Tél 2"].to_s != ""
p_customer.p_contacts << PContact.new(:comment => "Import tel 2", :tel => row["Tél 2"])
end
if row["Portable"].to_s != ""
p_customer.p_contacts << PContact.new(:comment => "Import portable", :tel => row["Portable"])
end
if row["E-mail"].to_s != ""
p_customer.p_contacts << PContact.new(:comment => "Import email", :tel => row["E-mail"])
end
p_customer.siret = row["Siret"]
if row["Bloqué"] == "Oui"
p_customer.enabled = false
p_customer.disabled_raison = "Bloqué lors de l'import"
end
#p_customer.particular = particular
if row["Code règl"] == "CHQ"
p_customer.p_payment_type_id = 1
elsif row["Code règl"] == "LCR"
p_customer.p_payment_type_id = 2
elsif row["Code règl"] == "VIR"
p_customer.p_payment_type_id = 3
end
p_customer.import_created_at = row["Date création"]
p_customer.website = row["Site internet"]
p_customer.soumis_tva = row["Soumis TVA"]
if p_customer.save
puts p_customer.save
else
puts p_customer.errors.messages.to_s
sdffsd = sfdfd
end
end
end
render :inline => "ok"
end
end
def importold
if true
@file = params[:csv_file]
require 'csv'
csv_text = File.read("#{Rails.root}/import_csv/clients5.csv").force_encoding('ISO-8859-1').encode('utf-8')
@csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
end
end
def import_cat
if true
@file = params[:csv_file]
require 'csv'
csv_text = File.read("#{Rails.root}/import_csv/cat-clients.csv", :encoding => 'UTF-8')
@csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
end
end
def admin_space
if params[:offre].to_s == "true"
@admin_space = "ventes"
else
@admin_space = "clients"
end
end
def index
params[:search] = params[:search] || {}
@p_customers = PCustomer.all #.joins(:p_documents).all
@p_customers = @p_customers.includes(:particular)
@p_customers = sort_by_sorting(@p_customers)
if true
@fav_p_customers = PCustomer.where(:fav => true).order(:name).all
#params[:search][:enabled] = "Oui" if !params[:search][:enabled]
if params[:search][:enabled].to_s == "Oui"
@p_customers = @p_customers.where(:enabled => true)
elsif params[:search][:enabled].to_s == "Non"
@p_customers = @p_customers.where(:enabled => false)
end
params[:search][:test_user] = "Non" if !params[:search][:test_user]
if params[:search][:test_user].to_s == "Oui"
@p_customers = @p_customers.where(:test_user => true)
elsif params[:search][:test_user].to_s == "Non"
@p_customers = @p_customers.where(:test_user => false)
end
if params[:search][:npai].to_s == "Oui"
@p_customers = @p_customers.where(:npai => true)
elsif params[:search][:npai].to_s == "Non"
@p_customers = @p_customers.where(:npai => false)
end
if params[:search][:sticker].to_s == "Oui"
@p_customers = @p_customers.where(:sticker => true)
elsif params[:search][:sticker].to_s == "Non"
@p_customers = @p_customers.where(:sticker => false)
end
if params[:code].to_s != ""
@p_customers = @p_customers.where("p_customers.code LIKE ?","#{params[:code]}%")
end
if params[:name].to_s != ""
@p_customers = @p_customers.joins(:particulars).where("p_customers.code LIKE ? or particulars.organisation LIKE ? or particulars.name LIKE ? or particulars.firstname LIKE ?","%#{params[:name]}%", "%#{params[:name]}%", "%#{params[:name]}%", "%#{params[:name]}%")
end
if params[:email].to_s != ""
@p_customers = @p_customers.where("p_customers.email LIKE ?","%#{params[:email]}%")
end
if params[:city].to_s != ""
@p_customers = @p_customers.joins(:particulars).where("particulars.cp LIKE ? or particulars.city LIKE ?","%#{params[:city]}%", "%#{params[:city]}%")
end
if params[:tel].to_s != ""
@p_customers = @p_customers.joins(:particulars).joins("INNER JOIN `p_contacts` AS `p_contacts` ON ((`p_contacts`.`contactable_id` = `p_customers`.`id` AND `p_contacts`.`contactable_type` = 'PCustomer') OR (`p_contacts`.`contactable_id` = `particulars`.`id` AND `p_contacts`.`contactable_type` = 'Particular'))").where("p_contacts.tel LIKE ? or p_contacts.tel LIKE ?","%#{params[:tel]}%","%#{params[:tel]}%")
end
if params[:document_number].to_s != ""
@p_customers = @p_customers.joins(:p_documents).where("p_documents.d_number LIKE ?","%#{params[:document_number]}%")
end
#FP38057504
if params[:encours_min].to_s != ""
@p_customers = @p_customers.where("cache_encours_th >= ?", params[:encours_min])
end
if params[:encours_max].to_s != ""
@p_customers = @p_customers.where("cache_encours_th <= ?", params[:encours_max])
end
if params[:ca_min].to_s != ""
@p_customers = @p_customers.where("cache_ca >= ?", params[:ca_min])
end
if params[:ca_max].to_s != ""
@p_customers = @p_customers.where("cache_ca <= ?", params[:ca_max])
end
if params[:search][:p_customer_cat_id].to_s != ""
if params[:search][:p_customer_cat_id].to_s == "null"
@p_customers = @p_customers.where(:p_customer_cat_id => nil)
else
@p_customers = @p_customers.where(:p_customer_cat_id => params[:search][:p_customer_cat_id])
end
end
if params[:search][:p_commercial_id].to_s != ""
if params[:search][:p_commercial_id].to_s == "null"
@p_customers = @p_customers.where(:p_commercial_id => nil)
else
@p_customers = @p_customers.where(:p_commercial_id => params[:search][:p_commercial_id])
end
end
date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i
# params[:start] = Date.today.beginning_of_month.strftime('%d/%m/%Y') if !params[:start]
if params[:start] and params[:start] =~ date_regex
#fsfds = sdfsfd
@start = Date.parse(params[:start]).beginning_of_day
params[:start]= @start.strftime('%d/%m/%Y')
else
@start = nil
end
if true
# params[:stop] = Date.today.end_of_month.strftime('%d/%m/%Y') if !params[:stop]
if params[:stop].to_s != "" # and params[:stop] =~ date_regex
@stop = Date.parse(params[:stop]).end_of_day
params[:stop]= @stop.strftime('%d/%m/%Y')
else
@stop = nil
end
end
@p_customers = @p_customers.where("created_at >= ?", @start) if @start
@p_customers = @p_customers.where("created_at <= ?", @stop.end_of_day) if @stop
if current_admin.p_commercial
@p_customers = @p_customers.where(:p_commercial_id => current_admin.p_commercial.id )
end
@p_customers = @p_customers.distinct
respond_to do |format|
format.html{
params[:per_page] = params[:per_page] || 50
per_page = params[:per_page]
page = (params[:page] and params[:page] != "") ? params[:page] : 1
@p_customers = @p_customers.page(page).per(per_page)
}
format.csv {
@headers = []
@columns = []
PCustomer.qi_table_order.each do |key, value|
if value.instance_of? Hash
name = value[:name]
else
name = value
end
if name != "Actions"
@headers << name.to_s
@columns << key
end
end
@csv = CSV.generate(:headers => true, :col_sep => ";") do |csv|
csv << @headers
@p_customers.each do |p_customer|
line = []
@columns.each do |column|
if (p_customer.respond_to?("csv_"+column.to_s))
line << p_customer.send("csv_"+column.to_s)
elsif (p_customer.respond_to?(column))
if p_customer.send(column.to_s).class.to_s == "BigDecimal"
line << p_customer.send(column.to_s).to_s.gsub('.', ',')
else
line << p_customer.send(column.to_s)
end
else
line << column.to_s
end
end
csv << line
end
end
@data_to_send = @csv
send_data @data_to_send, :filename => "export-csv.csv" #, :type => 'text/csv; charset=iso-8859-1; header=present'
}
end
end
end
def show
@p_customer = PCustomer.find(params[:id])
end
def new
@p_customer = PCustomer.new()
@p_customer.p_payment_type_id = 41
@p_customer.particulars << Particular.new(:pro => true, :skip_validation => false, :open_range_ids => OpenRange.all.ids)
end
def edit
@p_customer = PCustomer.find(params[:id])
end
def npai
@p_customer = PCustomer.find(params[:id])
end
def toggle_npai
@p_customer = PCustomer.find(params[:id])
if @p_customer.npai
@p_customer.npai = false
message = "NPAI levé"
mail_hist = MailHist.auto_generate_mail(:fr, "stickers-npai-a-jour", @p_customer.email, {:arguments => {}, :p_customer => @p_customer, :element => @p_customer})
else
@p_customer.npai = true
message = "NPAI signalé"
mail_hist = MailHist.auto_generate_mail(:fr, "stickers-npai", @p_customer.email, {:arguments => {}, :p_customer => @p_customer, :element => @p_customer})
end
@p_customer.save
@p_customer.particular.generate_hist(message) if @p_customer.particular
redirect_back(fallback_location:"/")
end
def create
@p_customer = PCustomer.new(params.require(:p_customer).permit!)
if !current_admin.has_permission?("customers-activer-desactiver")
@p_customer.enabled = false
@p_customer.disabled_raison = "Pas encore vérifié"
end
if current_admin.p_commercial
@p_customer.p_commercial = current_admin.p_commercial
end
if @p_customer.save
redirect_to [:admin, @p_customer]
else
#@p_customer.code = nil
render action: "new"
end
end
def update
@p_customer = PCustomer.find(params[:id])
if @p_customer.update_attributes(params.require(:p_customer).permit!)
redirect_to [:admin, @p_customer]
else
render action: "edit"
end
end
def destroy
@p_customer = PCustomer.find(params[:id])
@p_customer.destroy
end
def autocomplete
@p_customers = PCustomer.for_search(params[:search]) #.limit(50)
respond_to do |format|
format.json { render json: @p_customers.map {|va| { id: va.id, member_label: va.member_label, show_name: va.show_name_extend }}.to_json( :only => [:id, :show_name, :member_label] ) }
end
end
def autocomplete_apercu
@p_customer = PCustomer.find(params[:id])
end
def etat
@p_customer = PCustomer.find(params[:id]) if params[:id]
date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i
if params[:start] and params[:start] =~ date_regex
@start = Date.parse(params[:start]).beginning_of_day
params[:start]= @start.strftime('%d/%m/%Y')
else
@start = nil
end
if params[:stop] and params[:stop] =~ date_regex
@stop = Date.parse(params[:stop]).end_of_day
params[:stop]= @stop.strftime('%d/%m/%Y')
else
@stop = nil
end
if !@p_customer and !@start
@start = Date.today.beginning_of_year
params[:start]= @start.strftime('%d/%m/%Y')
end
if @p_customer
@p_compta_elements = @p_customer.p_compta_elements.order("date DESC")
else
@p_compta_elements = PComptaElement.order("date DESC")
end
@p_compta_elements = @p_compta_elements.where("date >= ?", @start) if @start
@p_compta_elements = @p_compta_elements.where("date < ?", @stop.end_of_day) if @stop
params[:ok] = "0" if params[:ok].to_s == "" and !@p_customer
if params[:ok].to_s != ""
if params[:ok] == "1"
@p_compta_elements = @p_compta_elements.where(:solde => true)
else
@p_compta_elements = @p_compta_elements.where(:solde => false)
end
end
params[:inline] = true
if params[:pdf] == "true"
require 'posix/spawn'
doc_number = ""+params[:id].to_s
#url = adr_admin_p_ship_tour_path(:id => params[:id],:html => true)
url = url_for( request.query_parameters.merge({html: true, pdf: false, :only_path => true}))
puts "AAA"
puts url
@final_file = "#{Rails.root}/pdf/p_documents/etat-#{doc_number}.pdf"
@temp_file = "#{Rails.root}/pdf/p_documents/etat-#{doc_number}-temp.pdf"
url = (Rails.env.development? ? "http://localhost:4000" : "http://3p.quartz.xyz").to_s+url
puts url
pdf = "pdfportrait"
command = "node #{pdf}.js #{Shellwords.escape(url)} #{Shellwords.escape(@temp_file)}"
puts command
system(command)
::POSIX::Spawn::Child.new 'pdftk', @temp_file, 'stamp', "#{Rails.root}/pdf_stamp/en-tete.pdf", 'output', @final_file
@data_to_send = File.open( @final_file).read
send_data @data_to_send, :filename =>"#{doc_number}.pdf" , :type => 'application/pdf',:disposition => (params[:inline] ? 'inline' : "attachment")
else
render :layout => false if params[:html]
end
end
end