Skip to content

Commit

Permalink
Fix: adjust tax precise amount when issuing credit notes (#2643)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
annvelents authored Oct 9, 2024
1 parent c6a8738 commit 41d4a71
Show file tree
Hide file tree
Showing 9 changed files with 681 additions and 25 deletions.
12 changes: 12 additions & 0 deletions app/models/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def sub_total_excluding_taxes_amount_cents
end
alias_method :sub_total_excluding_taxes_amount_currency, :currency

def precise_total
items.sum(&:precise_amount_cents) - precise_coupons_adjustment_amount_cents + precise_taxes_amount_cents
end

def taxes_rounding_adjustment
taxes_amount_cents - precise_taxes_amount_cents
end

def rounding_adjustment
total_amount_cents - precise_total
end

private

def ensure_number
Expand Down
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def creditable_amount_cents
fee_rate = fee.creditable_amount_cents.fdiv(fees_total_creditable)
prorated_credit_amount = credit_adjustement * fee_rate
(fee.creditable_amount_cents - prorated_credit_amount) * (fee.taxes_rate || 0)
end.fdiv(100).round
end.fdiv(100).round # BECAUSE OF THIS ROUND the returned value is not precise

fees_total_creditable - credit_adjustement + vat
end
Expand Down
16 changes: 15 additions & 1 deletion app/services/credit_notes/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,24 @@ def compute_amounts_and_taxes
credit_note.precise_coupons_adjustment_amount_cents = taxes_result.coupons_adjustment_amount_cents
credit_note.coupons_adjustment_amount_cents = taxes_result.coupons_adjustment_amount_cents.round
credit_note.precise_taxes_amount_cents = taxes_result.taxes_amount_cents
credit_note.taxes_amount_cents = taxes_result.taxes_amount_cents.round
adjust_credit_note_tax_rounding if credit_note_for_all_remaining_amount?

credit_note.taxes_amount_cents = credit_note.precise_taxes_amount_cents.round
credit_note.taxes_rate = taxes_result.taxes_rate

taxes_result.applied_taxes.each { |applied_tax| credit_note.applied_taxes << applied_tax }
end

def credit_note_for_all_remaining_amount?
credit_note.invoice.creditable_amount_cents == 0
end

def adjust_credit_note_tax_rounding
credit_note.precise_taxes_amount_cents -= all_rounding_tax_adjustments
end

def all_rounding_tax_adjustments
credit_note.invoice.credit_notes.sum(&:taxes_rounding_adjustment)
end
end
end
22 changes: 20 additions & 2 deletions app/services/credit_notes/estimate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def validate_items
end
end

def valid_credit_note?
CreditNotes::ValidateService.new(result, item: credit_note).valid?
end

def valid_item?(item)
CreditNotes::ValidateItemService.new(result, item:).valid?
end
Expand All @@ -71,21 +75,35 @@ def compute_amounts_and_taxes
credit_note.precise_coupons_adjustment_amount_cents = taxes_result.coupons_adjustment_amount_cents
credit_note.coupons_adjustment_amount_cents = taxes_result.coupons_adjustment_amount_cents.round
credit_note.precise_taxes_amount_cents = taxes_result.taxes_amount_cents
credit_note.taxes_amount_cents = taxes_result.taxes_amount_cents.round
adjust_credit_note_tax_precise_rounding if credit_note_for_all_remaining_amount?

credit_note.taxes_amount_cents = credit_note.precise_taxes_amount_cents.round
credit_note.taxes_rate = taxes_result.taxes_rate

taxes_result.applied_taxes.each { |applied_tax| credit_note.applied_taxes << applied_tax }

credit_note.credit_amount_cents = (
credit_note.items.sum(&:amount_cents) -
taxes_result.coupons_adjustment_amount_cents +
taxes_result.taxes_amount_cents
credit_note.precise_taxes_amount_cents
).round

compute_refundable_amount
credit_note.total_amount_cents = credit_note.credit_amount_cents
end

def credit_note_for_all_remaining_amount?
credit_note.items.sum(&:precise_amount_cents) == credit_note.invoice.fees.sum(&:creditable_amount_cents)
end

def adjust_credit_note_tax_precise_rounding
credit_note.precise_taxes_amount_cents -= all_rounding_tax_adjustments
end

def all_rounding_tax_adjustments
credit_note.invoice.credit_notes.sum(&:taxes_rounding_adjustment)
end

def compute_refundable_amount
credit_note.refund_amount_cents = credit_note.credit_amount_cents

Expand Down
1 change: 0 additions & 1 deletion app/services/credit_notes/validate_item_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ def valid?

valid_item_amount?
valid_individual_amount?
valid_global_amount?

if errors?
result.validation_failure!(errors:)
Expand Down
72 changes: 67 additions & 5 deletions spec/models/credit_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
require 'rails_helper'

RSpec.describe CreditNote, type: :model do
subject(:credit_note) { create(:credit_note) }
subject(:credit_note) do
create :credit_note, credit_amount_cents: 11000, total_amount_cents: 11000, taxes_amount_cents: 1000,
taxes_rate: 10.0, precise_taxes_amount_cents: 1000
end

let(:item) { create(:credit_note_item, credit_note:, precise_amount_cents: 10000, amount_cents: 1000) }

it_behaves_like 'paper_trail traceable'

Expand Down Expand Up @@ -243,10 +248,35 @@
end
end

describe ' #sub_total_excluding_taxes_amount_cents' do
it 'returs the total amount without the taxes' do
expect(credit_note.sub_total_excluding_taxes_amount_cents)
.to eq(credit_note.items.sum(&:precise_amount_cents) - credit_note.precise_coupons_adjustment_amount_cents)
context 'when calculating depends on related items' do
before do
item
credit_note.reload
end

describe '#sub_total_excluding_taxes_amount_cents' do
it 'returs the total amount without the taxes' do
expect(credit_note.sub_total_excluding_taxes_amount_cents)
.to eq(credit_note.items.sum(&:precise_amount_cents) - credit_note.precise_coupons_adjustment_amount_cents)
end
end

describe '#precise_total' do
it 'returns the total precise amount including precise taxes' do
expect(credit_note.precise_total).to eq(11000)
end
end
end

describe '#taxes_rounding_adjustment' do
it 'returns the difference between taxes and precise taxes' do
expect(credit_note.taxes_rounding_adjustment).to eq(0)
end
end

describe '#rounding_adjustment' do
it 'returns the difference between credit note total and credit note precise total' do
expect(credit_note.taxes_rounding_adjustment).to eq(0)
end
end

Expand Down Expand Up @@ -328,4 +358,36 @@
end
end
end

context 'when taxes are not precise' do
subject(:credit_note) do
create :credit_note, credit_amount_cents: 8200, total_amount_cents: 8200, taxes_amount_cents: 1367,
taxes_rate: 20.0, precise_taxes_amount_cents: 1366.6
end

let(:item) { create(:credit_note_item, credit_note:, precise_amount_cents: 6833, amount_cents: 6833) }

before do
item
credit_note.reload
end

describe '#precise_total' do
it 'returns the total precise amount including precise taxes' do
expect(credit_note.precise_total).to eq(8199.6)
end
end

describe '#taxes_rounding_adjustment' do
it 'returns the difference between taxes and precise taxes' do
expect(credit_note.taxes_rounding_adjustment).to eq(0.4)
end
end

describe '#rounding_adjustment' do
it 'returns the difference between credit note total and credit note precise total' do
expect(credit_note.taxes_rounding_adjustment).to eq(0.4)
end
end
end
end
Loading

0 comments on commit 41d4a71

Please sign in to comment.