# -*- encoding : utf-8 -*- class Admin::ExportsController < ApplicationController layout "admin" before_filter :auth_admin 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 = "Export-ventes" @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 @table_header = ["compte comptable", "compte client", "client","date", "numero piece","N° projet", "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 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 else line << "" end line << number_to_phone(j_bill.j_line.number) line << j_bill.j_line.hardware line << j_bill.bill_number 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 end line << ActionController::Base.helpers.number_to_currency(j_bill.amount) csv << 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' } end end end def index end end