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/itchy-baths-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@epilot/pricing': patch
---

Handle correct precision on net unit amount values
28 changes: 27 additions & 1 deletion src/variables/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
import { fixedDiscountCoupon } from '../coupons/__tests__/coupon.fixtures';
import type { I18n } from '../shared/types';
import type { PriceItemWithParent } from './types';
import { getUnitAmount } from './utils';
import { getDecimalPrecision, getUnitAmount } from './utils';

const mockI18n = {
t: (key: string, fallback: string) => key || fallback,
Expand Down Expand Up @@ -48,3 +48,29 @@ describe('getUnitAmount', () => {
expect(grossAmount).toBe('789,46\xa0€');
});
});

describe('getDecimalPrecision', () => {
it('returns 2 when input is undefined', () => {
expect(getDecimalPrecision(undefined)).toBe(2);
});

it('returns 2 when input has no decimal point', () => {
expect(getDecimalPrecision('12')).toBe(2);
});

it('returns 2 for a two-decimal string', () => {
expect(getDecimalPrecision('12.00')).toBe(2);
});

it('returns 1 for a one-decimal string', () => {
expect(getDecimalPrecision('12.5')).toBe(1);
});

it('returns the full precision for a high-precision string', () => {
expect(getDecimalPrecision('0.12345')).toBe(5);
});

it('returns the full precision for a high-precision string', () => {
expect(getDecimalPrecision('10.12345')).toBe(5);
});
});
18 changes: 15 additions & 3 deletions src/variables/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ export const getUnitAmount = (
});
};

export const getDecimalPrecision = (unitAmountDecimal?: string): number => {
const dotIndex = unitAmountDecimal?.indexOf('.');
return unitAmountDecimal && dotIndex !== undefined && dotIndex !== -1 ? unitAmountDecimal.length - dotIndex - 1 : 2;
};

const getTieredUnitAmount = (
item: PriceItemWithParent,
i18n: I18n,
Expand Down Expand Up @@ -248,6 +253,8 @@ const getTieredUnitAmount = (
item._price.pricing_model as PricingModel,
);

const netPrecision = useUnitAmountNet ? getDecimalPrecision(displayableTier?.unit_amount_decimal) : undefined;

const descriptionUnit = '';
const descriptionCurrency = item.currency as Currency;
const descriptionTranslationFactory = (key: string) => i18n.t(`table_order.${key}`);
Expand All @@ -265,7 +272,7 @@ const getTieredUnitAmount = (
language,
descriptionCurrency,
descriptionTranslationFactory,
{ showStartsAt: false, enableSubunitDisplay: true },
{ showStartsAt: false, enableSubunitDisplay: true, precision: netPrecision },
tax,
)?.split('/')[0]; // remove the unit part as it is not needed
}
Expand All @@ -278,7 +285,12 @@ const getTieredUnitAmount = (
language,
descriptionCurrency,
descriptionTranslationFactory,
{ showStartsAt: false, showOnRequest: !isUnitAmountApproved, enableSubunitDisplay: true },
{
showStartsAt: false,
showOnRequest: !isUnitAmountApproved,
enableSubunitDisplay: true,
precision: netPrecision,
},
tax,
)?.split('/')[0]; // remove the unit part as it is not needed
}
Expand All @@ -291,7 +303,7 @@ const getTieredUnitAmount = (
language,
descriptionCurrency,
descriptionTranslationFactory,
{ showStartsAt: false, enableSubunitDisplay: true },
{ showStartsAt: false, enableSubunitDisplay: true, precision: netPrecision },
tax,
)?.split('/')[0]; // remove the unit part as it is not needed
}
Expand Down
Loading