Fix VAT rate

#623
This commit is contained in:
Artur Beljajev 2018-03-13 09:02:08 +02:00
parent 6dfd57ee2e
commit a73a8a0b89
5 changed files with 17 additions and 12 deletions

View file

@ -32,8 +32,8 @@ class Invoice < ActiveRecord::Base
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
allow_nil: true allow_nil: true
after_initialize :apply_defaults
before_create :set_invoice_number before_create :set_invoice_number
before_create :apply_default_vat_rate, unless: :vat_rate?
attribute :vat_rate, ::Type::VATRate.new attribute :vat_rate, ::Type::VATRate.new
attr_readonly :vat_rate attr_readonly :vat_rate
@ -156,7 +156,7 @@ class Invoice < ActiveRecord::Base
def vat def vat
return 0 unless vat_rate return 0 unless vat_rate
(sum_without_vat * vat_rate).round(2) sum_without_vat * vat_rate / 100
end end
def sum def sum
@ -165,7 +165,7 @@ class Invoice < ActiveRecord::Base
private private
def apply_defaults def apply_default_vat_rate
self.vat_rate = buyer.effective_vat_rate unless vat_rate self.vat_rate = buyer.effective_vat_rate
end end
end end

View file

@ -24,7 +24,7 @@
%td= currency(@invoice.sum_without_vat) %td= currency(@invoice.sum_without_vat)
%tr %tr
%th.no-border{colspan: 3} %th.no-border{colspan: 3}
%th= t('vat', rate: (@invoice.vat_rate * 100).round) %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1))
%td= currency(@invoice.vat) %td= currency(@invoice.vat)
%tr %tr
%th.no-border{colspan: 3} %th.no-border{colspan: 3}

View file

@ -242,7 +242,7 @@
%td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}" %td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}"
%tr %tr
%th.no-border{colspan: 3} %th.no-border{colspan: 3}
%th= t('vat', rate: (@invoice.vat_rate * 100).round) %th= t('vat', rate: number_to_percentage(@invoice.vat_rate, precision: 1))
%td= "#{currency(@invoice.vat)} #{@invoice.currency}" %td= "#{currency(@invoice.vat)} #{@invoice.currency}"
%tr %tr
%th.no-border{colspan: 3} %th.no-border{colspan: 3}

View file

@ -543,7 +543,7 @@ en:
invoice_number: Invoice no. invoice_number: Invoice no.
seller: 'Seller' seller: 'Seller'
prepayment: 'Prepayment' prepayment: 'Prepayment'
vat: 'VAT (%{rate}%)' vat: 'VAT (%{rate})'
unpaid: 'Unpaid' unpaid: 'Unpaid'
your_current_account_balance_is: 'Your current account balance is %{balance} %{currency}' your_current_account_balance_is: 'Your current account balance is %{balance} %{currency}'
billing: 'Billing' billing: 'Billing'

View file

@ -35,17 +35,22 @@ class InvoiceVATRateTest < ActiveSupport::TestCase
def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar
registrar = registrars(:bestnames) registrar = registrars(:bestnames)
invoice = @invoice.dup
invoice.vat_rate = nil
invoice.buyer = registrar
invoice.invoice_items = @invoice.invoice_items
registrar.stub(:effective_vat_rate, 55) do registrar.stub(:effective_vat_rate, BigDecimal(55)) do
invoice = Invoice.new(buyer: registrar) invoice.save!
assert_equal 55, invoice.vat_rate
end end
assert_equal BigDecimal(55), invoice.vat_rate
end end
def test_vat_rate_cannot_be_updated def test_vat_rate_cannot_be_updated
@invoice.vat_rate = 21 @invoice.vat_rate = BigDecimal(21)
@invoice.save! @invoice.save!
@invoice.reload @invoice.reload
refute_equal 21, @invoice.vat_rate refute_equal BigDecimal(21), @invoice.vat_rate
end end
end end