diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 8bc5137d5..aaddbd77b 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -18,6 +18,8 @@ class Invoice < ActiveRecord::Base :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true before_create :set_invoice_number + before_create :check_vat + def set_invoice_number last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first @@ -34,6 +36,12 @@ class Invoice < ActiveRecord::Base false end + def check_vat + if buyer.country_code = 'EE' && buyer.vat_no.present? + self.vat_prc = 0 + end + end + before_save -> { self.sum_cache = sum } class << self