Implement GnuCash CSV generation
This commit is contained in:
parent
fad9076913
commit
cd488cbee8
48
stoptime.rb
48
stoptime.rb
|
@ -13,6 +13,7 @@
|
|||
|
||||
require "action_view"
|
||||
require "active_support"
|
||||
require "csv"
|
||||
require "camping"
|
||||
require "camping/mab"
|
||||
require "camping/ar"
|
||||
|
@ -1386,9 +1387,10 @@ module StopTime::Controllers
|
|||
|
||||
tex_file = PUBLIC_DIR + "invoices/#{@number}.tex"
|
||||
pdf_file = PUBLIC_DIR + "invoices/#{@number}.pdf"
|
||||
csv_file = PUBLIC_DIR + "invoices/#{@number}.csv"
|
||||
if @format == "html"
|
||||
@input = @invoice.attributes
|
||||
@invoice_file_present = tex_file.exist?
|
||||
@invoice_file_present = tex_file.exist? || csv_file.exist?
|
||||
render :invoice_form
|
||||
elsif @format == "tex"
|
||||
_generate_invoice_tex(@number) unless tex_file.exist?
|
||||
|
@ -1396,6 +1398,9 @@ module StopTime::Controllers
|
|||
elsif @format == "pdf"
|
||||
_generate_invoice_pdf(@number) unless pdf_file.exist?
|
||||
redirect R(Static, "") + "invoices/#{pdf_file.basename}"
|
||||
elsif @format == "csv"
|
||||
_generate_invoice_csv(@number) unless csv_file.exist?
|
||||
redirect R(Static, "") + "invoices/#{csv_file.basename}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1428,6 +1433,9 @@ module StopTime::Controllers
|
|||
pdf_file = PUBLIC_DIR + "invoices/#{invoice_number}.pdf"
|
||||
File.unlink(pdf_file) if pdf_file.exist?
|
||||
|
||||
csv_file = PUBLIC_DIR + "invoices/#{invoice_number}.csv"
|
||||
File.unlink(csv_file) if csv_file.exist?
|
||||
|
||||
redirect R(CustomersNInvoicesX, customer_id, invoice_number)
|
||||
end
|
||||
|
||||
|
@ -1485,6 +1493,44 @@ module StopTime::Controllers
|
|||
system("rubber --pdf --inplace #{tex_file}")
|
||||
system("rubber --clean --inplace #{tex_file}")
|
||||
end
|
||||
|
||||
# Generates a CSV file for the invoice with the give number
|
||||
# using {#_generate_invoice_csv}.
|
||||
#
|
||||
# @raise if CSV generation is not enabled due to missing
|
||||
# data
|
||||
def _generate_invoice_csv(number)
|
||||
raise "GnuCash CSV is not enabled due to missing data" unless @csv_enabled
|
||||
csv_file = PUBLIC_DIR + "invoices/#{number}.csv"
|
||||
|
||||
CSV.open(csv_file, "wb", col_sep: ";", headers: false) do |csv|
|
||||
id = @invoice.number
|
||||
date = @invoice.created_at.to_date
|
||||
owner_id = @customer.gnucash_customer_owner_id
|
||||
account = @company.gnucash_revenue_account_name
|
||||
@tasks.each do |task, line|
|
||||
desc = task.comment_or_name
|
||||
tax_table = config["gnucash_vat_table"][task.vat_rate]
|
||||
taxable = tax_table.present?
|
||||
if line[1].blank?
|
||||
# This is a fixed cost task
|
||||
action = "Project"
|
||||
quantity = 1
|
||||
price = number_with_precision(line[2])
|
||||
else
|
||||
# This is a task with an hourly rate
|
||||
action = "Hours"
|
||||
quantity = number_with_precision(line[0])
|
||||
price = number_with_precision(line[1])
|
||||
end
|
||||
due_date = date + 30.days # FIXME: hardcoded?!
|
||||
csv_row = [id, date, owner_id, nil, nil, date, desc, action,
|
||||
account, quantity, price, nil, nil, nil, taxable, nil,
|
||||
tax_table, due_date, nil, nil, nil, nil]
|
||||
csv << csv_row
|
||||
end
|
||||
end
|
||||
end
|
||||
end # class StopTime::Controllers::CustomerNInvoicesX
|
||||
|
||||
# == The invoice creating controller for a specifc customer
|
||||
|
|
Loading…
Reference in New Issue