@tallyui/pos 3.20.0 → 4.0.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 (117) hide show
  1. package/dist/index.d.ts +1005 -110
  2. package/dist/index.js +3156 -907
  3. package/package.json +6 -6
  4. package/src/app-settings/app-settings.ts +77 -0
  5. package/src/app-settings/index.ts +2 -0
  6. package/src/currency/currency-provider.tsx +5 -7
  7. package/src/currency/index.ts +1 -1
  8. package/src/customer/customer-patch.ts +70 -0
  9. package/src/customer-display/display-client.ts +11 -17
  10. package/src/customer-display/display-link.ts +10 -5
  11. package/src/customer-display/index.ts +3 -1
  12. package/src/customer-display/protocol.ts +1 -1
  13. package/src/customer-display/sale-display.ts +6 -8
  14. package/src/customer-display/snapshot-fallback.ts +14 -1
  15. package/src/customer-display/till-device.ts +62 -0
  16. package/src/customer-display/till-display-service.ts +44 -42
  17. package/src/customer-display/till-pairing.ts +20 -23
  18. package/src/customer-display/use-customer-display.ts +2 -1
  19. package/src/customer-display/use-till-pairing.ts +4 -4
  20. package/src/index.ts +37 -18
  21. package/src/order/allocate-order-discount.ts +5 -17
  22. package/src/order/apportion.ts +29 -0
  23. package/src/order/from-store-order.ts +115 -0
  24. package/src/order/index.ts +1 -0
  25. package/src/order/order-builder.ts +239 -52
  26. package/src/order/order-drafts.ts +45 -6
  27. package/src/order/order-manager.ts +2 -1
  28. package/src/order/tax-ids.ts +27 -0
  29. package/src/order/types.ts +59 -5
  30. package/src/order/woocommerce-tax.ts +9 -0
  31. package/src/outbox/index.ts +1 -1
  32. package/src/outbox/order-outbox.ts +138 -43
  33. package/src/outbox/register-outbox.ts +4 -3
  34. package/src/outbox/retry-delay.ts +8 -0
  35. package/src/outbox/use-order-outbox.ts +8 -2
  36. package/src/pos-order/__fixtures__/order-create-v10.json +97 -0
  37. package/src/pos-order/__fixtures__/order-create-v11.json +90 -0
  38. package/src/pos-order/__fixtures__/order-create-v12.json +95 -0
  39. package/src/pos-order/__fixtures__/order-create-v9.json +89 -0
  40. package/src/pos-order/attention-handled.ts +24 -0
  41. package/src/pos-order/command.ts +132 -12
  42. package/src/pos-order/extras-delivery.ts +14 -0
  43. package/src/pos-order/finalize.ts +138 -29
  44. package/src/pos-order/index.ts +7 -3
  45. package/src/pos-order/money-divergence.ts +19 -0
  46. package/src/pos-order/needs-attention.ts +9 -1
  47. package/src/pos-order/refund-due.ts +35 -0
  48. package/src/pos-order/reopen.ts +96 -12
  49. package/src/pos-order/schema.ts +35 -10
  50. package/src/pos-order/till-payments.ts +4 -0
  51. package/src/pos-order/types.ts +44 -5
  52. package/src/print/escpos/__fixtures__/thermal-every-node.xml +33 -0
  53. package/src/print/escpos/commands.ts +79 -0
  54. package/src/print/escpos/drawer.ts +25 -0
  55. package/src/print/escpos/encode.ts +1 -0
  56. package/src/print/escpos/index.ts +6 -1
  57. package/src/print/escpos/network-sinks.ts +130 -0
  58. package/src/print/escpos/render-escpos.ts +155 -0
  59. package/src/print/escpos/transport.ts +2 -1
  60. package/src/print/index.ts +5 -2
  61. package/src/print/resolve-printer.ts +46 -0
  62. package/src/print/types.ts +1 -1
  63. package/src/product/index.ts +2 -0
  64. package/src/product/terms.ts +42 -0
  65. package/src/product/tile-fields.ts +27 -0
  66. package/src/receipt/build-receipt-data.ts +12 -7
  67. package/src/receipt/carried-rows.ts +29 -0
  68. package/src/receipt/index.ts +1 -0
  69. package/src/receipt/reprint.ts +14 -8
  70. package/src/receipt/types.ts +3 -1
  71. package/src/receipt-template/default-templates.ts +3 -0
  72. package/src/receipt-template/template-data.ts +3 -0
  73. package/src/receipt-template/wcpos/build.ts +1 -0
  74. package/src/receipt-template/wcpos/format.ts +2 -2
  75. package/src/receipt-template/wcpos/from-orders.ts +6 -2
  76. package/src/receipt-template/wcpos/types.ts +5 -1
  77. package/src/register/closure-document.ts +42 -4
  78. package/src/register/closure-rows.ts +3 -2
  79. package/src/register/document-labels.ts +25 -0
  80. package/src/register/expected.ts +49 -10
  81. package/src/register/index.ts +5 -5
  82. package/src/register/money.ts +23 -0
  83. package/src/register/register-adoption.ts +6 -5
  84. package/src/register/register-commands.ts +4 -3
  85. package/src/register/schemas.ts +11 -3
  86. package/src/register/session-store.ts +44 -23
  87. package/src/register/settled-figures.ts +5 -19
  88. package/src/register/use-register-session.ts +18 -19
  89. package/src/reopen/index.ts +3 -0
  90. package/src/reopen/reopen-entry.ts +48 -0
  91. package/src/reopen/reopen-store-order.ts +78 -0
  92. package/src/reopen/send-order-reopen.ts +61 -0
  93. package/src/reports/adapters.ts +15 -7
  94. package/src/reports/sales-figures.ts +15 -1
  95. package/src/reports/types.ts +2 -0
  96. package/src/sale/cart.ts +4 -2
  97. package/src/sale/index.ts +1 -1
  98. package/src/sale/open-sales.ts +3 -0
  99. package/src/sale/use-sale.ts +388 -83
  100. package/src/sale/woocommerce/coupon-refetch.ts +1 -0
  101. package/src/sale/woocommerce/coupon-source.ts +4 -4
  102. package/src/sale/woocommerce/coupons.ts +6 -0
  103. package/src/sale/woocommerce/discount-codes.ts +1 -6
  104. package/src/scanner/analyze-trace.ts +86 -0
  105. package/src/scanner/device-scanner.ts +57 -0
  106. package/src/scanner/hid-scanner.ts +14 -41
  107. package/src/scanner/index.ts +2 -0
  108. package/src/scanner/serial-scanner.ts +15 -42
  109. package/src/scanner/use-barcode-scanner.ts +107 -5
  110. package/src/scanner/wedge-detector.ts +33 -15
  111. package/src/tax/exact.ts +1 -13
  112. package/src/tax/taxed-lines.ts +26 -0
  113. package/src/tender/index.ts +6 -1
  114. package/src/tender/split-units.ts +109 -0
  115. package/src/tender/tender-figures.ts +114 -0
  116. package/src/tender/tender-state.ts +105 -17
  117. package/src/currency/format-money-with.ts +0 -45
