From 35a7d1365d7b3ffcb6a359004af2bec6e2d5395b Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Wed, 26 Jun 2019 11:37:13 +0200 Subject: [PATCH] suite exports --- app/controllers/admin/exports_controller.rb | 384 ++++++------------ app/models/export_hist.rb | 2 + app/views/admin/exports/index.html.haml | 31 +- .../admin/exports/prepare_file.html.haml | 2 +- 4 files changed, 153 insertions(+), 266 deletions(-) diff --git a/app/controllers/admin/exports_controller.rb b/app/controllers/admin/exports_controller.rb index 322e9f7..65ec1ce 100644 --- a/app/controllers/admin/exports_controller.rb +++ b/app/controllers/admin/exports_controller.rb @@ -4,6 +4,10 @@ class Admin::ExportsController < ApplicationController layout "admin" before_filter :auth_admin + def show + send_file "private_medias/exports/#{params[:id]}.xlsx" + end + def prepare_file @file = params[:csv_file] @@ -29,288 +33,144 @@ class Admin::ExportsController < ApplicationController @sheet = @xlsx.sheet(0) if @sheet.cell(1,1) == "Factures vente enregistrées" - @filename = "Export-ventes" + @filename = "Ventes" + @credit = true + elsif @sheet.cell(1,1) == "Factures achat enregistrées" + @filename = "Achats" @credit = true - - headers = @sheet.row(2) - - @rows = @sheet.parse() - - @rows= @rows.drop(1) - @rows= @rows.drop(1) - - @elements = [] - @rows.each do |r| - i = 0 - t = {} - r.each do |c| - - t[headers[i]] = c - - - i += 1 - end - @elements << t - end end + + + + + headers = @sheet.row(2) - @table_header = ["compte comptable", "compte client", "client","date", "numero piece","N° projet", "débit", "crédit"] + @rows = @sheet.parse() + + @rows= @rows.drop(1) + #@rows= @rows.drop(1) + + @elements = [] + @rows.each do |r| + i = 0 + t = {} + r.each do |c| + + t[headers[i]] = c + + + i += 1 + end + @elements << t + end + + + @table_header = ["compte comptable", "compte client", "client","date", "numero piece","N° projet","libellé", "débit", "crédit"] @table = [] @elements.each do |e| - #TTC - line = [] - line << "41100000" - line << e["N° client à facturer"] - line << e["Nom"] - line << e["Date document"] - line << e["N°"] - line << e["N° projet"] - - if @credit - line << ('%.2f' % e["Montant TTC"]).to_s.sub(".",",") - line << "" - else - line << "" - line << ('%.2f' % e["Montant TTC"]).to_s.sub(".",",") - end - - @table << line - - if e["Centre cout Code"] == "VENTES" - cpt = "70100000" - elsif e["Centre cout Code"] == "LOCATION" - cpt = "70830000" - else - cpt = "" - end - - line = [] - line << cpt - line << e["N° client à facturer"] - line << e["Nom"] - line << e["Date document"] - line << e["N°"] - line << e["N° projet"] - - if @credit - line << "" - line << ('%.2f' % e["Montant"]).to_s.sub(".",",") - else - line << ('%.2f' % e["Montant"]).to_s.sub(".",",") - line << "" - end - - - - @table << line - - #TVA - line = [] - line << "44566000" - line << e["N° client à facturer"] - line << e["Nom"] - line << e["Date document"] - line << e["N°"] - line << e["N° projet"] - - if @credit - line << "" - line << ('%.2f' % (e["Montant TTC"] - e["Montant"])).to_s.sub(".",",") - else - line << ('%.2f' % (e["Montant TTC"] - e["Montant"])).to_s.sub(".",",") - line << "" - end - - - @table << line - - end - - - - respond_to do |format| - format.html{ - - - - } - - - format.xlsx { - response.headers['Content-Disposition'] = "attachment; filename=#{@filename}-#{Time.now.to_s.to_slug}.xlsx" - } - - end - - - - - - - - - - - - - - - - if false - - - require 'csv' - - csv_text = @file.tempfile.read.force_encoding('ISO-8859-1').encode('utf-8') - @imported_csv = CSV.parse(csv_text, :headers => true, :col_sep => ";") - - @headers = [ - "Opérateur", - "Forfait", - "Date fin d'engagement", - "Numéro de ligne", - "Site", - "Numéro" - - ] - - j_bill_line_types.each do |j_bill_line_type| - @headers << j_bill_line_type.name - @headers << j_bill_line_type.name - - end - - @headers << "Total" - - - - - - - - - @csv = CSV.generate(:headers => true, :col_sep => ";") do |csv| - csv << @headers - date = @start - - line = [ - "", - "", - "", - "", - "" - ] - j_bill_line_types.each do |j_bill_line_type| - line << "Prix" - line << "Volume" - - - end - - - line << "" - - - - csv << line - - - j_bills.joins(:j_line).order("j_lines.number ASC").each do |j_bill| - j_line = j_bill.j_line + + d = Date.parse(e["Date document"].to_s) + + if d >= Date.parse("2019/06/01") + if ExportHist.new(:piece => e["N°"]).save + e["Désignation"] = e["Libellé écriture"] if !e["Désignation"] + #TTC line = [] - line << j_bill.j_forfait.j_operateur.name if j_bill.j_forfait and j_bill.j_forfait.j_operateur - line << j_bill.j_forfait.name if j_bill.j_forfait - if j_bill.j_line and j_bill.j_line.active_forfait - line << j_bill.j_line.active_forfait.end_at + line << "41100000" + line << e["N° client à facturer"] + line << e["Nom"] + line << e["Date document"] + line << e["N°"] + line << e["N° projet"] + line << e["Désignation"] + + if @credit + line << ('%.2f' % e["Montant TTC"]).to_s.sub(".",",") + line << "" else line << "" + line << ('%.2f' % e["Montant TTC"]).to_s.sub(".",",") end - - line << number_to_phone(j_bill.j_line.number) - line << j_bill.j_line.hardware - line << j_bill.bill_number - + + @table << line - j_bill_line_types.each do |j_bill_line_type| - - pr = j_bill.j_bill_lines.where(:j_bill_line_type_id => j_bill_line_type.id).sum(:amount) - - v = j_bill.j_bill_lines.where(:j_bill_line_type_id => j_bill_line_type.id).sum("volume").to_f - - if pr != 00 or v != 0 - if j_bill_line_type.unit == "Secondes" - line << short_format_time(v) - else - line << v - end - else - line << "" - end - - if pr != 00 - line << ActionController::Base.helpers.number_to_currency(pr) - - else - line << "" - end - - - - + if e["Centre cout Code"] == "VENTES" + cpt = "70100000" + elsif e["Centre cout Code"] == "LOCATION" + cpt = "70830000" + else + cpt = "" end - - line << ActionController::Base.helpers.number_to_currency(j_bill.amount) - - csv << line + + line = [] + line << cpt + line << e["N° client à facturer"] + line << e["Nom"] + line << e["Date document"] + line << e["N°"] + line << e["N° projet"] + line << e["Désignation"] + + if @credit + line << "" + line << ('%.2f' % e["Montant"]).to_s.sub(".",",") + else + line << ('%.2f' % e["Montant"]).to_s.sub(".",",") + line << "" + end + + + + @table << line + + #TVA + line = [] + line << "44566000" + line << e["N° client à facturer"] + line << e["Nom"] + line << e["Date document"] + line << e["N°"] + line << e["N° projet"] + line << e["Désignation"] + + if @credit + line << "" + line << ('%.2f' % (e["Montant TTC"] - e["Montant"])).to_s.sub(".",",") + else + line << ('%.2f' % (e["Montant TTC"] - e["Montant"])).to_s.sub(".",",") + line << "" + end + + + @table << line end - - - end - - - @data_to_send = @csv.encode('iso-8859-1', :undef => :replace, :replace => '') - - file_path = Rails.root.join("pdf", "export_mois.csv") - - - - File.open(file_path, "w+") do |f| - f.write(@data_to_send) - end - - - - - - respond_to do |format| - format.html{ - - - - require 'csv' - - csv_text = File.read(file_path).encode('utf-8') - @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";") - - } - - - - format.csv { - - - - send_file file_path, :filename => "export-csv.csv", :type => 'text/csv; charset=iso-8859-1; header=present' + else - } end - end + + xlsx_package = Axlsx::Package.new + wb = xlsx_package.workbook + wb.add_worksheet(name: "import") do |sheet| + sheet.add_row @table_header + @table.each do |tr| + sheet.add_row tr + end + end + + directory_name = "exports" + Dir.mkdir("private_medias/#{directory_name}") unless File.exists?("private_medias/#{directory_name}") + + xlsx_package.serialize("private_medias/#{directory_name}/#{@filename}--#{Time.now.to_s.to_slug}-----#{@table.size/3}-lignes.xlsx") + + redirect_to :back + + end diff --git a/app/models/export_hist.rb b/app/models/export_hist.rb index 1f8d844..76473f0 100644 --- a/app/models/export_hist.rb +++ b/app/models/export_hist.rb @@ -1,2 +1,4 @@ class ExportHist < ActiveRecord::Base + validates :piece, :presence => true, :uniqueness => true + end diff --git a/app/views/admin/exports/index.html.haml b/app/views/admin/exports/index.html.haml index b26a314..c0c4bc2 100644 --- a/app/views/admin/exports/index.html.haml +++ b/app/views/admin/exports/index.html.haml @@ -1,4 +1,29 @@ -=form_tag prepare_file_admin_exports_path(:format => :xlsx), multipart: true do - =file_field_tag :csv_file +.qi_row + .qi_pannel.qi_plain.padding + =form_tag prepare_file_admin_exports_path(), multipart: true do + =file_field_tag :csv_file - =submit_tag "importer", :class => "btn btn-primary" \ No newline at end of file + =submit_tag "importer", :class => "btn btn-primary" + + +.qi_row + .qi_pannel.qi_plain.padding + %table.table.table-striped + %tr + %th Type + %th Nom du fichier + %th Date du fichier + %th nombre de lignes + -Dir["private_medias/exports/*.xlsx"].sort.reverse.each do |dir| + %tr + %td + =File.basename(dir).split(".")[0].split("----")[0].split("-")[0] + %td + =link_to File.basename(dir).split(".")[0].split("----")[0], admin_export_path(:id => File.basename(dir).split(".")[0]) + %td + -t = File.basename(dir).split(".")[0].split("----")[0].split("--")[1].split("2019-")[0]+"2019 "+File.basename(dir).split(".")[0].split("----")[0].split("--")[1].split("2019-")[1].sub("-", ":").sub("-", ":") + =link_to l(Time.parse( t ) ), admin_export_path(:id => File.basename(dir).split(".")[0]) + %td + =File.basename(dir).split("----")[1].split(".")[0].split('-')[1] + lignes + \ No newline at end of file diff --git a/app/views/admin/exports/prepare_file.html.haml b/app/views/admin/exports/prepare_file.html.haml index 1c00025..7cc947d 100644 --- a/app/views/admin/exports/prepare_file.html.haml +++ b/app/views/admin/exports/prepare_file.html.haml @@ -1,7 +1,7 @@ %p{:style => "margin:1em;"} =# link_to ic(:download)+' Télécharger en .xlsx', params.merge(format: :xlsx), :class => "btn btn-primary" - +=#debug @elements %table.table.table-striped %tr -@table_header.each do |th|