@tallyui/pos 3.13.0 → 3.15.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.
@@ -0,0 +1,103 @@
1
+ /**
2
+ * What a cashier switch, a register switch or a sign-out leaves behind, and the hand-over slip (v2 parity A4, A12).
3
+ * Port provenance: WCPOS v2 monorepo `main` 51e7c4351, `packages/core/src/screens/main/pos/cart/switch-confirm.tsx`
4
+ * (the warnings it shows) and `services/register-session/use-session-report.ts:26-65` with
5
+ * `closure-document.ts:206-273` (the slip is the open session's X report, marked as a hand-over, naming the cashier).
6
+ */
7
+ import type { Cashier } from '@tallyui/core';
8
+ import type { OutboxHealth } from '../outbox/outbox-health';
9
+ import { buildXReportDocument, type ClosureContext } from './closure-document';
10
+ import type { RegisterSession } from './schemas';
11
+
12
+ export type SwitchKind = 'cashier' | 'register' | 'signOut';
13
+ export type SwitchWarning =
14
+ | { code: 'unsentSales'; count: number }
15
+ | { code: 'unsentTillUpdates'; count: number }
16
+ | { code: 'parkedSales'; count: number }
17
+ | { code: 'partlyPaidSale' }
18
+ | { code: 'openSession' };
19
+ export interface SwitchWarningInput {
20
+ kind: SwitchKind;
21
+ /** `summarizeOutboxHealth`'s result (or `useOutboxHealth().health`). */
22
+ outbox: Pick<OutboxHealth, 'sales' | 'tillUpdates'>;
23
+ /** `useParkedSales(...).parked.length`. */
24
+ parkedSales: number;
25
+ /** Captured minor units on the sale in progress; 0 when nothing is paid. */
26
+ paidMinor: number;
27
+ /** The bound register's session status; `null` or absent when no session. */
28
+ sessionStatus?: RegisterSession['status'] | null;
29
+ }
30
+ export interface SwitchWarnings {
31
+ kind: SwitchKind;
32
+ /** In this order: unsentSales, unsentTillUpdates, parkedSales, partlyPaidSale, openSession; only those that apply. */
33
+ warnings: SwitchWarning[];
34
+ /** `outbox.sales.pending`: the confirm wording reads it. */
35
+ unsentSales: number;
36
+ /** An `open` or `counting` session, for `register` and `signOut` only (a cashier switch keeps the drawer open by design). */
37
+ sessionOpen: boolean;
38
+ }
39
+
40
+ /** What the switch would leave behind. Pure. */
41
+ export function switchWarnings({ kind, outbox, parkedSales, paidMinor, sessionStatus }: SwitchWarningInput): SwitchWarnings {
42
+ const unsentSales = outbox.sales.pending;
43
+ const sessionOpen = kind !== 'cashier' && (sessionStatus === 'open' || sessionStatus === 'counting');
44
+ const warnings: SwitchWarning[] = [];
45
+ if (unsentSales > 0) warnings.push({ code: 'unsentSales', count: unsentSales });
46
+ if (outbox.tillUpdates.pending > 0) warnings.push({ code: 'unsentTillUpdates', count: outbox.tillUpdates.pending });
47
+ if (parkedSales > 0) warnings.push({ code: 'parkedSales', count: parkedSales });
48
+ if (paidMinor > 0) warnings.push({ code: 'partlyPaidSale' });
49
+ if (sessionOpen) warnings.push({ code: 'openSession' });
50
+ return { kind, warnings, unsentSales, sessionOpen };
51
+ }
52
+
53
+ export interface HandoverSlipInput {
54
+ session: RegisterSession;
55
+ /** The cashier handing over. */
56
+ cashier: Pick<Cashier, 'name'>;
57
+ registerName: string;
58
+ /** Who opened the session; '' when unknown. */
59
+ openedByName?: string;
60
+ /** `useRegisterSession().salesCount`. */
61
+ salesCount: number;
62
+ /** `useRegisterSession().expected`, minor units per tender. */
63
+ expected: Record<string, number>;
64
+ /** `useRegisterSession().blind`: no expected figures on the slip. */
65
+ blind: boolean;
66
+ outbox: Pick<OutboxHealth, 'sales' | 'tillUpdates'>;
67
+ context: ClosureContext;
68
+ }
69
+ type XReportDocument = ReturnType<typeof buildXReportDocument>;
70
+ export interface HandoverSlipData {
71
+ registerName: string;
72
+ cashierName: string;
73
+ openedByName: string;
74
+ /** `document.closure.opened_at.time`. */
75
+ openedAtTime: string;
76
+ salesCount: number;
77
+ /** `null` while blind. */
78
+ expected: Record<string, number> | null;
79
+ unsentSales: number;
80
+ unsentTillUpdates: number;
81
+ /** The X-report envelope for the closure template and the print queue (`kind: 'report'`), marked as a hand-over. */
82
+ document: XReportDocument & { fiscal: XReportDocument['fiscal'] & { document_type: 'handover'; is_handover: true } };
83
+ }
84
+
85
+ /** The hand-over slip's data and its printable envelope. Pure. */
86
+ export function handoverSlip({
87
+ session, cashier, registerName, openedByName = '', salesCount, expected, blind, outbox, context,
88
+ }: HandoverSlipInput): HandoverSlipData {
89
+ const doc = buildXReportDocument(session, {
90
+ ...context,
91
+ expected: blind ? {} : expected,
92
+ breakdowns: {
93
+ ...context.breakdowns, register_name: registerName, cashier_name: cashier.name, opened_by_name: openedByName,
94
+ transaction_count: salesCount,
95
+ },
96
+ });
97
+ return {
98
+ registerName, cashierName: cashier.name, openedByName, openedAtTime: doc.closure.opened_at.time ?? '', salesCount,
99
+ expected: blind ? null : expected,
100
+ unsentSales: outbox.sales.pending, unsentTillUpdates: outbox.tillUpdates.pending,
101
+ document: { ...doc, fiscal: { ...doc.fiscal, document_type: 'handover', is_handover: true } },
102
+ };
103
+ }
@@ -37,3 +37,5 @@ export {
37
37
  } from './register-adoption';
