@endora-commerce/contracts 0.107.0 → 0.109.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.
Files changed (273) hide show
  1. package/dist/admin-actions.d.ts +12 -3
  2. package/dist/admin-actions.d.ts.map +1 -1
  3. package/dist/admin-actions.js +34 -3
  4. package/dist/admin-actions.js.map +1 -1
  5. package/dist/admin-contributions.d.ts +35 -6
  6. package/dist/admin-contributions.d.ts.map +1 -1
  7. package/dist/admin-contributions.js +46 -0
  8. package/dist/admin-contributions.js.map +1 -1
  9. package/dist/admin-feedback.d.ts +61 -0
  10. package/dist/admin-feedback.d.ts.map +1 -0
  11. package/dist/admin-feedback.js +71 -0
  12. package/dist/admin-feedback.js.map +1 -0
  13. package/dist/admin-name.d.ts +21 -0
  14. package/dist/admin-name.d.ts.map +1 -0
  15. package/dist/admin-name.js +30 -0
  16. package/dist/admin-name.js.map +1 -0
  17. package/dist/admin-notifications.d.ts +17 -1
  18. package/dist/admin-notifications.d.ts.map +1 -1
  19. package/dist/admin-preferences.d.ts +108 -0
  20. package/dist/admin-preferences.d.ts.map +1 -0
  21. package/dist/admin-preferences.js +133 -0
  22. package/dist/admin-preferences.js.map +1 -0
  23. package/dist/admin.d.ts +15 -1
  24. package/dist/admin.d.ts.map +1 -1
  25. package/dist/admin.js +7 -0
  26. package/dist/admin.js.map +1 -1
  27. package/dist/assets-library.d.ts +97 -3
  28. package/dist/assets-library.d.ts.map +1 -1
  29. package/dist/assets-library.js +5 -0
  30. package/dist/assets-library.js.map +1 -1
  31. package/dist/blog.d.ts +57 -0
  32. package/dist/blog.d.ts.map +1 -1
  33. package/dist/blog.js +63 -1
  34. package/dist/blog.js.map +1 -1
  35. package/dist/carts.d.ts +303 -50
  36. package/dist/carts.d.ts.map +1 -1
  37. package/dist/carts.js +95 -13
  38. package/dist/carts.js.map +1 -1
  39. package/dist/catalog.d.ts +512 -40
  40. package/dist/catalog.d.ts.map +1 -1
  41. package/dist/catalog.js +362 -23
  42. package/dist/catalog.js.map +1 -1
  43. package/dist/cms-brand.d.ts +2233 -0
  44. package/dist/cms-brand.d.ts.map +1 -0
  45. package/dist/cms-brand.js +311 -0
  46. package/dist/cms-brand.js.map +1 -0
  47. package/dist/cms-contact-form.d.ts +55 -0
  48. package/dist/cms-contact-form.d.ts.map +1 -0
  49. package/dist/cms-contact-form.js +100 -0
  50. package/dist/cms-contact-form.js.map +1 -0
  51. package/dist/cms.d.ts +537 -3
  52. package/dist/cms.d.ts.map +1 -1
  53. package/dist/cms.js +251 -0
  54. package/dist/cms.js.map +1 -1
  55. package/dist/common.d.ts +4 -0
  56. package/dist/common.d.ts.map +1 -1
  57. package/dist/common.js +14 -0
  58. package/dist/common.js.map +1 -1
  59. package/dist/comparisons.d.ts +7 -0
  60. package/dist/comparisons.d.ts.map +1 -1
  61. package/dist/comparisons.js +2 -0
  62. package/dist/comparisons.js.map +1 -1
  63. package/dist/credit-limits.d.ts.map +1 -1
  64. package/dist/credit-limits.js +3 -2
  65. package/dist/credit-limits.js.map +1 -1
  66. package/dist/crm-rich-text.d.ts +13 -0
  67. package/dist/crm-rich-text.d.ts.map +1 -0
  68. package/dist/crm-rich-text.js +13 -0
  69. package/dist/crm-rich-text.js.map +1 -0
  70. package/dist/crm.d.ts +180 -62
  71. package/dist/crm.d.ts.map +1 -1
  72. package/dist/crm.js +63 -107
  73. package/dist/crm.js.map +1 -1
  74. package/dist/custom-fields.d.ts +436 -5
  75. package/dist/custom-fields.d.ts.map +1 -1
  76. package/dist/custom-fields.js +325 -10
  77. package/dist/custom-fields.js.map +1 -1
  78. package/dist/customer-accounts.d.ts +30 -0
  79. package/dist/customer-accounts.d.ts.map +1 -1
  80. package/dist/customer-accounts.js +31 -0
  81. package/dist/customer-accounts.js.map +1 -1
  82. package/dist/document-templates.d.ts +211 -23
  83. package/dist/document-templates.d.ts.map +1 -1
  84. package/dist/document-templates.js +98 -1
  85. package/dist/document-templates.js.map +1 -1
  86. package/dist/document-values.d.ts +12 -1
  87. package/dist/document-values.d.ts.map +1 -1
  88. package/dist/document-values.js +41 -11
  89. package/dist/document-values.js.map +1 -1
  90. package/dist/email.d.ts +5 -0
  91. package/dist/email.d.ts.map +1 -1
  92. package/dist/errors.d.ts +142 -8
  93. package/dist/errors.d.ts.map +1 -1
  94. package/dist/errors.js +189 -8
  95. package/dist/errors.js.map +1 -1
  96. package/dist/index.d.ts +24 -1
  97. package/dist/index.d.ts.map +1 -1
  98. package/dist/index.js +43 -1
  99. package/dist/index.js.map +1 -1
  100. package/dist/inventory.d.ts +3 -3
  101. package/dist/invoices.d.ts +6 -2
  102. package/dist/invoices.d.ts.map +1 -1
  103. package/dist/invoices.js +10 -4
  104. package/dist/invoices.js.map +1 -1
  105. package/dist/line-groups.d.ts +80 -0
  106. package/dist/line-groups.d.ts.map +1 -0
  107. package/dist/line-groups.js +130 -0
  108. package/dist/line-groups.js.map +1 -0
  109. package/dist/localized-name.d.ts +20 -0
  110. package/dist/localized-name.d.ts.map +1 -0
  111. package/dist/localized-name.js +45 -0
  112. package/dist/localized-name.js.map +1 -0
  113. package/dist/megamenu.d.ts +674 -0
  114. package/dist/megamenu.d.ts.map +1 -1
  115. package/dist/megamenu.js +96 -3
  116. package/dist/megamenu.js.map +1 -1
  117. package/dist/mfa.d.ts +1 -0
  118. package/dist/mfa.d.ts.map +1 -1
  119. package/dist/mfa.js +8 -2
  120. package/dist/mfa.js.map +1 -1
  121. package/dist/modules.d.ts +8 -1
  122. package/dist/modules.d.ts.map +1 -1
  123. package/dist/modules.js +5 -0
  124. package/dist/modules.js.map +1 -1
  125. package/dist/money-decimal.d.ts +162 -0
  126. package/dist/money-decimal.d.ts.map +1 -0
  127. package/dist/money-decimal.js +365 -0
  128. package/dist/money-decimal.js.map +1 -0
  129. package/dist/money-input.d.ts +64 -0
  130. package/dist/money-input.d.ts.map +1 -0
  131. package/dist/money-input.js +111 -0
  132. package/dist/money-input.js.map +1 -0
  133. package/dist/newsletter.d.ts +3 -3
  134. package/dist/order-minimums.d.ts +111 -0
  135. package/dist/order-minimums.d.ts.map +1 -0
  136. package/dist/order-minimums.js +196 -0
  137. package/dist/order-minimums.js.map +1 -0
  138. package/dist/orders.d.ts +206 -2
  139. package/dist/orders.d.ts.map +1 -1
  140. package/dist/orders.js +106 -5
  141. package/dist/orders.js.map +1 -1
  142. package/dist/page-builder-props.d.ts +1401 -0
  143. package/dist/page-builder-props.d.ts.map +1 -0
  144. package/dist/page-builder-props.js +266 -0
  145. package/dist/page-builder-props.js.map +1 -0
  146. package/dist/page-builder-values.d.ts +95 -0
  147. package/dist/page-builder-values.d.ts.map +1 -0
  148. package/dist/page-builder-values.js +164 -0
  149. package/dist/page-builder-values.js.map +1 -0
  150. package/dist/payment-methods.d.ts.map +1 -1
  151. package/dist/payment-methods.js +2 -1
  152. package/dist/payment-methods.js.map +1 -1
  153. package/dist/pim-connector.d.ts +1 -1
  154. package/dist/platform-language.d.ts +9 -0
  155. package/dist/platform-language.d.ts.map +1 -1
  156. package/dist/platform-language.js +14 -0
  157. package/dist/platform-language.js.map +1 -1
  158. package/dist/price-book-decimal.d.ts +84 -0
  159. package/dist/price-book-decimal.d.ts.map +1 -0
  160. package/dist/price-book-decimal.js +175 -0
  161. package/dist/price-book-decimal.js.map +1 -0
  162. package/dist/price-books.d.ts +2206 -0
  163. package/dist/price-books.d.ts.map +1 -0
  164. package/dist/price-books.js +777 -0
  165. package/dist/price-books.js.map +1 -0
  166. package/dist/price-lists.d.ts +309 -614
  167. package/dist/price-lists.d.ts.map +1 -1
  168. package/dist/price-lists.js +113 -453
  169. package/dist/price-lists.js.map +1 -1
  170. package/dist/product-feeds.d.ts +195 -54
  171. package/dist/product-feeds.d.ts.map +1 -1
  172. package/dist/product-feeds.js +36 -1
  173. package/dist/product-feeds.js.map +1 -1
  174. package/dist/product-scopes.d.ts +628 -0
  175. package/dist/product-scopes.d.ts.map +1 -0
  176. package/dist/product-scopes.js +365 -0
  177. package/dist/product-scopes.js.map +1 -0
  178. package/dist/product-summary-text.d.ts +23 -0
  179. package/dist/product-summary-text.d.ts.map +1 -0
  180. package/dist/product-summary-text.js +64 -0
  181. package/dist/product-summary-text.js.map +1 -0
  182. package/dist/product-url-keys.d.ts +204 -0
  183. package/dist/product-url-keys.d.ts.map +1 -0
  184. package/dist/product-url-keys.js +166 -0
  185. package/dist/product-url-keys.js.map +1 -0
  186. package/dist/product-value-resolver.d.ts +6 -0
  187. package/dist/product-value-resolver.d.ts.map +1 -1
  188. package/dist/product-value-resolver.js +6 -1
  189. package/dist/product-value-resolver.js.map +1 -1
  190. package/dist/projects.d.ts +2355 -0
  191. package/dist/projects.d.ts.map +1 -0
  192. package/dist/projects.js +837 -0
  193. package/dist/projects.js.map +1 -0
  194. package/dist/promotions.d.ts +2 -2
  195. package/dist/promotions.d.ts.map +1 -1
  196. package/dist/promotions.js +8 -2
  197. package/dist/promotions.js.map +1 -1
  198. package/dist/prompt-actions.d.ts +19 -1
  199. package/dist/prompt-actions.d.ts.map +1 -1
  200. package/dist/prompt-actions.js +31 -2
  201. package/dist/prompt-actions.js.map +1 -1
  202. package/dist/quick-order.d.ts +39 -1
  203. package/dist/quick-order.d.ts.map +1 -1
  204. package/dist/quick-order.js +44 -1
  205. package/dist/quick-order.js.map +1 -1
  206. package/dist/quote-requests.d.ts +305 -1
  207. package/dist/quote-requests.d.ts.map +1 -1
  208. package/dist/quote-requests.js +173 -5
  209. package/dist/quote-requests.js.map +1 -1
  210. package/dist/record-references.d.ts +252 -0
  211. package/dist/record-references.d.ts.map +1 -0
  212. package/dist/record-references.js +179 -0
  213. package/dist/record-references.js.map +1 -0
  214. package/dist/record-rich-text.d.ts +66 -0
  215. package/dist/record-rich-text.d.ts.map +1 -0
  216. package/dist/record-rich-text.js +303 -0
  217. package/dist/record-rich-text.js.map +1 -0
  218. package/dist/returns.d.ts +81 -2
  219. package/dist/returns.d.ts.map +1 -1
  220. package/dist/returns.js +39 -4
  221. package/dist/returns.js.map +1 -1
  222. package/dist/sales-channels.d.ts +53 -7
  223. package/dist/sales-channels.d.ts.map +1 -1
  224. package/dist/sales-channels.js +40 -4
  225. package/dist/sales-channels.js.map +1 -1
  226. package/dist/saved-list-views.d.ts +28 -0
  227. package/dist/saved-list-views.d.ts.map +1 -1
  228. package/dist/saved-list-views.js +14 -1
  229. package/dist/saved-list-views.js.map +1 -1
  230. package/dist/scoped-translations.d.ts +48 -0
  231. package/dist/scoped-translations.d.ts.map +1 -0
  232. package/dist/scoped-translations.js +78 -0
  233. package/dist/scoped-translations.js.map +1 -0
  234. package/dist/search.d.ts +73 -5
  235. package/dist/search.d.ts.map +1 -1
  236. package/dist/search.js +4 -0
  237. package/dist/search.js.map +1 -1
  238. package/dist/seo.d.ts +64 -5
  239. package/dist/seo.d.ts.map +1 -1
  240. package/dist/seo.js +31 -4
  241. package/dist/seo.js.map +1 -1
  242. package/dist/settings.d.ts +32 -3
  243. package/dist/settings.d.ts.map +1 -1
  244. package/dist/settings.js +37 -3
  245. package/dist/settings.js.map +1 -1
  246. package/dist/shipping-methods.d.ts.map +1 -1
  247. package/dist/shipping-methods.js +2 -1
  248. package/dist/shipping-methods.js.map +1 -1
  249. package/dist/shopping-lists.d.ts +24 -0
  250. package/dist/shopping-lists.d.ts.map +1 -1
  251. package/dist/shopping-lists.js +20 -1
  252. package/dist/shopping-lists.js.map +1 -1
  253. package/dist/storefront-cache-tags.d.ts +29 -0
  254. package/dist/storefront-cache-tags.d.ts.map +1 -1
  255. package/dist/storefront-cache-tags.js +24 -0
  256. package/dist/storefront-cache-tags.js.map +1 -1
  257. package/dist/storefront-language-path.d.ts +77 -0
  258. package/dist/storefront-language-path.d.ts.map +1 -0
  259. package/dist/storefront-language-path.js +123 -0
  260. package/dist/storefront-language-path.js.map +1 -0
  261. package/dist/storefront-value-overrides.d.ts +67 -0
  262. package/dist/storefront-value-overrides.d.ts.map +1 -0
  263. package/dist/storefront-value-overrides.js +93 -0
  264. package/dist/storefront-value-overrides.js.map +1 -0
  265. package/dist/tasks.d.ts +2842 -0
  266. package/dist/tasks.d.ts.map +1 -0
  267. package/dist/tasks.js +901 -0
  268. package/dist/tasks.js.map +1 -0
  269. package/dist/transactional-emails.d.ts +7 -4
  270. package/dist/transactional-emails.d.ts.map +1 -1
  271. package/dist/transactional-emails.js +13 -0
  272. package/dist/transactional-emails.js.map +1 -1
  273. package/package.json +2 -2
