@tallyui/pos 3.16.0 → 3.18.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.
@@ -47,6 +47,14 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
47
47
  registerId: string; cashierRef: string; capabilities?: ServerCapabilities;
48
48
  /** The platform's discount codes (ADR-077 amendment 3), e.g. woocommerceDiscountCodes.createDiscountCodes(source); without them, applyCoupon refuses with COUPONS_UNSUPPORTED. */
49
49
  discountCodes?: SaleDiscountCodes<any>;
50
+ /**
51
+ * Put on every new sale, as v2's store setting `default_customer` (or the cashier, `default_customer_is_cashier`)
52
+ * is on every new cart: the first sale and each one after complete, discard, park, newSale or a switch to New,
53
+ * never a resumed one. The app resolves which customer. A cashier's change, Guest included, stays on that sale.
54
+ * A change applies at once to an idle cart, else to the next new sale. One `setCustomer` would refuse is skipped
55
+ * (the sale starts on guest, with no error).
56
+ */
57
+ defaultCustomer?: CustomerSummary | null;
50
58
  /** Where `park()` keeps parked carts; create it with `orderDraftSchema` */
51
59
  drafts?: RxCollection;
52
60
  /**
@@ -75,7 +83,19 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
75
83
  }) {
76
84
  const taxContext = useTax();
77
85
  const madeWith = useRef({ taxContext, currency: settings.currency });
78
- const [rendered, setBuilder] = useState(() => createOrderBuilder({ currency: settings.currency, taxContext }));
86
+ // The default customer for a new sale, unless the server would refuse it: then the sale starts on guest.
87
+ function defaulted(): CustomerSummary | null {
88
+ const customer = opts.defaultCustomer ?? null;
89
+ return customer && !customerRefusal(customer) ? customer : null;
90
+ }
91
+ const defaultKey = opts.defaultCustomer ? JSON.stringify([opts.defaultCustomer.id, opts.defaultCustomer.name, opts.defaultCustomer.email]) : '';
92
+ const defaultSeen = useRef(defaultKey);
93
+ const [rendered, setBuilder] = useState(() => {
94
+ const first = createOrderBuilder({ currency: settings.currency, taxContext });
95
+ const customer = defaulted();
96
+ if (customer) first.setCustomer(customer);
97
+ return first;
98
+ });
79
99
  // The live builder, which every mutator reads (#301): newSale() sets it before setBuilder(next) renders, so a
80
100
  // call from an older render never reaches a discarded builder. The state only moves the `order` subscription.
81
101
  const builderNow = useRef(rendered);
@@ -136,8 +156,8 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
136
156
  if (active) setError(SALE_SAVING);
137
157
  return active;
138
158
  }
139
- // newSale()'s body (see its doc comment for the guards): `newSale()` is `resetSale(null)`.
140
- function resetSale(customer: CustomerSummary | null, restored?: OrderBuilder) {
159
+ // newSale()'s body (see its doc comment for the guards): `newSale()` is `resetSale()`, on the default customer.
160
+ function resetSale(customer: CustomerSummary | null = defaulted(), restored?: OrderBuilder) {
141
161
  if (inFlight.current && !pending.current) return setError(SALE_SAVING);
142
162
  if (pending.current && confirmed.current !== pending.current) {
143
163
  if (inFlight.current) checkStored(pending.current);
@@ -170,10 +190,13 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
170
190
  const idle = stage.kind === 'cart' && !order.lineItems.length && !order.fees?.length && !order.shipping?.length;
171
191
  // A layout effect, on live idleness (#301): it runs inside the settings' commit, before any tap can be handled, and
172
192
  // the rendered `idle` would miss a line added since this render (it would then be dropped with the old builder).
193
+ // A new default customer starts a new sale on it at once when idle, as v2 seeds its new cart again; never held pending.
173
194
  useLayoutEffect(() => {
174
195
  const live = builderNow.current.getSnapshot();
175
196
  const liveIdle = stageNow.current.kind === 'cart' && !live.lineItems.length && !live.fees?.length && !live.shipping?.length && !inFlight.current && !pending.current;
176
- if (liveIdle && (madeWith.current.taxContext !== taxContext || madeWith.current.currency !== settings.currency)) resetSale(live.customer ?? null);
197
+ const newDefault = defaultSeen.current !== defaultKey;
198
+ defaultSeen.current = defaultKey;
199
+ if (liveIdle && (newDefault || madeWith.current.taxContext !== taxContext || madeWith.current.currency !== settings.currency)) resetSale(newDefault ? undefined : live.customer ?? null);
177
200
  });
178
201
  // A screen that unmounts (medusapos: Sign out) mid-save must still leave a trace of the loss.
179
202
  useEffect(() => () => {
@@ -291,8 +314,9 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
291
314
  saving,
292
315
  /** The failed save's order is confirmed stored (see `isStored`): the UI also offers Continue (`continueSale()`). */
