diff --git a/src/__tests__/fixtures/price.samples.ts b/src/__tests__/fixtures/price.samples.ts index 92776c8..1b10cda 100644 --- a/src/__tests__/fixtures/price.samples.ts +++ b/src/__tests__/fixtures/price.samples.ts @@ -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], diff --git a/src/__tests__/fixtures/pricing.results.ts b/src/__tests__/fixtures/pricing.results.ts index abcb503..d36ce34 100644 --- a/src/__tests__/fixtures/pricing.results.ts +++ b/src/__tests__/fixtures/pricing.results.ts @@ -6668,7 +6668,7 @@ export const computedResultWithPricesWithAndWithoutCoupons = { tax: { tax: { _id: '10', type: 'VAT', rate: 10 }, amount: 1364 }, }, ], - cashbacks: [] + cashbacks: [], }, }, items: [ @@ -6957,8 +6957,8 @@ export const computedPriceWithFixedAmountCashbackCoupon = { ], cashbacks: [ { - amount_total: 10000000000000, - cashback_period: "12", + amount_total: 1000, + cashback_period: '12', }, ], }, @@ -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, @@ -7101,7 +7101,7 @@ export const computedPriceWithPercentageCashbackCoupon = { ], cashbacks: [ { - amount_total: 10000000000000, + amount_total: 1000, cashback_period: '12', }, ], @@ -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, @@ -7245,7 +7245,7 @@ export const computedRecurringPriceWithFixedAmountCashbackCoupon = { ], cashbacks: [ { - amount_total: 10000000000000, + amount_total: 1000, cashback_period: '12', }, ], @@ -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, diff --git a/src/pricing.ts b/src/pricing.ts index c9fcb6a..bf469b8 100644 --- a/src/pricing.ts +++ b/src/pricing.ts @@ -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) @@ -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(), }); @@ -589,7 +590,7 @@ const recomputeDetailTotals = ( taxes, recurrences, recurrencesByTax, - cashbacks: cashbackTotals, + cashbacks, }, }, }; @@ -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(), + })), }, }, };