# -*- encoding : utf-8 -*- class Admin::PCustomerSheetsController < ApplicationController layout "admin" before_action :auth_admin before_action :admin_space def admin_space @admin_space = "ventes" end def index @p_customer_sheets = PCustomerSheet.all if params[:search][:cc_com_counter].to_s != "" @p_customer_sheets = @p_customer_sheets.where("p_customer_sheets.cc_com_counter LIKE ?","%#{params[:search][:cc_com_counter]}%") end if params[:p_customer_sheet_ids].to_s != "" @p_customer_sheets = @p_customer_sheets.where(:id => params[:p_customer_sheet_ids]) end if current_admin.p_commercial @p_customer_sheets = @p_customer_sheets.where(:p_commercial_id => current_admin.p_commercial.id ) else if params[:search][:p_commercial_id].to_s != "" if params[:search][:p_commercial_id].to_s == "null" @p_customer_sheets = @p_customer_sheets.where(:p_commercial_id => nil) else @p_customer_sheets = @p_customer_sheets.where(:p_commercial_id => params[:search][:p_commercial_id]) end end end if params[:search][:state].to_s == "" @p_customer_sheets = @p_customer_sheets.where("state != 'Panier web'") end if params[:search][:state].to_s != "" @p_customer_sheets = @p_customer_sheets.where(:state => params[:search][:state]) 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_year.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_year.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_customer_sheets = @p_customer_sheets.where("p_customer_sheets.created_at >= ?", @start) if @start @p_customer_sheets = @p_customer_sheets.where("p_customer_sheets.created_at <= ?", @stop.end_of_day) if @stop #params[:wish_date_start] = Date.today.beginning_of_year.strftime('%d/%m/%Y') if !params[:wish_date_start] if params[:wish_date_start] and params[:wish_date_start] =~ date_regex #fsfds = sdfsfd @wish_date_start = Date.parse(params[:wish_date_start]).beginning_of_day params[:wish_date_start]= @wish_date_start.strftime('%d/%m/%Y') else @wish_date_start = nil end if true #params[:wish_date_stop] = Date.today.end_of_year.strftime('%d/%m/%Y') if !params[:wish_date_stop] if params[:wish_date_stop].to_s != "" # and params[:wish_date_stop] =~ date_regex @wish_date_stop = Date.parse(params[:wish_date_stop]).end_of_day params[:wish_date_stop]= @wish_date_stop.strftime('%d/%m/%Y') else @wish_date_stop = nil end end @p_customer_sheets = @p_customer_sheets.where("price_line_blocks.wish_date >= ?", @wish_date_start) if @wish_date_start @p_customer_sheets = @p_customer_sheets.where("price_line_blocks.wish_date <= ?", @wish_date_stop.end_of_day) if @wish_date_stop @p_customer_sheets = @p_customer_sheets.joins(:price_line_block) @p_customer_sheets = sort_by_sorting(@p_customer_sheets, "p_customer_sheets.created_at DESC") respond_to do |format| format.html{ params[:search][:per_page] = params[:search][:per_page] || 50 per_page = params[:search][:per_page] page = (params[:page] and params[:page] != "") ? params[:page] : 1 @all_p_customer_sheets = @p_customer_sheets @p_customer_sheets = @p_customer_sheets.page(page).per(per_page) } end end def show @p_customer_sheet = PCustomerSheet.find(params[:id]) end def new @p_customer_sheet = PCustomerSheet.new() @p_customer = PCustomer.find(params[:p_customer_id]) if params[:p_customer_id] @p_customer_sheet.p_customer = @p_customer @p_customer_sheet.price_line_block = PriceLineBlock.new @p_customer_sheet.price_line_block.price_lines.build @p_customer_sheet.price_line_block.p_customer = @p_customer @p_customer_sheet.price_line_block.build_particular_send(owner: @p_customer) @p_customer_sheet.price_line_block.build_particular_bill(owner: @p_customer) end def edit @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer = @p_customer_sheet.p_customer end def create @p_customer_sheet = PCustomerSheet.new(params.require(:p_customer_sheet).permit!) @p_customer_sheet.admin = current_admin if @p_customer_sheet.save redirect_to admin_p_customer_sheet_path(@p_customer_sheet) else puts "ERRORSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" puts params puts @p_customer_sheet.errors.messages puts "ERRORSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" @p_customer = @p_customer_sheet.price_line_block.p_customer render action: "new" end end def update @p_customer_sheet = PCustomerSheet.find(params[:id]) if @p_customer_sheet.update_attributes(params.require(:p_customer_sheet).permit!) @p_customer = @p_customer_sheet.price_line_block.p_customer respond_to do |format| format.html{redirect_to admin_p_customer_sheet_path(@p_customer_sheet)} format.js{} end else render action: "edit" end end def destroy @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.destroy end def generate_d @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.generate_d redirect_back(fallback_location: admin_p_customer_sheets_path) end def generate_fp @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.generate_fp redirect_back(fallback_location: admin_p_customer_sheets_path) end def generate_bc @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.generate_bc redirect_back(fallback_location: admin_p_customer_sheets_path) end def generate_bl @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.generate_bl redirect_back(fallback_location: admin_p_customer_sheets_path) end def generate_f @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.generate_f redirect_back(fallback_location: admin_p_customer_sheets_path) end def reject @p_customer_sheet = PCustomerSheet.find(params[:id]) @p_customer_sheet.state = "refusée" @p_customer_sheet.save @p_customer_sheet.archive_now if !@p_customer_sheet.archived redirect_back(fallback_location: admin_p_customer_sheets_path) end def block_price @p_customer_sheet = PCustomerSheet.find(params[:id]) if @p_customer_sheet.price_blocked @p_customer_sheet.unblock_price else @p_customer_sheet.block_price end redirect_back(fallback_location: admin_p_customer_sheets_path) end def export_xls @p_customer_sheets = PCustomerSheet.all if params[:p_customer_sheet_ids].present? @p_customer_sheets = PCustomerSheet.where(id: params[:p_customer_sheet_ids]) end respond_to do |format| format.csv { @headers = ["BUID", "DATE", "NUMERO", "CLIENT", "REFERENCE", "EAN", "DESIGNATION", "QTE", "PRIX 1", "PRIX 2", "DEEE", "SORECOP", "FOURNISSEUR", "CODE ARTICLE MAG", "NomMarque", "Code Douanier", "Type SORECOP"] @columns = [] # PCustomerSheet.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 xlsx_package = Axlsx::Package.new wb = xlsx_package.workbook wb.add_worksheet(name: "BASIC extract to Chronopost") do |sheet| sheet.add_row @headers @p_customer_sheets.each do |p_customer_sheet| p_customer_sheet.price_line_block.price_lines.each do |price_line| line = [] line << price_line.id line << p_customer_sheet.created_at.strftime("%d/%d/%Y") line << p_customer_sheet.id line << p_customer_sheet.p_customer.code + " - " + p_customer_sheet.p_customer.particulars.first.organisation line << price_line.p_product_ref.cc_name # line << price_line.p_product_ref.cc_ean line << price_line.p_product_ref.ean line << price_line.p_product_ref.cc_name line << price_line.qte line << price_line.cc_price_u_ht line << price_line.ct_u_price_ht line << price_line.p_product_ref.deee line << price_line.p_product_ref.sorecop # line << price_line.p_product_ref.referal_fournisseur line << "Fournisseur" line << "" line << price_line.p_product_ref.p_product.s_brand.name line << "Code douanier" line << price_line.p_product_ref.sorecop_comment sheet.add_row line, types: line.map{|t| cell_type_from_value(t)} # puts line end # @columns.each do |column| # if (p_customer_sheet.respond_to?("csv_"+column.to_s)) # line << p_customer_sheet.send("csv_"+column.to_s) # elsif (p_customer_sheet.respond_to?(column)) # if p_customer_sheet.send(column.to_s).class.to_s == "BigDecimal" # line << p_customer_sheet.send(column.to_s).to_s.gsub('.', ',') # else # line << p_customer_sheet.send(column.to_s) # end # else # line << column.to_s # end # end # sheet.add_row line, types: line.map{|t| cell_type_from_value(t)} end end @final_file = "#{Rails.root}/private_medias/export-inscrits-#{Time.now.to_s.to_slug}.xlsx" xlsx_package.serialize(@final_file) send_file @final_file } end end end