Fix vat rate

#623
This commit is contained in:
Artur Beljajev 2018-03-11 19:33:21 +02:00
parent 026c36d066
commit 7d72d9cb34
8 changed files with 121 additions and 94 deletions

View file

@ -32,9 +32,11 @@ class Invoice < ActiveRecord::Base
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
allow_nil: true
before_create :set_invoice_number, :check_vat
after_initialize :apply_defaults
before_create :set_invoice_number
before_save :check_vat
attribute :vat_rate, ::Type::VATRate.new
attr_readonly :vat_rate
def set_invoice_number
last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first
@ -52,12 +54,6 @@ class Invoice < ActiveRecord::Base
false
end
def check_vat
if buyer.country_code != 'EE' && buyer.vat_no.present?
self.vat_rate = 0
end
end
before_save -> { self.sum_cache = sum }
class << self
@ -159,10 +155,17 @@ class Invoice < ActiveRecord::Base
end
def vat
return 0 unless vat_rate
(sum_without_vat * vat_rate).round(2)
end
def sum
(sum_without_vat + vat).round(2)
end
private
def apply_defaults
self.vat_rate = buyer.effective_vat_rate unless vat_rate
end
end