2019-08-01 11:26:40 +02:00
|
|
|
# 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
|
2019-09-18 13:28:53 +02:00
|
|
|
chronology = []
|
|
|
|
end_date = DateTime.now
|
|
|
|
Setting.find_by(name: 'invoice_VAT-active').history_values.order(created_at: 'DESC').each do |v|
|
|
|
|
chronology.push(start: v.created_at, end: end_date, enabled: v.value == 'true')
|
|
|
|
end_date = v.created_at
|
2019-08-01 11:26:40 +02:00
|
|
|
end
|
2019-10-29 15:20:25 +01:00
|
|
|
chronology.push(start: DateTime.new(0), end: end_date, enabled: false)
|
2019-09-18 13:28:53 +02:00
|
|
|
date_rates = []
|
|
|
|
Setting.find_by(name: 'invoice_VAT-rate').history_values.order(created_at: 'ASC').each do |rate|
|
2019-10-29 15:20:25 +01:00
|
|
|
range = chronology.select { |p| rate.created_at.to_i.between?(p[:start].to_i, p[:end].to_i) }.first
|
2019-09-18 13:28:53 +02:00
|
|
|
date = range[:enabled] ? rate.created_at : range[:end]
|
|
|
|
date_rates.push(date: date, rate: rate.value.to_i)
|
2019-08-01 11:26:40 +02:00
|
|
|
end
|
2019-09-18 13:28:53 +02:00
|
|
|
chronology.reverse_each do |period|
|
|
|
|
date_rates.push(date: period[:start], rate: 0) unless period[:enabled]
|
|
|
|
end
|
|
|
|
date_rates.sort_by { |k| k[:date] }
|
2019-08-01 11:26:40 +02:00
|
|
|
end
|
2019-09-18 13:28:53 +02:00
|
|
|
end
|