@tallyui/pos 3.20.1 → 4.0.1
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/index.d.ts +1008 -111
- package/dist/index.js +3158 -907
- package/package.json +6 -6
- package/src/app-settings/app-settings.ts +77 -0
- package/src/app-settings/index.ts +2 -0
- package/src/currency/currency-provider.tsx +5 -7
- package/src/currency/index.ts +1 -1
- package/src/customer/customer-patch.ts +70 -0
- package/src/customer-display/display-client.ts +11 -17
- package/src/customer-display/display-link.ts +10 -5
- package/src/customer-display/index.ts +3 -1
- package/src/customer-display/protocol.ts +1 -1
- package/src/customer-display/sale-display.ts +6 -8
- package/src/customer-display/snapshot-fallback.ts +14 -1
- package/src/customer-display/till-device.ts +62 -0
- package/src/customer-display/till-display-service.ts +44 -42
- package/src/customer-display/till-pairing.ts +20 -23
- package/src/customer-display/use-customer-display.ts +2 -1
- package/src/customer-display/use-till-pairing.ts +4 -4
- package/src/index.ts +37 -18
- package/src/order/allocate-order-discount.ts +5 -17
- package/src/order/apportion.ts +29 -0
- package/src/order/from-store-order.ts +115 -0
- package/src/order/index.ts +1 -0
- package/src/order/order-builder.ts +239 -52
- package/src/order/order-drafts.ts +46 -6
- package/src/order/order-manager.ts +2 -1
- package/src/order/tax-ids.ts +27 -0
- package/src/order/types.ts +61 -5
- package/src/order/woocommerce-tax.ts +9 -0
- package/src/outbox/index.ts +1 -1
- package/src/outbox/order-outbox.ts +138 -43
- package/src/outbox/register-outbox.ts +4 -3
- package/src/outbox/retry-delay.ts +8 -0
- package/src/outbox/use-order-outbox.ts +8 -2
- package/src/pos-order/__fixtures__/order-create-v10.json +97 -0
- package/src/pos-order/__fixtures__/order-create-v11.json +90 -0
- package/src/pos-order/__fixtures__/order-create-v12.json +95 -0
- package/src/pos-order/__fixtures__/order-create-v9.json +89 -0
- package/src/pos-order/attention-handled.ts +24 -0
- package/src/pos-order/command.ts +132 -12
- package/src/pos-order/extras-delivery.ts +14 -0
- package/src/pos-order/finalize.ts +138 -29
- package/src/pos-order/index.ts +7 -3
- package/src/pos-order/money-divergence.ts +19 -0
- package/src/pos-order/needs-attention.ts +9 -1
- package/src/pos-order/refund-due.ts +35 -0
- package/src/pos-order/reopen.ts +96 -12
- package/src/pos-order/schema.ts +35 -10
- package/src/pos-order/till-payments.ts +4 -0
- package/src/pos-order/types.ts +45 -6
- package/src/print/escpos/__fixtures__/thermal-every-node.xml +33 -0
- package/src/print/escpos/commands.ts +79 -0
- package/src/print/escpos/drawer.ts +25 -0
- package/src/print/escpos/encode.ts +1 -0
- package/src/print/escpos/index.ts +6 -1
- package/src/print/escpos/network-sinks.ts +130 -0
- package/src/print/escpos/render-escpos.ts +155 -0
- package/src/print/escpos/transport.ts +2 -1
- package/src/print/index.ts +5 -2
- package/src/print/resolve-printer.ts +46 -0
- package/src/print/types.ts +1 -1
- package/src/product/index.ts +2 -0
- package/src/product/terms.ts +42 -0
- package/src/product/tile-fields.ts +27 -0
- package/src/receipt/build-receipt-data.ts +12 -7
- package/src/receipt/carried-rows.ts +29 -0
- package/src/receipt/index.ts +1 -0
- package/src/receipt/reprint.ts +14 -8
- package/src/receipt/types.ts +3 -1
- package/src/receipt-template/default-templates.ts +3 -0
- package/src/receipt-template/template-data.ts +3 -0
- package/src/receipt-template/wcpos/build.ts +1 -0
- package/src/receipt-template/wcpos/format.ts +2 -2
- package/src/receipt-template/wcpos/from-orders.ts +6 -2
- package/src/receipt-template/wcpos/types.ts +5 -1
- package/src/register/closure-document.ts +42 -4
- package/src/register/closure-rows.ts +3 -2
- package/src/register/document-labels.ts +25 -0
- package/src/register/expected.ts +49 -10
- package/src/register/index.ts +5 -5
- package/src/register/money.ts +23 -0
- package/src/register/register-adoption.ts +6 -5
- package/src/register/register-commands.ts +4 -3
- package/src/register/schemas.ts +11 -3
- package/src/register/session-store.ts +44 -23
- package/src/register/settled-figures.ts +5 -19
- package/src/register/use-register-session.ts +18 -19
- package/src/reopen/index.ts +3 -0
- package/src/reopen/reopen-entry.ts +48 -0
- package/src/reopen/reopen-store-order.ts +78 -0
- package/src/reopen/send-order-reopen.ts +61 -0
- package/src/reports/adapters.ts +15 -7
- package/src/reports/sales-figures.ts +15 -1
- package/src/reports/types.ts +2 -0
- package/src/sale/cart.ts +4 -2
- package/src/sale/index.ts +1 -1
- package/src/sale/open-sales.ts +3 -0
- package/src/sale/use-sale.ts +388 -83
- package/src/sale/woocommerce/coupon-refetch.ts +1 -0
- package/src/sale/woocommerce/coupon-source.ts +4 -4
- package/src/sale/woocommerce/coupons.ts +6 -0
- package/src/sale/woocommerce/discount-codes.ts +1 -6
- package/src/scanner/analyze-trace.ts +86 -0
- package/src/scanner/device-scanner.ts +57 -0
- package/src/scanner/hid-scanner.ts +14 -41
- package/src/scanner/index.ts +2 -0
- package/src/scanner/serial-scanner.ts +15 -42
- package/src/scanner/use-barcode-scanner.ts +107 -5
- package/src/scanner/wedge-detector.ts +33 -15
- package/src/tax/exact.ts +1 -13
- package/src/tax/taxed-lines.ts +26 -0
- package/src/tender/index.ts +6 -1
- package/src/tender/split-units.ts +109 -0
- package/src/tender/tender-figures.ts +114 -0
- package/src/tender/tender-state.ts +105 -17
- package/src/currency/format-money-with.ts +0 -45
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { Payment } from '../order/types';
|
|
2
|
+
import { unitAllocationsFromLegs, type UnitAllocations } from './split-units';
|
|
3
|
+
|
|
1
4
|
/** The transport a payment terminal is reached over: hardware-neutral, not tied to any backend. */
|
|
2
5
|
export type PaymentTransport = 'bluetooth' | 'usb' | 'network' | 'tap_to_pay';
|
|
3
6
|
|
|
@@ -5,9 +8,32 @@ export type TenderView = 'select' | 'amount' | 'cancel';
|
|
|
5
8
|
export type TenderLineId = string | number;
|
|
6
9
|
|
|
7
10
|
export type TenderPlan =
|
|
8
|
-
| {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
| {
|
|
12
|
+
kind: 'even'; ways: number;
|
|
13
|
+
/** @deprecated The captured-leg count when the plan was set; read only when `before` is absent (#1133). */
|
|
14
|
+
from: number;
|
|
15
|
+
/** Every payment id on the order when the plan was set; the plan's legs are the captured legs not in it (#1133). */
|
|
16
|
+
before?: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
kind: 'fixed';
|
|
20
|
+
firstMinor: number;
|
|
21
|
+
title: string | null;
|
|
22
|
+
/** Equal shares the rest is split into after the first payment; absent means 1. */
|
|
23
|
+
thenWays?: number;
|
|
24
|
+
/** @deprecated The captured-leg count when the plan was set; read only when `before` is absent (#1133). */
|
|
25
|
+
from: number;
|
|
26
|
+
/** Every payment id on the order when the plan was set; the plan's legs are the captured legs not in it (#1133). */
|
|
27
|
+
before?: readonly string[];
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
kind: 'items'; lineIds: TenderLineId[]; firstMinor: number; ways: number;
|
|
31
|
+
/** @deprecated The captured-leg count when the plan was set; read only when `before` is absent (#1133). */
|
|
32
|
+
from: number;
|
|
33
|
+
/** Every payment id on the order when the plan was set; the plan's legs are the captured legs not in it (#1133). */
|
|
34
|
+
before?: readonly string[];
|
|
35
|
+
};
|
|
36
|
+
/** @deprecated The split sub-view retired in WCPOS v2 split V1; kept for existing callers. */
|
|
11
37
|
export type SplitTab = 'even' | 'amount' | 'percent' | 'item';
|
|
12
38
|
|
|
13
39
|
export interface TenderState {
|
|
@@ -20,11 +46,17 @@ export interface TenderState {
|
|
|
20
46
|
entryMinor: number;
|
|
21
47
|
/** False until the cashier has touched the keypad since the entry was pre-filled. */
|
|
22
48
|
entryDirty: boolean;
|
|
49
|
+
/** Money reads the keypad as minor units; percent reads it as a whole percent of the balance. */
|
|
50
|
+
entryMode: 'money' | 'percent';
|
|
23
51
|
plan: TenderPlan | null;
|
|
24
52
|
splitView: boolean;
|
|
25
53
|
splitTab: SplitTab;
|
|
26
54
|
pickedLineIds: TenderLineId[];
|
|
27
55
|
linesPaidBy: Record<TenderLineId, string[]>;
|
|
56
|
+
/** THIS payment's picked units per line (`String(line id)`); cleared when the payment is pressed. */
|
|
57
|
+
pickedUnits: Record<string, number>;
|
|
58
|
+
/** Each pressed payment's units, frozen at its press (`tender-started` with `paymentId`). Read paid units with `paidUnitsByLine`. */
|
|
59
|
+
unitAllocations: UnitAllocations;
|
|
28
60
|
}
|
|
29
61
|
|
|
30
62
|
/** '0'..'9' plus the two edit keys. There is deliberately no decimal key: digits shift in from the right. */
|
|
@@ -41,9 +73,10 @@ export type TenderAction =
|
|
|
41
73
|
}
|
|
42
74
|
| { type: 'pick-transport'; transport: PaymentTransport }
|
|
43
75
|
| { type: 'pick-reader'; readerId: string | null }
|
|
44
|
-
| { type: 'tender-started' }
|
|
76
|
+
| { type: 'tender-started'; paymentId?: string; /** The units the recorded leg carries; frozen in place of pickedUnits when given (`{}` for a money-only leg). */ units?: Readonly<Record<string, number>> }
|
|
45
77
|
| { type: 'key'; key: TenderKey }
|
|
46
78
|
| { type: 'set-entry'; minor: number }
|
|
79
|
+
| { type: 'set-entry-mode'; mode: 'money' | 'percent' }
|
|
47
80
|
| { type: 'back' }
|
|
48
81
|
| {
|
|
49
82
|
type: 'tender-recorded';
|
|
@@ -55,6 +88,7 @@ export type TenderAction =
|
|
|
55
88
|
| { type: 'close-split' }
|
|
56
89
|
| { type: 'set-split-tab'; tab: SplitTab }
|
|
57
90
|
| { type: 'toggle-split-line'; lineId: TenderLineId }
|
|
91
|
+
| { type: 'set-split-units'; lineId: TenderLineId; units: number; unpaidUnits: number }
|
|
58
92
|
| { type: 'set-plan'; plan: TenderPlan; balanceMinor: number }
|
|
59
93
|
| { type: 'clear-plan'; balanceMinor: number }
|
|
60
94
|
| { type: 'arm-custom' }
|
|
@@ -69,11 +103,14 @@ export const initialTenderState: TenderState = {
|
|
|
69
103
|
transport: null,
|
|
70
104
|
entryMinor: 0,
|
|
71
105
|
entryDirty: false,
|
|
106
|
+
entryMode: 'money',
|
|
72
107
|
plan: null,
|
|
73
108
|
splitView: false,
|
|
74
109
|
splitTab: 'even',
|
|
75
110
|
pickedLineIds: [],
|
|
76
111
|
linesPaidBy: {},
|
|
112
|
+
pickedUnits: {},
|
|
113
|
+
unitAllocations: {},
|
|
77
114
|
};
|
|
78
115
|
|
|
79
116
|
/**
|
|
@@ -84,12 +121,15 @@ export const initialTenderState: TenderState = {
|
|
|
84
121
|
export function initTenderState({
|
|
85
122
|
methodId,
|
|
86
123
|
balanceMinor,
|
|
124
|
+
payments,
|
|
87
125
|
}: {
|
|
88
126
|
methodId: string | null;
|
|
89
127
|
balanceMinor: number;
|
|
128
|
+
payments?: readonly Payment[];
|
|
90
129
|
}): TenderState {
|
|
91
|
-
|
|
92
|
-
|
|
130
|
+
const state = payments ? { ...initialTenderState, unitAllocations: unitAllocationsFromLegs(payments ?? []) } : initialTenderState;
|
|
131
|
+
if (!methodId) return state;
|
|
132
|
+
return { ...state, view: 'amount', methodId, entryMinor: balanceMinor };
|
|
93
133
|
}
|
|
94
134
|
|
|
95
135
|
export function tenderReducer(state: TenderState, action: TenderAction): TenderState {
|
|
@@ -131,10 +171,21 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
131
171
|
entryMinor: Math.max(0, Math.min(action.minor, MAX_TENDER_MINOR)),
|
|
132
172
|
entryDirty: true,
|
|
133
173
|
};
|
|
134
|
-
case '
|
|
174
|
+
case 'set-entry-mode':
|
|
175
|
+
return { ...state, entryMode: action.mode, entryMinor: 0, entryDirty: false };
|
|
176
|
+
case 'tender-started': {
|
|
135
177
|
// A leg is being recorded: the method stays chosen for the next one; the entry
|
|
136
|
-
// clears until `tender-recorded` pre-types the next planned leg.
|
|
137
|
-
|
|
178
|
+
// clears until `tender-recorded` pre-types the next planned leg. Picked units freeze
|
|
179
|
+
// to the pressed payment's id, so a void of that payment later frees them. When the action names the leg's units, those freeze, not the raw picks.
|
|
180
|
+
const leg = action.units ?? state.pickedUnits;
|
|
181
|
+
const freeze = action.paymentId !== undefined && Object.keys(leg).length > 0;
|
|
182
|
+
const clear = action.paymentId !== undefined && Object.keys(state.pickedUnits).length > 0;
|
|
183
|
+
const units = {
|
|
184
|
+
...(freeze ? { unitAllocations: { ...state.unitAllocations, [String(action.paymentId)]: leg } } : {}),
|
|
185
|
+
...(clear ? { pickedUnits: {} } : {}),
|
|
186
|
+
};
|
|
187
|
+
return { ...state, entryMinor: 0, entryDirty: false, entryMode: 'money', splitView: false, ...units };
|
|
188
|
+
}
|
|
138
189
|
case 'back':
|
|
139
190
|
return {
|
|
140
191
|
...state,
|
|
@@ -144,6 +195,7 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
144
195
|
transport: null,
|
|
145
196
|
entryMinor: 0,
|
|
146
197
|
entryDirty: false,
|
|
198
|
+
entryMode: 'money',
|
|
147
199
|
};
|
|
148
200
|
case 'tender-recorded': {
|
|
149
201
|
const linesPaidBy = { ...state.linesPaidBy };
|
|
@@ -155,6 +207,10 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
155
207
|
// Preserve the group's original methods when subsequently taking the rest.
|
|
156
208
|
linesPaidBy[id] ??= [...new Set(action.rowsSinceFrom.map((row) => row.title))];
|
|
157
209
|
}
|
|
210
|
+
} else if (state.plan?.kind === 'items') {
|
|
211
|
+
for (const id of state.plan.lineIds) {
|
|
212
|
+
delete linesPaidBy[id];
|
|
213
|
+
}
|
|
158
214
|
}
|
|
159
215
|
// The method stays chosen for the next leg (the selector is always on screen now);
|
|
160
216
|
// the entry pre-types the next planned leg, or the balance when no plan is left.
|
|
@@ -171,7 +227,9 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
171
227
|
view: state.methodId ? 'amount' : 'select',
|
|
172
228
|
entryMinor,
|
|
173
229
|
entryDirty: false,
|
|
174
|
-
|
|
230
|
+
entryMode: 'money',
|
|
231
|
+
// Split closed means no picks: while units are picked (a leg recorded or settled elsewhere), Split stays open.
|
|
232
|
+
splitView: Object.keys(state.pickedUnits).length > 0 ? state.splitView : false,
|
|
175
233
|
pickedLineIds: [],
|
|
176
234
|
linesPaidBy,
|
|
177
235
|
};
|
|
@@ -185,8 +243,9 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
185
243
|
? state.plan.lineIds.filter((id) => !state.linesPaidBy[id])
|
|
186
244
|
: state.pickedLineIds,
|
|
187
245
|
};
|
|
246
|
+
// Split closed means no picks: every action that closes Split clears pickedUnits, except tender-recorded (above) and tender-started without a payment id (S5); allocations stay.
|
|
188
247
|
case 'close-split':
|
|
189
|
-
return { ...state, splitView: false };
|
|
248
|
+
return { ...state, splitView: false, pickedUnits: {} };
|
|
190
249
|
case 'set-split-tab':
|
|
191
250
|
return { ...state, splitTab: action.tab };
|
|
192
251
|
case 'toggle-split-line':
|
|
@@ -197,17 +256,29 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
197
256
|
? state.pickedLineIds.filter((id) => id !== action.lineId)
|
|
198
257
|
: [...state.pickedLineIds, action.lineId],
|
|
199
258
|
};
|
|
259
|
+
case 'set-split-units': {
|
|
260
|
+
// A fractional or negative count is refused outright: such a line cannot be split by units.
|
|
261
|
+
if (!Number.isInteger(action.units) || action.units < 0) return state;
|
|
262
|
+
if (!Number.isInteger(action.unpaidUnits) || action.unpaidUnits < 0) return state;
|
|
263
|
+
// No picks under a plan: the planned leg is paid by money, so a pick would freeze into its allocations.
|
|
264
|
+
if (state.plan && action.units > 0) return state;
|
|
265
|
+
const units = Math.min(action.units, action.unpaidUnits);
|
|
266
|
+
const next = { ...state.pickedUnits, [String(action.lineId)]: units };
|
|
267
|
+
return { ...state, pickedUnits: Object.fromEntries(Object.entries(next).filter(([, n]) => n > 0)) };
|
|
268
|
+
}
|
|
200
269
|
case 'set-plan':
|
|
201
270
|
return {
|
|
202
271
|
...state,
|
|
203
272
|
plan: action.plan,
|
|
204
273
|
splitView: false,
|
|
274
|
+
pickedUnits: {},
|
|
205
275
|
pickedLineIds: [],
|
|
206
276
|
entryMinor:
|
|
207
277
|
state.view === 'amount'
|
|
208
278
|
? planLegs(action.plan, [], action.balanceMinor).thisPaymentMinor
|
|
209
279
|
: state.entryMinor,
|
|
210
280
|
entryDirty: state.view !== 'amount' && state.entryDirty,
|
|
281
|
+
entryMode: 'money',
|
|
211
282
|
};
|
|
212
283
|
case 'clear-plan':
|
|
213
284
|
case 'arm-custom':
|
|
@@ -215,12 +286,14 @@ export function tenderReducer(state: TenderState, action: TenderAction): TenderS
|
|
|
215
286
|
...state,
|
|
216
287
|
plan: null,
|
|
217
288
|
splitView: false,
|
|
289
|
+
pickedUnits: {},
|
|
218
290
|
view: action.type === 'arm-custom' ? 'amount' : state.view,
|
|
219
291
|
entryMinor: action.type === 'clear-plan' ? action.balanceMinor : 0,
|
|
220
292
|
entryDirty: false,
|
|
293
|
+
entryMode: 'money',
|
|
221
294
|
};
|
|
222
295
|
case 'request-cancel':
|
|
223
|
-
return { ...state, view: 'cancel', splitView: false };
|
|
296
|
+
return { ...state, view: 'cancel', splitView: false, pickedUnits: {} };
|
|
224
297
|
case 'reset':
|
|
225
298
|
return initialTenderState;
|
|
226
299
|
}
|
|
@@ -274,6 +347,11 @@ export function evenSplitShareMinor(balanceMinor: number, ways: number): number
|
|
|
274
347
|
return wholeShare + (remainder * 2 >= ways ? 1 : 0);
|
|
275
348
|
}
|
|
276
349
|
|
|
350
|
+
/** Round a percentage half-up to minor units, never outside the remaining balance. */
|
|
351
|
+
export function percentOfMinor(balanceMinor: number, percent: number): number {
|
|
352
|
+
return Math.max(0, Math.min(balanceMinor, Math.round((balanceMinor * percent) / 100)));
|
|
353
|
+
}
|
|
354
|
+
|
|
277
355
|
/** Completion is derived from payment rows, never an extra counter in the reducer. */
|
|
278
356
|
export function activePlan(
|
|
279
357
|
plan: TenderPlan | null,
|
|
@@ -281,7 +359,8 @@ export function activePlan(
|
|
|
281
359
|
balanceMinor: number,
|
|
282
360
|
): TenderPlan | null {
|
|
283
361
|
if (!plan || balanceMinor === 0) return null;
|
|
284
|
-
if (plan.kind !== 'items' && taken >= (plan.kind === 'even' ? plan.ways :
|
|
362
|
+
if (plan.kind !== 'items' && taken >= (plan.kind === 'even' ? plan.ways : 1 + (plan.thenWays ?? 1)))
|
|
363
|
+
return null;
|
|
285
364
|
return plan;
|
|
286
365
|
}
|
|
287
366
|
|
|
@@ -305,8 +384,9 @@ export function planLegs(
|
|
|
305
384
|
? Math.min(balanceMinor, Math.max(0, plan.firstMinor - groupPaid))
|
|
306
385
|
: balanceMinor;
|
|
307
386
|
const rest = plan.kind === 'items' && groupLeft === 0;
|
|
308
|
-
const ways = plan.kind === 'fixed' ?
|
|
309
|
-
const
|
|
387
|
+
const ways = plan.kind === 'fixed' ? 1 + (plan.thenWays ?? 1) : Math.max(plan.ways, taken + 1);
|
|
388
|
+
const firstFixed = plan.kind === 'fixed' && taken === 0;
|
|
389
|
+
const left = firstFixed || rest ? 1 : Math.max(1, ways - taken);
|
|
310
390
|
const amount = rest ? balanceMinor : groupLeft;
|
|
311
391
|
const each =
|
|
312
392
|
plan.kind === 'fixed' && taken === 0
|
|
@@ -314,11 +394,19 @@ export function planLegs(
|
|
|
314
394
|
: evenSplitShareMinor(amount, left);
|
|
315
395
|
let remaining = amount;
|
|
316
396
|
for (let i = 0; i < left; i++) {
|
|
317
|
-
const minor = Math.min(remaining, i === left - 1 &&
|
|
397
|
+
const minor = Math.min(remaining, i === left - 1 && !firstFixed ? remaining : each);
|
|
318
398
|
legs.push({ minor, state: i === 0 ? 'now' : 'todo' });
|
|
319
399
|
remaining -= minor;
|
|
320
400
|
}
|
|
321
|
-
if (
|
|
401
|
+
if (firstFixed && remaining > 0) {
|
|
402
|
+
const thenWays = plan.thenWays ?? 1;
|
|
403
|
+
const share = evenSplitShareMinor(remaining, thenWays);
|
|
404
|
+
for (let i = 0; i < thenWays; i++) {
|
|
405
|
+
const minor = Math.min(remaining, i === thenWays - 1 ? remaining : share);
|
|
406
|
+
legs.push({ minor, state: 'todo' });
|
|
407
|
+
remaining -= minor;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
322
410
|
if (plan.kind === 'items' && !rest && balanceMinor > groupLeft)
|
|
323
411
|
legs.push({ minor: balanceMinor - groupLeft, state: 'rest' });
|
|
324
412
|
const title = plan.kind === 'fixed' && taken === 0 ? plan.title : null;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { minorUnitDigits, type Money } from '@tallyui/core';
|
|
2
|
-
|
|
3
|
-
/** A store's own money format (WCPOS v2 General settings); a field left out keeps the locale's. */
|
|
4
|
-
export interface CurrencyFormat {
|
|
5
|
-
position?: 'left' | 'right' | 'left_space' | 'right_space';
|
|
6
|
-
thousandSeparator?: string;
|
|
7
|
-
decimalSeparator?: string;
|
|
8
|
-
decimals?: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Money in a store's format, ported from WCPOS v2 (MIT): the position map of
|
|
13
|
-
* `packages/core/src/screens/main/hooks/use-currency-format.tsx` and the separators and places
|
|
14
|
-
* of `use-number-format.ts`. As there, the places are always shown, the integer is grouped every
|
|
15
|
-
* three digits, and a minus leads the symbol. The symbol is Intl's for the currency and locale.
|
|
16
|
-
* Returns undefined for 'XXX' or a code Intl refuses, so callers can fall back.
|
|
17
|
-
*/
|
|
18
|
-
export function formatMoneyWith(money: Money, format: CurrencyFormat, locale?: string): string | undefined {
|
|
19
|
-
if (money.currency === 'XXX') return undefined;
|
|
20
|
-
const minor = minorUnitDigits(money.currency);
|
|
21
|
-
const decimals = format.decimals ?? minor;
|
|
22
|
-
const abs = Math.abs(money.amount);
|
|
23
|
-
// Fewer places round whole minor units half away from zero, so -12.50 and 12.50 read alike.
|
|
24
|
-
const major = decimals >= minor ? abs / 10 ** minor : Math.round(abs / 10 ** (minor - decimals)) / 10 ** decimals;
|
|
25
|
-
let parts: Intl.NumberFormatPart[];
|
|
26
|
-
try {
|
|
27
|
-
parts = new Intl.NumberFormat(locale, {
|
|
28
|
-
style: 'currency', currency: money.currency, minimumFractionDigits: decimals, maximumFractionDigits: decimals,
|
|
29
|
-
}).formatToParts(major);
|
|
30
|
-
} catch {
|
|
31
|
-
return undefined;
|
|
32
|
-
}
|
|
33
|
-
const text = (...types: string[]) => parts.filter((p) => types.includes(p.type)).map((p) => p.value).join('');
|
|
34
|
-
const { position, thousandSeparator, decimalSeparator } = format;
|
|
35
|
-
const integer = thousandSeparator === undefined
|
|
36
|
-
? text('integer', 'group')
|
|
37
|
-
: text('integer').replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
|
|
38
|
-
const number = integer + (decimals > 0 ? (decimalSeparator ?? text('decimal')) + text('fraction') : '');
|
|
39
|
-
const symbol = text('currency');
|
|
40
|
-
const left = position
|
|
41
|
-
? position.startsWith('left')
|
|
42
|
-
: parts.findIndex((p) => p.type === 'currency') < parts.findIndex((p) => p.type === 'integer');
|
|
43
|
-
const gap = position ? (position.endsWith('_space') ? ' ' : '') : text('literal');
|
|
44
|
-
return (money.amount < 0 && major > 0 ? '-' : '') + (left ? symbol + gap + number : number + gap + symbol);
|
|
45
|
-
}
|