mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-28 09:24:24 +01:00
[bug] use vat history while regenerating invoices
This commit is contained in:
parent
3d3be70e29
commit
61c1d09ac8
@ -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
|
||||
|
0
app/services/accounting_export_service.rb
Normal file
0
app/services/accounting_export_service.rb
Normal file
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…
Reference in New Issue
Block a user