Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/tiers/compute-cumulative-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ describe('computeCumulativeValue', () => {
tiers | quantityToSelectTier | unit | locale | currency | isTaxInclusive | showOnRequest | showStartsAt | tax | expected
${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }}
${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }}
${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }}
${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '11,90\xa0€/kWh', totalAmountDecimal: '11,90\xa0€' }] }}
${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }}
${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }}
${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }}
${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }}
${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '11,90\xa0€/kWh', totalAmountDecimal: '23,80\xa0€' }] }}
${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }}
${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }}
${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', subAverage: '€8.12/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }}
Expand Down
9 changes: 6 additions & 3 deletions src/tiers/compute-cumulative-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const computeCumulativeValue = (
};

const breakdown: CumulativePriceBreakdownItem[] = [];
const taxRate = getTaxValue(tax);

const total = priceTiersForQuantity.reduce(
(total: Dinero, tier: PriceTier, index: number) => {
Expand All @@ -84,17 +85,20 @@ export const computeCumulativeValue = (
quantity: quantityToSelectTier,
});
const tierAmount = toDinero(tier.unit_amount_decimal!, formatOptions.currency).multiply(graduatedQuantity);
const tierAmountWithTax = isTaxInclusive ? tierAmount : tierAmount.multiply(1 + taxRate);
const unitAmount = toDinero(tier.unit_amount_decimal!, formatOptions.currency);
const unitAmountWithTax = isTaxInclusive ? unitAmount : unitAmount.multiply(1 + taxRate);

breakdown.push({
quantityUsed: `${graduatedQuantity.toLocaleString(formatOptions.locale, {
maximumFractionDigits: 6,
})} ${formattedUnit}`,
tierAmountDecimal: `${formatAmountFromString({
decimalAmount: tier.unit_amount_decimal!,
decimalAmount: addSeparatorToDineroString(unitAmountWithTax.getAmount().toString()),
...formatOptions,
})}${formattedUnit ? `/${formattedUnit}` : ''}`,
totalAmountDecimal: formatAmountFromString({
decimalAmount: addSeparatorToDineroString(tierAmount.getAmount().toString()),
decimalAmount: addSeparatorToDineroString(tierAmountWithTax.getAmount().toString()),
...formatOptions,
}),
});
Expand All @@ -111,7 +115,6 @@ export const computeCumulativeValue = (
defaultValue: 'Starts at',
});

const taxRate = getTaxValue(tax);
const amountTotal = isTaxInclusive ? total : total.multiply(1 + taxRate);
const amountSubtotal = isTaxInclusive ? total.divide(1 + taxRate) : total;

Expand Down
Loading