mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-26 20:54:21 +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:
|
Metrics/ClassLength:
|
||||||
Max: 200
|
Max: 200
|
||||||
Metrics/BlockLength:
|
Metrics/BlockLength:
|
||||||
|
Max: 30
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'lib/tasks/**/*.rake'
|
- 'lib/tasks/**/*.rake'
|
||||||
- 'config/routes.rb'
|
- 'config/routes.rb'
|
||||||
|
- 'app/pdfs/pdf/*.rb'
|
||||||
Style/BracesAroundHashParameters:
|
Style/BracesAroundHashParameters:
|
||||||
EnforcedStyle: context_dependent
|
EnforcedStyle: context_dependent
|
||||||
Style/RegexpLiteral:
|
Style/RegexpLiteral:
|
||||||
|
@ -28,12 +28,9 @@ class AccountingPeriod < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def invoices_with_vat(invoices)
|
def invoices_with_vat(invoices)
|
||||||
|
vat_service = VatHistoryService.new
|
||||||
invoices.map do |i|
|
invoices.map do |i|
|
||||||
if i.type == 'Avoir'
|
{ invoice: i, vat_rate: vat_service.invoice_vat(i) }
|
||||||
{ invoice: i, vat_rate: vat_rate(i.avoir_date) }
|
|
||||||
else
|
|
||||||
{ invoice: i, vat_rate: vat_rate(i.created_at) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,34 +54,12 @@ class AccountingPeriod < ActiveRecord::Base
|
|||||||
footprint == compute_footprint
|
footprint == compute_footprint
|
||||||
end
|
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
|
def previous_period
|
||||||
AccountingPeriod.where('closed_at < ?', closed_at).order(closed_at: :desc).limit(1).last
|
AccountingPeriod.where('closed_at < ?', closed_at).order(closed_at: :desc).limit(1).last
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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
|
def archive_closed_data
|
||||||
ArchiveWorker.perform_async(id)
|
ArchiveWorker.perform_async(id)
|
||||||
end
|
end
|
||||||
|
@ -32,6 +32,8 @@ class PDF::Invoice < Prawn::Document
|
|||||||
puts "Unable to decode invoice logo from base64: #{e}"
|
puts "Unable to decode invoice logo from base64: #{e}"
|
||||||
end
|
end
|
||||||
move_down 20
|
move_down 20
|
||||||
|
# the following line is a special comment to workaround RubyMine inspection problem
|
||||||
|
# noinspection RubyScope
|
||||||
font('Open-Sans', size: 10) do
|
font('Open-Sans', size: 10) do
|
||||||
# general information
|
# general information
|
||||||
if invoice.is_a?(Avoir)
|
if invoice.is_a?(Avoir)
|
||||||
@ -227,7 +229,8 @@ class PDF::Invoice < Prawn::Document
|
|||||||
if Setting.find_by(name: 'invoice_VAT-active').value == 'true'
|
if Setting.find_by(name: 'invoice_VAT-active').value == 'true'
|
||||||
data += [[I18n.t('invoices.total_including_all_taxes'), number_to_currency(total)]]
|
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)
|
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_VAT_RATE', RATE: vat_rate), number_to_currency(total - vat)]]
|
||||||
data += [[I18n.t('invoices.including_total_excluding_taxes'), number_to_currency(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
|
if invoice.wallet_amount
|
||||||
wallet_amount = invoice.wallet_amount / 100.0
|
wallet_amount = invoice.wallet_amount / 100.0
|
||||||
total -= wallet_amount
|
total -= wallet_amount
|
||||||
|
else
|
||||||
|
wallet_amount = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# payment method
|
# payment method
|
||||||
@ -315,11 +320,11 @@ class PDF::Invoice < Prawn::Document
|
|||||||
payment_verbose += ' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(total))
|
payment_verbose += ' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(total))
|
||||||
end
|
end
|
||||||
if invoice.wallet_amount
|
if invoice.wallet_amount
|
||||||
if total.positive?
|
payment_verbose += if total.positive?
|
||||||
payment_verbose += ' ' + I18n.t('invoices.and') + ' ' + I18n.t('invoices.by_wallet') + ' ' +
|
' ' + I18n.t('invoices.and') + ' ' + I18n.t('invoices.by_wallet') + ' ' +
|
||||||
I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
||||||
else
|
else
|
||||||
payment_verbose += ' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
' ' + I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(wallet_amount))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
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…
x
Reference in New Issue
Block a user