@tallyui/pos 4.0.1 → 4.2.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.
- package/dist/index.d.ts +123 -3
- package/dist/index.js +267 -84
- package/package.json +6 -6
- package/src/index.ts +7 -3
- package/src/order/order-builder.ts +5 -3
- package/src/order/order-drafts.ts +2 -0
- package/src/order/taken-at.ts +5 -0
- package/src/order/types.ts +2 -0
- package/src/pos-order/finalize.ts +2 -0
- package/src/pos-order/reopen.ts +2 -0
- package/src/pos-order/schema.ts +4 -2
- package/src/pos-order/types.ts +2 -0
- package/src/print/escpos/bluetooth-sink.ts +114 -0
- package/src/print/escpos/index.ts +4 -0
- package/src/print/escpos/sinks.test-d.ts +77 -0
- package/src/print/escpos/transport.ts +3 -2
- package/src/print/escpos/usb-sink.ts +84 -0
- package/src/print/index.ts +2 -2
- package/src/sale/use-sale.ts +16 -9
- package/src/store-orders/index.ts +2 -0
- package/src/store-orders/use-store-orders-in-range.ts +127 -0
- package/src/tax/index.ts +1 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import type { ListOrdersQuery, StoreOrder, StoreOrderPage, SyncContext, TallyConnector } from '@tallyui/core';
|
|
3
|
+
|
|
4
|
+
/** Progress of one ranged read; null when idle, finished, failed or aborted. */
|
|
5
|
+
export interface OrderDownloadProgress { downloaded: number; total: number | null }
|
|
6
|
+
|
|
7
|
+
export interface StoreOrdersInRange {
|
|
8
|
+
orders: StoreOrder[];
|
|
9
|
+
progress: OrderDownloadProgress | null;
|
|
10
|
+
unreadable: number;
|
|
11
|
+
error: unknown | null;
|
|
12
|
+
complete: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type StoreOrdersRangeQuery = Omit<ListOrdersQuery, 'page' | 'perPage'>;
|
|
16
|
+
|
|
17
|
+
/** The most `ListOrdersQuery.perPage` allows. */
|
|
18
|
+
const PER_PAGE = 100;
|
|
19
|
+
/** The error a read ends with when a page says `hasMore: true` but adds no new order id. */
|
|
20
|
+
const STALLED = 'listOrders answered hasMore: true with a page that added no new order';
|
|
21
|
+
|
|
22
|
+
type Read = {
|
|
23
|
+
connector: TallyConnector;
|
|
24
|
+
context: SyncContext;
|
|
25
|
+
key: string;
|
|
26
|
+
orders: StoreOrder[];
|
|
27
|
+
unreadable: number;
|
|
28
|
+
total: number | null;
|
|
29
|
+
} & ({ status: 'reading' | 'complete' } | { status: 'error'; error: unknown });
|
|
30
|
+
|
|
31
|
+
const IDLE: StoreOrdersInRange = { orders: [], progress: null, unreadable: 0, error: null, complete: false };
|
|
32
|
+
const STARTING: StoreOrdersInRange = {
|
|
33
|
+
orders: [], progress: { downloaded: 0, total: null }, unreadable: 0, error: null, complete: false,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Calls `listOrders` page after page, one at a time, until a page has `hasMore: false` or `live()` turns false. */
|
|
37
|
+
async function readPages(
|
|
38
|
+
connector: TallyConnector,
|
|
39
|
+
context: SyncContext,
|
|
40
|
+
query: StoreOrdersRangeQuery,
|
|
41
|
+
live: () => boolean,
|
|
42
|
+
onPage: (page: StoreOrderPage, first: boolean) => void,
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
for (let page = 1; ; page++) {
|
|
45
|
+
// Yield first so a read abandoned before its call (StrictMode's double effect) calls nothing. A synchronous
|
|
46
|
+
// throw from `listOrders` still counts as a rejection, since this is an async function.
|
|
47
|
+
await Promise.resolve();
|
|
48
|
+
if (!live()) return;
|
|
49
|
+
const result = await connector.listOrders!(context, { ...query, page, perPage: PER_PAGE });
|
|
50
|
+
if (!live()) return;
|
|
51
|
+
onPage(result, page === 1);
|
|
52
|
+
if (!result.hasMore) return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Every store order matching `query`, read online page by page (ADR-081 decision 6). Nothing is stored or cached.
|
|
58
|
+
*
|
|
59
|
+
* - Idle (`progress: null`, nothing called) without `connector`, `context`, `query` or `connector.listOrders`.
|
|
60
|
+
* - `progress` is `{ downloaded, total }` while pages are fetched: `{ downloaded: 0, total: null }` until the first
|
|
61
|
+
* page lands, then `total` is that page's `total` (`null` when the store gives no count) and stays fixed for the
|
|
62
|
+
* read. `downloaded` is always `orders.length + unreadable`.
|
|
63
|
+
* - Orders are deduplicated by id as pages land; the first copy stays. `unreadable` sums the `unreadable` of the first
|
|
64
|
+
* page and of each later page that adds a new order id; a later page that adds none adds nothing to it.
|
|
65
|
+
* - A page with `hasMore: false` finishes: `complete: true`, `progress: null`, `orders` in `listOrders`' own order.
|
|
66
|
+
* - A rejection stops the read: `error` is what was thrown, `progress` is `null`, the orders read so far stay.
|
|
67
|
+
* - A page with `hasMore: true` that adds no new order id also stops the read, with an `Error`, even when it reports
|
|
68
|
+
* unreadable orders, so a store that keeps repeating a page stops.
|
|
69
|
+
* - A `query` that differs by value, or a new `connector` or `context`, abandons the read and starts again from
|
|
70
|
+
* page 1, showing none of the old orders; an equal new object changes nothing. Unmounting abandons the read.
|
|
71
|
+
* So keep `connector` and `context` the same objects across renders: a new object each render restarts the read on
|
|
72
|
+
* every render.
|
|
73
|
+
*
|
|
74
|
+
* Offset paging over a live store can repeat a row, which is dropped, or push a row past a page boundary mid-read,
|
|
75
|
+
* which is then missed until the next read. Nothing is cached, so opening the screen again corrects it.
|
|
76
|
+
*/
|
|
77
|
+
export function useStoreOrdersInRange(
|
|
78
|
+
connector: TallyConnector | null,
|
|
79
|
+
context: SyncContext | null,
|
|
80
|
+
query: StoreOrdersRangeQuery | null,
|
|
81
|
+
): StoreOrdersInRange {
|
|
82
|
+
const key = query ? JSON.stringify(query) : null;
|
|
83
|
+
const [read, setRead] = useState<Read | null>(null);
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (!connector?.listOrders || !context || key === null) return;
|
|
87
|
+
let live = true;
|
|
88
|
+
const seen = new Set<string>();
|
|
89
|
+
let current: Read = { connector, context, key, orders: [], unreadable: 0, total: null, status: 'reading' };
|
|
90
|
+
const publish = (next: Read) => {
|
|
91
|
+
current = next;
|
|
92
|
+
setRead(next);
|
|
93
|
+
};
|
|
94
|
+
readPages(connector, context, JSON.parse(key) as StoreOrdersRangeQuery, () => live, (page, first) => {
|
|
95
|
+
const fresh = page.orders.filter((order) => {
|
|
96
|
+
if (seen.has(order.id)) return false;
|
|
97
|
+
seen.add(order.id);
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
if (page.hasMore && fresh.length === 0) throw new Error(STALLED);
|
|
101
|
+
publish({
|
|
102
|
+
...current,
|
|
103
|
+
orders: [...current.orders, ...fresh],
|
|
104
|
+
unreadable: current.unreadable + (first || fresh.length > 0 ? (page.unreadable ?? 0) : 0),
|
|
105
|
+
total: first ? (page.total ?? null) : current.total,
|
|
106
|
+
status: page.hasMore ? 'reading' : 'complete',
|
|
107
|
+
});
|
|
108
|
+
}).catch((error: unknown) => {
|
|
109
|
+
if (live) publish({ ...current, status: 'error', error });
|
|
110
|
+
});
|
|
111
|
+
// Abandons the read in flight on a new connector, context or query value, or on unmount, and clears its published
|
|
112
|
+
// value so a switch back cannot show it; the memo's check covers the render before this cleanup runs.
|
|
113
|
+
return () => {
|
|
114
|
+
live = false;
|
|
115
|
+
setRead(null);
|
|
116
|
+
};
|
|
117
|
+
}, [connector, context, key]);
|
|
118
|
+
|
|
119
|
+
return useMemo(() => {
|
|
120
|
+
if (!connector?.listOrders || !context || key === null) return IDLE;
|
|
121
|
+
// A read for another connector, context or query is stale: the current one is only starting.
|
|
122
|
+
if (!read || read.connector !== connector || read.context !== context || read.key !== key) return STARTING;
|
|
123
|
+
const { orders, unreadable, total } = read;
|
|
124
|
+
const progress = read.status === 'reading' ? { downloaded: orders.length + unreadable, total } : null;
|
|
125
|
+
return { orders, progress, unreadable, error: read.status === 'error' ? read.error : null, complete: read.status === 'complete' };
|
|
126
|
+
}, [connector, context, key, read]);
|
|
127
|
+
}
|
package/src/tax/index.ts
CHANGED
|
@@ -9,3 +9,4 @@ export { saleTaxContexts } from './sale-tax-contexts';
|
|
|
9
9
|
export type { SaleTaxContext } from './sale-tax-contexts';
|
|
10
10
|
export { taxDerivation } from './tax-derivation';
|
|
11
11
|
export type { TaxDerivation, TaxDerivationClass, TaxDerivationRate, TaxRateWhy, SaleTaxLocation } from './tax-derivation';
|
|
12
|
+
export { taxedLinesFor } from './taxed-lines';
|