1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-26 20:54:21 +01:00

wallet client line in accounting export + factorized accountingExportService

This commit is contained in:
Sylvain 2019-09-19 13:57:33 +02:00
parent 7d26932f6c
commit 2d6f5c3e81
4 changed files with 99 additions and 173 deletions

View File

@ -16,6 +16,8 @@ Metrics/BlockLength:
- 'lib/tasks/**/*.rake' - 'lib/tasks/**/*.rake'
- 'config/routes.rb' - 'config/routes.rb'
- 'app/pdfs/pdf/*.rb' - 'app/pdfs/pdf/*.rb'
Metrics/ParameterLists:
CountKeywordArgs: false
Style/BracesAroundHashParameters: Style/BracesAroundHashParameters:
EnforcedStyle: context_dependent EnforcedStyle: context_dependent
Style/RegexpLiteral: Style/RegexpLiteral:

View File

@ -1,3 +1,3 @@
web: bundle exec rails server puma -p $PORT -b0.0.0.0 web: bundle exec rails server puma -p $PORT -b0.0.0.0
worker: bundle exec sidekiq -C ./config/sidekiq.yml #worker: bundle exec sidekiq -C ./config/sidekiq.yml
mail: bundle exec mailcatcher --foreground mail: bundle exec mailcatcher --foreground

View File

@ -230,6 +230,18 @@ class Invoice < ActiveRecord::Base
total - (wallet_amount || 0) total - (wallet_amount || 0)
end end
# return a summary of the payment means used
def payment_means
res = []
res.push(means: :wallet, amount: wallet_amount) if wallet_transaction && wallet_amount.positive?
if paid_with_stripe?
res.push(means: :card, amount: amount_paid)
else
res.push(means: :other, amount: amount_paid)
end
res
end
def add_environment def add_environment
self.environment = Rails.env self.environment = Rails.env
end end

View File

