183 lines
3.9 KiB
Ruby
183 lines
3.9 KiB
Ruby
# -*- encoding : utf-8 -*-
|
|
|
|
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]
|
|
|
|
require 'roo'
|
|
|
|
@xlsx = Roo::Spreadsheet.open(@file)
|
|
#xlsx = Roo::Excelx.new("./new_prices.xlsx")
|
|
|
|
# Use the extension option if the extension is ambiguous.
|
|
#xlsx = Roo::Spreadsheet.open('./rails_temp_upload', extension: :xlsx)
|
|
|
|
@xlsx.info
|
|
|
|
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
@sheet = @xlsx.sheet(0)
|
|
|
|
if @sheet.cell(1,1) == "Factures vente enregistrées"
|
|
@filename = "Ventes"
|
|
@credit = true
|
|
elsif @sheet.cell(1,1) == "Factures achat enregistrées"
|
|
@filename = "Achats"
|
|
@credit = true
|
|
end
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
@table_header = ["compte comptable", "compte client", "client","date", "numero piece","N° projet","libellé", "débit", "crédit"]
|
|
@table = []
|
|
|
|
@elements.each do |e|
|
|
|
|
|
|
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 << "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
|
|
|
|
@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"]
|
|
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
|
|
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
|
|
|
|
|
|
def index
|
|
|
|
end
|
|
|
|
|
|
end
|