52 lines
1.1 KiB
Ruby
52 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "vat_rate/version"
|
|
|
|
require 'psych'
|
|
require 'date'
|
|
|
|
class VatRate
|
|
CATEGORIES = %i(standard parking reduced super_reduced)
|
|
RATES = Psych.safe_load_file(__dir__ + "/vat_rate.yml", permitted_classes: [Date])
|
|
|
|
def initialize(country:, category: :standard, date: Date.today, rate: nil)
|
|
raise ArgumentError unless CATEGORIES.include?(category)
|
|
|
|
@category = category
|
|
@country = country.to_s
|
|
@date = date
|
|
|
|
if rate
|
|
@rate = rate
|
|
elsif RATES["#{@category}_rates"].key?(@country)
|
|
@rate = RATES["#{@category}_rates"][@country].detect do |rate_data|
|
|
if rate
|
|
rate == rate_data['rate']
|
|
else
|
|
!rate_data.key?('from') || date.to_date >= rate_data['from']
|
|
end
|
|
end&.fetch('rate') || 0
|
|
else
|
|
@rate = 0
|
|
end
|
|
end
|
|
|
|
attr_reader :rate
|
|
|
|
def amount_with_vat(amount)
|
|
amount * (100 + @rate) / 100.0
|
|
end
|
|
|
|
def amount_without_vat(amount)
|
|
amount * 100.0 / (100 + @rate)
|
|
end
|
|
|
|
def vat_amount(amount, has_vat: false)
|
|
if has_vat
|
|
amount * @rate / (100.0 + @rate)
|
|
else
|
|
amount * @rate / 100.0
|
|
end
|
|
end
|
|
end
|