Skip to content

Commit

Permalink
Merge pull request #1215 from internetee/update-invoice-after-update-…
Browse files Browse the repository at this point in the history
…billing-profile

update invoice total if billing profile has been updated
  • Loading branch information
vohmar authored Feb 16, 2024
2 parents b7b6019 + c4b140c commit be34862
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 0 additions & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def deposit
end

def recalculate_vat_rate
return if billing_profile_id == billing_profile_id_was
return unless payable?

self.vat_rate = assign_vat_rate
Expand Down
34 changes: 34 additions & 0 deletions test/models/invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(:+)
Expand Down

0 comments on commit be34862

Please sign in to comment.