Skip to content

Commit c002822

Browse files
committed
feat: add tests for product invoice tax totals
1 parent e04eb59 commit c002822

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

spec/services/invoices/compute_amounts_from_fees_spec.rb

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
precise_amount_cents: 151
5353
)
5454
end
55+
let(:fee2) do
56+
create(
57+
:product_fee,
58+
invoice:,
59+
subscription:,
60+
rate_card_rate:,
61+
amount_cents: 379,
62+
precise_amount_cents: 379,
63+
precise_coupons_amount_cents: 100
64+
)
65+
end
5566

5667
before do
5768
create(:rate_card_applied_tax, rate_card:, tax: tax1, organization:)
@@ -62,6 +73,92 @@
6273

6374
expect(fee1.reload.applied_taxes.map(&:tax_code)).to contain_exactly(tax1.code)
6475
expect(fee1).to have_attributes(taxes_rate: 10, taxes_amount_cents: 15)
76+
expect(invoice.applied_taxes.sole).to have_attributes(
77+
tax: tax1,
78+
tax_code: tax1.code,
79+
tax_rate: 10,
80+
amount_cents: 43,
81+
fees_amount_cents: 430
82+
)
83+
expect(invoice).to have_attributes(
84+
taxes_rate: 10,
85+
taxes_amount_cents: 43,
86+
sub_total_including_taxes_amount_cents: 473,
87+
total_amount_cents: 473
88+
)
89+
end
90+
91+
context "with different taxes that have the same rate" do
92+
let(:same_rate_tax) { create(:tax, organization:, rate: tax1.rate, code: "same-rate-tax") }
93+
94+
before do
95+
create(:rate_card_applied_tax, rate_card:, tax: same_rate_tax, organization:)
96+
end
97+
98+
it "keeps separate invoice tax groups" do
99+
compute_amounts.call
100+
101+
expect(invoice.applied_taxes.order(:tax_code).pluck(:tax_code, :tax_rate, :amount_cents)).to eq(
102+
[
103+
[same_rate_tax.code, 10, 43],
104+
[tax1.code, 10, 43]
105+
]
106+
)
107+
end
108+
end
109+
110+
context "with a zero-rate tax" do
111+
let(:zero_rate_tax) { create(:tax, organization:, rate: 0, code: "zero-rate-tax") }
112+
113+
before do
114+
create(:rate_card_applied_tax, rate_card:, tax: zero_rate_tax, organization:)
115+
end
116+
117+
it "keeps the zero-rate tax on the invoice" do
118+
compute_amounts.call
119+
120+
expect(invoice.applied_taxes.find_by!(tax: zero_rate_tax)).to have_attributes(
121+
tax_code: zero_rate_tax.code,
122+
tax_rate: 0,
123+
amount_cents: 0,
124+
fees_amount_cents: 430
125+
)
126+
end
127+
end
128+
129+
context "when fee tax amounts require rounding" do
130+
let(:fee1) do
131+
create(
132+
:product_fee,
133+
invoice:,
134+
subscription:,
135+
rate_card_rate:,
136+
amount_cents: 55,
137+
precise_amount_cents: 55
138+
)
139+
end
140+
let(:fee2) do
141+
create(
142+
:product_fee,
143+
invoice:,
144+
subscription:,
145+
rate_card_rate:,
146+
amount_cents: 55,
147+
precise_amount_cents: 55
148+
)
149+
end
150+
151+
before do
152+
invoice.credits.destroy_all
153+
end
154+
155+
it "rounds the combined invoice tax amount" do
156+
compute_amounts.call
157+
158+
expect(invoice.fees.sum(&:taxes_amount_cents)).to eq(12)
159+
expect(invoice.applied_taxes.sole.amount_cents).to eq(11)
160+
expect(invoice.taxes_amount_cents).to eq(11)
161+
end
65162
end
66163
end
67164

@@ -188,6 +285,42 @@
188285
expect(invoice.total_amount_cents).to eq(272)
189286
end
190287

288+
context "when the invoice contains a product fee" do
289+
let(:plan) { create(:plan, organization:) }
290+
let(:subscription) { create(:subscription, organization:, customer:, plan:) }
291+
let(:rate_card) { create(:rate_card, organization:) }
292+
let(:rate_card_rate) { create(:rate_card_rate, organization:, rate_card:) }
293+
let(:fee1) do
294+
create(
295+
:product_fee,
296+
invoice:,
297+
subscription:,
298+
rate_card_rate:,
299+
amount_cents: 151,
300+
precise_amount_cents: 151
301+
)
302+
end
303+
304+
it "preserves provider tax metadata on the invoice" do
305+
described_class.new(invoice:, provider_taxes: [fee_taxes]).call
306+
307+
expect(invoice.applied_taxes.order(:tax_code).pluck(
308+
:tax_description,
309+
:tax_code,
310+
:tax_name,
311+
:tax_rate,
312+
:amount_cents,
313+
:taxable_base_amount_cents
314+
)).to eq(
315+
[
316+
["type1", "tax_1", "tax 1", 50, 76, 151],
317+
["type2", "tax_2", "tax 2", 30, 45, 151]
318+
]
319+
)
320+
expect(invoice).to have_attributes(taxes_amount_cents: 121, taxes_rate: 80)
321+
end
322+
end
323+
191324
context "when provider taxes are not provided" do
192325
subject(:compute_amounts) { described_class.new(invoice:, provider_taxes: nil) }
193326

0 commit comments

Comments
 (0)