mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
consider vat history in accounting export + [bug] use vat history while regenerating invoices
This commit is contained in:
parent
acacd9a19e
commit
38ab06840e
@ -11,9 +11,11 @@ Metrics/AbcSize:
|
||||
Metrics/ClassLength:
|
||||
Max: 200
|
||||
Metrics/BlockLength:
|
||||
Max: 30
|
||||
Exclude:
|
||||
- 'lib/tasks/**/*.rake'
|
||||
- 'config/routes.rb'
|
||||
- 'app/pdfs/pdf/*.rb'
|
||||
Style/BracesAroundHashParameters:
|
||||
EnforcedStyle: context_dependent
|
||||
Style/RegexpLiteral:
|
||||
|
@ -28,12 +28,9 @@ class AccountingPeriod < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def invoices_with_vat(invoices)
|
||||
vat_service = VatHistoryService.new
|
||||
invoices.map do |i|
|
||||
if i.type == 'Avoir'
|
||||
{ invoice: i, vat_rate: vat_rate(i.avoir_date) }
|
||||
else
|
||||
{ invoice: i, vat_rate: vat_rate(i.created_at) }
|
||||
end
|
||||
{ invoice: i, vat_rate: vat_service.invoice_vat(i) }
|
||||
end
|
||||
end
|
||||
|
||||
@ -57,34 +54,12 @@ class AccountingPeriod < ActiveRecord::Base
|
||||
footprint == compute_footprint
|
||||
end
|
||||
|
||||
def vat_rate(date)
|
||||
@vat_rates = vat_history if @vat_rates.nil?
|
||||
|
||||
first_rate = @vat_rates.first
|
||||
return first_rate[:rate] if date < first_rate[:date]
|
||||
|
||||
@vat_rates.each_index do |i|
|
||||
return @vat_rates[i][:rate] if date >= @vat_rates[i][:date] && (@vat_rates[i + 1].nil? || date < @vat_rates[i + 1][:date])
|
||||
end
|
||||
end
|
||||
|
||||
def previous_period
|
||||
AccountingPeriod.where('closed_at < ?', closed_at).order(closed_at: :desc).limit(1).last
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def vat_history
|
||||
key_dates = []
|
||||
Setting.find_by(name: 'invoice_VAT-rate').history_values.each do |rate|
|
||||
key_dates.push(date: rate.created_at, rate: (rate.value.to_i / 100.0))
|
||||
end
|
||||
Setting.find_by(name: 'invoice_VAT-active').history_values.each do |v|
|
||||
key_dates.push(date: v.created_at, rate: 0) if v.value == 'false'
|
||||
end
|
||||
key_dates.sort_by { |k| k[:date] }
|
||||
end
|
||||
|
||||
def archive_closed_data
|
||||
ArchiveWorker.perform_async(id)
|
||||
end
|
||||
|
@ -32,6 +32,8 @@ class PDF::Invoice < Prawn::Document
|
||||
puts "Unable to decode invoice logo from base64: #{e}"
|
||||
end
|
||||
move_down 20
|
||||
# the following line is a special comment to workaround RubyMine inspection problem
|
||||
# noinspection RubyScope
|
||||
font('Open-Sans', size: 10) do
|
||||
# general information
|
||||
if invoice.is_a?(Avoir)
|
||||
@ -227,7 +229,8 @@ class PDF::Invoice < Prawn::Document
|
||||
if Setting.find_by(name: 'invoice_VAT-active').value == 'true'
|
||||
data += [[I18n.t('invoices.total_including_all_taxes'), number_to_currency(total)]]
|
||||
|
||||
vat_rate = Setting.find_by(name: 'invoice_VAT-rate').value.to_f
|
||||
vat_service = VatHistoryService.new
|
||||
vat_rate = vat_service.invoice_vat(invoice)
|
||||
vat = total / (vat_rate / 100 + 1)
|
||||
data += [[I18n.t('invoices.including_VAT_RATE', RATE: vat_rate), number_to_currency(total - vat)]]
|
||||
data += [[I18n.t('invoices.including_total_excluding_taxes'), number_to_currency(vat)]]
|
||||
@ -298,6 +301,8 @@ class PDF::Invoice < Prawn::Document
|
||||
if invoice.wallet_amount
|
||||
wallet_amount = invoice.wallet_amount / 100.0
|
||||
total -= wallet_amount
|
||||
else
|
||||
wallet_amount = nil
|
||||
end
|
||||
|
||||
# payment method
|
||||
@ -315,12 +320,12 @@ class PDF::Invoice < Prawn::Document
|
||||
payment_verbose += ' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(total))
|
||||
end
|
||||
if invoice.wallet_amount
|
||||
if total.positive?
|
||||
payment_verbose += ' ' + I18n.t('invoices.and') + ' ' + I18n.t('invoices.by_wallet') + ' ' +
|
||||
I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
||||
else
|
||||
payment_verbose += ' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
||||
end
|
||||
payment_verbose += if total.positive?
|
||||
' ' + I18n.t('invoices.and') + ' ' + I18n.t('invoices.by_wallet') + ' ' +
|
||||
I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
||||
else
|
||||
' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
||||
end
|
||||
end
|
||||
end
|
||||
text payment_verbose
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
# Provides the routine to export the accounting data to an external accounting software
|
||||
class AccountingExportService
|
||||
attr_reader :encoding, :format, :separator, :journal_code, :date_format, :columns, :vat_rate
|
||||
attr_reader :encoding, :format, :separator, :journal_code, :date_format, :columns, :vat_service
|
||||
|
||||
def initialize(columns, encoding = 'UTF-8', format = 'CSV', separator = ';', date_format = '%d/%m/%Y')
|
||||
@encoding = encoding
|
||||
@ -11,7 +11,7 @@ class AccountingExportService
|
||||
@journal_code = Setting.find_by(name: 'accounting_journal_code')&.value || ''
|
||||
@date_format = date_format
|
||||
@columns = columns
|
||||
@vat_rate = Setting.find_by(name: 'invoice_VAT-rate')&.value&.to_f || 0
|
||||
@vat_service = VatHistoryService.new
|
||||
end
|
||||
|
||||
def export(start_date, end_date, file)
|
||||
@ -91,7 +91,7 @@ class AccountingExportService
|
||||
|
||||
# Generate the "reservation" row, which contains the credit to the reservation account, all taxes excluded
|
||||
def reservation_row(invoice, item)
|
||||
wo_taxes = (item.amount / (vat_rate / 100 + 1)) / 100.0
|
||||
wo_taxes = (item.amount / (vat_service.invoice_vat(invoice) / 100 + 1)) / 100.0
|
||||
row = ''
|
||||
columns.each do |column|
|
||||
case column
|
||||
@ -128,7 +128,7 @@ class AccountingExportService
|
||||
# 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 = (subscription_item.amount / (vat_rate / 100 + 1)) / 100.0
|
||||
wo_taxes = (subscription_item.amount / (vat_service.invoice_vat(invoice) / 100 + 1)) / 100.0
|
||||
row = ''
|
||||
columns.each do |column|
|
||||
case column
|
||||
@ -164,7 +164,7 @@ class AccountingExportService
|
||||
|
||||
# Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only
|
||||
def vat_row(invoice)
|
||||
vat = (invoice.total - (invoice.total / (vat_rate / 100 + 1))) / 100.0
|
||||
vat = (invoice.total - (invoice.total / (vat_service.invoice_vat(invoice) / 100 + 1))) / 100.0
|
||||
row = ''
|
||||
columns.each do |column|
|
||||
case column
|
||||
|
38
app/services/vat_history_service.rb
Normal file
38
app/services/vat_history_service.rb
Normal file
@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Provides the VAT rate in use at the given date
|
||||
class VatHistoryService
|
||||
# return the VAT rate for the given Invoice/Avoir
|
||||
def invoice_vat(invoice)
|
||||
if invoice.is_a?(Avoir)
|
||||
vat_rate(invoice.avoir_date)
|
||||
else
|
||||
vat_rate(invoice.created_at)
|
||||
end
|
||||
end
|
||||
|
||||
# return the VAT rate foe the given date
|
||||
def vat_rate(date)
|
||||
@vat_rates = vat_history if @vat_rates.nil?
|
||||
|
||||
first_rate = @vat_rates.first
|
||||
return first_rate[:rate] if date < first_rate[:date]
|
||||
|
||||
@vat_rates.each_index do |i|
|
||||
return @vat_rates[i][:rate] if date >= @vat_rates[i][:date] && (@vat_rates[i + 1].nil? || date < @vat_rates[i + 1][:date])
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def vat_history
|
||||
key_dates = []
|
||||
Setting.find_by(name: 'invoice_VAT-rate').history_values.each do |rate|
|
||||
key_dates.push(date: rate.created_at, rate: (rate.value.to_i / 100.0))
|
||||
end
|
||||
Setting.find_by(name: 'invoice_VAT-active').history_values.each do |v|
|
||||
key_dates.push(date: v.created_at, rate: 0) if v.value == 'false'
|
||||
end
|
||||
key_dates.sort_by { |k| k[:date] }
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user