diff --git a/app/models/invoice.rb b/app/models/invoice.rb index d78c24d07..7dd8bbe67 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -32,8 +32,8 @@ class Invoice < ActiveRecord::Base validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, allow_nil: true - after_initialize :apply_defaults before_create :set_invoice_number + before_create :apply_default_vat_rate, unless: :vat_rate? attribute :vat_rate, ::Type::VATRate.new attr_readonly :vat_rate @@ -156,7 +156,7 @@ class Invoice < ActiveRecord::Base def vat return 0 unless vat_rate - (sum_without_vat * vat_rate).round(2) + sum_without_vat * vat_rate / 100 end def sum @@ -165,7 +165,7 @@ class Invoice < ActiveRecord::Base private - def apply_defaults - self.vat_rate = buyer.effective_vat_rate unless vat_rate + def apply_default_vat_rate + self.vat_rate = buyer.effective_vat_rate end end diff --git a/app/views/registrar/invoices/partials/_items.haml b/app/views/registrar/invoices/partials/_items.haml index 64fc71786..eef06c1ce 100644 --- a/app/views/registrar/invoices/partials/_items.haml +++ b/app/views/registrar/invoices/partials/_items.haml @@ -24,7 +24,7 @@ %td= currency(@invoice.sum_without_vat) %tr %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) %tr %th.no-border{colspan: 3} diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 6f92c4a8a..a5f035fe0 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -242,7 +242,7 @@ %td= "#{currency(@invoice.sum_without_vat)} #{@invoice.currency}" %tr %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}" %tr %th.no-border{colspan: 3} diff --git a/config/locales/en.yml b/config/locales/en.yml index 19f62fe3f..73045367b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -543,7 +543,7 @@ en: invoice_number: Invoice no. seller: 'Seller' prepayment: 'Prepayment' - vat: 'VAT (%{rate}%)' + vat: 'VAT (%{rate})' unpaid: 'Unpaid' your_current_account_balance_is: 'Your current account balance is %{balance} %{currency}' billing: 'Billing' diff --git a/test/models/invoice/vat_rate_test.rb b/test/models/invoice/vat_rate_test.rb index 27bc4b705..d1e892b72 100644 --- a/test/models/invoice/vat_rate_test.rb +++ b/test/models/invoice/vat_rate_test.rb @@ -35,17 +35,22 @@ class InvoiceVATRateTest < ActiveSupport::TestCase def test_vat_rate_defaults_to_effective_vat_rate_of_a_registrar 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 - invoice = Invoice.new(buyer: registrar) - assert_equal 55, invoice.vat_rate + registrar.stub(:effective_vat_rate, BigDecimal(55)) do + invoice.save! end + + assert_equal BigDecimal(55), invoice.vat_rate end def test_vat_rate_cannot_be_updated - @invoice.vat_rate = 21 + @invoice.vat_rate = BigDecimal(21) @invoice.save! @invoice.reload - refute_equal 21, @invoice.vat_rate + refute_equal BigDecimal(21), @invoice.vat_rate end end