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

Fix: adjust tax precise amount when issuing credit notes #2643

Merged
merged 9 commits into from
Oct 9, 2024

Conversation

annvelents
Copy link
Contributor

@annvelents annvelents commented Sep 30, 2024

Context

We have this issue that when we issue Invoice, we apply tax on sum of items,
but when we issue a credit note, we apply tax on each item separately.
as result, some values can be rounded in deffirent direction which result in not matching sum.

Example that our client had (all values are amount_cents):

Invoice 001
charge01 = 68_33
charge 02 = 68_33
charge03 = 57_50
charge04 = 85_00

if we apply 20% taxes on the sum of charges, we get:

(68_33 + 68_33 + 57_50 + 85_00) * 1.2 = 334_99,2

but because it is in cents, when issuing the invoice, the value is rounded to 334_99

Next, when we create credit notes for this invoice, and we create a separate CN per each item:

CN01 = 68_33 * 1.2 = 8199.6 => 82_00
CN02 = 57_50 * 1.2 = 69_00
CN03 = 85_00 * 1.2 = 102_00
CN04 = 68_33 * 1.2 = 8199.6 = 82_00

Sum of these credit notes is 335_00 which is more than the invoice total, and we fail vthe validation, despite the fees amount is correct

Description

To help this situation, we can operate adjustments that were made when credit notes are issued.
So when we're creating CN1-3, we simply create credit notes.
when we create CN4, we are subtracting all rounding adjustments that were made for all credit notes for this invoice. in our case, we will need to subtract 0.4 cents from precise taxes amount, and then for the last credit note
CN04 = 68_33 * 1.2 = 8199.6 -0.4 = 8199.2 => 8199
and the values are matching

also removed validation on credit_note_item total amount, as it tries to predict taxes, but they are adjusted further in the credit note creation service

@annvelents annvelents changed the title wip adjusting tax precise amount Fix: adjust tax precise amount when issuing credit notes Oct 4, 2024
@annvelents annvelents force-pushed the fix-rounding-adjustment-on-cn branch from 3026866 to 2507dad Compare October 4, 2024 12:32
@annvelents annvelents marked this pull request as ready for review October 4, 2024 13:21
Copy link
Contributor

@ancorcruz ancorcruz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

app/models/invoice.rb Outdated Show resolved Hide resolved
spec/support/scenarios_helper.rb Outdated Show resolved Hide resolved
spec/services/credit_notes/validate_item_service_spec.rb Outdated Show resolved Hide resolved
app/services/credit_notes/validate_item_service.rb Outdated Show resolved Hide resolved
app/services/credit_notes/estimate_service.rb Outdated Show resolved Hide resolved
spec/scenarios/credit_note_spec.rb Show resolved Hide resolved
@annvelents annvelents merged commit 41d4a71 into main Oct 9, 2024
6 checks passed
@annvelents annvelents deleted the fix-rounding-adjustment-on-cn branch October 9, 2024 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants