@tallyui/connector-woocommerce 3.20.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
  }
@@ -1455,7 +1474,7 @@ function createWooOrders() {
1455
1474
  }
1456
1475
 
1457
1476
  // src/store-settings.ts
1458
- 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";
1459
1478
  var WOO_ORDER_CREATE_VERSION = 5;
1460
1479
  var WOO_NOTE_ORDER_CREATE_VERSION = 7;
1461
1480
  var WOO_TAX_ADDRESS_ORDER_CREATE_VERSION = 8;
@@ -1509,7 +1528,7 @@ async function wooStoreSettings(context) {
1509
1528
  taxRatesPpm
1510
1529
  };
1511
1530
  if (store.calc_taxes !== "yes") return settings;
1512
- settings.taxBasedOn = woocommerceTax.normalizeTaxBasedOn(store.tax_based_on);
1531
+ settings.taxBasedOn = woocommerceTax2.normalizeTaxBasedOn(store.tax_based_on);
1513
1532
  const rates = [];
1514
1533
  for (let page = 1; page <= 100; page++) {
1515
1534
  const response = await (context.fetch ?? globalThis.fetch)(`${context.baseUrl}/taxes?per_page=100&page=${page}&orderby=order&order=asc`, init);
@@ -1536,7 +1555,7 @@ async function wooStoreSettings(context) {
1536
1555
  ...rate.postcodes.length > 0 ? { postcodes: rate.postcodes } : {},
1537
1556
  ...rate.cities.length > 0 ? { cities: rate.cities } : {}
1538
1557
  },
1539
- taxClass: woocommerceTax.normalizeTaxClass(rate.class),
1558
+ taxClass: woocommerceTax2.normalizeTaxClass(rate.class),
1540
1559
  priority: rate.priority,
1541
1560
  compound: rate.compound,
1542
1561
  shipping: rate.shipping
@@ -1551,9 +1570,9 @@ async function wooStoreSettings(context) {
1551
1570
  const baseTaxAddress = { country: text(address.country), state: text(address.state), city: text(address.city), postcode: text(address.postcode) };
1552
1571
  if (baseTaxAddress.country) settings.baseTaxAddress = baseTaxAddress;
1553
1572
  if (baseTaxAddress.country) settings.taxRateTable = rates;
1554
- 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);
1555
1574
  for (const rate of matched) {
1556
- const slug = woocommerceTax.normalizeTaxClass(rate.class);
1575
+ const slug = woocommerceTax2.normalizeTaxClass(rate.class);
1557
1576
  (taxRates[slug] ??= []).push({
1558
1577
  id: rate.id,
1559
1578
  code: [rate.country, rate.state, rate.name || "TAX", String(rate.priority)].filter(Boolean).join("-").toUpperCase(),
@@ -1788,7 +1807,7 @@ async function wooReceiptTemplates(context, kind = "receipt") {
1788
1807
  }
1789
1808
 
1790
1809
  // package.json
1791
- var version = "3.20.0";
1810
+ var version = "3.20.1";
1792
1811
 
1793
1812
  // src/index.ts
1794
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.20.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.20.0"
30
+ "@tallyui/core": "^3.20.1"
31
31
  },
32
32
  "devDependencies": {
33
- "@tallyui/database": "3.20.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')}`;