1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +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'
- 'config/routes.rb'
- 'app/pdfs/pdf/*.rb'
Metrics/ParameterLists:
CountKeywordArgs: false
Style/BracesAroundHashParameters:
EnforcedStyle: context_dependent
Style/RegexpLiteral:

View File

@ -1,3 +1,3 @@
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

View File

@ -230,6 +230,18 @@ class Invoice < ActiveRecord::Base
total - (wallet_amount || 0)
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
self.environment = Rails.env
end

View File

@ -4,7 +4,7 @@
class AccountingExportService
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
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 || ''
@date_format = date_format
@columns = columns
@vat_service = VatHistoryService.new
end
def set_options(decimal_separator: ',', date_format: '%d/%m/%Y', label_max_length: 50, export_zeros: false)
@ -52,10 +51,12 @@ class AccountingExportService
end
def generate_rows(invoice)
rows = client_rows(invoice) + items_rows(invoice)
vat = vat_row(invoice)
"#{client_row(invoice)}\n" \
"#{items_rows(invoice)}" \
"#{vat.nil? ? '' : "#{vat}\n"}"
rows += "#{vat}\n" unless vat.nil?
rows
end
# Generate the "subscription" and "reservation" rows associated with the provided invoice
@ -71,159 +72,72 @@ class AccountingExportService
rows
end
# Generate the "client" row, which contains the debit to the client account, all taxes included
def client_row(invoice)
total = invoice.total / 100.00
row = ''
columns.each do |column|
case column
when 'journal_code'
row << journal_code
when 'date'
row << invoice.created_at&.strftime(date_format)
when 'account_code'
row << account(invoice, :client)
when 'account_label'
row << account(invoice, :client, :label)
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
row << separator
# Generate the "client" rows, which contains the debit to the client account, all taxes included
def client_rows(invoice)
rows = ''
invoice.payment_means.each_with_index do |details, index|
rows << row(
invoice,
account(invoice, :client, means: details[:means]),
account(invoice, :client, means: details[:means], type: :label),
details[:amount] / 100.00,
line_label: index.zero? ? label(invoice) : '',
debit_method: :debit_client,
credit_method: :credit_client
)
rows << "\n"
end
row
rows
end
# Generate the "reservation" row, which contains the credit to the reservation account, all taxes excluded
def reservation_row(invoice, item)
wo_taxes_coupon = item.net_amount / 100.00
row = ''
columns.each do |column|
case column
when 'journal_code'
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
row(
invoice,
account(invoice, :reservation),
account(invoice, :reservation, type: :label),
item.net_amount / 100.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
wo_taxes_coupon = subscription_item.net_amount / 100.00
row = ''
columns.each do |column|
case column
when 'journal_code'
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
row(
invoice,
account(invoice, :subscription),
account(invoice, :subscription, type: :label),
subscription_item.net_amount / 100.00
)
end
# 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
def wallet_row(invoice)
row = ''
price = invoice.invoice_items.first.net_amount / 100.00
columns.each do |column|
case column
when 'journal_code'
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
row(
invoice,
account(invoice, :wallet),
account(invoice, :wallet, type: :label),
invoice.invoice_items.first.net_amount / 100.00
)
end
# Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only
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
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 = ''
columns.each do |column|
case column
@ -232,21 +146,21 @@ class AccountingExportService
when 'date'
row << invoice.created_at&.strftime(date_format)
when 'account_code'
row << account(invoice, :vat)
row << account_code
when 'account_label'
row << account(invoice, :vat, :label)
row << account_label
when 'piece'
row << invoice.reference
when 'line_label'
row << ''
row << line_label
when 'debit_origin'
row << debit(invoice, vat)
row << method(debit_method).call(invoice, amount)
when 'credit_origin'
row << credit(invoice, vat)
row << method(credit_method).call(invoice, amount)
when 'debit_euro'
row << debit(invoice, vat)
row << method(debit_method).call(invoice, amount)
when 'credit_euro'
row << credit(invoice, vat)
row << method(credit_method).call(invoice, amount)
when 'lettering'
row << ''
else
@ -258,35 +172,33 @@ class AccountingExportService
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)
res = case account
when :client
means = invoice.paid_with_stripe? ? 'card' : 'site'
Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value
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'
Setting.find_by(name: "accounting_#{invoice.invoiced.reservable_type}_#{type}")&.value
else
puts "WARN: Invoice #{invoice.id} has no reservation"
end
when :wallet
if invoice.invoiced_type == 'WalletTransaction'
Setting.find_by(name: "accounting_wallet_#{type}")&.value
else
puts "WARN: Invoice #{invoice.id} is not a wallet credit"
end
else
puts "Unsupported account #{account}"
end
res || ''
def account(invoice, account, type: :code, means: :other)
case account
when :client
Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value
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'
Setting.find_by(name: "accounting_#{invoice.invoiced.reservable_type}_#{type}")&.value
else
puts "WARN: Invoice #{invoice.id} has no reservation"
end
when :wallet
if invoice.invoiced_type == 'WalletTransaction'
Setting.find_by(name: "accounting_wallet_#{type}")&.value
else
puts "WARN: Invoice #{invoice.id} is not a wallet credit"
end
else
puts "Unsupported account #{account}"
end || ''
end
# Fill the value of the "debit" column: if the invoice is a refund, returns the given amount, returns 0 otherwise