@ -4,7 +4,7 @@
class AccountingExportService class AccountingExportService
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
attr_reader :encoding, :format, :separator, :journal_code, :date_format, :columns, :vat_service, :decimal_separator, :label_max_length, attr_reader :encoding, :format, :separator, :journal_code, :date_format, :columns, :decimal_separator, :label_max_length,
:export_zeros :export_zeros
def initialize(columns, encoding: 'UTF-8', format: 'CSV', separator: ';') def initialize(columns, encoding: 'UTF-8', format: 'CSV', separator: ';')
@ -18,7 +18,6 @@ class AccountingExportService
@journal_code = Setting.find_by(name: 'accounting_journal_code')&.value || '' @journal_code = Setting.find_by(name: 'accounting_journal_code')&.value || ''
@date_format = date_format @date_format = date_format
@columns = columns @columns = columns
@vat_service = VatHistoryService.new
end end
def set_options(decimal_separator: ',', date_format: '%d/%m/%Y', label_max_length: 50, export_zeros: false) def set_options(decimal_separator: ',', date_format: '%d/%m/%Y', label_max_length: 50, export_zeros: false)
@ -52,10 +51,12 @@ class AccountingExportService
end end
def generate_rows(invoice) def generate_rows(invoice)
rows = client_rows(invoice) + items_rows(invoice)
vat = vat_row(invoice) vat = vat_row(invoice)
"#{client_row(invoice)}\n" \ rows += "#{vat}\n" unless vat.nil?
"#{items_rows(invoice)}" \
"#{vat.nil? ? '' : "#{vat}\n"}" rows
end end
# Generate the "subscription" and "reservation" rows associated with the provided invoice # Generate the "subscription" and "reservation" rows associated with the provided invoice
@ -71,159 +72,72 @@ class AccountingExportService
rows rows
end end
# Generate the "client" row, which contains the debit to the client account, all taxes included # Generate the "client" rows, which contains the debit to the client account, all taxes included
def client_row(invoice) def client_rows(invoice)
total = invoice.total / 100.00 rows = ''
row = '' invoice.payment_means.each_with_index do |details, index|
columns.each do |column| rows << row(
case column invoice,
when 'journal_code' account(invoice, :client, means: details[:means]),
row << journal_code account(invoice, :client, means: details[:means], type: :label),
when 'date' details[:amount] / 100.00,
row << invoice.created_at&.strftime(date_format) line_label: index.zero? ? label(invoice) : '',
when 'account_code' debit_method: :debit_client,
row << account(invoice, :client) credit_method: :credit_client
when 'account_label' )
row << account(invoice, :client, :label) rows << "\n"
when 'piece'
row << invoice.reference
when 'line_label'
row << label(invoice)
when 'debit_origin'
row << debit_client(invoice, total)
when 'credit_origin'
row << credit_client(invoice, total)
when 'debit_euro'
row << debit_client(invoice, total)
when 'credit_euro'
row << credit_client(invoice, total)
when 'lettering'
row << ''
else
puts "Unsupported column: #{column}"
end end
row << separator rows
end
row
end end
# Generate the "reservation" row, which contains the credit to the reservation account, all taxes excluded # Generate the "reservation" row, which contains the credit to the reservation account, all taxes excluded
def reservation_row(invoice, item) def reservation_row(invoice, item)
wo_taxes_coupon = item.net_amount / 100.00 row(
row = '' invoice,
columns.each do |column| account(invoice, :reservation),
case column account(invoice, :reservation, type: :label),
when 'journal_code' item.net_amount / 100.00
row << journal_code )
when 'date'
row << invoice.created_at&.strftime(date_format)
when 'account_code'
row << account(invoice, :reservation)
when 'account_label'
row << account(invoice, :reservation, :label)
when 'piece'
row << invoice.reference
when 'line_label'
row << ''
when 'debit_origin'
row << debit(invoice, wo_taxes_coupon)
when 'credit_origin'
row << credit(invoice, wo_taxes_coupon)
when 'debit_euro'
row << debit(invoice, wo_taxes_coupon)
when 'credit_euro'
row << credit(invoice, wo_taxes_coupon)
when 'lettering'
row << ''
else
puts "Unsupported column: #{column}"
end
row << separator
end
row
end end
# Generate the "subscription" row, which contains the credit to the subscription account, all taxes excluded # Generate the "subscription" row, which contains the credit to the subscription account, all taxes excluded
def subscription_row(invoice) def subscription_row(invoice)
subscription_item = invoice.invoice_items.select(&:subscription).first subscription_item = invoice.invoice_items.select(&:subscription).first
wo_taxes_coupon = subscription_item.net_amount / 100.00 row(
row = '' invoice,
columns.each do |column| account(invoice, :subscription),
case column account(invoice, :subscription, type: :label),
when 'journal_code' subscription_item.net_amount / 100.00
row << journal_code )
when 'date'
row << invoice.created_at&.strftime(date_format)
when 'account_code'
row << account(invoice, :subscription)
when 'account_label'
row << account(invoice, :subscription, :label)
when 'piece'
row << invoice.reference
when 'line_label'
row << ''
when 'debit_origin'
row << debit(invoice, wo_taxes_coupon)
when 'credit_origin'
row << credit(invoice, wo_taxes_coupon)
when 'debit_euro'
row << debit(invoice, wo_taxes_coupon)
when 'credit_euro'
row << credit(invoice, wo_taxes_coupon)
when 'lettering'
row << ''
else
puts "Unsupported column: #{column}"
end
row << separator
end
row
end end
# Generate the "wallet" row, which contains the credit to the wallet account, all taxes excluded # Generate the "wallet" row, which contains the credit to the wallet account, all taxes excluded
# This applies to wallet crediting, when an Avoir is generated at this time # This applies to wallet crediting, when an Avoir is generated at this time
def wallet_row(invoice) def wallet_row(invoice)
row = '' row(
price = invoice.invoice_items.first.net_amount / 100.00 invoice,
columns.each do |column| account(invoice, :wallet),
case column account(invoice, :wallet, type: :label),
when 'journal_code' invoice.invoice_items.first.net_amount / 100.00
row << journal_code )
when 'date'
row << invoice.created_at&.strftime(date_format)
when 'account_code'
row << account(invoice, :wallet)
when 'account_label'
row << account(invoice, :wallet, :label)
when 'piece'
row << invoice.reference
when 'line_label'
row << ''
when 'debit_origin'
row << debit(invoice, price)
when 'credit_origin'
row << credit(invoice, price)
when 'debit_euro'
row << debit(invoice, price)
when 'credit_euro'
row << credit(invoice, price)
when 'lettering'
row << ''
else
puts "Unsupported column: #{column}"
end
row << separator
end
row
end end
# Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only # Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only
def vat_row(invoice) def vat_row(invoice)
rate = vat_service.invoice_vat(invoice) rate = VatHistoryService.new.invoice_vat(invoice)
# we do not render the VAT row if it was disabled for this invoice # we do not render the VAT row if it was disabled for this invoice
return nil if rate.zero? return nil if rate.zero?
vat = invoice.invoice_items.map(&:vat).map(&:to_i).reduce(:+) / 100.00 row(
invoice,
account(invoice, :vat),
account(invoice, :vat, type: :label),
invoice.invoice_items.map(&:vat).map(&:to_i).reduce(:+) / 100.00
)
end
# Generate a row of the export, filling the configured columns with the provided values
def row(invoice, account_code, account_label, amount, line_label: '', debit_method: :debit, credit_method: :credit)
row = '' row = ''
columns.each do |column| columns.each do |column|
case column case column
@ -232,21 +146,21 @@ class AccountingExportService
when 'date' when 'date'
row << invoice.created_at&.strftime(date_format) row << invoice.created_at&.strftime(date_format)
when 'account_code' when 'account_code'
row << account(invoice, :vat) row << account_code
when 'account_label' when 'account_label'
row << account(invoice, :vat, :label) row << account_label
when 'piece' when 'piece'
row << invoice.reference row << invoice.reference
when 'line_label' when 'line_label'
row << '' row << line_label
when 'debit_origin' when 'debit_origin'
row << debit(invoice, vat) row << method(debit_method).call(invoice, amount)
when 'credit_origin' when 'credit_origin'
row << credit(invoice, vat) row << method(credit_method).call(invoice, amount)
when 'debit_euro' when 'debit_euro'
row << debit(invoice, vat) row << method(debit_method).call(invoice, amount)
when 'credit_euro' when 'credit_euro'
row << credit(invoice, vat) row << method(credit_method).call(invoice, amount)
when 'lettering' when 'lettering'
row << '' row << ''
else else
@ -258,10 +172,9 @@ class AccountingExportService
end end
# Get the account code (or label) for the given invoice and the specified line type (client, vat, subscription or reservation) # 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) def account(invoice, account, type: :code, means: :other)
res = case account case account
when :client when :client
means = invoice.paid_with_stripe? ? 'card' : 'site'
Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value
when :vat when :vat
Setting.find_by(name: "accounting_VAT_#{type}")&.value Setting.find_by(name: "accounting_VAT_#{type}")&.value
@ -285,8 +198,7 @@ class AccountingExportService
end end
else else
puts "Unsupported account #{account}" puts "Unsupported account #{account}"
end end || ''
res || ''
end end
# Fill the value of the "debit" column: if the invoice is a refund, returns the given amount, returns 0 otherwise # Fill the value of the "debit" column: if the invoice is a refund, returns the given amount, returns 0 otherwise