@endora-commerce/contracts 0.107.0 → 0.108.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin-feedback.d.ts +61 -0
- package/dist/admin-feedback.d.ts.map +1 -0
- package/dist/admin-feedback.js +71 -0
- package/dist/admin-feedback.js.map +1 -0
- package/dist/admin-name.d.ts +21 -0
- package/dist/admin-name.d.ts.map +1 -0
- package/dist/admin-name.js +30 -0
- package/dist/admin-name.js.map +1 -0
- package/dist/carts.d.ts +197 -1
- package/dist/carts.d.ts.map +1 -1
- package/dist/carts.js +29 -0
- package/dist/carts.js.map +1 -1
- package/dist/catalog.d.ts +243 -10
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +107 -4
- package/dist/catalog.js.map +1 -1
- package/dist/common.d.ts +4 -0
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +14 -0
- package/dist/common.js.map +1 -1
- package/dist/comparisons.d.ts +7 -0
- package/dist/comparisons.d.ts.map +1 -1
- package/dist/comparisons.js +2 -0
- package/dist/comparisons.js.map +1 -1
- package/dist/credit-limits.d.ts.map +1 -1
- package/dist/credit-limits.js +3 -2
- package/dist/credit-limits.js.map +1 -1
- package/dist/crm-rich-text.d.ts +64 -0
- package/dist/crm-rich-text.d.ts.map +1 -0
- package/dist/crm-rich-text.js +303 -0
- package/dist/crm-rich-text.js.map +1 -0
- package/dist/crm.d.ts +91 -1
- package/dist/crm.d.ts.map +1 -1
- package/dist/crm.js +49 -34
- package/dist/crm.js.map +1 -1
- package/dist/custom-fields.d.ts +414 -1
- package/dist/custom-fields.d.ts.map +1 -1
- package/dist/custom-fields.js +319 -10
- package/dist/custom-fields.js.map +1 -1
- package/dist/document-templates.d.ts +184 -8
- package/dist/document-templates.d.ts.map +1 -1
- package/dist/document-templates.js +98 -1
- package/dist/document-templates.js.map +1 -1
- package/dist/document-values.d.ts +4 -1
- package/dist/document-values.d.ts.map +1 -1
- package/dist/document-values.js +18 -6
- package/dist/document-values.js.map +1 -1
- package/dist/errors.d.ts +37 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +66 -8
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -1
- package/dist/invoices.d.ts.map +1 -1
- package/dist/invoices.js +7 -4
- package/dist/invoices.js.map +1 -1
- package/dist/line-groups.d.ts +80 -0
- package/dist/line-groups.d.ts.map +1 -0
- package/dist/line-groups.js +130 -0
- package/dist/line-groups.js.map +1 -0
- package/dist/localized-name.d.ts +20 -0
- package/dist/localized-name.d.ts.map +1 -0
- package/dist/localized-name.js +45 -0
- package/dist/localized-name.js.map +1 -0
- package/dist/money-decimal.d.ts +162 -0
- package/dist/money-decimal.d.ts.map +1 -0
- package/dist/money-decimal.js +365 -0
- package/dist/money-decimal.js.map +1 -0
- package/dist/money-input.d.ts +64 -0
- package/dist/money-input.d.ts.map +1 -0
- package/dist/money-input.js +111 -0
- package/dist/money-input.js.map +1 -0
- package/dist/order-minimums.d.ts +111 -0
- package/dist/order-minimums.d.ts.map +1 -0
- package/dist/order-minimums.js +196 -0
- package/dist/order-minimums.js.map +1 -0
- package/dist/orders.d.ts +181 -2
- package/dist/orders.d.ts.map +1 -1
- package/dist/orders.js +92 -5
- package/dist/orders.js.map +1 -1
- package/dist/payment-methods.d.ts.map +1 -1
- package/dist/payment-methods.js +2 -1
- package/dist/payment-methods.js.map +1 -1
- package/dist/price-book-decimal.d.ts +84 -0
- package/dist/price-book-decimal.d.ts.map +1 -0
- package/dist/price-book-decimal.js +175 -0
- package/dist/price-book-decimal.js.map +1 -0
- package/dist/price-books.d.ts +2206 -0
- package/dist/price-books.d.ts.map +1 -0
- package/dist/price-books.js +777 -0
- package/dist/price-books.js.map +1 -0
- package/dist/price-lists.d.ts +268 -607
- package/dist/price-lists.d.ts.map +1 -1
- package/dist/price-lists.js +92 -455
- package/dist/price-lists.js.map +1 -1
- package/dist/product-feeds.d.ts +89 -8
- package/dist/product-feeds.d.ts.map +1 -1
- package/dist/product-feeds.js +18 -1
- package/dist/product-feeds.js.map +1 -1
- package/dist/product-value-resolver.d.ts +6 -0
- package/dist/product-value-resolver.d.ts.map +1 -1
- package/dist/product-value-resolver.js +6 -1
- package/dist/product-value-resolver.js.map +1 -1
- package/dist/promotions.d.ts +2 -2
- package/dist/promotions.d.ts.map +1 -1
- package/dist/promotions.js +8 -2
- package/dist/promotions.js.map +1 -1
- package/dist/quick-order.d.ts +39 -0
- package/dist/quick-order.d.ts.map +1 -1
- package/dist/quick-order.js +43 -0
- package/dist/quick-order.js.map +1 -1
- package/dist/quote-requests.d.ts +270 -1
- package/dist/quote-requests.d.ts.map +1 -1
- package/dist/quote-requests.js +150 -6
- package/dist/quote-requests.js.map +1 -1
- package/dist/returns.d.ts +55 -2
- package/dist/returns.d.ts.map +1 -1
- package/dist/returns.js +39 -4
- package/dist/returns.js.map +1 -1
- package/dist/sales-channels.d.ts +2 -0
- package/dist/sales-channels.d.ts.map +1 -1
- package/dist/sales-channels.js +2 -0
- package/dist/sales-channels.js.map +1 -1
- package/dist/search.d.ts +60 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +4 -0
- package/dist/search.js.map +1 -1
- package/dist/shipping-methods.d.ts.map +1 -1
- package/dist/shipping-methods.js +2 -1
- package/dist/shipping-methods.js.map +1 -1
- package/dist/shopping-lists.d.ts +24 -0
- package/dist/shopping-lists.d.ts.map +1 -1
- package/dist/shopping-lists.js +20 -1
- package/dist/shopping-lists.js.map +1 -1
- package/dist/storefront-value-overrides.d.ts +67 -0
- package/dist/storefront-value-overrides.d.ts.map +1 -0
- package/dist/storefront-value-overrides.js +93 -0
- package/dist/storefront-value-overrides.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exact decimal money arithmetic — issue #417, the sibling of
|
|
3
|
+
* `price-book-decimal.ts` for the amounts that are *computed* from prices
|
|
4
|
+
* rather than read from a price book: a line's net, its tax and its gross, an
|
|
5
|
+
* order's and an invoice's totals, a correction's net backed out of a credited
|
|
6
|
+
* gross, a percentage discount, a refund, an available credit.
|
|
7
|
+
*
|
|
8
|
+
* Every one of those used to be computed on doubles and rounded with
|
|
9
|
+
* `Number#toFixed(2)` or `Math.round(x * 100) / 100`, which round the binary
|
|
10
|
+
* value: 29.50 × 1.23 is the double 36.28499…, so an invoice line read 36.28
|
|
11
|
+
* where the exact gross is 36.285 and half-up gives 36.29
|
|
12
|
+
* (`specs/pricing/worked-examples.md` §10.16 and §10.17).
|
|
13
|
+
*
|
|
14
|
+
* Here every value is read as the decimal it is written as — a string
|
|
15
|
+
* verbatim, a number as the shortest decimal that round-trips it
|
|
16
|
+
* ({@link decimalText}) — and carried as an integer with a scale, so a sum or a
|
|
17
|
+
* product is exact and the one rounding, half-up (half away from zero for a
|
|
18
|
+
* negative amount), happens where the caller says. All results are decimal
|
|
19
|
+
* strings: a two-place string read with `Number` is the nearest double to that
|
|
20
|
+
* decimal, which `toFixed(2)`, `Intl.NumberFormat` and `String` all write back
|
|
21
|
+
* as the same two places, so a caller that has to publish a number may.
|
|
22
|
+
*
|
|
23
|
+
* The minor unit is two places: every money **amount** column in the platform
|
|
24
|
+
* is `numeric(…, 2)` and no currency with another minor unit is supported, so
|
|
25
|
+
* `places` defaults to 2 rather than being looked up per currency. A **unit
|
|
26
|
+
* price** is not an amount: it is carried at four places, unrounded, from the
|
|
27
|
+
* price book to the invoice line (`numeric(14,4)` everywhere, owner ruling of
|
|
28
|
+
* 2026-10-25), and only the line value, `unit × quantity`, is rounded to two —
|
|
29
|
+
* once, by {@link lineAmountHalfUp} (`specs/pricing/worked-examples.md` §10.18).
|
|
30
|
+
* No dependency (Constitution IV).
|
|
31
|
+
*/
|
|
32
|
+
import { decimalText } from './price-book-decimal.js';
|
|
33
|
+
const SIGNED_DECIMAL = /^([+-])?(\d+)(?:\.(\d+))?$/;
|
|
34
|
+
function exact(value) {
|
|
35
|
+
const text = decimalText(value);
|
|
36
|
+
const match = SIGNED_DECIMAL.exec(text);
|
|
37
|
+
if (!match)
|
|
38
|
+
throw new Error(`Not a decimal amount: "${String(value)}"`);
|
|
39
|
+
const fraction = match[3] ?? '';
|
|
40
|
+
const magnitude = BigInt(match[2] + fraction);
|
|
41
|
+
return { digits: match[1] === '-' ? -magnitude : magnitude, scale: fraction.length };
|
|
42
|
+
}
|
|
43
|
+
function rescale(value, scale) {
|
|
44
|
+
return value.digits * 10n ** BigInt(scale - value.scale);
|
|
45
|
+
}
|
|
46
|
+
function render(value) {
|
|
47
|
+
const negative = value.digits < 0n;
|
|
48
|
+
const abs = (negative ? -value.digits : value.digits).toString().padStart(value.scale + 1, '0');
|
|
49
|
+
const whole = abs.slice(0, abs.length - value.scale);
|
|
50
|
+
const fraction = value.scale > 0 ? `.${abs.slice(abs.length - value.scale)}` : '';
|
|
51
|
+
return `${negative ? '-' : ''}${whole}${fraction}`;
|
|
52
|
+
}
|
|
53
|
+
/** `numerator / denominator` rounded half away from zero; the denominator is positive. */
|
|
54
|
+
function divideHalfUp(numerator, denominator) {
|
|
55
|
+
const negative = numerator < 0n;
|
|
56
|
+
const abs = negative ? -numerator : numerator;
|
|
57
|
+
const quotient = (2n * abs + denominator) / (2n * denominator);
|
|
58
|
+
return negative ? -quotient : quotient;
|
|
59
|
+
}
|
|
60
|
+
function round(value, places) {
|
|
61
|
+
if (value.scale <= places)
|
|
62
|
+
return { digits: rescale(value, places), scale: places };
|
|
63
|
+
const digits = divideHalfUp(value.digits, 10n ** BigInt(value.scale - places));
|
|
64
|
+
return { digits, scale: places };
|
|
65
|
+
}
|
|
66
|
+
function sum(values) {
|
|
67
|
+
const parsed = values.map(exact);
|
|
68
|
+
const scale = parsed.reduce((max, v) => Math.max(max, v.scale), 0);
|
|
69
|
+
return { digits: parsed.reduce((acc, v) => acc + rescale(v, scale), 0n), scale };
|
|
70
|
+
}
|
|
71
|
+
function product(a, b) {
|
|
72
|
+
return { digits: a.digits * b.digits, scale: a.scale + b.scale };
|
|
73
|
+
}
|
|
74
|
+
/** A rate as a fraction: `23` in percent is `0.23`. */
|
|
75
|
+
function rateFraction(rate, unit) {
|
|
76
|
+
const parsed = exact(rate);
|
|
77
|
+
if (parsed.digits < 0n)
|
|
78
|
+
throw new Error(`Not a non-negative rate: "${String(rate)}"`);
|
|
79
|
+
return unit === 'percent' ? { digits: parsed.digits, scale: parsed.scale + 2 } : parsed;
|
|
80
|
+
}
|
|
81
|
+
function checkPlaces(places) {
|
|
82
|
+
if (!Number.isInteger(places) || places < 0)
|
|
83
|
+
throw new Error(`Not a number of places: ${places}`);
|
|
84
|
+
return places;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A decimal rounded to `places` (default two) **half-up on its decimal
|
|
88
|
+
* digits**, half away from zero for a negative value, with exactly `places`
|
|
89
|
+
* fractional digits; `-0.00` is written `0.00`. `roundHalfUp(x)` and
|
|
90
|
+
* `roundAmountHalfUp(x)` agree on every input.
|
|
91
|
+
*/
|
|
92
|
+
export function roundHalfUp(value, places = 2) {
|
|
93
|
+
const rounded = round(exact(value), checkPlaces(places));
|
|
94
|
+
return render(rounded.digits === 0n ? { digits: 0n, scale: rounded.scale } : rounded);
|
|
95
|
+
}
|
|
96
|
+
/** The exact sum of decimals, unrounded. */
|
|
97
|
+
export function decimalSum(...values) {
|
|
98
|
+
return render(sum(values));
|
|
99
|
+
}
|
|
100
|
+
/** The exact difference `a − b`, unrounded. */
|
|
101
|
+
export function decimalDifference(a, b) {
|
|
102
|
+
const x = exact(a);
|
|
103
|
+
const y = exact(b);
|
|
104
|
+
const scale = Math.max(x.scale, y.scale);
|
|
105
|
+
return render({ digits: rescale(x, scale) - rescale(y, scale), scale });
|
|
106
|
+
}
|
|
107
|
+
/** The exact product `a × b`, unrounded. */
|
|
108
|
+
export function decimalProduct(a, b) {
|
|
109
|
+
return render(product(exact(a), exact(b)));
|
|
110
|
+
}
|
|
111
|
+
/** Numeric comparison of two signed decimals of any scale: −1, 0 or 1. */
|
|
112
|
+
export function compareDecimals(a, b) {
|
|
113
|
+
const x = exact(a);
|
|
114
|
+
const y = exact(b);
|
|
115
|
+
const scale = Math.max(x.scale, y.scale);
|
|
116
|
+
const left = rescale(x, scale);
|
|
117
|
+
const right = rescale(y, scale);
|
|
118
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
119
|
+
}
|
|
120
|
+
/** The sum of money amounts, rounded half-up to `places` once, at the end. */
|
|
121
|
+
export function moneySum(values, places = 2) {
|
|
122
|
+
return roundHalfUp(render(sum(values)), places);
|
|
123
|
+
}
|
|
124
|
+
/** `a − b` for money amounts, rounded half-up to `places`. */
|
|
125
|
+
export function moneyDifference(a, b, places = 2) {
|
|
126
|
+
return roundHalfUp(decimalDifference(a, b), places);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* A line's amount: `unit price × quantity`, rounded half-up to `places` — **the
|
|
130
|
+
* rounding point** of a unit price (owner ruling of 2026-10-25). The unit price
|
|
131
|
+
* is the four-place one the line carries, never a two-place rounding of it:
|
|
132
|
+
* 0.0125 × 10 000 is 125.00, and 0.0150 × 3 is 0.045, so 0.05.
|
|
133
|
+
*/
|
|
134
|
+
export function lineAmountHalfUp(unitPrice, quantity, places = 2) {
|
|
135
|
+
return roundHalfUp(decimalProduct(unitPrice, quantity), places);
|
|
136
|
+
}
|
|
137
|
+
/** The places a unit price is carried at: `numeric(14,4)` from the price book to the invoice line. */
|
|
138
|
+
export const UNIT_PRICE_PLACES = 4;
|
|
139
|
+
/**
|
|
140
|
+
* A unit price at its four places (owner ruling of 2026-10-25): a resolver
|
|
141
|
+
* amount, an agreed or a typed price, written the way `numeric(14,4)` stores
|
|
142
|
+
* it. Nothing is lost for a value of at most four places — `0.0125` stays
|
|
143
|
+
* `0.0125`, `1.5` becomes `1.5000` — and a value with more is read at four,
|
|
144
|
+
* half-up. What a cart line, an order line, a quote line and an invoice line
|
|
145
|
+
* carry; never round a unit price to two places.
|
|
146
|
+
*/
|
|
147
|
+
export function unitPriceHalfUp(value) {
|
|
148
|
+
return roundHalfUp(value, UNIT_PRICE_PLACES);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* How many fraction digits a unit price is **shown** with — the one display
|
|
152
|
+
* rule (owner ruling of 2026-10-25): two when its third and fourth places are
|
|
153
|
+
* zero (`1.50`), otherwise its significant places, up to four (`0.0125`,
|
|
154
|
+
* `1.505`). A line value and a total always show two; this is for a unit price
|
|
155
|
+
* only. Every money formatter (the admin's and the storefront's `formatMoney`,
|
|
156
|
+
* the documents, the e-mails) asks this.
|
|
157
|
+
*/
|
|
158
|
+
export function unitPriceFractionDigits(value) {
|
|
159
|
+
const fraction = (unitPriceHalfUp(value).split('.')[1] ?? '').replace(/0+$/, '');
|
|
160
|
+
return fraction.length <= 2 ? 2 : fraction.length;
|
|
161
|
+
}
|
|
162
|
+
/** A unit price as a decimal string at its display places ({@link unitPriceFractionDigits}): `0.0125`, `1.50`, `1.505`. */
|
|
163
|
+
export function unitPriceDisplayText(value) {
|
|
164
|
+
return roundHalfUp(unitPriceHalfUp(value), unitPriceFractionDigits(value));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The gross of a **unit** price, for display beside a net one: the exact
|
|
168
|
+
* `net × (1 + rate)` rounded half-up to two places when the net shows two
|
|
169
|
+
* (`29.50` at 23 % is `36.29`, §10.16 G1), and to four when the net carries a
|
|
170
|
+
* sub-cent place (`0.0125` at 23 % is `0.015375`, so `0.0154`) — a gross unit
|
|
171
|
+
* price of a sub-cent net rounded to the cent would misstate it by up to a
|
|
172
|
+
* third. Informational: a document's gross is computed from its line nets
|
|
173
|
+
* (`vatByRate`), never from this. `unit` is `percent` (`23`) unless said.
|
|
174
|
+
*/
|
|
175
|
+
export function grossUnitPriceHalfUp(net, rate, unit = 'percent') {
|
|
176
|
+
const places = unitPriceFractionDigits(net) === 2 ? 2 : UNIT_PRICE_PLACES;
|
|
177
|
+
return grossFromNetHalfUp(unitPriceHalfUp(net), rate, unit, places);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The tax on a net amount: `net × rate`, rounded half-up on the exact product.
|
|
181
|
+
* The rate is a fraction (`0.23`, as orders, invoices and the tax resolver
|
|
182
|
+
* carry it) or a percentage (`23`) — said explicitly, because the two differ
|
|
183
|
+
* by a factor of a hundred and `grossAmountHalfUp` defaults to percent.
|
|
184
|
+
*/
|
|
185
|
+
export function taxAmountHalfUp(net, rate, unit, places = 2) {
|
|
186
|
+
return render(round(product(exact(net), rateFraction(rate, unit)), checkPlaces(places)));
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The exact, unrounded tax on a net amount, for a total that rounds the sum of
|
|
190
|
+
* its lines' taxes once (the order's rule) rather than each line's.
|
|
191
|
+
*/
|
|
192
|
+
export function taxAmountExact(net, rate, unit) {
|
|
193
|
+
return render(product(exact(net), rateFraction(rate, unit)));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The gross of a net amount: `net × (1 + rate)`, rounded half-up on the exact
|
|
197
|
+
* product. The net may be negative (a discount line), which rounds half away
|
|
198
|
+
* from zero. On a non-negative net this is `grossAmountHalfUp`.
|
|
199
|
+
*/
|
|
200
|
+
export function grossFromNetHalfUp(net, rate, unit, places = 2) {
|
|
201
|
+
const factor = rateFraction(rate, unit);
|
|
202
|
+
const onePlusRate = { digits: 10n ** BigInt(factor.scale) + factor.digits, scale: factor.scale };
|
|
203
|
+
return render(round(product(exact(net), onePlusRate), checkPlaces(places)));
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* `dividend / divisor`, rounded half-up to `places` on the exact quotient —
|
|
207
|
+
* a unit price backed out of a line amount and a quantity, for one. The
|
|
208
|
+
* divisor is non-zero.
|
|
209
|
+
*/
|
|
210
|
+
export function quotientHalfUp(dividend, divisor, places = 2) {
|
|
211
|
+
const x = exact(dividend);
|
|
212
|
+
const y = exact(divisor);
|
|
213
|
+
if (y.digits === 0n)
|
|
214
|
+
throw new Error('Division by zero');
|
|
215
|
+
const p = checkPlaces(places);
|
|
216
|
+
// x / y = (x.digits / 10^x.scale) / (y.digits / 10^y.scale); scaled to 10^p.
|
|
217
|
+
let numerator = x.digits * 10n ** BigInt(y.scale + p);
|
|
218
|
+
let denominator = y.digits * 10n ** BigInt(x.scale);
|
|
219
|
+
if (denominator < 0n) {
|
|
220
|
+
numerator = -numerator;
|
|
221
|
+
denominator = -denominator;
|
|
222
|
+
}
|
|
223
|
+
return render({ digits: divideHalfUp(numerator, denominator), scale: p });
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The net inside a gross amount: `gross / (1 + rate)`, rounded half-up on the
|
|
227
|
+
* exact quotient — what a correction credits net when it is given a gross.
|
|
228
|
+
*/
|
|
229
|
+
export function netFromGrossHalfUp(gross, rate, unit, places = 2) {
|
|
230
|
+
const factor = rateFraction(rate, unit);
|
|
231
|
+
const onePlusRate = render({ digits: 10n ** BigInt(factor.scale) + factor.digits, scale: factor.scale });
|
|
232
|
+
return quotientHalfUp(gross, onePlusRate, places);
|
|
233
|
+
}
|
|
234
|
+
/** `amount × percent / 100`, rounded half-up on the exact product — a percentage discount. */
|
|
235
|
+
export function percentOfHalfUp(amount, percent, places = 2) {
|
|
236
|
+
const p = exact(percent);
|
|
237
|
+
return render(round(product(exact(amount), { digits: p.digits, scale: p.scale + 2 }), checkPlaces(places)));
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* How many whole `step`s fit in `amount` — `floor(amount / step)` on the exact
|
|
241
|
+
* quotient, for a non-negative amount and a positive step ("for every 100.00
|
|
242
|
+
* spent"). On doubles, `Math.floor(0.3 / 0.1)` is 2.
|
|
243
|
+
*/
|
|
244
|
+
export function wholeMultiples(amount, step) {
|
|
245
|
+
const x = exact(amount);
|
|
246
|
+
const y = exact(step);
|
|
247
|
+
if (x.digits < 0n)
|
|
248
|
+
throw new Error(`Not a non-negative amount: "${String(amount)}"`);
|
|
249
|
+
if (y.digits <= 0n)
|
|
250
|
+
throw new Error(`Not a positive step: "${String(step)}"`);
|
|
251
|
+
const scale = Math.max(x.scale, y.scale);
|
|
252
|
+
return Number(rescale(x, scale) / rescale(y, scale));
|
|
253
|
+
}
|
|
254
|
+
/** A rate as a fraction with no trailing zeros — the key two notations of one rate share. */
|
|
255
|
+
function rateKey(rate) {
|
|
256
|
+
let { digits, scale } = rate;
|
|
257
|
+
while (scale > 0 && digits % 10n === 0n) {
|
|
258
|
+
digits /= 10n;
|
|
259
|
+
scale -= 1;
|
|
260
|
+
}
|
|
261
|
+
return { digits, scale };
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* `total` shared over `parts` at `places`: each part rounded half-up, and the
|
|
265
|
+
* cents the rounding gained or lost handed back one at a time — to the parts
|
|
266
|
+
* rounded furthest below their exact value when cents are missing, to those
|
|
267
|
+
* rounded furthest above when there are too many, the earlier part first on a
|
|
268
|
+
* tie. The result adds up to `total` exactly. Used where exact shares are owed:
|
|
269
|
+
* a refund of a rate group split over the lines it came from.
|
|
270
|
+
*/
|
|
271
|
+
export function allocateHalfUp(total, parts, places = 2) {
|
|
272
|
+
const p = checkPlaces(places);
|
|
273
|
+
const exacts = parts.map(exact);
|
|
274
|
+
const scale = Math.max(p, ...exacts.map((e) => e.scale));
|
|
275
|
+
const unit = 10n ** BigInt(scale - p);
|
|
276
|
+
const rounded = exacts.map((e) => round(e, p).digits);
|
|
277
|
+
// The remainder of each part, at the common scale: exact − rounded.
|
|
278
|
+
const remainders = exacts.map((e, i) => rescale(e, scale) - rounded[i] * unit);
|
|
279
|
+
let missing = round(exact(total), p).digits - rounded.reduce((a, b) => a + b, 0n);
|
|
280
|
+
const order = remainders.map((r, i) => ({ r, i }));
|
|
281
|
+
if (missing > 0n) {
|
|
282
|
+
order.sort((a, b) => (a.r === b.r ? a.i - b.i : a.r > b.r ? -1 : 1));
|
|
283
|
+
for (let k = 0; missing > 0n && order.length > 0; k = (k + 1) % order.length, missing -= 1n) {
|
|
284
|
+
rounded[order[k].i] += 1n;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else if (missing < 0n) {
|
|
288
|
+
order.sort((a, b) => (a.r === b.r ? a.i - b.i : a.r < b.r ? -1 : 1));
|
|
289
|
+
for (let k = 0; missing < 0n && order.length > 0; k = (k + 1) % order.length, missing += 1n) {
|
|
290
|
+
rounded[order[k].i] -= 1n;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return rounded.map((digits) => render({ digits, scale: p }));
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* **The VAT method — one, everywhere** (owner ruling of 2026-10-24, issue
|
|
297
|
+
* #417; art. 106e ust. 1 pkt 13–14 of the Polish VAT Act): per rate, the VAT
|
|
298
|
+
* is the sum of the nets at that rate times the rate, rounded half-up **once**;
|
|
299
|
+
* the rate's gross is its net plus its VAT; the totals are the sums of the
|
|
300
|
+
* rows. Every document an order produces computes its VAT with this function —
|
|
301
|
+
* the checkout preview, the order, the invoice, a correction, a refund — so
|
|
302
|
+
* they cannot disagree.
|
|
303
|
+
*
|
|
304
|
+
* A line's gross (`lines`) is informational and is **the line's own rounding**,
|
|
305
|
+
* `round(net × (1 + rate))` — what the shop shows (owner ruling of
|
|
306
|
+
* 2026-10-24): 29.50 at 23 % shows 36.29 even beside 16.50 at the same rate,
|
|
307
|
+
* whose rate VAT is 10.58. A document's line grosses may therefore add up to a
|
|
308
|
+
* cent more or less than its total; the VAT and the totals are the rows'.
|
|
309
|
+
* Nets are two-place amounts; a 0 % line (delivery, a discount) is a row of its
|
|
310
|
+
* own with no VAT.
|
|
311
|
+
*/
|
|
312
|
+
export function vatByRate(lines, unit) {
|
|
313
|
+
const groups = new Map();
|
|
314
|
+
lines.forEach((line, index) => {
|
|
315
|
+
const rate = rateKey(rateFraction(line.rate, unit));
|
|
316
|
+
const key = render(rate);
|
|
317
|
+
const group = groups.get(key) ?? { rate, indexes: [] };
|
|
318
|
+
group.indexes.push(index);
|
|
319
|
+
groups.set(key, group);
|
|
320
|
+
});
|
|
321
|
+
const lineShares = lines.map(() => ({ vat: '0.00', gross: '0.00' }));
|
|
322
|
+
const rows = [...groups.entries()]
|
|
323
|
+
.sort((a, b) => compareDecimals(a[0], b[0]))
|
|
324
|
+
.map(([key, group]) => {
|
|
325
|
+
const nets = group.indexes.map((i) => roundHalfUp(lines[i].net));
|
|
326
|
+
const net = moneySum(nets);
|
|
327
|
+
const vat = render(round(product(exact(net), group.rate), 2));
|
|
328
|
+
const onePlusRate = { digits: 10n ** BigInt(group.rate.scale) + group.rate.digits, scale: group.rate.scale };
|
|
329
|
+
group.indexes.forEach((lineIndex, k) => {
|
|
330
|
+
// The line's own rounding — informational (owner ruling of 2026-10-24).
|
|
331
|
+
const gross = render(round(product(exact(nets[k]), onePlusRate), 2));
|
|
332
|
+
lineShares[lineIndex] = { vat: moneyDifference(gross, nets[k]), gross };
|
|
333
|
+
});
|
|
334
|
+
return { rate: key, net, vat, gross: moneySum([net, vat]) };
|
|
335
|
+
});
|
|
336
|
+
return {
|
|
337
|
+
rows,
|
|
338
|
+
lines: lineShares,
|
|
339
|
+
net: moneySum(rows.map((r) => r.net)),
|
|
340
|
+
vat: moneySum(rows.map((r) => r.vat)),
|
|
341
|
+
gross: moneySum(rows.map((r) => r.gross)),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* The net behind a gross at one rate, under {@link vatByRate}'s rule: the net
|
|
346
|
+
* `N` with `N + round(N × rate) = gross`, so a gross that rule produced gives
|
|
347
|
+
* back exactly the net it came from (`exact: true`). A gross the rule cannot
|
|
348
|
+
* produce — at 23 %, no net gives 0.03 — answers the nearest net,
|
|
349
|
+
* `round(gross ÷ (1 + rate))`, with the VAT as the remainder (`exact: false`).
|
|
350
|
+
* What a correction credits net when it is handed a gross.
|
|
351
|
+
*/
|
|
352
|
+
export function netForGrossAtRate(gross, rate, unit) {
|
|
353
|
+
const target = roundHalfUp(gross);
|
|
354
|
+
const nearest = netFromGrossHalfUp(target, rate, unit);
|
|
355
|
+
const factor = rateFraction(rate, unit);
|
|
356
|
+
const centsOf = (value) => exact(value).digits;
|
|
357
|
+
for (const delta of [0n, -1n, 1n]) {
|
|
358
|
+
const net = render({ digits: centsOf(nearest) + delta, scale: 2 });
|
|
359
|
+
const vat = render(round(product(exact(net), factor), 2));
|
|
360
|
+
if (compareDecimals(moneySum([net, vat]), target) === 0)
|
|
361
|
+
return { net, vat, exact: true };
|
|
362
|
+
}
|
|
363
|
+
return { net: nearest, vat: moneyDifference(target, nearest), exact: false };
|
|
364
|
+
}
|
|
365
|
+
//# sourceMappingURL=money-decimal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"money-decimal.js","sourceRoot":"","sources":["../src/money-decimal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAQtD,MAAM,cAAc,GAAG,4BAA4B,CAAC;AAEpD,SAAS,KAAK,CAAC,KAAsB;IACnC,MAAM,IAAI,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACxE,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAChC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAE,GAAG,QAAQ,CAAC,CAAC;IAC/C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,EAAE,KAAK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GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAC/E,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* A typed money amount has at most two decimal places — owner ruling of
|
|
4
|
+
* 2026-10-24, issue #417. Every money field a person or an integration enters
|
|
5
|
+
* (a delivery cost, a payment surcharge, a refund, a credit limit, a
|
|
6
|
+
* promotion's fixed amount) refuses more with `400 AMOUNT_PRECISION_EXCEEDED`,
|
|
7
|
+
* and nothing rounds it: an amount the platform stores is the amount that was
|
|
8
|
+
* typed. Price-book prices have their own four-place grammar and do not use this.
|
|
9
|
+
*
|
|
10
|
+
* A typed **unit price** — a quote's desired, agreed or proposed price — keeps
|
|
11
|
+
* up to four places (owner ruling of 2026-10-25): a unit price is carried at
|
|
12
|
+
* four places from the price book to the invoice line and only the line value
|
|
13
|
+
* is rounded (`specs/pricing/worked-examples.md` §10.18). It takes
|
|
14
|
+
* {@link fourDecimalUnitPrice}, and more than four places is refused with its
|
|
15
|
+
* own code, `400 UNIT_PRICE_PRECISION_EXCEEDED`, whose sentence says four.
|
|
16
|
+
*
|
|
17
|
+
* The refusal is a Zod issue tagged with the code, so a schema states it once
|
|
18
|
+
* and the error envelope (`@endora-commerce/platform/http`) turns a request
|
|
19
|
+
* whose only failures are this one into the error code.
|
|
20
|
+
*/
|
|
21
|
+
export declare const AMOUNT_PRECISION_EXCEEDED: "AMOUNT_PRECISION_EXCEEDED";
|
|
22
|
+
/** The refusal of a typed unit price with more than four decimal places (owner ruling of 2026-10-25). */
|
|
23
|
+
export declare const UNIT_PRICE_PRECISION_EXCEEDED: "UNIT_PRICE_PRECISION_EXCEEDED";
|
|
24
|
+
/** Whether a number (read as its shortest decimal) or a decimal string has at most two decimal places. */
|
|
25
|
+
export declare function hasAtMostTwoDecimals(value: number | string): boolean;
|
|
26
|
+
/** Whether a number (read as its shortest decimal) or a decimal string has at most four decimal places. */
|
|
27
|
+
export declare function hasAtMostFourDecimals(value: number | string): boolean;
|
|
28
|
+
/** A number schema refusing more than two decimal places with the precision issue. */
|
|
29
|
+
export declare function twoDecimalAmount<T extends z.ZodNumber>(schema: T): T;
|
|
30
|
+
/**
|
|
31
|
+
* A number schema refusing more than four decimal places with the unit-price
|
|
32
|
+
* precision issue — a typed unit price (owner ruling of 2026-10-25). Trailing
|
|
33
|
+
* zeros are not significant, as for {@link twoDecimalAmount}.
|
|
34
|
+
*/
|
|
35
|
+
export declare function fourDecimalUnitPrice<T extends z.ZodNumber>(schema: T): T;
|
|
36
|
+
/**
|
|
37
|
+
* A decimal-string schema: a string that is not a decimal is refused with a
|
|
38
|
+
* **format** issue (`options.message`, `Not a decimal amount` by default) and
|
|
39
|
+
* nothing else; a decimal with a significant digit beyond the second place is
|
|
40
|
+
* refused with the precision issue. Trailing zeros are not significant
|
|
41
|
+
* (`"123.4500"` is 123.45). `options.pattern` narrows the format (a
|
|
42
|
+
* non-negative amount, a bounded number of digits).
|
|
43
|
+
*/
|
|
44
|
+
export declare function twoDecimalAmountString<T extends z.ZodString>(schema: T, options?: {
|
|
45
|
+
pattern?: RegExp;
|
|
46
|
+
message?: string;
|
|
47
|
+
}): T;
|
|
48
|
+
/**
|
|
49
|
+
* The unit-price sibling of {@link twoDecimalAmountString}: the same grammar
|
|
50
|
+
* and the same trim behaviour (pass `z.string().trim()` to accept surrounding
|
|
51
|
+
* whitespace; trailing zeros are not significant), refusing a significant
|
|
52
|
+
* digit beyond the **fourth** place with the unit-price precision issue.
|
|
53
|
+
*/
|
|
54
|
+
export declare function fourDecimalUnitPriceString<T extends z.ZodString>(schema: T, options?: {
|
|
55
|
+
pattern?: RegExp;
|
|
56
|
+
message?: string;
|
|
57
|
+
}): T;
|
|
58
|
+
/** The precision issue, for a `superRefine` that decides per field (a promotion's value is money only for `amount_off`). */
|
|
59
|
+
export declare function addAmountPrecisionIssue(ctx: z.RefinementCtx, path: PropertyKey[]): void;
|
|
60
|
+
/** Whether a Zod issue (or a Fastify-wrapped one, whose `params` carry the issue's) is the precision issue. */
|
|
61
|
+
export declare function isAmountPrecisionIssue(issue: unknown): boolean;
|
|
62
|
+
/** Whether a Zod issue (or a Fastify-wrapped one) is the unit-price precision issue. */
|
|
63
|
+
export declare function isUnitPricePrecisionIssue(issue: unknown): boolean;
|
|
64
|
+
//# sourceMappingURL=money-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"money-input.d.ts","sourceRoot":"","sources":["../src/money-input.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAG7B;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,yBAAyB,EAAG,2BAAoC,CAAC;AAE9E,yGAAyG;AACzG,eAAO,MAAM,6BAA6B,EAAG,+BAAwC,CAAC;AAatF,0GAA0G;AAC1G,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAEpE;AAED,2GAA2G;AAC3G,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAErE;AASD,sFAAsF;AACtF,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAExE;AAsBD;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAC1D,MAAM,EAAE,CAAC,EACT,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,CAAC,CAEH;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,EAC9D,MAAM,EAAE,CAAC,EACT,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAO,GACnD,CAAC,CAEH;AAED,4HAA4H;AAC5H,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,CAAC,CAAC,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,IAAI,CAEvF;AAQD,+GAA+G;AAC/G,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9D;AAED,wFAAwF;AACxF,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEjE"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { decimalText } from './price-book-decimal.js';
|
|
2
|
+
/**
|
|
3
|
+
* A typed money amount has at most two decimal places — owner ruling of
|
|
4
|
+
* 2026-10-24, issue #417. Every money field a person or an integration enters
|
|
5
|
+
* (a delivery cost, a payment surcharge, a refund, a credit limit, a
|
|
6
|
+
* promotion's fixed amount) refuses more with `400 AMOUNT_PRECISION_EXCEEDED`,
|
|
7
|
+
* and nothing rounds it: an amount the platform stores is the amount that was
|
|
8
|
+
* typed. Price-book prices have their own four-place grammar and do not use this.
|
|
9
|
+
*
|
|
10
|
+
* A typed **unit price** — a quote's desired, agreed or proposed price — keeps
|
|
11
|
+
* up to four places (owner ruling of 2026-10-25): a unit price is carried at
|
|
12
|
+
* four places from the price book to the invoice line and only the line value
|
|
13
|
+
* is rounded (`specs/pricing/worked-examples.md` §10.18). It takes
|
|
14
|
+
* {@link fourDecimalUnitPrice}, and more than four places is refused with its
|
|
15
|
+
* own code, `400 UNIT_PRICE_PRECISION_EXCEEDED`, whose sentence says four.
|
|
16
|
+
*
|
|
17
|
+
* The refusal is a Zod issue tagged with the code, so a schema states it once
|
|
18
|
+
* and the error envelope (`@endora-commerce/platform/http`) turns a request
|
|
19
|
+
* whose only failures are this one into the error code.
|
|
20
|
+
*/
|
|
21
|
+
export const AMOUNT_PRECISION_EXCEEDED = 'AMOUNT_PRECISION_EXCEEDED';
|
|
22
|
+
/** The refusal of a typed unit price with more than four decimal places (owner ruling of 2026-10-25). */
|
|
23
|
+
export const UNIT_PRICE_PRECISION_EXCEEDED = 'UNIT_PRICE_PRECISION_EXCEEDED';
|
|
24
|
+
const DECIMAL = /^-?\d+(?:\.(\d+))?$/;
|
|
25
|
+
/** Whether a number (read as its shortest decimal) or a decimal string has at most `places` significant decimal places. */
|
|
26
|
+
function hasAtMostDecimals(value, places) {
|
|
27
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
28
|
+
return false;
|
|
29
|
+
const match = DECIMAL.exec(decimalText(value));
|
|
30
|
+
if (!match)
|
|
31
|
+
return false;
|
|
32
|
+
// Trailing zeros are not significant: `123.4500` is 123.45 (review of PR #423).
|
|
33
|
+
return (match[1] ?? '').replace(/0+$/, '').length <= places;
|
|
34
|
+
}
|
|
35
|
+
/** Whether a number (read as its shortest decimal) or a decimal string has at most two decimal places. */
|
|
36
|
+
export function hasAtMostTwoDecimals(value) {
|
|
37
|
+
return hasAtMostDecimals(value, 2);
|
|
38
|
+
}
|
|
39
|
+
/** Whether a number (read as its shortest decimal) or a decimal string has at most four decimal places. */
|
|
40
|
+
export function hasAtMostFourDecimals(value) {
|
|
41
|
+
return hasAtMostDecimals(value, 4);
|
|
42
|
+
}
|
|
43
|
+
/** A fresh object each time: Zod normalises a refinement's params in place. */
|
|
44
|
+
function precisionIssue(code = AMOUNT_PRECISION_EXCEEDED) {
|
|
45
|
+
return { message: code, params: { code } };
|
|
46
|
+
}
|
|
47
|
+
/** A number schema refusing more than two decimal places with the precision issue. */
|
|
48
|
+
export function twoDecimalAmount(schema) {
|
|
49
|
+
return schema.refine((value) => hasAtMostTwoDecimals(value), precisionIssue());
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A number schema refusing more than four decimal places with the unit-price
|
|
53
|
+
* precision issue — a typed unit price (owner ruling of 2026-10-25). Trailing
|
|
54
|
+
* zeros are not significant, as for {@link twoDecimalAmount}.
|
|
55
|
+
*/
|
|
56
|
+
export function fourDecimalUnitPrice(schema) {
|
|
57
|
+
return schema.refine((value) => hasAtMostFourDecimals(value), precisionIssue(UNIT_PRICE_PRECISION_EXCEEDED));
|
|
58
|
+
}
|
|
59
|
+
/** A plain decimal string: an optional minus, digits, and optionally a point and digits. */
|
|
60
|
+
const DECIMAL_STRING = /^-?\d+(?:\.\d+)?$/;
|
|
61
|
+
function decimalStringRefinement(schema, options, places, code) {
|
|
62
|
+
const pattern = options.pattern ?? DECIMAL_STRING;
|
|
63
|
+
const message = options.message ?? 'Not a decimal amount';
|
|
64
|
+
return schema.superRefine((value, ctx) => {
|
|
65
|
+
if (!pattern.test(value) || !DECIMAL_STRING.test(value)) {
|
|
66
|
+
ctx.addIssue({ code: 'custom', message });
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!hasAtMostDecimals(value, places))
|
|
70
|
+
ctx.addIssue({ code: 'custom', ...precisionIssue(code) });
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A decimal-string schema: a string that is not a decimal is refused with a
|
|
75
|
+
* **format** issue (`options.message`, `Not a decimal amount` by default) and
|
|
76
|
+
* nothing else; a decimal with a significant digit beyond the second place is
|
|
77
|
+
* refused with the precision issue. Trailing zeros are not significant
|
|
78
|
+
* (`"123.4500"` is 123.45). `options.pattern` narrows the format (a
|
|
79
|
+
* non-negative amount, a bounded number of digits).
|
|
80
|
+
*/
|
|
81
|
+
export function twoDecimalAmountString(schema, options = {}) {
|
|
82
|
+
return decimalStringRefinement(schema, options, 2, AMOUNT_PRECISION_EXCEEDED);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The unit-price sibling of {@link twoDecimalAmountString}: the same grammar
|
|
86
|
+
* and the same trim behaviour (pass `z.string().trim()` to accept surrounding
|
|
87
|
+
* whitespace; trailing zeros are not significant), refusing a significant
|
|
88
|
+
* digit beyond the **fourth** place with the unit-price precision issue.
|
|
89
|
+
*/
|
|
90
|
+
export function fourDecimalUnitPriceString(schema, options = {}) {
|
|
91
|
+
return decimalStringRefinement(schema, options, 4, UNIT_PRICE_PRECISION_EXCEEDED);
|
|
92
|
+
}
|
|
93
|
+
/** The precision issue, for a `superRefine` that decides per field (a promotion's value is money only for `amount_off`). */
|
|
94
|
+
export function addAmountPrecisionIssue(ctx, path) {
|
|
95
|
+
ctx.addIssue({ code: 'custom', path, ...precisionIssue() });
|
|
96
|
+
}
|
|
97
|
+
function hasPrecisionCode(issue, code) {
|
|
98
|
+
if (!issue || typeof issue !== 'object')
|
|
99
|
+
return false;
|
|
100
|
+
const params = issue.params;
|
|
101
|
+
return params?.code === code || params?.params?.code === code;
|
|
102
|
+
}
|
|
103
|
+
/** Whether a Zod issue (or a Fastify-wrapped one, whose `params` carry the issue's) is the precision issue. */
|
|
104
|
+
export function isAmountPrecisionIssue(issue) {
|
|
105
|
+
return hasPrecisionCode(issue, AMOUNT_PRECISION_EXCEEDED);
|
|
106
|
+
}
|
|
107
|
+
/** Whether a Zod issue (or a Fastify-wrapped one) is the unit-price precision issue. */
|
|
108
|
+
export function isUnitPricePrecisionIssue(issue) {
|
|
109
|
+
return hasPrecisionCode(issue, UNIT_PRICE_PRECISION_EXCEEDED);
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=money-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"money-input.js","sourceRoot":"","sources":["../src/money-input.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,2BAAoC,CAAC;AAE9E,yGAAyG;AACzG,MAAM,CAAC,MAAM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