1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-06 01:08:21 +01:00

WIP: improve VatHistoryService

This commit is contained in:
Sylvain 2021-12-28 19:42:04 +01:00
parent 66cae9fb07
commit 44853930ed
5 changed files with 22 additions and 14 deletions

View File

@ -37,7 +37,7 @@ class AccountingPeriod < ApplicationRecord
vat_rate_group = {} vat_rate_group = {}
i.invoice_items.each do |item| i.invoice_items.each do |item|
vat_type = item.invoice_item_type vat_type = item.invoice_item_type
vat_rate_group[vat_type] = vat_service.invoice_vat(item) / 100.0 unless vat_rate_group[vat_type] vat_rate_group[vat_type] = vat_service.invoice_item_vat(item) / 100.0 unless vat_rate_group[vat_type]
end end
{ invoice: i, vat_rate: vat_rate_group } { invoice: i, vat_rate: vat_rate_group }
end end

View File

@ -27,7 +27,7 @@ class InvoiceItem < Footprintable
def net_amount def net_amount
# deduct VAT # deduct VAT
vat_service = VatHistoryService.new vat_service = VatHistoryService.new
vat_rate = vat_service.invoice_vat(self) vat_rate = vat_service.invoice_item_vat(self)
Rational(amount_after_coupon / (vat_rate / 100.00 + 1)).round.to_f Rational(amount_after_coupon / (vat_rate / 100.00 + 1)).round.to_f
end end
@ -36,19 +36,15 @@ class InvoiceItem < Footprintable
amount_after_coupon - net_amount amount_after_coupon - net_amount
end end
# return invoice item type (Matchine/Training/Space/Event/Subscription) # return invoice item type (Machine/Training/Space/Event/Subscription) used to determine the VAT rate
def invoice_item_type def invoice_item_type
if object_type == Reservation.name if object_type == Reservation.name
reservable_type = object.try(:reservable_type) object.try(:reservable_type) || ''
if reservable_type
return reservable_type
else
return ''
end
elsif object_type == Subscription.name elsif object_type == Subscription.name
return Subscription.name Subscription.name
else
''
end end
''
end end
private private

View File

@ -233,7 +233,7 @@ class PDF::Invoice < Prawn::Document
vat_rate_group = {} vat_rate_group = {}
invoice.invoice_items.each do |item| invoice.invoice_items.each do |item|
vat_type = item.invoice_item_type vat_type = item.invoice_item_type
vat_rate_group[vat_type] = { vat_rate: vat_service.invoice_vat(item), total_vat: 0, amount: 0 } unless vat_rate_group[vat_type] vat_rate_group[vat_type] = { vat_rate: vat_service.invoice_item_vat(item), total_vat: 0, amount: 0 } unless vat_rate_group[vat_type]
vat_rate_group[vat_type][:total_vat] += item.vat vat_rate_group[vat_type][:total_vat] += item.vat
vat_rate_group[vat_type][:amount] += item.amount.to_i vat_rate_group[vat_type][:amount] += item.amount.to_i
end end

View File

@ -2,8 +2,20 @@
# Provides the VAT rate in use at the given date # Provides the VAT rate in use at the given date
class VatHistoryService class VatHistoryService
# @return the VAT rate for the given Invoice
def invoice_vat(invoice)
vat_rate_group = {}
invoice.invoice_items.each do |item|
vat_type = item.invoice_item_type
vat_rate_group[vat_type] = { vat_rate: invoice_item_vat(item), total_vat: 0, amount: 0 } unless vat_rate_group[vat_type]
vat_rate_group[vat_type][:total_vat] += item.vat
vat_rate_group[vat_type][:amount] += item.amount.to_i
end
vat_rate_group
end
# return the VAT rate for the given InvoiceItem # return the VAT rate for the given InvoiceItem
def invoice_vat(invoice_item) def invoice_item_vat(invoice_item)
if invoice_item.invoice.is_a?(Avoir) if invoice_item.invoice.is_a?(Avoir)
vat_rate(invoice_item.invoice.avoir_date, invoice_item.invoice_item_type) vat_rate(invoice_item.invoice.avoir_date, invoice_item.invoice_item_type)
else else

View File

@ -105,7 +105,7 @@ class ActiveSupport::TestCase
vat_service = VatHistoryService.new vat_service = VatHistoryService.new
invoice.invoice_items.each do |item| invoice.invoice_items.each do |item|
vat_rate = vat_service.invoice_vat(item) vat_rate = vat_service.invoice_item_vat(item)
if vat_rate.positive? if vat_rate.positive?
computed_ht = sprintf('%.2f', (item.amount_after_coupon / (vat_rate / 100.00 + 1)) / 100.00).to_f computed_ht = sprintf('%.2f', (item.amount_after_coupon / (vat_rate / 100.00 + 1)) / 100.00).to_f