@tallyui/connector-woocommerce 3.19.0 → 3.20.1

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
@@ -83,6 +83,11 @@ interface WooLocalOrder {
83
83
  id: string;
84
84
  netMicros?: string; /** The line's discount in its own tax mode (gross when inclusive), as stored. */
85
85
  discountMinor?: number;
86
+ taxLines?: ReadonlyArray<{
87
+ rateId?: number;
88
+ compound?: boolean;
89
+ ratePpm: number;
90
+ }>;
86
91
  }>;
87
92
  fees?: ReadonlyArray<{
88
93
  id: string;
package/dist/index.js CHANGED
@@ -1012,7 +1012,7 @@ import {
1012
1012
  } from "@tallyui/core";
1013
1013
 
1014
1014
  // src/commands/transport.ts
1015
- import { minorUnitDigits, moneyFromDecimalString, moneyFromMajor as moneyFromMajor2 } from "@tallyui/core";
1015
+ import { minorUnitDigits, moneyFromDecimalString, moneyFromMajor as moneyFromMajor2, woocommerceTax } from "@tallyui/core";
1016
1016
  function wooMinorFromDecimal(text, currency) {
1017
1017
  const trimmed = text.trim();
1018
1018
  if (!/^\d+(\.\d+)?$/.test(trimmed)) return void 0;
@@ -1023,6 +1023,20 @@ function wooMinorFromDecimal(text, currency) {
1023
1023
  if (end > digits) return moneyFromMajor2(trimmed, currency)?.amount;
1024
1024
  return moneyFromDecimalString(end ? `${whole}.${fraction.slice(0, end)}` : whole, currency)?.amount;
1025
1025
  }
1026
+ function preCouponNetMicros(minor2, digits, stored) {
1027
+ if (!stored || stored.taxLines?.some((tax) => typeof tax.rateId !== "number" || typeof tax.compound !== "boolean")) return void 0;
1028
+ const factor = 10 ** digits, precision = woocommerceTax.getRoundingPrecision(digits);
1029
+ const input = { amount: Number(minor2) / factor, rates: (stored.taxLines ?? []).map((tax, index) => ({
1030
+ id: tax.rateId,
1031
+ rate: String(tax.ratePpm / 1e4),
1032
+ compound: tax.compound,
1033
+ priority: index,
1034
+ order: index
1035
+ })), amountIncludesTax: true, dp: digits, perRatePrecision: precision };
1036
+ const result = woocommerceTax.calculateTaxes(input);
1037
+ const net = woocommerceTax.roundHalfUp(input.amount - result.total, precision);
1038
+ return String(Math.round(net * factor * 1e6));
1039
+ }
1026
1040
  function stripTags(text) {
1027
1041
  let previous;
1028
1042
  do {
@@ -1102,6 +1116,11 @@ function toWooOrderPayload(envelope, local, options) {
1102
1116
  const discount = (line.taxInclusive ?? p.pricesIncludeTax) && (line.discountMinor ?? 0) > 0 ? local?.lines.find((stored) => stored.id === line.clientLineId)?.discountMinor : line.discountMinor ?? 0;
1103
1117
  if (discount === void 0) return { error: { code: "unsupported_tax_mode", message: "Tax-inclusive prices need the till's WooCommerce tax figures; update the till." } };
1104
1118
  const quantity = BigInt(line.quantity), minor2 = BigInt(line.unitPriceMinor) * quantity - BigInt(discount);
1119
+ if (line.taxInclusive ?? p.pricesIncludeTax) {
1120
+ const net = inclusiveNet(preCouponNetMicros(minor2, digits, local?.lines.find((stored) => stored.id === line.clientLineId)), line.clientLineId);
1121
+ if (typeof net !== "string") return net;
1122
+ subtotal = total = net;
1123
+ }
1105
1124
  const divisor = quantity * 10n ** BigInt(digits), micros = (minor2 * 1000000n * 2n + divisor) / (divisor * 2n);
1106
1125
  price = minor2 % quantity === 0n ? major(Number(minor2 / quantity)) : `${micros / 1000000n}.${(micros % 1000000n).toString().padStart(6, "0")}`;
1107
1126
  }
@@ -1437,7 +1456,8 @@ function createWooOrders() {
1437
1456
  const orders = body.map(toWooStoreOrder);
1438
1457
  const totalPages = headers.get("X-WP-TotalPages");
1439
1458
  const hasMore = totalPages !== null && /^\d+$/.test(totalPages.trim()) ? page < Number.parseInt(totalPages, 10) : orders.length === perPage;
1440
- return { orders, hasMore };
1459
+ const total = headers.get("X-WP-Total");
1460
+ return { orders, hasMore, ...total && /^\d+$/.test(total) ? { total: Number(total) } : {} };
1441
1461
  },
1442
1462
  async getOrders(context, ids) {
1443
1463
  const wanted = [...new Set(ids.filter((id) => /^[1-9]\d*$/.test(id)))];
@@ -1454,7 +1474,7 @@ function createWooOrders() {
1454
1474
  }
1455
1475
 
1456
1476
  // src/store-settings.ts
1457
- import { ConnectorUnauthorizedError as ConnectorUnauthorizedError2, StoreSettingsError, minorUnitDigits as minorUnitDigits3, woocommerceTax } from "@tallyui/core";
1477
+ import { ConnectorUnauthorizedError as ConnectorUnauthorizedError2, StoreSettingsError, minorUnitDigits as minorUnitDigits3, woocommerceTax as woocommerceTax2 } from "@tallyui/core";
1458
1478
  var WOO_ORDER_CREATE_VERSION = 5;
1459
1479
  var WOO_NOTE_ORDER_CREATE_VERSION = 7;
1460
1480
  var WOO_TAX_ADDRESS_ORDER_CREATE_VERSION = 8;
@@ -1503,11 +1523,12 @@ async function wooStoreSettings(context) {
1503
1523
  calcDiscountsSequentially: (store.woocommerce_calc_discounts_sequentially ?? store.calc_discounts_sequentially) === "yes",
1504
1524
  ...typeof store.shipping_tax_class === "string" ? { shippingTaxClass: store.shipping_tax_class } : {},
1505
1525
  taxClassSlugs: classes.map((row) => row.slug),
1526
+ taxClasses: classes.map((row) => ({ slug: row.slug, name: typeof row.name === "string" && row.name !== "" ? row.name : row.slug })),
1506
1527
  taxRates,
1507
1528
  taxRatesPpm
1508
1529
  };
1509
1530
  if (store.calc_taxes !== "yes") return settings;
1510
- settings.taxBasedOn = woocommerceTax.normalizeTaxBasedOn(store.tax_based_on);
1531
+ settings.taxBasedOn = woocommerceTax2.normalizeTaxBasedOn(store.tax_based_on);
1511
1532
  const rates = [];
1512
1533
  for (let page = 1; page <= 100; page++) {
1513
1534
  const response = await (context.fetch ?? globalThis.fetch)(`${context.baseUrl}/taxes?per_page=100&page=${page}&orderby=order&order=asc`, init);
@@ -1534,7 +1555,7 @@ async function wooStoreSettings(context) {
1534
1555
  ...rate.postcodes.length > 0 ? { postcodes: rate.postcodes } : {},
1535
1556
  ...rate.cities.length > 0 ? { cities: rate.cities } : {}
1536
1557
  },
1537
- taxClass: woocommerceTax.normalizeTaxClass(rate.class),
1558
+ taxClass: woocommerceTax2.normalizeTaxClass(rate.class),
1538
1559
  priority: rate.priority,
1539
1560
  compound: rate.compound,
1540
1561
  shipping: rate.shipping
@@ -1549,9 +1570,9 @@ async function wooStoreSettings(context) {
1549
1570
  const baseTaxAddress = { country: text(address.country), state: text(address.state), city: text(address.city), postcode: text(address.postcode) };
1550
1571
  if (baseTaxAddress.country) settings.baseTaxAddress = baseTaxAddress;
1551
1572
  if (baseTaxAddress.country) settings.taxRateTable = rates;
1552
- const matched = woocommerceTax.filterTaxRates(rates, address.country, address.state, address.postcode, address.city);
1573
+ const matched = woocommerceTax2.filterTaxRates(rates, address.country, address.state, address.postcode, address.city);
1553
1574
  for (const rate of matched) {
1554
- const slug = woocommerceTax.normalizeTaxClass(rate.class);
1575
+ const slug = woocommerceTax2.normalizeTaxClass(rate.class);
1555
1576
  (taxRates[slug] ??= []).push({
1556
1577
  id: rate.id,
1557
1578
  code: [rate.country, rate.state, rate.name || "TAX", String(rate.priority)].filter(Boolean).join("-").toUpperCase(),
@@ -1786,7 +1807,7 @@ async function wooReceiptTemplates(context, kind = "receipt") {
1786
1807
  }
1787
1808
 
1788
1809
  // package.json
1789
- var version = "3.19.0";
1810
+ var version = "3.20.1";
1790
1811
 
1791
1812
  // src/index.ts
1792
1813
  import { ConnectorUnauthorizedError as ConnectorUnauthorizedError5 } from "@tallyui/core";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tallyui/connector-woocommerce",
3
- "version": "3.19.0",
3
+ "version": "3.20.1",
4
4
  "type": "module",
5
5
  "description": "WooCommerce connector for Tally UI",
6
6
  "main": "./dist/index.js",
@@ -27,10 +27,10 @@
27
27
  "directory": "connectors/woocommerce"
28
28
  },
29
29
  "peerDependencies": {
30
- "@tallyui/core": "^3.19.0"
30
+ "@tallyui/core": "^3.20.1"
31
31
  },
32
32
  "devDependencies": {
33
- "@tallyui/database": "3.19.0",
33
+ "@tallyui/database": "3.20.1",
34
34
  "rxdb": "17.5.0"
35
35
  },
36
36
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  // ADR-073; M3-spike-paid-push.md proves paid order pushes against WCPOS 1.10.20.
2
- import { minorUnitDigits, moneyFromDecimalString, moneyFromMajor, type CommandResult, type OrderAddress, type OrderCreateEnvelope } from '@tallyui/core';
2
+ import { minorUnitDigits, moneyFromDecimalString, moneyFromMajor, woocommerceTax, type CommandResult, type OrderAddress, type OrderCreateEnvelope } from '@tallyui/core';
3
3
 
4
4
  export type WooTransportOutcome =
5
5
  | { kind: 'results'; results: CommandResult[] }
@@ -32,11 +32,24 @@ export function wooMinorFromDecimal(text: string, currency: string): number | un
32
32
  }
33
33
 
34
34
  export interface WooLocalOrder {
35
- lines: ReadonlyArray<{ id: string; netMicros?: string; /** The line's discount in its own tax mode (gross when inclusive), as stored. */ discountMinor?: number }>;
35
+ lines: ReadonlyArray<{ id: string; netMicros?: string; /** The line's discount in its own tax mode (gross when inclusive), as stored. */ discountMinor?: number;
36
+ taxLines?: ReadonlyArray<{ rateId?: number; compound?: boolean; ratePpm: number }> }>;
36
37
  fees?: ReadonlyArray<{ id: string; netMicros?: string }>;
37
38
  shipping?: ReadonlyArray<{ id: string; netMicros?: string }>;
38
39
  }
39
40
 
41
+ // Match woocommerceLine's pre-coupon net calculation exactly (ADR-076).
42
+ function preCouponNetMicros(minor: bigint, digits: number, stored: WooLocalOrder['lines'][number] | undefined): string | undefined {
43
+ if (!stored || stored.taxLines?.some((tax) => typeof tax.rateId !== 'number' || typeof tax.compound !== 'boolean')) return undefined;
44
+ const factor = 10 ** digits, precision = woocommerceTax.getRoundingPrecision(digits);
45
+ const input = { amount: Number(minor) / factor, rates: (stored.taxLines ?? []).map((tax, index) => ({
46
+ id: tax.rateId!, rate: String(tax.ratePpm / 10000), compound: tax.compound!, priority: index, order: index,
47
+ })), amountIncludesTax: true, dp: digits, perRatePrecision: precision };
48
+ const result = woocommerceTax.calculateTaxes(input);
49
+ const net = woocommerceTax.roundHalfUp(input.amount - result.total, precision);
50
+ return String(Math.round(net * factor * 1e6));
51
+ }
52
+
40
53
  // Repeated until stable so no removal can join a new tag (CodeQL js/incomplete-multi-character-sanitization).
41
54
  function stripTags(text: string): string {
42
55
  let previous;
@@ -119,6 +132,11 @@ export function toWooOrderPayload(envelope: OrderCreateEnvelope, local?: WooLoca
119
132
  ? local?.lines.find((stored) => stored.id === line.clientLineId)?.discountMinor : line.discountMinor ?? 0;
120
133
  if (discount === undefined) return { error: { code: 'unsupported_tax_mode', message: "Tax-inclusive prices need the till's WooCommerce tax figures; update the till." } };
121
134
  const quantity = BigInt(line.quantity), minor = BigInt(line.unitPriceMinor) * quantity - BigInt(discount);
135
+ if (line.taxInclusive ?? p.pricesIncludeTax) {
136
+ const net = inclusiveNet(preCouponNetMicros(minor, digits, local?.lines.find((stored) => stored.id === line.clientLineId)), line.clientLineId);
137
+ if (typeof net !== 'string') return net;
138
+ subtotal = total = net;
139
+ }
122
140
  const divisor = quantity * 10n ** BigInt(digits), micros = (minor * 1000000n * 2n + divisor) / (divisor * 2n);
123
141
  price = minor % quantity === 0n ? major(Number(minor / quantity))
124
142
  : `${micros / 1000000n}.${(micros % 1000000n).toString().padStart(6, '0')}`;
package/src/orders.ts CHANGED
@@ -180,7 +180,9 @@ export function createWooOrders(): Required<Pick<TallyConnector, 'listOrders' |
180
180
  const hasMore = totalPages !== null && /^\d+$/.test(totalPages.trim())
181
181
  ? page < Number.parseInt(totalPages, 10)
182
182
  : orders.length === perPage;
183
- return { orders, hasMore };
183
+ // X-WP-Total counts every order the query matches.
184
+ const total = headers.get('X-WP-Total');
185
+ return { orders, hasMore, ...(total && /^\d+$/.test(total) ? { total: Number(total) } : {}) };
184
186
  },
185
187
 
186
188
  async getOrders(context: SyncContext, ids: readonly string[]) {
@@ -60,6 +60,7 @@ export async function wooStoreSettings(context: SyncContext): Promise<StoreSetti
60
60
  calcDiscountsSequentially: (store.woocommerce_calc_discounts_sequentially ?? store.calc_discounts_sequentially) === 'yes',
61
61
  ...(typeof store.shipping_tax_class === 'string' ? { shippingTaxClass: store.shipping_tax_class } : {}),
62
62
  taxClassSlugs: classes.map((row) => row.slug),
63
+ taxClasses: classes.map((row) => ({ slug: row.slug, name: typeof row.name === 'string' && row.name !== '' ? row.name : row.slug })),
63
64
  taxRates,
64
65
  taxRatesPpm,
65
66
  };