1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

(feat) use advanced accounting codes in accounting export

This commit is contained in:
Sylvain 2022-11-15 17:26:16 +01:00
parent 462a764276
commit 25edbcfbb0
2 changed files with 90 additions and 22 deletions

View File

@ -0,0 +1,83 @@
# frozen_string_literal: false
# module definition
module Accounting; end
# fetch the code matching the given resource
class Accounting::AccountingCodeService
class << self
def client_account(payment_mean, type: :code)
raise ArgumentError('invalid type') unless %i[code label].include?(type)
Setting.get("accounting_#{payment_mean}_client_#{type}")
end
def vat_account(type: :code)
raise ArgumentError('invalid type') unless %i[code label].include?(type)
Setting.get("accounting_VAT_#{type}")
end
def sales_account(invoice_item, type: :code, section: :code)
raise ArgumentError('invalid type') unless %i[code label].include?(type)
raise ArgumentError('invalid section') unless %i[code analytical_section].include?(section)
case invoice_item.object_type
when 'Reservation'
reservation_account_code(invoice_item, type: type, section: section)
when 'Subscription'
subscription_account_code(invoice_item, type: type, section: section)
when 'StatisticProfilePrepaidPack'
Setting.get("accounting_Pack_#{type}") unless section == :analytical_section
when 'OrderItem'
product_account_code(invoice_item, type: type, section: section)
when 'WalletTransaction'
Setting.get("accounting_wallet_#{type}") unless section == :analytical_section
else
Setting.get("accounting_#{invoice_item.object_type}_#{type}") unless section == :analytical_section
end
end
def reservation_account_code(invoice_item, type: :code, section: :code)
raise ArgumentError('invalid type') unless %i[code label].include?(type)
raise ArgumentError('invalid section') unless %i[code analytical_section].include?(section)
if type == :code
item_code = Setting.get('advanced_accounting') ? invoice_item.object.reservable.advanced_accounting.send(section) : nil
return Setting.get("accounting_#{invoice_item.object.reservable_type}_code") if item_code.nil? && section == :code
item_code
else
Setting.get("accounting_#{invoice_item.object.reservable_type}_label")
end
end
def subscription_account_code(invoice_item, type: :code, section: :code)
raise ArgumentError('invalid type') unless %i[code label].include?(type)
raise ArgumentError('invalid section') unless %i[code analytical_section].include?(section)
if type == :code
item_code = Setting.get('advanced_accounting') ? invoice_item.object.plan.advanced_accounting.send(section) : nil
return Setting.get('accounting_subscription_code') if item_code.nil? && section == :code
item_code
else
Setting.get('accounting_subscription_label')
end
end
def product_account_code(invoice_item, type: :code, section: :code)
raise ArgumentError('invalid type') unless %i[code label].include?(type)
raise ArgumentError('invalid section') unless %i[code analytical_section].include?(section)
if type == :code
item_code = Setting.get('advanced_accounting') ? invoice_item.object.orderable.advanced_accounting.send(section) : nil
return Setting.get('accounting_Product_code') if item_code.nil? && section == :code
item_code
else
Setting.get('accounting_Product_label')
end
end
end
end

View File

@ -65,16 +65,13 @@ class Accounting::AccountingExportService
# Generate the "subscription" and "reservation" rows associated with the provided invoice
def items_rows(invoice)
rows = ''
{
subscription: 'Subscription', reservation: 'Reservation', wallet: 'WalletTransaction',
pack: 'StatisticProfilePrepaidPack', product: 'OrderItem', error: 'Error'
}.each do |type, object_type|
%w[Subscription Reservation WalletTransaction StatisticProfilePrepaidPack OrderItem Error].each do |object_type|
items = invoice.invoice_items.filter { |ii| ii.object_type == object_type }
items.each do |item|
rows << "#{row(
invoice,
account(invoice, type),
account(invoice, type, type: :label),
Accounting::AccountingCodeService.sales_account(item),
Accounting::AccountingCodeService.sales_account(item, type: :label),
item.net_amount / 100.00,
line_label: label(invoice)
)}\n"
@ -89,8 +86,8 @@ class Accounting::AccountingExportService
invoice.payment_means.each do |details|
rows << row(
invoice,
account(invoice, :client, means: details[:means]),
account(invoice, :client, means: details[:means], type: :label),
Accounting::AccountingCodeService.client_account(details[:means]),
Accounting::AccountingCodeService.client_account(details[:means], type: :label),
details[:amount] / 100.00,
line_label: label(invoice),
debit_method: :debit_client,
@ -109,8 +106,8 @@ class Accounting::AccountingExportService
row(
invoice,
account(invoice, :vat),
account(invoice, :vat, type: :label),
Accounting::AccountingCodeService.vat_account,
Accounting::AccountingCodeService.vat_account(type: :label),
invoice.invoice_items.map(&:vat).map(&:to_i).reduce(:+) / 100.00,
line_label: label(invoice)
)
@ -147,18 +144,6 @@ class Accounting::AccountingExportService
row
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, means: :other)
case account
when :client
Setting.get("accounting_#{means}_client_#{type}")
when :reservation
Setting.get("accounting_#{invoice.main_item.object.reservable_type}_#{type}") if invoice.main_item.object_type == 'Reservation'
else
Setting.get("accounting_#{account}_#{type}")
end || ''
end
# 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