@@ -0,0 +1,162 @@
1
+ /**
2
+ * A decimal rounded to `places` (default two) **half-up on its decimal
3
+ * digits**, half away from zero for a negative value, with exactly `places`
4
+ * fractional digits; `-0.00` is written `0.00`. `roundHalfUp(x)` and
5
+ * `roundAmountHalfUp(x)` agree on every input.
6
+ */
7
+ export declare function roundHalfUp(value: string | number, places?: number): string;
8
+ /** The exact sum of decimals, unrounded. */
9
+ export declare function decimalSum(...values: ReadonlyArray<string | number>): string;
10
+ /** The exact difference `a − b`, unrounded. */
11
+ export declare function decimalDifference(a: string | number, b: string | number): string;
12
+ /** The exact product `a × b`, unrounded. */
13
+ export declare function decimalProduct(a: string | number, b: string | number): string;
14
+ /** Numeric comparison of two signed decimals of any scale: −1, 0 or 1. */
15
+ export declare function compareDecimals(a: string | number, b: string | number): -1 | 0 | 1;
16
+ /** The sum of money amounts, rounded half-up to `places` once, at the end. */
17
+ export declare function moneySum(values: ReadonlyArray<string | number>, places?: number): string;
18
+ /** `a − b` for money amounts, rounded half-up to `places`. */
19
+ export declare function moneyDifference(a: string | number, b: string | number, places?: number): string;
20
+ /**
21
+ * A line's amount: `unit price × quantity`, rounded half-up to `places` — **the
22
+ * rounding point** of a unit price (owner ruling of 2026-10-25). The unit price
23
+ * is the four-place one the line carries, never a two-place rounding of it:
24
+ * 0.0125 × 10 000 is 125.00, and 0.0150 × 3 is 0.045, so 0.05.
25
+ */
26
+ export declare function lineAmountHalfUp(unitPrice: string | number, quantity: string | number, places?: number): string;
27
+ /** The places a unit price is carried at: `numeric(14,4)` from the price book to the invoice line. */
28
+ export declare const UNIT_PRICE_PLACES = 4;
29
+ /**
30
+ * A unit price at its four places (owner ruling of 2026-10-25): a resolver
31
+ * amount, an agreed or a typed price, written the way `numeric(14,4)` stores
32
+ * it. Nothing is lost for a value of at most four places — `0.0125` stays
33
+ * `0.0125`, `1.5` becomes `1.5000` — and a value with more is read at four,
34
+ * half-up. What a cart line, an order line, a quote line and an invoice line
35
+ * carry; never round a unit price to two places.
36
+ */
37
+ export declare function unitPriceHalfUp(value: string | number): string;
38
+ /**
39
+ * How many fraction digits a unit price is **shown** with — the one display
40
+ * rule (owner ruling of 2026-10-25): two when its third and fourth places are
41
+ * zero (`1.50`), otherwise its significant places, up to four (`0.0125`,
42
+ * `1.505`). A line value and a total always show two; this is for a unit price
43
+ * only. Every money formatter (the admin's and the storefront's `formatMoney`,
44
+ * the documents, the e-mails) asks this.
45
+ */
46
+ export declare function unitPriceFractionDigits(value: string | number): 2 | 3 | 4;
47
+ /** A unit price as a decimal string at its display places ({@link unitPriceFractionDigits}): `0.0125`, `1.50`, `1.505`. */
48
+ export declare function unitPriceDisplayText(value: string | number): string;
49
+ /**
50
+ * The gross of a **unit** price, for display beside a net one: the exact
51
+ * `net × (1 + rate)` rounded half-up to two places when the net shows two
52
+ * (`29.50` at 23 % is `36.29`, §10.16 G1), and to four when the net carries a
53
+ * sub-cent place (`0.0125` at 23 % is `0.015375`, so `0.0154`) — a gross unit
54
+ * price of a sub-cent net rounded to the cent would misstate it by up to a
55
+ * third. Informational: a document's gross is computed from its line nets
56
+ * (`vatByRate`), never from this. `unit` is `percent` (`23`) unless said.
57
+ */
58
+ export declare function grossUnitPriceHalfUp(net: string | number, rate: string | number, unit?: 'fraction' | 'percent'): string;
59
+ /**
60
+ * The tax on a net amount: `net × rate`, rounded half-up on the exact product.
61
+ * The rate is a fraction (`0.23`, as orders, invoices and the tax resolver
62
+ * carry it) or a percentage (`23`) — said explicitly, because the two differ
63
+ * by a factor of a hundred and `grossAmountHalfUp` defaults to percent.
64
+ */
65
+ export declare function taxAmountHalfUp(net: string | number, rate: string | number, unit: 'fraction' | 'percent', places?: number): string;
66
+ /**
67
+ * The exact, unrounded tax on a net amount, for a total that rounds the sum of
68
+ * its lines' taxes once (the order's rule) rather than each line's.
69
+ */
70
+ export declare function taxAmountExact(net: string | number, rate: string | number, unit: 'fraction' | 'percent'): string;
71
+ /**
72
+ * The gross of a net amount: `net × (1 + rate)`, rounded half-up on the exact
73
+ * product. The net may be negative (a discount line), which rounds half away
74
+ * from zero. On a non-negative net this is `grossAmountHalfUp`.
75
+ */
76
+ export declare function grossFromNetHalfUp(net: string | number, rate: string | number, unit: 'fraction' | 'percent', places?: number): string;
77
+ /**
78
+ * `dividend / divisor`, rounded half-up to `places` on the exact quotient —
79
+ * a unit price backed out of a line amount and a quantity, for one. The
80
+ * divisor is non-zero.
81
+ */
82
+ export declare function quotientHalfUp(dividend: string | number, divisor: string | number, places?: number): string;
83
+ /**
84
+ * The net inside a gross amount: `gross / (1 + rate)`, rounded half-up on the
85
+ * exact quotient — what a correction credits net when it is given a gross.
86
+ */
87
+ export declare function netFromGrossHalfUp(gross: string | number, rate: string | number, unit: 'fraction' | 'percent', places?: number): string;
88
+ /** `amount × percent / 100`, rounded half-up on the exact product — a percentage discount. */
89
+ export declare function percentOfHalfUp(amount: string | number, percent: string | number, places?: number): string;
90
+ /**
91
+ * How many whole `step`s fit in `amount` — `floor(amount / step)` on the exact
92
+ * quotient, for a non-negative amount and a positive step ("for every 100.00
93
+ * spent"). On doubles, `Math.floor(0.3 / 0.1)` is 2.
94
+ */
95
+ export declare function wholeMultiples(amount: string | number, step: string | number): number;
96
+ /**
97
+ * `total` shared over `parts` at `places`: each part rounded half-up, and the
98
+ * cents the rounding gained or lost handed back one at a time — to the parts
99
+ * rounded furthest below their exact value when cents are missing, to those
100
+ * rounded furthest above when there are too many, the earlier part first on a
101
+ * tie. The result adds up to `total` exactly. Used where exact shares are owed:
102
+ * a refund of a rate group split over the lines it came from.
103
+ */
104
+ export declare function allocateHalfUp(total: string | number, parts: ReadonlyArray<string | number>, places?: number): string[];
105
+ /** One rate's row of a VAT summary: the rate as a fraction, and two-place amounts. */
106
+ export interface VatRateRow {
107
+ rate: string;
108
+ net: string;
109
+ vat: string;
110
+ gross: string;
111
+ }
112
+ /** {@link vatByRate}'s answer. */
113
+ export interface VatByRate {
114
+ /** One row per rate, ascending. */
115
+ rows: VatRateRow[];
116
+ /**
117
+ * Per input line, in input order: its own gross, `round(net × (1 + rate))`, and that minus its
118
+ * net — informational: the line grosses may differ from the total by a cent.
119
+ */
120
+ lines: Array<{
121
+ vat: string;
122
+ gross: string;
123
+ }>;
124
+ net: string;
125
+ vat: string;
126
+ gross: string;
127
+ }
128
+ /**
129
+ * **The VAT method — one, everywhere** (owner ruling of 2026-10-24, issue
130
+ * #417; art. 106e ust. 1 pkt 13–14 of the Polish VAT Act): per rate, the VAT
131
+ * is the sum of the nets at that rate times the rate, rounded half-up **once**;
132
+ * the rate's gross is its net plus its VAT; the totals are the sums of the
133
+ * rows. Every document an order produces computes its VAT with this function —
134
+ * the checkout preview, the order, the invoice, a correction, a refund — so
135
+ * they cannot disagree.
136
+ *
137
+ * A line's gross (`lines`) is informational and is **the line's own rounding**,
138
+ * `round(net × (1 + rate))` — what the shop shows (owner ruling of
139
+ * 2026-10-24): 29.50 at 23 % shows 36.29 even beside 16.50 at the same rate,
140
+ * whose rate VAT is 10.58. A document's line grosses may therefore add up to a
141
+ * cent more or less than its total; the VAT and the totals are the rows'.
142
+ * Nets are two-place amounts; a 0 % line (delivery, a discount) is a row of its
143
+ * own with no VAT.
144
+ */
145
+ export declare function vatByRate(lines: ReadonlyArray<{
146
+ net: string | number;
147
+ rate: string | number;
148
+ }>, unit: 'fraction' | 'percent'): VatByRate;
149
+ /**
150
+ * The net behind a gross at one rate, under {@link vatByRate}'s rule: the net
151
+ * `N` with `N + round(N × rate) = gross`, so a gross that rule produced gives
152
+ * back exactly the net it came from (`exact: true`). A gross the rule cannot
153
+ * produce — at 23 %, no net gives 0.03 — answers the nearest net,
154
+ * `round(gross ÷ (1 + rate))`, with the VAT as the remainder (`exact: false`).
155
+ * What a correction credits net when it is handed a gross.
156
+ */
157
+ export declare function netForGrossAtRate(gross: string | number, rate: string | number, unit: 'fraction' | 'percent'): {
158
+ net: string;
159
+ vat: string;
160
+ exact: boolean;
161
+ };
162
+ //# sourceMappingURL=money-decimal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"money-decimal.d.ts","sourceRoot":"","sources":["../src/money-decimal.ts"],"names":[],"mappings":"AAkGA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,SAAI,GAAG,MAAM,CAGtE;AAED,4CAA4C;AAC5C,wBAAgB,UAAU,CAAC,GAAG,MAAM,EAAE,aAAa,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,CAE5E;AAED,+CAA+C;AAC/C,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAKhF;AAED,4CAA4C;AAC5C,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAE7E;AAED,0EAA0E;AAC1E,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAOlF;AAED,8EAA8E;AAC9E,wBAAgB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CAEnF;AAED,8DAA8D;AAC9D,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,SAAI,GAAG,MAAM,CAE1F;AAED;;;;;GA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ACvB,KAAK,EAAE,aAAa,CAAC;IAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,EACrE,IAAI,EAAE,UAAU,GAAG,SAAS,GAC3B,SAAS,CA+BX;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,IAAI,EAAE,UAAU,GAAG,SAAS,GAC3B;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAW9C"}
@@ -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,C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;QACnE,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAC1D,IAAI,eAAe,CAAC,QAAQ,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC;YAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IAC5F,CAAC;IACD,OAAO,EAAE,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,C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