2019-07-31 16:52:11 +02:00
|
|
|
# frozen_string_literal: false
|
2019-07-29 17:51:53 +02:00
|
|
|
|
|
|
|
# Provides the routine to export the accounting data to an external accounting software
|
|
|
|
class AccountingExportService
|
2019-09-17 12:35:34 +02:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2019-07-29 17:51:53 +02:00
|
|
|
|
2019-09-17 12:35:34 +02:00
|
|
|
attr_reader :encoding, :format, :separator, :journal_code, :date_format, :columns, :vat_service, :decimal_separator, :label_max_length,
|
|
|
|
:export_zeros
|
|
|
|
|
|
|
|
def initialize(columns, encoding: 'UTF-8', format: 'CSV', separator: ';')
|
2019-07-29 17:51:53 +02:00
|
|
|
@encoding = encoding
|
|
|
|
@format = format
|
|
|
|
@separator = separator
|
2019-09-17 12:35:34 +02:00
|
|
|
@decimal_separator = ','
|
|
|
|
@date_format = '%d/%m/%Y'
|
|
|
|
@label_max_length = 50
|
|
|
|
@export_zeros = false
|
2019-08-01 10:24:34 +02:00
|
|
|
@journal_code = Setting.find_by(name: 'accounting_journal_code')&.value || ''
|
2019-07-30 16:06:35 +02:00
|
|
|
@date_format = date_format
|
2019-07-29 17:51:53 +02:00
|
|
|
@columns = columns
|
2019-08-01 11:26:40 +02:00
|
|
|
@vat_service = VatHistoryService.new
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
2019-09-17 12:35:34 +02:00
|
|
|
def set_options(decimal_separator: ',', date_format: '%d/%m/%Y', label_max_length: 50, export_zeros: false)
|
|
|
|
@decimal_separator = decimal_separator
|
|
|
|
@date_format = date_format
|
|
|
|
@label_max_length = label_max_length
|
|
|
|
@export_zeros = export_zeros
|
|
|
|
end
|
|
|
|
|
2019-07-30 16:06:35 +02:00
|
|
|
def export(start_date, end_date, file)
|
2019-07-29 17:51:53 +02:00
|
|
|
# build CVS content
|
2019-08-01 09:49:09 +02:00
|
|
|
content = header_row
|
2019-07-29 17:51:53 +02:00
|
|
|
invoices = Invoice.where('created_at >= ? AND created_at <= ?', start_date, end_date).order('created_at ASC')
|
2019-09-17 12:35:34 +02:00
|
|
|
invoices = invoices.where('total > 0') unless export_zeros
|
2019-07-29 17:51:53 +02:00
|
|
|
invoices.each do |i|
|
2019-08-01 09:49:09 +02:00
|
|
|
content << generate_rows(i)
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# write content to file
|
2019-08-01 09:49:09 +02:00
|
|
|
File.open(file, "w:#{encoding}") { |f| f.puts content.encode(encoding, invalid: :replace, undef: :replace) }
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-01 09:49:09 +02:00
|
|
|
def header_row
|
|
|
|
row = ''
|
|
|
|
columns.each do |column|
|
|
|
|
row << I18n.t("accounting_export.#{column}") << separator
|
|
|
|
end
|
2019-08-01 10:24:34 +02:00
|
|
|
"#{row}\n"
|
2019-08-01 09:49:09 +02:00
|
|
|
end
|
|
|
|
|
2019-07-29 17:51:53 +02:00
|
|
|
def generate_rows(invoice)
|
2019-09-18 13:28:53 +02:00
|
|
|
vat = vat_row(invoice)
|
2019-08-01 09:49:09 +02:00
|
|
|
"#{client_row(invoice)}\n" \
|
2019-08-01 10:24:34 +02:00
|
|
|
"#{items_rows(invoice)}" \
|
2019-09-18 13:28:53 +02:00
|
|
|
"#{vat.nil? ? '' : "#{vat}\n"}"
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate the "subscription" and "reservation" rows associated with the provided invoice
|
|
|
|
def items_rows(invoice)
|
2019-08-01 09:49:09 +02:00
|
|
|
rows = invoice.subscription_invoice? ? "#{subscription_row(invoice)}\n" : ''
|
|
|
|
if invoice.invoiced_type == 'Reservation'
|
|
|
|
invoice.invoice_items.each do |item|
|
|
|
|
rows << "#{reservation_row(invoice, item)}\n"
|
|
|
|
end
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
rows
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate the "client" row, which contains the debit to the client account, all taxes included
|
|
|
|
def client_row(invoice)
|
2019-08-14 10:56:30 +02:00
|
|
|
total = invoice.total / 100.00
|
2019-07-29 17:51:53 +02:00
|
|
|
row = ''
|
|
|
|
columns.each do |column|
|
|
|
|
case column
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'journal_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << journal_code
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'date'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.created_at&.strftime(date_format)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :client)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_label'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :client, :label)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'piece'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.reference
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'line_label'
|
2019-09-17 12:35:34 +02:00
|
|
|
row << label(invoice)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_origin'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << debit_client(invoice, total)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_origin'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << credit_client(invoice, total)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_euro'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << debit_client(invoice, total)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_euro'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << credit_client(invoice, total)
|
|
|
|
when 'lettering'
|
|
|
|
row << ''
|
2019-07-29 17:51:53 +02:00
|
|
|
else
|
|
|
|
puts "Unsupported column: #{column}"
|
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row << separator
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate the "reservation" row, which contains the credit to the reservation account, all taxes excluded
|
|
|
|
def reservation_row(invoice, item)
|
2019-08-14 10:56:30 +02:00
|
|
|
wo_taxes = (item.amount / (vat_service.invoice_vat(invoice) / 100.00 + 1)) / 100.00
|
2019-09-17 17:39:05 +02:00
|
|
|
wo_taxes_coupon = amount_after_coupon(invoice, wo_taxes)
|
2019-07-29 17:51:53 +02:00
|
|
|
row = ''
|
|
|
|
columns.each do |column|
|
|
|
|
case column
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'journal_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << journal_code
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'date'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.created_at&.strftime(date_format)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :reservation)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_label'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :reservation, :label)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'piece'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.reference
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'line_label'
|
2019-09-17 12:35:34 +02:00
|
|
|
row << ''
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_origin'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << debit(invoice, wo_taxes_coupon)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_origin'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << credit(invoice, wo_taxes_coupon)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_euro'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << debit(invoice, wo_taxes_coupon)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_euro'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << credit(invoice, wo_taxes_coupon)
|
2019-08-01 09:49:09 +02:00
|
|
|
when 'lettering'
|
|
|
|
row << ''
|
2019-07-29 17:51:53 +02:00
|
|
|
else
|
|
|
|
puts "Unsupported column: #{column}"
|
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row << separator
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate the "subscription" row, which contains the credit to the subscription account, all taxes excluded
|
|
|
|
def subscription_row(invoice)
|
|
|
|
subscription_item = invoice.invoice_items.select(&:subscription).first
|
2019-08-14 10:56:30 +02:00
|
|
|
wo_taxes = (subscription_item.amount / (vat_service.invoice_vat(invoice) / 100.00 + 1)) / 100.00
|
2019-09-17 17:39:05 +02:00
|
|
|
wo_taxes_coupon = amount_after_coupon(invoice, wo_taxes)
|
2019-07-29 17:51:53 +02:00
|
|
|
row = ''
|
|
|
|
columns.each do |column|
|
|
|
|
case column
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'journal_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << journal_code
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'date'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.created_at&.strftime(date_format)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :subscription)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_label'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :subscription, :label)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'piece'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.reference
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'line_label'
|
2019-09-17 12:35:34 +02:00
|
|
|
row << ''
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_origin'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << debit(invoice, wo_taxes_coupon)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_origin'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << credit(invoice, wo_taxes_coupon)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_euro'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << debit(invoice, wo_taxes_coupon)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_euro'
|
2019-09-17 17:39:05 +02:00
|
|
|
row << credit(invoice, wo_taxes_coupon)
|
2019-08-01 09:49:09 +02:00
|
|
|
when 'lettering'
|
|
|
|
row << ''
|
2019-07-29 17:51:53 +02:00
|
|
|
else
|
|
|
|
puts "Unsupported column: #{column}"
|
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row << separator
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-07-30 10:27:47 +02:00
|
|
|
|
2019-07-29 17:51:53 +02:00
|
|
|
# Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only
|
|
|
|
def vat_row(invoice)
|
2019-09-18 13:28:53 +02:00
|
|
|
rate = vat_service.invoice_vat(invoice)
|
|
|
|
# we do not render the VAT row if it was disabled for this invoice
|
|
|
|
return nil if rate.zero?
|
|
|
|
|
2019-09-18 15:09:14 +02:00
|
|
|
# FIXME, +/-0.01
|
2019-09-18 13:28:53 +02:00
|
|
|
vat = (invoice.total - (invoice.total / (rate / 100.00 + 1))) / 100.00
|
2019-07-29 17:51:53 +02:00
|
|
|
row = ''
|
|
|
|
columns.each do |column|
|
|
|
|
case column
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'journal_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << journal_code
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'date'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.created_at&.strftime(date_format)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_code'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :vat)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'account_label'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << account(invoice, :vat, :label)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'piece'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << invoice.reference
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'line_label'
|
2019-09-17 12:35:34 +02:00
|
|
|
row << ''
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_origin'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << debit(invoice, vat)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_origin'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << credit(invoice, vat)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'debit_euro'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << debit(invoice, vat)
|
2019-07-31 16:52:11 +02:00
|
|
|
when 'credit_euro'
|
2019-08-01 09:49:09 +02:00
|
|
|
row << credit(invoice, vat)
|
|
|
|
when 'lettering'
|
|
|
|
row << ''
|
2019-07-29 17:51:53 +02:00
|
|
|
else
|
|
|
|
puts "Unsupported column: #{column}"
|
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row << separator
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-08-01 09:49:09 +02:00
|
|
|
row
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get the account code (or label) for the given invoice and the specified line type (client, vat, subscription or reservation)
|
|
|
|
def account(invoice, account, type = :code)
|
2019-08-01 09:49:09 +02:00
|
|
|
res = case account
|
|
|
|
when :client
|
2019-09-17 12:35:34 +02:00
|
|
|
means = invoice.paid_with_stripe? ? 'card' : 'site'
|
|
|
|
Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value
|
2019-08-01 09:49:09 +02:00
|
|
|
when :vat
|
|
|
|
Setting.find_by(name: "accounting_VAT_#{type}")&.value
|
|
|
|
when :subscription
|
|
|
|
if invoice.subscription_invoice?
|
|
|
|
Setting.find_by(name: "accounting_subscription_#{type}")&.value
|
|
|
|
else
|
|
|
|
puts "WARN: Invoice #{invoice.id} has no subscription"
|
|
|
|
end
|
|
|
|
when :reservation
|
|
|
|
if invoice.invoiced_type == 'Reservation'
|
2019-09-18 15:09:14 +02:00
|
|
|
Setting.find_by(name: "accounting_#{invoice.invoiced.reservable_type}_#{type}")&.value
|
2019-08-01 09:49:09 +02:00
|
|
|
else
|
|
|
|
puts "WARN: Invoice #{invoice.id} has no reservation"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Unsupported account #{account}"
|
|
|
|
end
|
|
|
|
res || ''
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|
2019-07-30 10:27:47 +02:00
|
|
|
|
|
|
|
# Fill the value of the "debit" column: if the invoice is a refund, returns the given amount, returns 0 otherwise
|
|
|
|
def debit(invoice, amount)
|
|
|
|
avoir = invoice.is_a? Avoir
|
2019-09-17 12:35:34 +02:00
|
|
|
avoir ? format_number(amount) : '0'
|
2019-07-30 10:27:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Fill the value of the "credit" column: if the invoice is a refund, returns 0, otherwise, returns the given amount
|
|
|
|
def credit(invoice, amount)
|
|
|
|
avoir = invoice.is_a? Avoir
|
2019-09-17 12:35:34 +02:00
|
|
|
avoir ? '0' : format_number(amount)
|
2019-07-30 10:27:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Fill the value of the "debit" column for the client row: if the invoice is a refund, returns 0, otherwise, returns the given amount
|
|
|
|
def debit_client(invoice, amount)
|
|
|
|
credit(invoice, amount)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Fill the value of the "credit" column, for the client row: if the invoice is a refund, returns the given amount, returns 0 otherwise
|
|
|
|
def credit_client(invoice, amount)
|
|
|
|
debit(invoice, amount)
|
|
|
|
end
|
2019-08-01 14:58:10 +02:00
|
|
|
|
2019-09-17 17:39:05 +02:00
|
|
|
# Return the given amount, after the coupon attached to the given invoice was applied, if any
|
|
|
|
def amount_after_coupon(invoice, amount)
|
|
|
|
cs = CouponService.new
|
|
|
|
invoice.coupon_id.nil? ? amount : cs.ventilate(cs.invoice_total_no_coupon(invoice), amount, invoice.coupon)
|
|
|
|
end
|
|
|
|
|
2019-09-17 12:35:34 +02:00
|
|
|
# Format the given number as a string, using the configured separator
|
|
|
|
def format_number(num)
|
|
|
|
number_to_currency(num, unit: '', separator: decimal_separator, delimiter: '', precision: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a text from the given invoice, matching the accounting software rules for the labels
|
|
|
|
def label(invoice)
|
|
|
|
name = "#{invoice.invoicing_profile.last_name} #{invoice.invoicing_profile.first_name}".tr separator, ''
|
|
|
|
reference = invoice.reference
|
|
|
|
items = invoice.subscription_invoice? ? [I18n.t('accounting_export.subscription')] : []
|
|
|
|
items.push I18n.t("accounting_export.#{invoice.reservation.reservable_type}_reservation") if invoice.invoiced_type == 'Reservation'
|
|
|
|
summary = items.join(' + ')
|
|
|
|
res = "#{reference}, #{summary}"
|
|
|
|
"#{name.truncate(label_max_length - res.length)}, #{res}"
|
2019-08-01 14:58:10 +02:00
|
|
|
end
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|