38
38
  export type { ClosureApprovalStatus } from './register-adoption';
39
39
  export type { RegisterCommand, RegisterCommandCollection } from './register-commands';
40
+ export { switchWarnings, handoverSlip } from './handover';
41
+ export type { SwitchKind, SwitchWarning, SwitchWarningInput, SwitchWarnings, HandoverSlipInput, HandoverSlipData } from './handover';
@@ -1,6 +1,9 @@
1
1
  import { businessDayOf } from '../register/session-store';
2
+ import type { DatePreset } from './presets';
2
3
  import type { DayRange, ReportLine, ReportOrder, ReportRefund, StoreDay } from './types';
3
4
 
5
+ const DAY_MS = 86_400_000;
6
+
4
7
  export interface Margin {
5
8
  costedNetMinor: number;
6
9
  costMinor: number;
@@ -54,19 +57,30 @@ export function daysOf(range: DayRange): StoreDay[] {
54
57
  }
55
58
  }
56
59
 
57
- export function comparisonRange(range: DayRange, mode: 'previous' | 'week'): DayRange {
60
+ /**
61
+ * The range a report is compared with, period to date. Port of WCPOS v2 `useReportsPeriod`
62
+ * (`screens/main/reports/context.tsx`). A week or month preset is a week or a month; any other
63
+ * range is a day (one day), a week (up to seven) or a month (longer). A day goes back one day
64
+ * (`previous`) or seven (`week`), and a week seven, so Monday to today meets the same weekdays of
65
+ * the week before. A month starts a month earlier, a 31st clamped to a shorter month's last day:
66
+ * a whole calendar month meets the whole month before, and any other range keeps its day count,
67
+ * so 1–10 Nov meets 1–10 Oct.
68
+ */
69
+ export function comparisonRange(range: DayRange, mode: 'previous' | 'week', preset?: DatePreset): DayRange {
70
+ const iso = (at: number): StoreDay => new Date(at).toISOString().slice(0, 10);
58
71
  const [year, month, day] = range.from.split('-').map(Number);
59
72
  const from = Date.UTC(year, month - 1, day);
60
73
  const [toYear, toMonth, toDay] = range.to.split('-').map(Number);
61
74
  const to = Date.UTC(toYear, toMonth - 1, toDay);
62
- if (mode === 'previous' && day === 1 && to === Date.UTC(year, month, 0)) {
63
- return {
64
- from: new Date(Date.UTC(year, month - 2, 1)).toISOString().slice(0, 10),
65
- to: new Date(Date.UTC(year, month - 1, 0)).toISOString().slice(0, 10),
66
- };
75
+ const days = daysOf(range).length;
76
+ const period = preset?.endsWith('Month') ? 'month' : preset?.endsWith('Week') ? 'week' : days === 1 ? 'day' : days <= 7 ? 'week' : 'month';
77
+ if (period !== 'month') {
78
+ const shift = (period === 'day' && mode === 'previous' ? 1 : 7) * DAY_MS;
79
+ return { from: iso(from - shift), to: iso(to - shift) };
67
80
  }
68
- const shift = (mode === 'week' ? 7 : daysOf(range).length) * 86_400_000;
69
- return { from: new Date(from - shift).toISOString().slice(0, 10), to: new Date(to - shift).toISOString().slice(0, 10) };
81
+ const start = Date.UTC(year, month - 2, Math.min(day, new Date(Date.UTC(year, month - 1, 0)).getUTCDate()));
82
+ const wholeMonth = day === 1 && to === Date.UTC(year, month, 0);
83
+ return { from: iso(start), to: iso(wholeMonth ? Date.UTC(year, month - 1, 0) : start + (days - 1) * DAY_MS) };
70
84
  }
71
85
 
72
86
  export function marginOf(lines: readonly ReportLine[]): Margin {
@@ -0,0 +1,18 @@
1
+ /** The part of a cart line the in-cart count reads (`LineItem` has all three). */
2
+ export type CartQuantityLine = { productId: string; quantity: number; custom?: true };
3
+
4
+ /**
5
+ * Quantity per product id, summed across the product's lines, so a product with variants counts every
6
+ * variant's lines; a custom line belongs to no catalogue product and counts for none.
7
+ *
8
+ * Ported from WCPOS v2 `useProductCount`, packages/core/src/screens/main/pos/products/rows/product-row.tsx
9
+ * (MIT, WCPOS): the cashier reads how many are in the cart, not how many lines.
10
+ */
11
+ export function cartQuantities(lines: readonly CartQuantityLine[]): Map<string, number> {
12
+ const quantities = new Map<string, number>();
13
+ for (const line of lines) {
14
+ if (line.custom) continue;
15
+ quantities.set(line.productId, (quantities.get(line.productId) ?? 0) + line.quantity);
16
+ }
17
+ return quantities;
18
+ }
package/src/sale/index.ts CHANGED
@@ -8,3 +8,5 @@ export * as woocommerceDiscountCodes from './woocommerce';
8
8
  export { addEntryToCart, CartError } from './cart';
9
9
  export { catalogueEntries, findEntryByCode, variantPriceLabel } from './catalogue';
10
10
  export type { CatalogueEntry } from './catalogue';
11
+ export { cartQuantities } from './in-cart';
12
+ export type { CartQuantityLine } from './in-cart';