293
316
  canContinue,
294
- add(entry: CatalogueEntry<any>, traits: ProductTraits<any>) {
295
- if (locked()) return;
317
+ /** Adds the entry, as a new line or one more on its line; returns that line's id, or the refusal `error` now shows. */
318
+ add(entry: CatalogueEntry<any>, traits: ProductTraits<any>): { lineId: string } | string {
319
+ if (locked()) return SALE_SAVING;
296
320
  const builder = builderNow.current;
297
321
  try {
298
322
  const known = new Set(builder.getSnapshot().lineItems.map((line) => line.id));
@@ -310,11 +334,13 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
310
334
  const refused = line && [line.variantId !== undefined ? referenceError(`${name}: the variant id`, line.variantId)
311
335
  : referenceError(`${name}: the product id`, line.productId),
312
336
  ...taxLines.map((tax) => referenceError(`${name}: the tax code`, tax.code))].find((message) => message !== null);
313
- if (refused) { builder.removeItem(lineId); return setError(refused); }
337
+ if (refused) { builder.removeItem(lineId); setError(refused); return refused; }
314
338
  setError(null);
339
+ return { lineId };
315
340
  } catch (error) {
316
341
  if (!(error instanceof CartError)) throw error;
317
342
  setError(error.message);
343
+ return error.message;
318
344
  }
319
345
  },
320
346
  setQuantity(lineId: string, quantity: number) { if (!locked()) builderNow.current.updateQuantity(lineId, quantity); },
@@ -569,7 +595,7 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
569
595
  * clears the screen, never the record (the Front desk, 2026-09-25). `newSale()` never deletes,
570
596
  * updates or requeues `pos_orders` itself.
571
597
  */
572
- newSale() { resetSale(null); },
598
+ newSale() { resetSale(); },
573
599
  /** Parks the cart in `drafts`; resolves to a refusal message, or null once parked. */
574
600
  async park(): Promise<string | null> {
575
601
  if (locked()) return SALE_SAVING;
@@ -580,7 +606,7 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
580
606
  if (!snapshot.lineItems.length && !snapshot.fees?.length && !snapshot.shipping?.length) return 'There is nothing to park';
581
607
  await writeOrderDraft(opts.drafts, snapshot);
582
608
  if (builderNow.current !== builder || builder.getSnapshot() !== snapshot) return 'The sale changed while it was being parked; park it again';
583
- resetSale(null);
609
+ resetSale();
584
610
  return null;
585
611
  },
586
612
  /** Resumes a draft into an empty cart; resolves to a refusal message, or null once resumed. */
@@ -659,7 +685,7 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
659
685
  await writeOrderDraft(opts.drafts, snapshot);
660
686
  if (builderNow.current !== builder || builder.getSnapshot() !== snapshot) return 'The sale changed while it was being parked; park it again';
661
687
  }
662
- resetSale(null);
688
+ resetSale();
663
689
  return draftId === null ? null : result.resume(draftId);
664
690
  },
665
691
  /** Discards the current sale (nothing is parked) and starts an empty one; refused while saving, once a leg of an unfinished sale took or holds money, or in the tender stage. */
@@ -668,7 +694,7 @@ export function useSale(settings: Pick<StoreSettings, 'currency' | 'calcDiscount
668
694
  const { payments } = builderNow.current.getSnapshot();
669
695
  if (stageNow.current.kind !== 'receipt' && (capturedMinor(payments) > 0 || payments.some(isInMotion))) return SALE_PARTLY_PAID;
670
696
  if (stageNow.current.kind === 'tender') return SALE_IN_TENDER;
671
- resetSale(null);
697
+ resetSale();
672
698
  return null;
673
699
  },
674
700
  /**