suite exports

This commit is contained in:
Nicolas Bally 2019-06-26 11:37:13 +02:00
parent bfd2801043
commit 35a7d1365d
4 changed files with 153 additions and 266 deletions

View File

@ -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[""]
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[""]
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[""]
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[""]).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[""]
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[""]
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[""]
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

View File

@ -1,2 +1,4 @@
class ExportHist < ActiveRecord::Base
validates :piece, :presence => true, :uniqueness => true
end

View File

@ -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"
=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

View File

@ -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|