From 61d42cf4d6a4a25e1d3cc3a4dac1f8d260fff02e Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 8 Feb 2024 12:05:33 +0200 Subject: [PATCH] update invoice total if billing profile has been updated --- app/models/invoice.rb | 2 +- test/models/invoice_test.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 9dc446b07..ea6d4342a 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -119,7 +119,7 @@ def deposit end def recalculate_vat_rate - return if billing_profile_id == billing_profile_id_was + return if billing_profile_id == billing_profile_id_was && !vat_code_changed? return unless payable? self.vat_rate = assign_vat_rate diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 09c279fec..c8ec1372d 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -231,6 +231,40 @@ def test_if_auction_has_deposit_it_should_be_destracted_from_total assert_equal @payable_invoice.total.to_f, 5.0 end + def test_invoice_should_be_recalculated_if_billing_profile_updated + assert_equal @payable_invoice.total.to_f, 10.0 + + billing_profile = @payable_invoice.billing_profile + billing_profile.update(vat_code: nil, alpha_two_country_code: 'LV') && @payable_invoice.reload && billing_profile.reload + + assert_equal Countries.vat_rate_from_alpha2_code(billing_profile.alpha_two_country_code), 0.21 + assert_equal @payable_invoice.total.to_f, 12.1 + + billing_profile = @payable_invoice.billing_profile + + billing_profile.vat_code = '123456' + billing_profile.alpha_two_country_code = 'LV' + billing_profile.save(validate: false) && @payable_invoice.reload && billing_profile.reload + + assert_equal @payable_invoice.total.to_f, 10.0 + end + + def test_invoice_should_be_recalculated_if_billing_profile_changed + assert_equal @payable_invoice.total.to_f, 10.0 + billing_profile = @payable_invoice.billing_profile + assert billing_profile.vat_code.present? + + private_person = billing_profiles(:private_person) + assert_not private_person.vat_code.present? + private_person.update(alpha_two_country_code: 'LV') && private_person.reload + + @payable_invoice.billing_profile = private_person + @payable_invoice.save(validate: false) && @payable_invoice.reload + + assert_equal Countries.vat_rate_from_alpha2_code(private_person.alpha_two_country_code), 0.21 + assert_equal @payable_invoice.total.to_f, 12.1 + end + def invoices_total(invoices) invoices.map(&:total) .reduce(:+)