mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-17 11:54:22 +01:00
(feat) use advanced accounting codes in accounting export
This commit is contained in:
parent
462a764276
commit
25edbcfbb0
83
app/services/accounting/accounting_code_service.rb
Normal file
83
app/services/accounting/accounting_code_service.rb
Normal 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
|
@ -65,16 +65,13 @@ class Accounting::AccountingExportService
|
|||||||
# Generate the "subscription" and "reservation" rows associated with the provided invoice
|
# Generate the "subscription" and "reservation" rows associated with the provided invoice
|
||||||
def items_rows(invoice)
|
def items_rows(invoice)
|
||||||
rows = ''
|
rows = ''
|
||||||
{
|
%w[Subscription Reservation WalletTransaction StatisticProfilePrepaidPack OrderItem Error].each do |object_type|
|
||||||
subscription: 'Subscription', reservation: 'Reservation', wallet: 'WalletTransaction',
|
|
||||||
pack: 'StatisticProfilePrepaidPack', product: 'OrderItem', error: 'Error'
|
|
||||||
}.each do |type, object_type|
|
|
||||||
items = invoice.invoice_items.filter { |ii| ii.object_type == object_type }
|
items = invoice.invoice_items.filter { |ii| ii.object_type == object_type }
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
rows << "#{row(
|
rows << "#{row(
|
||||||
invoice,
|
invoice,
|
||||||
account(invoice, type),
|
Accounting::AccountingCodeService.sales_account(item),
|
||||||
account(invoice, type, type: :label),
|
Accounting::AccountingCodeService.sales_account(item, type: :label),
|
||||||
item.net_amount / 100.00,
|
item.net_amount / 100.00,
|
||||||
line_label: label(invoice)
|
line_label: label(invoice)
|
||||||
)}\n"
|
)}\n"
|
||||||
@ -89,8 +86,8 @@ class Accounting::AccountingExportService
|
|||||||
invoice.payment_means.each do |details|
|
invoice.payment_means.each do |details|
|
||||||
rows << row(
|
rows << row(
|
||||||
invoice,
|
invoice,
|
||||||
account(invoice, :client, means: details[:means]),
|
Accounting::AccountingCodeService.client_account(details[:means]),
|
||||||
account(invoice, :client, means: details[:means], type: :label),
|
Accounting::AccountingCodeService.client_account(details[:means], type: :label),
|
||||||
details[:amount] / 100.00,
|
details[:amount] / 100.00,
|
||||||
line_label: label(invoice),
|
line_label: label(invoice),
|
||||||
debit_method: :debit_client,
|
debit_method: :debit_client,
|
||||||
@ -109,8 +106,8 @@ class Accounting::AccountingExportService
|
|||||||
|
|
||||||
row(
|
row(
|
||||||
invoice,
|
invoice,
|
||||||
account(invoice, :vat),
|
Accounting::AccountingCodeService.vat_account,
|
||||||
account(invoice, :vat, type: :label),
|
Accounting::AccountingCodeService.vat_account(type: :label),
|
||||||
invoice.invoice_items.map(&:vat).map(&:to_i).reduce(:+) / 100.00,
|
invoice.invoice_items.map(&:vat).map(&:to_i).reduce(:+) / 100.00,
|
||||||
line_label: label(invoice)
|
line_label: label(invoice)
|
||||||
)
|
)
|
||||||
@ -147,18 +144,6 @@ class Accounting::AccountingExportService
|
|||||||
row
|
row
|
||||||
end
|
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
|
# Fill the value of the "debit" column: if the invoice is a refund, returns the given amount, returns 0 otherwise
|
||||||
def debit(invoice, amount)
|
def debit(invoice, amount)
|
||||||
avoir = invoice.is_a? Avoir
|
avoir = invoice.is_a? Avoir
|
||||||
|
Loading…
x
Reference in New Issue
Block a user