Skip to content

Commit

Permalink
Fix formatting and precision issues
Browse files Browse the repository at this point in the history
  • Loading branch information
José Carneiro committed Nov 18, 2024
1 parent 9eac5b6 commit b5d7777
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/__tests__/fixtures/price.samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3049,10 +3049,9 @@ const baseForPriceItemWithCashback: PriceItem = {
is_tax_inclusive: true,
cashback_amount: 1000,
cashback_amount_decimal: '10.00',
cashback_period:"12",
cashback_period: '12',
};


export const priceItemWithFixedDiscount: PriceItemDto = {
...baseForPriceItemWithDiscount,
_coupons: [fixedDiscountCoupon],
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/fixtures/pricing.results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6668,7 +6668,7 @@ export const computedResultWithPricesWithAndWithoutCoupons = {
tax: { tax: { _id: '10', type: 'VAT', rate: 10 }, amount: 1364 },
},
],
cashbacks: []
cashbacks: [],
},
},
items: [
Expand Down Expand Up @@ -6957,8 +6957,8 @@ export const computedPriceWithFixedAmountCashbackCoupon = {
],
cashbacks: [
{
amount_total: 10000000000000,
cashback_period: "12",
amount_total: 1000,
cashback_period: '12',
},
],
},
Expand Down Expand Up @@ -7038,7 +7038,7 @@ export const computedPriceWithFixedAmountCashbackCoupon = {
unit_amount_decimal: '100',
amount_subtotal: 9091,
amount_total: 10000,
cashback_period: "12",
cashback_period: '12',
cashback_amount: 1000,
cashback_amount_decimal: '10',
after_cashback_amount_total: 9000,
Expand Down Expand Up @@ -7101,7 +7101,7 @@ export const computedPriceWithPercentageCashbackCoupon = {
],
cashbacks: [
{
amount_total: 10000000000000,
amount_total: 1000,
cashback_period: '12',
},
],
Expand Down Expand Up @@ -7180,7 +7180,7 @@ export const computedPriceWithPercentageCashbackCoupon = {
unit_amount_decimal: '100',
amount_subtotal: 9091,
amount_total: 10000,
cashback_period: "12",
cashback_period: '12',
cashback_amount: 1000,
cashback_amount_decimal: '10',
after_cashback_amount_total: 9000,
Expand Down Expand Up @@ -7245,7 +7245,7 @@ export const computedRecurringPriceWithFixedAmountCashbackCoupon = {
],
cashbacks: [
{
amount_total: 10000000000000,
amount_total: 1000,
cashback_period: '12',
},
],
Expand Down Expand Up @@ -7335,7 +7335,7 @@ export const computedRecurringPriceWithFixedAmountCashbackCoupon = {
unit_amount_decimal: '100',
amount_subtotal: 9091,
amount_total: 10000,
cashback_period: "12",
cashback_period: '12',
cashback_amount: 1000,
cashback_amount_decimal: '10',
after_cashback_amount_total: 9917,
Expand Down
19 changes: 12 additions & 7 deletions src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ const recomputeDetailTotals = (
const subtotal = toDineroFromInteger(details.amount_subtotal!);
const totalTax = toDineroFromInteger(details?.total_details?.amount_tax!);

const cashbackTotals = details.total_details?.breakdown?.cashbacks || [];
const cashbacks = [...(details.total_details?.breakdown?.cashbacks ?? [])];

const priceUnitAmountGross = toDineroFromInteger(priceItemToAppend.unit_amount_gross!);
const priceUnitAmountNet = Number.isInteger(priceItemToAppend.unit_amount_net)
Expand Down Expand Up @@ -565,14 +565,15 @@ const recomputeDetailTotals = (

// Cashback totals
if (priceCashBackAmount) {
const cashbackPeriod = priceItemToAppend?.cashback_period;
const cashbackMatchIndex = cashbackTotals.findIndex((cashback) => cashback.cashback_period === cashbackPeriod);
const cashbackPeriod = priceItemToAppend.cashback_period;
const cashbackMatchIndex = cashbacks.findIndex((cashback) => cashback.cashback_period === cashbackPeriod);

if (cashbackMatchIndex !== -1) {
const cashbackAmountTotal = toDineroFromInteger(cashbackTotals[cashbackMatchIndex].amount_total!);
cashbackTotals[cashbackMatchIndex].amount_total = cashbackAmountTotal.add(priceCashBackAmount).getAmount();
const matchingCashback = cashbacks[cashbackMatchIndex]!;
const cashbackAmountTotal = toDineroFromInteger(matchingCashback.amount_total!);
matchingCashback.amount_total = cashbackAmountTotal.add(priceCashBackAmount).getAmount();
} else {
cashbackTotals.push({
cashbacks.push({
cashback_period: cashbackPeriod,
amount_total: priceCashBackAmount.getAmount(),
});
Expand All @@ -589,7 +590,7 @@ const recomputeDetailTotals = (
taxes,
recurrences,
recurrencesByTax,
cashbacks: cashbackTotals,
cashbacks,
},
},
};
Expand Down Expand Up @@ -1034,6 +1035,10 @@ const convertBreakDownPrecision = (details: PricingDetails | CompositePriceItem,
},
};
}),
cashbacks: details.total_details?.breakdown?.cashbacks?.map((cashback) => ({
...cashback,
amount_total: toDineroFromInteger(cashback.amount_total!).convertPrecision(precision).getAmount(),
})),
},
},
};
Expand Down

0 comments on commit b5d7777

Please sign in to comment.