@tallyui/pos 3.19.0 → 3.20.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 CHANGED
@@ -595,6 +595,12 @@ type StoreSettingsState = {
595
595
  state: 'ready';
596
596
  settings: StoreSettings;
597
597
  choice?: StoreSettingsChoice;
598
+ /** Re-reads the store's settings and tax rounding in place, for a "sync" button; the till stays `ready` meanwhile. A call while one runs is ignored. */
599
+ refresh: () => void;
600
+ /** True while a refresh runs; `settings` are still the ones in hand. */
601
+ refreshing?: boolean;
602
+ /** Why the last refresh failed; `settings` are the ones from before it. Cleared when the next refresh starts. */
603
+ refreshError?: unknown;
598
604
  } | {
599
605
  state: 'choose';
600
606
  choices: StoreSettingsChoices;
@@ -625,6 +631,7 @@ type StoreSettingsState = {
625
631
  * if (store.state === 'choose')
626
632
  * return <StoreSettingsChoiceScreen choices={store.choices} initial={store.initial} onSubmit={store.choose} />;
627
633
  * if (store.state !== 'ready') return <Loading />; // or an error with store.retry, or the app's own config when unsupported
634
+ * // Use store.refresh for a sync button.
628
635
  * const syncContext = withPricingContext(context, store.settings); // for the replication
629
636
  * return <TaxProvider {...taxProviderProps(store.settings)}>{children}</TaxProvider>;
630
637
  * ```
package/dist/index.js CHANGED
@@ -582,10 +582,11 @@ function cutText(text2, max) {
582
582
  const high = clean.charCodeAt(max - 2);
583
583
  return `${clean.slice(0, high >= 55296 && high <= 56319 ? max - 2 : max - 1)}\u2026`;
584
584
  }
585
- function netDiscountMinor(line, pricesIncludeTax) {
585
+ function netDiscountMinor(line, pricesIncludeTax, woocommerce) {
586
586
  if (!(line.taxInclusive ?? pricesIncludeTax) || line.discountMinor === 0) return line.discountMinor;
587
587
  const ratePpm = line.taxLines.reduce((sum3, tax) => sum3 + tax.ratePpm, 0);
588
- const taxAfter = line.taxLines.reduce((sum3, tax) => sum3 + BigInt(tax.taxMicros), 0n);
588
+ const afterDiscount = line.unitPriceMinor * line.quantity - line.discountMinor;
589
+ const taxAfter = !woocommerce && line.netMinor < afterDiscount ? taxMicros(afterDiscount, ratePpm, true) : line.taxLines.reduce((sum3, tax) => sum3 + BigInt(tax.taxMicros), 0n);
589
590
  const taxBefore = taxMicros(line.unitPriceMinor * line.quantity, ratePpm, true);
590
591
  return roundMicrosToMinor(BigInt(line.discountMinor) * MICROS_PER_MINOR - taxBefore + taxAfter);
591
592
  }
@@ -615,7 +616,7 @@ function toOrderCreateEnvelope(order, deviceId, attempt2 = 1, options) {
615
616
  const note = wireNote(order.note);
616
617
  if (taxAddress && base < 3) throw new Error(`Order ${order.id} is taxed on the customer's ${taxAddress} address but has no figures`);
617
618
  const version = taxAddress ? TAX_ADDRESS_ORDER_CREATE_VERSION : note && base >= 3 && cap !== void 0 && cap >= 7 ? 7 : base;
618
- const lineDiscounts = order.lines.map((line) => base >= 4 ? netDiscountMinor(line, order.pricesIncludeTax) : line.discountMinor);
619
+ const lineDiscounts = order.lines.map((line) => base >= 4 ? netDiscountMinor(line, order.pricesIncludeTax, order.taxRounding?.granularity === "woocommerce") : line.discountMinor);
619
620
  const discountMinor = lineDiscounts.reduce((sum3, discount) => sum3 + discount, 0);
620
621
  const email = order.customer?.email;
621
622
  const id2 = order.customer?.id;
@@ -902,19 +903,26 @@ function useStoreSettings(options) {
902
903
  const retryTimerRef = useRef(void 0);
903
904
  const retryDelayRef = useRef(INITIAL_RETRY_MS);
904
905
  const [state, setState] = useState(LOADING);
905
- const resolve = useCallback2((choice, from, known) => {
906
+ const resolve = useCallback2((choice, from, known, previous) => {
906
907
  if (from !== void 0 && from !== requestRef.current) return;
907
908
  clearTimeout(retryTimerRef.current);
908
909
  const request = ++requestRef.current;
909
910
  const current = () => request === requestRef.current;
910
- setState(LOADING);
911
+ const ready = (value) => {
912
+ const next = { state: "ready", ...value, refresh: () => resolve(value.choice, request, void 0, next) };
913
+ return next;
914
+ };
915
+ if (previous) {
916
+ const { refreshError: _error, ...refreshing } = previous;
917
+ setState({ ...refreshing, refreshing: true });
918
+ } else setState(LOADING);
911
919
  const rounding = known ?? readTaxRounding(optionsRef.current);
912
920
  Promise.all([resolveStoreSettings(optionsRef.current, choice), rounding]).then(
913
921
  ([result, taxRounding]) => {
914
922
  if (!current()) return;
915
- retryDelayRef.current = INITIAL_RETRY_MS;
923
+ if (!previous) retryDelayRef.current = INITIAL_RETRY_MS;
916
924
  if (result.status === "ready")
917
- setState({ state: "ready", settings: taxRounding ? { ...result.settings, taxRounding } : result.settings, choice: result.choice });
925
+ setState(ready({ settings: taxRounding ? { ...result.settings, taxRounding } : result.settings, choice: result.choice }));
918
926
  else if (result.status === "choose")
919
927
  setState({ state: "choose", choices: result.choices, initial: result.initial, choose: (pick) => resolve(pick, request, rounding) });
920
928
  else setState({ state: "unsupported" });
@@ -922,6 +930,10 @@ function useStoreSettings(options) {
922
930
  // A retry keeps the choice that failed: a new pick is saved only once it resolves.
923
931
  (error) => {
924
932
  if (!current()) return;
933
+ if (previous) {
934
+ setState(ready({ settings: previous.settings, choice: previous.choice, refreshError: error }));
935
+ return;
936
+ }
925
937
  const retry = () => resolve(choice, request);
926
938
  if (error instanceof StoreCapabilitiesUnavailableError) {
927
939
  const delay = retryDelayRef.current;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tallyui/pos",
3
- "version": "3.19.0",
3
+ "version": "3.20.0",
4
4
  "type": "module",
5
5
  "description": "Shared POS business logic for Tally UI — tax, orders, payments, discounts, receipts",
6
6
  "main": "./dist/index.js",
@@ -34,20 +34,20 @@
34
34
  "mustache": "^4.2.0"
35
35
  },
36
36
  "peerDependencies": {
37
- "@tallyui/core": "^3.19.0",
37
+ "@tallyui/core": "^3.20.0",
38
38
  "react": ">=18",
39
39
  "rxdb": "~17.5.0",
40
40
  "rxjs": ">=7"
41
41
  },
42
42
  "devDependencies": {
43
- "@tallyui/core": "3.19.0",
43
+ "@tallyui/core": "3.20.0",
44
44
  "@types/react": ">=18",
45
45
  "react": "^19.2.4",
46
46
  "rxdb": "17.5.0",
47
47
  "rxjs": "7.8.2",
48
- "@tallyui/connector-medusa": "3.19.0",
49
- "@tallyui/connector-woocommerce": "3.19.0",
50
- "@tallyui/database": "3.19.0"
48
+ "@tallyui/connector-medusa": "3.20.0",
49
+ "@tallyui/connector-woocommerce": "3.20.0",
50
+ "@tallyui/database": "3.20.0"
51
51
  },
52
52
  "scripts": {
53
53
  "build": "tsup",
@@ -48,13 +48,16 @@ export function cutText(text: string, max: number): string {
48
48
 
49
49
  /**
50
50
  * Version 4 (#286): a line's discount made tax-exclusive. An exclusive line's is unchanged; an inclusive line's
51
- * gross discount D on its amount A becomes `net(A) − net(A − D)`, exact in millionths (net(A − D) from its stored
52
- * taxLines, the tax on A − D; net(A) from `taxMicros`, as the builder taxes), rounded half away from zero.
51
+ * gross discount D on its amount A becomes `net(A) − net(A − D)`, exact in millionths, rounded half away from zero.
52
+ * Tax comes from stored taxLines after D, recomputed before a store-priced coupon, and from `taxMicros` before D.
53
53
  */
54
- function netDiscountMinor(line: PosOrderLine, pricesIncludeTax: boolean): number {
54
+ function netDiscountMinor(line: PosOrderLine, pricesIncludeTax: boolean, woocommerce: boolean): number {
55
55
  if (!(line.taxInclusive ?? pricesIncludeTax) || line.discountMinor === 0) return line.discountMinor;
56
56
  const ratePpm = line.taxLines.reduce((sum, tax) => sum + tax.ratePpm, 0);
57
- const taxAfter = line.taxLines.reduce((sum, tax) => sum + BigInt(tax.taxMicros), 0n);
57
+ const afterDiscount = line.unitPriceMinor * line.quantity - line.discountMinor;
58
+ // A store-priced coupon leaves stored taxLines after the coupon; the line discount needs tax before it.
59
+ const taxAfter = !woocommerce && line.netMinor < afterDiscount ? taxMicros(afterDiscount, ratePpm, true)
60
+ : line.taxLines.reduce((sum, tax) => sum + BigInt(tax.taxMicros), 0n);
58
61
  const taxBefore = taxMicros(line.unitPriceMinor * line.quantity, ratePpm, true);
59
62
  return roundMicrosToMinor(BigInt(line.discountMinor) * MICROS_PER_MINOR - taxBefore + taxAfter);
60
63
  }
@@ -117,7 +120,7 @@ export function toOrderCreateEnvelope(order: PosOrder, deviceId: string, attempt
117
120
  if (taxAddress && base < 3) throw new Error(`Order ${order.id} is taxed on the customer's ${taxAddress} address but has no figures`);
118
121
  const version = (taxAddress ? TAX_ADDRESS_ORDER_CREATE_VERSION
119
122
  : note && base >= 3 && cap !== undefined && cap >= 7 ? 7 : base) as OrderCreateEnvelope['version'];
120
- const lineDiscounts = order.lines.map((line) => base >= 4 ? netDiscountMinor(line, order.pricesIncludeTax) : line.discountMinor);
123
+ const lineDiscounts = order.lines.map((line) => base >= 4 ? netDiscountMinor(line, order.pricesIncludeTax, order.taxRounding?.granularity === 'woocommerce') : line.discountMinor);
121
124
  // The order's discount is the sum of its lines', on one basis, so the payload's two always agree.
122
125
  const discountMinor = lineDiscounts.reduce((sum, discount) => sum + discount, 0);
123
126
  const email = order.customer?.email;
@@ -6,7 +6,13 @@ import type { ResolveStoreSettingsOptions } from './resolve-store-settings';
6
6
 
7
7
  export type StoreSettingsState =
8
8
  | { state: 'loading' }
9
- | { state: 'ready'; settings: StoreSettings; choice?: StoreSettingsChoice }
9
+ | { state: 'ready'; settings: StoreSettings; choice?: StoreSettingsChoice;
10
+ /** Re-reads the store's settings and tax rounding in place, for a "sync" button; the till stays `ready` meanwhile. A call while one runs is ignored. */
11
+ refresh: () => void;
12
+ /** True while a refresh runs; `settings` are still the ones in hand. */
13
+ refreshing?: boolean;
14
+ /** Why the last refresh failed; `settings` are the ones from before it. Cleared when the next refresh starts. */
15
+ refreshError?: unknown }
10
16
  | { state: 'choose'; choices: StoreSettingsChoices; initial?: StoreSettingsChoice; choose: (choice: StoreSettingsChoice) => void }
11
17
  | { state: 'error'; error: unknown; retry: () => void; nextRetryAt?: number }
12
18
  | { state: 'unsupported' };
@@ -60,6 +66,7 @@ function readTaxRounding({ connector, context }: ResolveStoreSettingsOptions): P
60
66
  * if (store.state === 'choose')
61
67
  * return <StoreSettingsChoiceScreen choices={store.choices} initial={store.initial} onSubmit={store.choose} />;
62
68
  * if (store.state !== 'ready') return <Loading />; // or an error with store.retry, or the app's own config when unsupported
69
+ * // Use store.refresh for a sync button.
63
70
  * const syncContext = withPricingContext(context, store.settings); // for the replication
64
71
  * return <TaxProvider {...taxProviderProps(store.settings)}>{children}</TaxProvider>;
65
72
  * ```
@@ -77,19 +84,26 @@ export function useStoreSettings(options: ResolveStoreSettingsOptions): StoreSet
77
84
  // arriving after a store switch made that request stale is ignored, so it can't apply an old
78
85
  // pick to the new store. `known`, when given, is the rounding that request already read, so
79
86
  // a pick doesn't read it again.
80
- const resolve = useCallback((choice?: StoreSettingsChoice, from?: number, known?: Promise<TaxRounding | undefined>) => {
87
+ const resolve = useCallback((choice?: StoreSettingsChoice, from?: number, known?: Promise<TaxRounding | undefined>, previous?: Extract<StoreSettingsState, { state: 'ready' }>) => {
81
88
  if (from !== undefined && from !== requestRef.current) return;
82
89
  clearTimeout(retryTimerRef.current);
83
90
  const request = ++requestRef.current;
84
91
  const current = () => request === requestRef.current;
85
- setState(LOADING);
92
+ const ready = (value: Omit<Extract<StoreSettingsState, { state: 'ready' }>, 'state' | 'refresh'>) => {
93
+ const next: Extract<StoreSettingsState, { state: 'ready' }> = { state: 'ready', ...value, refresh: () => resolve(value.choice, request, undefined, next) };
94
+ return next;
95
+ };
96
+ if (previous) {
97
+ const { refreshError: _error, ...refreshing } = previous;
98
+ setState({ ...refreshing, refreshing: true });
99
+ } else setState(LOADING);
86
100
  const rounding = known ?? readTaxRounding(optionsRef.current);
87
101
  Promise.all([resolveStoreSettings(optionsRef.current, choice), rounding]).then(
88
102
  ([result, taxRounding]) => {
89
103
  if (!current()) return;
90
- retryDelayRef.current = INITIAL_RETRY_MS;
104
+ if (!previous) retryDelayRef.current = INITIAL_RETRY_MS;
91
105
  if (result.status === 'ready')
92
- setState({ state: 'ready', settings: taxRounding ? { ...result.settings, taxRounding } : result.settings, choice: result.choice });
106
+ setState(ready({ settings: taxRounding ? { ...result.settings, taxRounding } : result.settings, choice: result.choice }));
93
107
  else if (result.status === 'choose')
94
108
  setState({ state: 'choose', choices: result.choices, initial: result.initial, choose: (pick) => resolve(pick, request, rounding) });
95
109
  else setState({ state: 'unsupported' });
@@ -97,6 +111,10 @@ export function useStoreSettings(options: ResolveStoreSettingsOptions): StoreSet
97
111
  // A retry keeps the choice that failed: a new pick is saved only once it resolves.
98
112
  (error: unknown) => {
99
113
  if (!current()) return;
114
+ if (previous) {
115
+ setState(ready({ settings: previous.settings, choice: previous.choice, refreshError: error }));
116
+ return;
117
+ }
100
118
  const retry = () => resolve(choice, request);
101
119
  if (error instanceof StoreCapabilitiesUnavailableError) {
102
120
  const delay = retryDelayRef.current;