Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update invoice total if billing profile has been updated #1215

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading