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
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
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 @@ -272,7 +272,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 @@ -204,10 +204,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