Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tidy-tigers-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@epilot/pricing': patch
---

Support TaxItem (an ad-hoc tax rate with no backing entity) alongside Tax wherever a tax object is accepted or returned, following the @epilot/pricing-client bump that introduced it. Ad-hoc/custom line items with no product or price reference can now be taxed without a full Tax entity.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@
"dinero.js": "^1.9.1"
},
"peerDependencies": {
"@epilot/pricing-client": "^3.53.8",
"@epilot/pricing-client": "^3.55.1",
"i18next": "^25.1.2"
},
"devDependencies": {
"@changesets/cli": "^2.30.0",
"@epilot/eslint-config": "^3.0.5",
"@epilot/pricing-client": "^3.53.8",
"@epilot/pricing-client": "^3.55.1",
"@types/dinero.js": "^1.9.4",
"@vitest/coverage-v8": "^1.6.1",
"concurrently": "^9.1.2",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion src/computations/apply-discounts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TaxItem } from '@epilot/pricing-client';
import { isCashbackCoupon, isFixedValueCoupon, isPercentageCoupon } from '../coupons/guards';
import { toDineroFromInteger, toDinero } from '../money/to-dinero';
import { PricingModel } from '../prices/constants';
Expand All @@ -21,7 +22,7 @@ export const applyDiscounts = (
currency: Currency;
isTaxInclusive: boolean;
unitAmountMultiplier: number;
tax?: Tax;
tax?: Tax | TaxItem;
coupon: Coupon;
},
): PriceItemsTotals => {
Expand Down
4 changes: 2 additions & 2 deletions src/computations/compute-external-dynamic-tariff-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PriceDynamicTariff, Tax } from '@epilot/pricing-client';
import type { PriceDynamicTariff, Tax, TaxItem } from '@epilot/pricing-client';
import type { Currency } from 'dinero.js';
import { toDinero } from '../money/to-dinero';
import { ModeDynamicTariff } from '../prices/constants';
Expand All @@ -19,7 +19,7 @@ export const computeExternalDynamicTariffValues = ({
isTaxInclusive: boolean;
unitAmountMultiplier: number;
externalFeeAmountDecimal?: string;
tax?: Tax;
tax?: Tax | TaxItem;
}): PriceItemsTotals => {
if (externalFeeAmountDecimal === undefined || dynamicTariff === undefined) {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/computations/compute-external-get-agitem-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PriceGetAg, Tax } from '@epilot/pricing-client';
import type { PriceGetAg, Tax, TaxItem } from '@epilot/pricing-client';
import type { Currency } from 'dinero.js';
import { toDinero, toDineroFromInteger } from '../money/to-dinero';
import { MarkupPricingModel, TypeGetAg } from '../prices/constants';
Expand All @@ -22,7 +22,7 @@ export const computeExternalGetAGItemValues = ({
unitAmountMultiplier: number;
userInput: number;
externalFeeAmountDecimal?: string;
tax?: Tax;
tax?: Tax | TaxItem;
}): PriceItemsTotals => {
if (externalFeeAmountDecimal === undefined || getAg === undefined || userInput === 0) {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/computations/compute-per-unit-price-item-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tax } from '@epilot/pricing-client';
import type { Tax, TaxItem } from '@epilot/pricing-client';
import type { Currency, Dinero } from 'dinero.js';
import { toDinero } from '../money/to-dinero';
import type { PriceItemsTotals } from '../prices/types';
Expand All @@ -15,7 +15,7 @@ export const computePerUnitPriceItemValues = ({
currency: Currency;
isTaxInclusive: boolean;
unitAmountMultiplier: number;
tax?: Tax;
tax?: Tax | TaxItem;
}): PriceItemsTotals => {
const unitAmount = toDinero(unitAmountDecimal, currency);
const taxRate = getTaxValue(tax);
Expand Down
3 changes: 2 additions & 1 deletion src/computations/compute-price-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
RedeemedPromo,
PriceItem,
Price,
TaxItem,
} from '@epilot/pricing-client';
import type { Currency } from 'dinero.js';
import { isValidCoupon } from '../coupons/guards';
Expand Down Expand Up @@ -86,7 +87,7 @@ export const computePriceItem = (
externalFeeMapping,
redeemedPromos,
}: {
tax?: Tax;
tax?: Tax | TaxItem;
quantity: number;
priceMapping?: PriceInputMapping;
externalFeeMapping?: ExternalFeeMapping;
Expand Down
4 changes: 2 additions & 2 deletions src/computations/compute-tiered-flat-fee-price-item-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PriceTier, Tax, Price } from '@epilot/pricing-client';
import type { PriceTier, Tax, Price, TaxItem } from '@epilot/pricing-client';
import type { Currency } from 'dinero.js';
import { toDineroFromInteger } from '../money/to-dinero';
import type { PriceItemsTotals } from '../prices/types';
Expand All @@ -19,7 +19,7 @@ export const computeTieredFlatFeePriceItemValues = ({
currency: Currency;
isTaxInclusive: boolean;
quantityToSelectTier: number;
tax?: Tax;
tax?: Tax | TaxItem;
quantity: number;
isUsingPriceMappingToSelectTier: boolean;
unchangedPriceDisplayInJourneys: Price['price_display_in_journeys'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PriceTier, Tax, Price } from '@epilot/pricing-client';
import type { PriceTier, Tax, Price, TaxItem } from '@epilot/pricing-client';
import type { Currency } from 'dinero.js';
import { toDineroFromInteger } from '../money/to-dinero';
import type { PriceItemsTotals } from '../prices/types';
Expand All @@ -20,7 +20,7 @@ export const computeTieredGraduatedPriceItemValues = ({
currency: Currency;
isTaxInclusive: boolean;
quantityToSelectTier: number;
tax?: Tax;
tax?: Tax | TaxItem;
quantity: number;
isUsingPriceMappingToSelectTier: boolean;
unchangedPriceDisplayInJourneys: Price['price_display_in_journeys'];
Expand Down
4 changes: 2 additions & 2 deletions src/computations/compute-tiered-volume-price-item-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PriceTier, Tax, Price } from '@epilot/pricing-client';
import type { PriceTier, Tax, Price, TaxItem } from '@epilot/pricing-client';
import type { Currency } from 'dinero.js';
import { toDineroFromInteger } from '../money/to-dinero';
import type { PriceItemsTotals } from '../prices/types';
Expand All @@ -18,7 +18,7 @@ export const computeTieredVolumePriceItemValues = ({
currency: Currency;
isTaxInclusive: boolean;
quantityToSelectTier: number;
tax: Tax | undefined;
tax: Tax | TaxItem | undefined;
unitAmountMultiplier: number;
unchangedPriceDisplayInJourneys: Price['price_display_in_journeys'];
}): PriceItemsTotals => {
Expand Down
19 changes: 14 additions & 5 deletions src/computations/compute-totals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
Price,
PriceItem,
Tax,
TaxItem,
Currency,
} from '../shared/types';
import { computeCompositePrice } from './compute-composite-price';
Expand All @@ -29,6 +30,13 @@ type ComputeAggregatedAndPriceTotalsOptions = {
redeemedPromos?: Array<RedeemedPromo>;
};

/**
* TaxItem (an ad-hoc tax with no backing entity) has no _id — unlike Tax, it
* can't be shared/deduplicated across price items by identity, only by rate.
*/
const getTaxId = (tax?: Tax | TaxItem | Partial<Tax>): string | undefined =>
tax && '_id' in tax ? tax._id : undefined;

/**
* Computes all the integer amounts for the price items using the string decimal representation defined on prices unit_amount field.
* All totals are computed with a decimal precision of DECIMAL_PRECISION.
Expand Down Expand Up @@ -161,9 +169,10 @@ const recomputeDetailTotals = (
* itemRateValue is only used for outdated prices, not migrated yet
*/
const itemRateValue = priceItemToAppend.taxes?.[0]?.rateValue;
const itemTaxId = getTaxId(itemTax);
const tax = taxes.find(
(item) =>
(item.tax?._id && itemTax?._id && item.tax?._id === itemTax?._id) ||
(item.tax?._id && itemTaxId && item.tax?._id === itemTaxId) ||
item.tax?.rate === itemTax?.rate ||
item.tax?.rate === itemRateValue,
);
Expand Down Expand Up @@ -218,19 +227,19 @@ const recomputeDetailTotals = (

if (tax.tax && itemTax) {
// Populates missing data in deprecated taxes
if (!tax.tax._id && itemTax._id) {
tax.tax._id = itemTax._id;
if (!tax.tax._id && itemTaxId) {
tax.tax._id = itemTaxId;
}

if (!tax.tax.type && itemTax.type) {
tax.tax.type = itemTax.type;
}
}
} else if (itemTax) {
const { _id, type, rate } = itemTax;
const { type, rate } = itemTax;
taxes.push({
tax: {
...(_id && { _id }),
...(itemTaxId && { _id: itemTaxId }),
...(type && { type }),
rate,
},
Expand Down
7 changes: 6 additions & 1 deletion src/prices/get-price-tax.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { TaxItem } from '@epilot/pricing-client';
import type { Tax, Price, TaxAmountDto } from '../shared/types';

/**
* Gets a price tax with the proper tax behavior override
*/
export const getPriceTax = (applicableTax?: Tax, price?: Price, priceItemTaxes?: TaxAmountDto[]): Tax | undefined => {
export const getPriceTax = (
applicableTax?: Tax | TaxItem,
price?: Price,
priceItemTaxes?: TaxAmountDto[],
): Tax | TaxItem | undefined => {
if (applicableTax) {
return applicableTax;
}
Expand Down
1 change: 1 addition & 0 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
PriceItems,
PriceItemsDto,
Tax,
TaxItem,
TaxAmountDto,
TaxAmount,
CompositePrice,
Expand Down
4 changes: 2 additions & 2 deletions src/taxes/extract-tax-from-price.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { CompositePriceItem, PriceItem, Tax } from '@epilot/pricing-client';
import type { CompositePriceItem, PriceItem, Tax, TaxItem } from '@epilot/pricing-client';
import { isCompositePriceItem } from '../prices/utils';

export const extractTaxFromPriceItem = (item: PriceItem | CompositePriceItem) => {
let tax: Tax | undefined = undefined;
let tax: Tax | TaxItem | undefined = undefined;

if (
isCompositePriceItem(item) &&
Expand Down
4 changes: 2 additions & 2 deletions src/taxes/get-tax-value.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Tax } from '@epilot/pricing-client';
import type { Tax, TaxItem } from '@epilot/pricing-client';
import { TaxRates } from './constants';

export const getTaxValue = (tax?: Tax): number => {
export const getTaxValue = (tax?: Tax | TaxItem): number => {
if (!tax) {
return TaxRates.nontaxable;
}
Expand Down
4 changes: 2 additions & 2 deletions src/variables/getag/meter-fees-details.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Currency } from 'dinero.js';
import type { Tax } from '../../shared/types';
import type { Tax, TaxItem } from '../../shared/types';
import type { I18n } from '../../shared/types';
import type { TimeFrequency } from '../../time-frequency/types';
import type { ExternalFeesMetadata, ExternalFeesDetails, ExternalFeesDetailsGroup } from '../types';
Expand All @@ -12,7 +12,7 @@ export const processMeterFeesDetails = (
i18n: I18n,
billingPeriod: TimeFrequency,
unitPricePeriod: TimeFrequency,
tax?: Tax,
tax?: Tax | TaxItem,
variableUnit?: string,
) => {
if (!result.groups) {
Expand Down
4 changes: 2 additions & 2 deletions src/variables/getag/network-fees-details.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Currency, I18n, Tax } from '../../shared/types';
import type { Currency, I18n, Tax, TaxItem } from '../../shared/types';
import type { TimeFrequency } from '../../time-frequency/types';
import type { ExternalFeesMetadata, ExternalFeesDetails, ExternalFeesDetailsGroup } from '../types';
import { getDetailsFee } from './utils';
Expand All @@ -10,7 +10,7 @@ export const processNetworkOperatingFeesDetails = (
i18n: I18n,
billingPeriod: TimeFrequency,
unitPricePeriod: TimeFrequency,
tax?: Tax,
tax?: Tax | TaxItem,
variableUnit?: string,
) => {
const type = externalFeesMetadata.inputs.type || 'power';
Expand Down
4 changes: 2 additions & 2 deletions src/variables/getag/other-fees-details.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Currency } from 'dinero.js';
import type { Tax } from '../../shared/types';
import type { Tax, TaxItem } from '../../shared/types';
import type { I18n } from '../../shared/types';
import type { TimeFrequency } from '../../time-frequency/types';
import type { ExternalFeesMetadata, ExternalFeesDetails, ExternalFeesDetailsGroup } from '../types';
Expand All @@ -12,7 +12,7 @@ export const processOtherFeesDetails = (
i18n: I18n,
billingPeriod: TimeFrequency,
unitPricePeriod: TimeFrequency,
tax?: Tax,
tax?: Tax | TaxItem,
variableUnit?: string,
) => {
const type = externalFeesMetadata.inputs.type || 'power';
Expand Down
4 changes: 2 additions & 2 deletions src/variables/getag/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { PriceGetAg, TariffTypeGetAg } from '@epilot/pricing-client';
import { formatFeeAmountFromString } from '../../getag/formatters';
import { DEFAULT_CURRENCY } from '../../money/constants';
import { toDinero } from '../../money/to-dinero';
import type { Currency, I18n, Tax } from '../../shared/types';
import type { Currency, I18n, Tax, TaxItem } from '../../shared/types';
import { getAmountWithTax } from '../../taxes/get-amount-with-tax';
import { normalizeValueToFrequencyUnit } from '../../time-frequency/normalizers';
import type { TimeFrequency } from '../../time-frequency/types';
Expand Down Expand Up @@ -77,7 +77,7 @@ const getDetailsFee = ({
i18n: I18n;
billingPeriod: TimeFrequency;
unitPricePeriod: TimeFrequency;
tax?: Tax;
tax?: Tax | TaxItem;
variableUnit?: string;
}): ExternalFeesDetailsFee | undefined => {
if (!fee) {
Expand Down
Loading