1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

vat history service receive invoice item param

This commit is contained in:
Du Peng 2021-12-23 09:25:48 +01:00 committed by Sylvain
parent 1b2be4a19e
commit 5e8c90458b
2 changed files with 35 additions and 8 deletions

View File

@ -28,6 +28,11 @@ class Setting < ApplicationRecord
invoice_order-nb
invoice_VAT-active
invoice_VAT-rate
invoice_VAT-rate_Machine
invoice_VAT-rate_Training
invoice_VAT-rate_Space
invoice_VAT-rate_Event
invoice_VAT-rate_Subscription
invoice_text
invoice_legals
booking_window_start

View File

@ -3,17 +3,17 @@
# 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)
def invoice_vat(invoice_item)
if invoice_item.invoice.is_a?(Avoir)
vat_rate(invoice.avoir_date, vat_type(invoice_item))
else
vat_rate(invoice.created_at)
vat_rate(invoice.created_at, vat_type(invoice_item))
end
end
# return the VAT rate for the given date
def vat_rate(date)
@vat_rates = vat_history if @vat_rates.nil?
def vat_rate(date, vat_rate_type)
@vat_rates = vat_history(vat_rate_type) if @vat_rates.nil?
first_rate = @vat_rates.first
return first_rate[:rate] if date < first_rate[:date]
@ -25,7 +25,13 @@ class VatHistoryService
private
def vat_history
def vat_type(invoice_item)
return invoice_item.object.reservable_type if invoice_item.object_type == 'Reservation'
'Subscription'
end
def vat_history(vat_rate_type)
chronology = []
end_date = DateTime.current
Setting.find_by(name: 'invoice_VAT-active').history_values.order(created_at: 'DESC').each do |v|
@ -34,7 +40,23 @@ class VatHistoryService
end
chronology.push(start: DateTime.new(0), end: end_date, enabled: false)
date_rates = []
Setting.find_by(name: 'invoice_VAT-rate').history_values.order(created_at: 'ASC').each do |rate|
vat_rate_history_values = []
vat_rate_by_type = Setting.find_by(name: "invoice_VAT-rate_#{vat_rate_type}")&.history_values&.order(created_at: 'ASC')
first_vat_rate_by_type = vat_rate_by_type.select { |v| v.value.present? }.first
if first_vat_rate_by_type
vat_rate_history_values = Setting.find_by(name: 'invoice_VAT-rate').history_values.where('created_at < ?', first_vat_rate_by_type.created_at).order(created_at: 'ASC').to_a
vat_rate_by_type = Setting.find_by(name: "invoice_VAT-rate_#{vat_rate_type}").history_values.where('created_at >= ?', first_vat_rate_by_type.created_at).order(created_at: 'ASC')
vat_rate_by_type.each do |rate|
if rate.value.blank?
vat_rate = Setting.find_by(name: 'invoice_VAT-rate').history_values.where('created_at < ?', rate.created_at).order(created_at: 'DESC').first
rate.value = vat_rate.value
end
vat_rate_history_values.push(rate)
end
else
vat_rate_history_values = Setting.find_by(name: 'invoice_VAT-rate').history_values.order(created_at: 'ASC').to_a
end
vat_rate_history_values.each do |rate|
range = chronology.select { |p| rate.created_at.to_i.between?(p[:start].to_i, p[:end].to_i) }.first
date = range[:enabled] ? rate.created_at : range[:end]
date_rates.push(date: date, rate: rate.value.to_i)