@@ -0,0 +1,115 @@
1
+ import type { StoreOrder, StoreOrderLine } from '@tallyui/core';
2
+ import type { CustomerSummary } from './types';
3
+ import { createOrderBuilder, type OrderBuilder } from './order-builder';
4
+ import type { TaxContext } from '../tax/types';
5
+ import { sendable, PAYLOAD_STRING_MAX, REVISION_MAX, customerRefusal } from '../pos-order/command';
6
+
7
+ /** Why the till cannot rebuild a store order as a reopened cart (ADR-090 D7c5). */
8
+ export type StoreOrderRebuildReason =
9
+ | 'currency' | 'tax_mode' | 'revision' | 'store_id' | 'refund' | 'payment_record' | 'coupon' | 'discount'
10
+ | 'product' | 'quantity' | 'unit_price' | 'charge' | 'customer' | 'payment' | 'totals';
11
+
12
+ /** The cashier-facing text for each reason. */
13
+ export const STORE_ORDER_REBUILD_REFUSALS: Record<StoreOrderRebuildReason, string> = {
14
+ currency: 'This order is in another currency',
15
+ tax_mode: "This order's prices are taxed another way than the till's",
16
+ revision: 'The store gave no revision for this order',
17
+ store_id: 'The store gave no usable id for part of this order',
18
+ refund: 'This order has a refund recorded',
19
+ payment_record: "The till cannot read this order's payment records",
20
+ coupon: 'This order has a coupon the till cannot rebuild',
21
+ discount: 'This order has a discount the till cannot rebuild',
22
+ product: 'A line of this order has no product',
23
+ quantity: 'A line of this order has a quantity the till cannot rebuild',
24
+ unit_price: 'A line of this order has a price that is not a whole amount per item',
25
+ charge: 'A fee or shipping line of this order cannot be rebuilt',
26
+ customer: "This order's customer cannot go on a sale",
27
+ payment: 'A payment on this order cannot be carried',
28
+ totals: "The till's total or tax for this order differs from the store's",
29
+ };
30
+
31
+ export class StoreOrderRebuildError extends Error {
32
+ constructor(readonly reason: StoreOrderRebuildReason) {
33
+ super(STORE_ORDER_REBUILD_REFUSALS[reason]);
34
+ this.name = 'StoreOrderRebuildError';
35
+ }
36
+ }
37
+
38
+ /** The customer a rebuilt cart carries for this read, or null (none, or one the rebuild refuses). */
39
+ export function storeOrderCustomer(read: StoreOrder): CustomerSummary | null {
40
+ if (!read.customer?.id || customerRefusal(read.customer) !== null) return null;
41
+ return { id: read.customer.id, name: read.customer.name ?? '', ...(read.customer.email ? { email: read.customer.email } : {}) };
42
+ }
43
+
44
+ export function orderBuilderFromStoreOrder(read: StoreOrder, options: {
45
+ currency: string;
46
+ taxContext: TaxContext;
47
+ /** The tax class and status the catalogue gives a read line; the read itself carries neither. */
48
+ lineTax?: (line: StoreOrderLine) => { taxClass?: string; taxStatus?: 'taxable' | 'none' } | undefined;
49
+ }): OrderBuilder {
50
+ if (read.currency.toUpperCase() !== options.currency.toUpperCase()) throw new StoreOrderRebuildError('currency');
51
+ if (read.pricesIncludeTax !== options.taxContext.pricesIncludeTax) throw new StoreOrderRebuildError('tax_mode');
52
+ if (!sendable(read.revision, REVISION_MAX)) throw new StoreOrderRebuildError('revision');
53
+ if (!sendable(read.id, 64) || (read.clientOrderId !== undefined && !sendable(read.clientOrderId, 64))
54
+ || read.lines.some((line) => !sendable(line.id, 64))
55
+ || [...read.fees, ...read.shipping].some((charge) => !sendable(charge.id, 64))
56
+ || read.payments.some((payment) => !sendable(payment.id, 64))) throw new StoreOrderRebuildError('store_id');
57
+ if (new Set([...read.lines, ...read.fees, ...read.shipping].map((item) => item.id)).size !== read.lines.length + read.fees.length + read.shipping.length
58
+ || new Set(read.payments.map((payment) => payment.id)).size !== read.payments.length) throw new StoreOrderRebuildError('store_id');
59
+ if (read.refunds.length > 0) throw new StoreOrderRebuildError('refund');
60
+ if (read.paymentRecordUnreadable === true) throw new StoreOrderRebuildError('payment_record');
61
+ if (read.coupons.length > 0) throw new StoreOrderRebuildError('coupon');
62
+ if (read.discountMinor !== 0 || read.discountTaxMinor !== 0
63
+ || read.lines.some((line) => line.totalMinor !== line.subtotalMinor || line.taxMinor !== line.subtotalTaxMinor))
64
+ throw new StoreOrderRebuildError('discount');
65
+ if (read.lines.some((line) => !line.productId)) throw new StoreOrderRebuildError('product');
66
+ if (read.lines.some((line) => !Number.isInteger(line.quantity) || line.quantity <= 0)) throw new StoreOrderRebuildError('quantity');
67
+ if (read.lines.some((line) => (line.subtotalMinor + (read.pricesIncludeTax ? line.subtotalTaxMinor : 0)) % line.quantity !== 0))
68
+ throw new StoreOrderRebuildError('unit_price');
69
+ if ([...read.fees, ...read.shipping].some((charge) => {
70
+ const amount = charge.totalMinor + (read.pricesIncludeTax ? charge.taxMinor : 0);
71
+ return charge.taxStatus === undefined || !charge.name.trim() || amount < 0 || !Number.isInteger(amount);
72
+ })) throw new StoreOrderRebuildError('charge');
73
+ if (read.customer?.id && customerRefusal(read.customer) !== null) throw new StoreOrderRebuildError('customer');
74
+ if (read.payments.some((payment) => !Number.isInteger(payment.amountMinor) || payment.amountMinor <= 0
75
+ || !sendable(payment.method, PAYLOAD_STRING_MAX))) throw new StoreOrderRebuildError('payment');
76
+
77
+ const builder = createOrderBuilder({
78
+ currency: options.currency, taxContext: options.taxContext,
79
+ storeOrder: { id: read.id, revision: read.revision, ...(read.clientOrderId !== undefined ? { uuid: read.clientOrderId } : {}) },
80
+ });
81
+ for (const line of read.lines) {
82
+ const tax = options.lineTax?.(line);
83
+ builder.addLine({
84
+ storeLineId: line.id, productId: line.productId!, name: line.name, quantity: line.quantity,
85
+ ...(line.variantId !== undefined ? { variantId: line.variantId } : {}),
86
+ ...(line.sku !== undefined ? { sku: line.sku } : {}),
87
+ unitPrice: {
88
+ amount: (line.subtotalMinor + (read.pricesIncludeTax ? line.subtotalTaxMinor : 0)) / line.quantity,
89
+ currency: options.currency,
90
+ },
91
+ ...(tax?.taxClass !== undefined ? { taxClass: tax.taxClass } : {}),
92
+ ...(tax?.taxStatus !== undefined ? { taxStatus: tax.taxStatus } : {}),
93
+ });
94
+ }
95
+ const customer = storeOrderCustomer(read);
96
+ if (customer) builder.setCustomer(customer);
97
+ for (const charge of read.fees) builder.addFee({
98
+ name: charge.name, amountMinor: charge.totalMinor + (read.pricesIncludeTax ? charge.taxMinor : 0),
99
+ taxStatus: charge.taxStatus, storeLineId: charge.id,
100
+ ...(charge.taxClass !== undefined ? { taxClass: charge.taxClass } : {}),
101
+ });
102
+ for (const charge of read.shipping) builder.addShipping({
103
+ name: charge.name, amountMinor: charge.totalMinor + (read.pricesIncludeTax ? charge.taxMinor : 0),
104
+ taxStatus: charge.taxStatus, storeLineId: charge.id,
105
+ ...(charge.taxClass !== undefined ? { taxClass: charge.taxClass } : {}),
106
+ ...(charge.methodId !== undefined ? { methodId: charge.methodId } : {}),
107
+ });
108
+ const snapshot = builder.getSnapshot();
109
+ if (snapshot.totalMinor !== read.totalMinor || snapshot.taxMinor !== read.taxMinor) throw new StoreOrderRebuildError('totals');
110
+ for (const payment of read.payments) builder.addPayment({
111
+ method: 'external', amountMinor: payment.amountMinor, reference: payment.method,
112
+ storePaymentId: payment.id, locked: true,
113
+ });
114
+ return builder;
115
+ }
@@ -11,6 +11,7 @@ export type { OrderManager, OrderManagerOptions, ParkedOrderSummary } from './or
11
11
  export type {
12
12
  Order,
13
13
  OrderCoupon,
14
+ OrderCouponLine,
14
15
  SentOrder,
15
16
  LineItem,
16
17
  LineTaxLine,