Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@epilot/pricing",
"version": "4.12.9",
"version": "4.12.10-0",
"description": "Pricing Library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -21,7 +21,7 @@
"gen-docs": "rm -rf docs && typedoc --plugin typedoc-plugin-markdown --out docs src && echo '{ \"label\": \"Pricing Library\", \"position\": 0 }' > docs/_category_.json",
"clean": "rm -rf dist",
"bundle-definition": "webpack --mode=production",
"build": "npm run clean && tsc && mkdir -p dist && cp -r src/types dist/types && npm run bundle-definition",
"build": "npm run clean && tsc && mkdir -p dist && cp -r src/types dist/types && npm run bundle-definition && npm run gen-docs",
"typegen": "echo '/* eslint-disable */\n/**\n * DO NOT MODIFY - GENERATED TYPES FROM OPENAPI\n **/' > src/types/pricing-types.d.ts && typegen ../../lambda/ApiHandlerFunction/openapi.yml >> src/types/pricing-types.d.ts",
"prepare": "npm run build",
"lint": "esw src --ext js,ts,tsx --cache --color",
Expand Down
201 changes: 186 additions & 15 deletions src/pricing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './types';

describe('computeAggregatedAndPriceTotals', () => {
describe('when is_composite_price = false', () => {
describe('when computing Simple Prices (is_composite_price = false)', () => {
it('should return the right result when there is one item per recurrence', () => {
const priceItems: PriceItemDto[] = [
samples.priceItem1,
Expand Down Expand Up @@ -483,7 +483,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -1818,
amount_total: -2000,
total_details: expect.objectContaining({
amount_tax: -182,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -525,7 +525,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -32727,
amount_total: -36000,
total_details: expect.objectContaining({
amount_tax: -3273,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -854,7 +854,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -2727,
amount_total: -3000,
total_details: expect.objectContaining({
amount_tax: -273,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -890,7 +890,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -54545,
amount_total: -60000,
total_details: expect.objectContaining({
amount_tax: -5455,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -1252,7 +1252,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -9091,
amount_total: -10000,
total_details: expect.objectContaining({
amount_tax: -909,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -1283,7 +1283,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -5455,
amount_total: -6000,
total_details: expect.objectContaining({
amount_tax: -545,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand All @@ -1296,9 +1296,178 @@ describe('computeAggregatedAndPriceTotals', () => {
);
});
});

describe('Negative Prices', () => {
it('should compute taxes as zero when dealing with simple negative prices, even if a tax is specified', () => {
const priceItems: PriceItemDto[] = [
{
...samples.priceItem1,
_price: {
...(samples.priceItem1._price as Price),
is_tax_inclusive: true,
},
unit_amount: -10000,
unit_amount_decimal: '-100.00',
quantity: 1,
},
];

const result = computeAggregatedAndPriceTotals(priceItems);

expect(result).toEqual(
expect.objectContaining({
amount_subtotal: -8403,
amount_total: -10000,
}),
);

expect(result.total_details).toEqual(
expect.objectContaining({
amount_tax: 0,
}),
);

expect(result.total_details?.breakdown).toEqual(
expect.objectContaining({
recurrences: [
expect.objectContaining({
amount_subtotal: -8403,
amount_tax: 0,
amount_total: -10000,
type: 'one_time',
}),
],
taxes: [{ amount: 0, tax: { _id: '19', rate: 19, type: 'VAT' } }],
}),
);

expect(result.items?.[0]).toEqual(
expect.objectContaining({
_price: expect.any(Object),
_product: expect.any(Object),
amount_subtotal: -8403,
amount_total: -10000,
taxes: [
{
amount: 0,
tax: expect.objectContaining({
_id: '19',
rate: 19,
}),
},
],
unit_amount: -10000,
unit_amount_decimal: '-100.00',
unit_amount_gross: -10000,
unit_amount_net: -8403,
}),
);
});

it('should compute taxes as zero when dealing with a composite with negative price components, even if a tax is specified', () => {
const priceItems: CompositePriceItemDto[] = [
{
...samples.compositePriceItemWithNegativePriceFlatFee,
quantity: 1,
},
];

const result = computeAggregatedAndPriceTotals(priceItems);

expect(result).toEqual(
expect.objectContaining({
amount_subtotal: -9091,
amount_total: -10000,
}),
);

expect(result.total_details).toEqual(
expect.objectContaining({
amount_tax: 0,
}),
);

expect(result.total_details?.breakdown).toEqual(
expect.objectContaining({
recurrences: [
expect.objectContaining({
amount_subtotal: -9091,
amount_tax: 0,
amount_total: -10000,
type: 'one_time',
}),
],
taxes: [{ amount: 0, tax: { _id: '10', rate: 10, type: 'VAT' } }],
}),
);

expect(result.items?.[0]).toEqual(expect.objectContaining({
_price: { is_composite_price: true },
amount_subtotal: -9091,
amount_total: -10000,
item_components: [
{
_price: {
_id: 'price#1-tiered-flat-fee',
is_tax_inclusive: true,
pricing_model: 'tiered_flatfee',
tiers: [
{ flat_fee_amount: -10000, flat_fee_amount_decimal: '-100.00', up_to: 5 },
{ flat_fee_amount: -8000, flat_fee_amount_decimal: '-80.00', up_to: 10 },
{ flat_fee_amount: -6000, flat_fee_amount_decimal: '-60.00' },
],
},
_product: undefined,
amount_subtotal: -9091,
amount_total: -10000,
currency: 'EUR',
price_id: 'price#1-tiered-flat-fee',
pricing_model: 'tiered_flatfee',
product_id: undefined,
quantity: 1,
taxes: [
{
amount: 0,
tax: {
_created_at: '2022-06-29T20:26:19.020Z',
_id: '10',
_org: '739224',
_schema: 'tax',
_title: '',
_updated_at: '2022-06-29T20:26:19.020Z',
rate: 10,
type: 'VAT',
},
},
],
type: undefined,
unit_amount: 0,
unit_amount_decimal: '0.0',
unit_amount_gross: -10000,
},
],
quantity: 1,
total_details: {
amount_tax: 0,
breakdown: {
recurrences: [
{
amount_subtotal: -9091,
amount_tax: 0,
amount_total: -10000,
type: 'one_time',
unit_amount_gross: -10000,
},
],
taxes: [{ amount: 0, tax: { _id: '10', rate: 10, type: 'VAT' } }],
},
}
}));
});
});
});

describe('when is_composite_price = true', () => {
describe('when computing with Composite Prices (is_composite_price = true)', () => {
it('should return 0 when number input is 0', () => {
const priceItems: CompositePriceItemDto[] = [samples.compositePriceWithNumberInputEqualsToZero];
expect(computeAggregatedAndPriceTotals(priceItems)).toStrictEqual(
Expand Down Expand Up @@ -1409,7 +1578,7 @@ describe('computeAggregatedAndPriceTotals', () => {
});
});

describe('when computing custom items', () => {
describe('when computing individual adjustments', () => {
it('should compute prices correctly when computing a composite price item with a custom item inside', () => {
const priceItems = [samples.compositePriceWithCustomItem];

Expand Down Expand Up @@ -1653,7 +1822,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -1818,
amount_total: -2000,
total_details: expect.objectContaining({
amount_tax: -182,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -1684,7 +1853,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -60000,
unit_amount_gross: -2400,
total_details: expect.objectContaining({
amount_tax: -6000,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -1841,7 +2010,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -909,
amount_total: -1000,
total_details: expect.objectContaining({
amount_tax: -91,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -1872,7 +2041,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -54545,
unit_amount_gross: -600,
total_details: expect.objectContaining({
amount_tax: -5455,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -2029,7 +2198,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -9091,
amount_total: -10000,
total_details: expect.objectContaining({
amount_tax: -909,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -2060,7 +2229,7 @@ describe('computeAggregatedAndPriceTotals', () => {
amount_subtotal: -5455,
amount_total: -6000,
total_details: expect.objectContaining({
amount_tax: -545,
amount_tax: 0,
}),
items: expect.arrayContaining([
expect.objectContaining({
Expand All @@ -2074,7 +2243,9 @@ describe('computeAggregatedAndPriceTotals', () => {
});
});
});
});

describe('Utility Functions', () => {
describe('extractPricingEntitiesBySlug', () => {
it('should return the pricing relations without duplicates', () => {
const priceItems = [
Expand Down
2 changes: 1 addition & 1 deletion src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ export const computePriceItem = (
taxes: [
{
...(priceTax ? { tax: priceTax } : { rate: 'nontaxable', rateValue: 0 }),
amount: itemValues.taxAmount,
amount: Number(itemValues.unitAmount) < 0 ? 0 : itemValues.taxAmount,
},
],
_price: {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const computePriceItemValues = (

const amountSubtotal = unitAmountNet.multiply(unitAmountMultiplier);
const amountTotal = unitAmountGross.multiply(unitAmountMultiplier);
const taxAmount = unitTaxAmount.multiply(unitAmountMultiplier);
const taxAmount = Number(unitAmount) < 0 ? d(0) : unitTaxAmount.multiply(unitAmountMultiplier);

return {
unitAmount: unitAmount.getAmount(),
Expand Down Expand Up @@ -160,7 +160,7 @@ export const computeTieredVolumePriceItemValues = (
unitAmountGross: d(tierValues.unitAmountGross!).getAmount(),
amountSubtotal: d(tierValues.amountSubtotal).getAmount(),
amountTotal: d(tierValues.amountTotal).getAmount(),
taxAmount: d(tierValues.taxAmount).getAmount(),
taxAmount: Number(tierValues.unitAmount) < 0 ? 0 : d(tierValues.taxAmount).getAmount(),
displayMode,
};
};
Expand Down Expand Up @@ -198,7 +198,7 @@ export const computeTieredFlatFeePriceItemValues = (
unitAmountGross: d(tierValues.unitAmountGross!).getAmount(),
amountSubtotal: d(tierValues.amountSubtotal).getAmount(),
amountTotal: d(tierValues.amountTotal).getAmount(),
taxAmount: d(tierValues.taxAmount).getAmount(),
taxAmount: Number(tierValues.unitAmount) < 0 ? 0 : d(tierValues.taxAmount).getAmount(),
displayMode,
};
};
Expand Down Expand Up @@ -236,7 +236,7 @@ export const computeTieredGraduatedPriceItemValues = (
unitAmountGross: d(totals.unitAmountGross!).add(d(tierValues.unitAmountGross!)).getAmount(),
amountSubtotal: d(totals.amountSubtotal).add(d(tierValues.amountSubtotal)).getAmount(),
amountTotal: d(totals.amountTotal).add(d(tierValues.amountTotal)).getAmount(),
taxAmount: d(totals.taxAmount).add(d(tierValues.taxAmount)).getAmount(),
taxAmount: Number(tierValues.unitAmount) < 0 ? 0 : d(totals.taxAmount).add(d(tierValues.taxAmount)).getAmount(),
displayMode,
};
},
Expand Down