coton_app/app/controllers/admin/p_customers_controller.rb
Nicolas Bally 1b60863a55 suite
2020-03-19 23:10:02 +01:00

225 lines
4.9 KiB
Ruby

# -*- encoding : utf-8 -*-
class Admin::PCustomersController < ApplicationController
layout "admin"
before_filter :auth_admin, :except => "etat"
before_filter :admin_space
def import
if false
@file = params[:csv_file]
require 'csv'
csv_text = File.read("#{Rails.root}/import_csv/clients2.csv").force_encoding('ISO-8859-1')
@csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
end
end
def admin_space
@admin_space = "clients"
end
def index
@p_customers = PCustomer.order("created_at DESC").all
if params[:search][:name].to_s != ""
@p_customers = @p_customers.for_search(params[:search][:name])
end
per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50
page = (params[:page] and params[:page] != "") ? params[:page] : 1
@p_customers = @p_customers.page(page).per(per_page)
end
def show
@p_customer = PCustomer.find(params[:id])
end
def new
@p_customer = PCustomer.new()
@p_customer.particulars << Particular.new(:pro => true, :skip_validation => true)
end
def edit
@p_customer = PCustomer.find(params[:id])
end
def create
@p_customer = PCustomer.new(params.require(:p_customer).permit!)
if @p_customer.save
@p_customer.generate_mdp_now if @p_customer.generate_mdp and @p_customer.generate_mdp != "0"
else
render action: "new"
end
end
def update
@p_customer = PCustomer.find(params[:id])
if @p_customer.update_attributes(params.require(:p_customer).permit!)
@p_customer.generate_mdp_now if @p_customer.generate_mdp and @p_customer.generate_mdp != "0"
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, show_name: va.show_name }}.to_json( :only => [:id, :show_name] ) }
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://shop.biocoton.net").to_s+url
puts url
pdf = "pdf-2"
command = "node #{pdf}.js #{Shellwords.escape(url)} #{Shellwords.escape(@temp_file)} A4 3cm 3cm 1.5cm 1.5cm"
puts command
system(command)
::POSIX::Spawn::Child.new 'pdftk', @temp_file, 'stamp', "#{Rails.root}/pdf_stamp/en-tete2.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