Skip to content

Commit

Permalink
fix: fix nontaxable prices
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Nardo committed Jan 9, 2024
1 parent 9be195b commit 3749279
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/__tests__/fixtures/price.samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,35 @@ export const priceItemNonTaxable: PriceItemDto = {
pricing_model: 'per_unit',
};

export const priceItemNonTaxable2: PriceItemDto = {
quantity: 1,
product_id: 'prod-id#12325',
price_id: 'price#4',
_price: {
_id: 'price#4',
unit_amount: 2000,
unit_amount_currency: 'EUR',
unit_amount_decimal: '20.00',
type: 'recurring',
billing_period: 'monthly',
billing_duration_amount: 1,
billing_duration_unit: 'years',
notice_time_amount: 1,
notice_time_unit: 'months',
termination_time_amount: 2,
termination_time_unit: 'weeks',
renewal_duration_amount: 1,
renewal_duration_unit: 'years',
tax: null!,
is_tax_inclusive: true,
description: 'Winter Lease 2',
_title: 'Winter Lease 2',
pricing_model: 'per_unit',
},
_product: {},
pricing_model: 'per_unit',
};

export const compositePrice: CompositePriceItemDto = {
price_id: 'price#4',
product_id: 'prod-id#1234',
Expand Down
44 changes: 44 additions & 0 deletions src/pricing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,50 @@ describe('computeAggregatedAndPriceTotals', () => {
);
});

it('should return the right result when multiple prices are nontaxable', () => {
const priceItems = [samples.priceItemNonTaxable, samples.priceItemNonTaxable2];

const result = computeAggregatedAndPriceTotals(priceItems);

expect(result).toEqual(
expect.objectContaining({
amount_subtotal: 3000,
amount_total: 3000,
items: expect.arrayContaining([
expect.objectContaining({
amount_subtotal: 1000,
amount_total: 1000,
unit_amount_decimal: '10.00',
unit_amount_gross: 1000,
unit_amount_net: 1000,
taxes: [
{
amount: 0,
rate: 'nontaxable',
rateValue: 0,
},
],
}),
expect.objectContaining({
amount_subtotal: 2000,
amount_total: 2000,
unit_amount_decimal: '20.00',
unit_amount_gross: 2000,
unit_amount_net: 2000,
taxes: [
{
amount: 0,
rate: 'nontaxable',
rateValue: 0,
},
],
}),
]),
total_details: expect.objectContaining({ amount_tax: 0 }),
}),
);
});

it('should return the right result when quantity=0', () => {
const priceItems = [{ ...samples.priceItem1, quantity: 0 }];

Expand Down
4 changes: 2 additions & 2 deletions src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ const recomputeDetailTotals = (details: PricingDetails, price: Price, priceItemT
recurrence.amount_tax = taxAmount.add(priceTax).getAmount();
}

const recurrenceTax = !tax && itemTax ? taxes.at(-1) : tax;
const recurrenceTax = !tax && itemTax ? taxes?.[taxes?.length - 1] : tax;

if (!recurrenceByTax) {
const type = price?.type || priceItemToAppend?.type;
Expand All @@ -449,7 +449,7 @@ const recomputeDetailTotals = (details: PricingDetails, price: Price, priceItemT
amount_total: priceTotal.getAmount(),
amount_subtotal: priceSubtotal.getAmount(),
amount_tax: priceTax.getAmount(),
tax: recurrenceTax,
tax: recurrenceTax ?? { amount: 0, rate: 'nontaxable', rateValue: 0, tax: { rate: 0 } },
});
} else {
const totalAmount = d(recurrenceByTax.amount_total);
Expand Down

0 comments on commit 3749279

Please sign in to comment.