@tallyui/pos 5.1.0 → 5.3.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.
@@ -10,7 +10,8 @@
10
10
  import { legMethodId } from '@tallyui/core';
11
11
  import { parkedTakenLegs } from '../order/order-drafts';
12
12
  import type { Payment } from '../order/types';
13
- import { tillPayments } from '../pos-order/till-payments';
13
+ import { countedTillPayments } from '../pos-order/till-payments';
14
+ import type { PosOrderStoreSettlement } from '../pos-order/types';
14
15
  import type { PosRefund } from '../refund/pos-refund';
15
16
 
16
17
  export type RefundRow = Pick<PosRefund, 'id' | 'sessionId' | 'status' | 'result'>;
@@ -39,10 +40,10 @@ export type LedgerRow = {
39
40
  * A locked store payment (storePaymentId set) is the store's earlier money, not this till's, and is left out (ADR-090 gate 11).
40
41
  * A payment stamped with the session that took it counts there, else in its order's (S3).
41
42
  */
42
- export function paymentLedgerRows<P extends { method: string; amountMinor: number; storePaymentId?: string; sessionId?: string }>(
43
- orders: readonly { sessionId?: string; payments: readonly P[] }[],
43
+ export function paymentLedgerRows<P extends { id?: string; method: string; amountMinor: number; storePaymentId?: string; sessionId?: string }>(
44
+ orders: readonly { sessionId?: string; payments: readonly P[]; storeSettlement?: TillOrder['storeSettlement'] }[],
44
45
  ): LedgerRow[] {
45
- return orders.flatMap((order) => tillPayments(order.payments).map((payment) => ({
46
+ return orders.flatMap((order) => countedTillPayments(order).map((payment) => ({
46
47
  session_id: payment.sessionId ?? order.sessionId, kind: payment.method === 'cash' ? 'cash' : 'other', method_id: payment.method,
47
48
  status: 'captured', amountMinor: payment.amountMinor,
48
49
  })));
@@ -174,11 +175,11 @@ export function deriveDrawerTerms(input: Parameters<typeof deriveExpected>[0]):
174
175
  };
175
176
  }
176
177
 
177
- type TillLeg = { method: string; methodId?: string; amountMinor: number; storePaymentId?: string; sessionId?: string };
178
- type TillOrder = { sessionId?: string; payments: readonly TillLeg[] };
178
+ type TillLeg = { id?: string; method: string; methodId?: string; amountMinor: number; storePaymentId?: string; sessionId?: string };
179
+ type TillOrder = { sessionId?: string; payments: readonly TillLeg[]; storeSettlement?: Pick<PosOrderStoreSettlement, 'kind' | 'state' | 'legs'> };
179
180
 
180
181
  /**
181
- * Takings per payment method for one session, for the closure sheet: this till's captured payments (`tillPayments`) in
182
+ * Takings per payment method for one session, for the closure sheet: this till's captured payments (`countedTillPayments`) in
182
183
  * `sessionId`: a payment's own session, else its order's (S3), keyed by `legMethodId`'s kind plus id (`external:stripe`), with the number of
183
184
  * payments behind each. Local only, never synced: register rows and payloads keep `method_id: payment.method` (#256, ADR-070
184
185
  * decision 1). A leg with no methodId keys exactly as deriveDrawerTerms does (`cash` or its method); the figures sum to
@@ -192,7 +193,7 @@ export function takingsByMethod(
192
193
  ): Record<string, DrawerTerm> {
193
194
  const takings: Record<string, DrawerTerm> = Object.create(null);
194
195
  for (const order of [...orders, { payments: extra }] as readonly TillOrder[]) {
195
- for (const payment of tillPayments(order.payments)) {
196
+ for (const payment of countedTillPayments(order)) {
196
197
  if ((payment.sessionId ?? order.sessionId) !== sessionId) continue;
197
198
  const term = (takings[legMethodId(payment)] ??= { amountMinor: 0, count: 0 });
198
199
  term.amountMinor += payment.amountMinor;
@@ -12,10 +12,10 @@
12
12
  */
13
13
  import type { RxCollection } from 'rxdb';
14
14
  import { DEFAULT_TAX_ROUNDING, taxLinesByRate } from '../tax/exact';
15
- import { taxedLinesFor } from '../tax/taxed-lines';
15
+ import { storedCharges, taxedLinesFor } from '../tax/taxed-lines';
16
16
  import type { PosOrder } from '../pos-order/types';
17
17
  import { replacedOrderIds } from '../pos-order/reopened';
18
- import { tillPayments } from '../pos-order/till-payments';
18
+ import { countedTillPayments, settledAwayOnStore, tillPayments } from '../pos-order/till-payments';
19
19
  import type { PosRefund } from '../refund/pos-refund';
20
20
  import { readFresh } from '../rxdb';
21
21
  import { parkedCashRoundings, parkedTakenLegs } from '../order/order-drafts';
@@ -573,12 +573,13 @@ export async function writeClosure({
573
573
  const taxRateKey = (code: string | undefined, ratePpm: number) => `${ratePpm}:${(code ?? '').replace(/[%.[\]\\]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`)}`;
574
574
  const taxRates = new Map<string, { ratePpm: number; code?: string; net_minor: number; tax_minor: number }>();
575
575
  const roundings = new Set<string>();
576
- for (const order of bound) {
576
+ const transactions = bound.filter((order) => !settledAwayOnStore(order));
577
+ for (const order of transactions) {
577
578
  // Grouped by the rule applied: a `custom` sale used the default figures, so it joins the default's group.
578
579
  const recorded = order.taxRounding ?? DEFAULT_TAX_ROUNDING;
579
580
  const rounding = recorded.granularity === 'custom' ? DEFAULT_TAX_ROUNDING : recorded;
580
581
  roundings.add(rounding.granularity === 'woocommerce' ? `woocommerce ${rounding.roundAtSubtotal}` : `${rounding.granularity} ${rounding.mode}`);
581
- const taxedLines = taxedLinesFor(order.lines, [...(order.fees ?? []), ...(order.shipping ?? [])], order.pricesIncludeTax, order.taxRounding);
582
+ const taxedLines = taxedLinesFor(order.lines, storedCharges([...(order.fees ?? []), ...(order.shipping ?? [])]), order.pricesIncludeTax, order.taxRounding);
582
583
  for (const { ratePpm, code, netMinor, amountMinor } of taxLinesByRate(taxedLines, order.taxMinor, undefined, order.taxRounding)) {
583
584
  const existing = taxRates.get(taxRateKey(code, ratePpm));
584
585
  taxRates.set(taxRateKey(code, ratePpm), {
@@ -592,7 +593,7 @@ export async function writeClosure({
592
593
  // ADR-094: a bound order's rounding counts as its leg's money does (S3): a till leg, in the session that took it.
593
594
  // A parked or live sale's entries count by the same rule, against that sale's own taken legs (#1281).
594
595
  const cashRoundingMinor = sum(bound.flatMap((order) => {
595
- const legs = new Map(tillPayments(order.payments).map((payment) => [payment.id, payment]));
596
+ const legs = new Map(countedTillPayments(order).map((payment) => [payment.id, payment]));
596
597
  return (order.cashRoundings ?? []).filter((entry) => {
597
598
  const leg = legs.get(entry.paymentId);
598
599
  return leg !== undefined && (leg.sessionId ?? order.sessionId) === session.id;
@@ -654,7 +655,7 @@ export async function writeClosure({
654
655
  movements: entries.map(({ id, type, amountMinor, reason, voids, voided_by, created_at_gmt, created_by }) => ({
655
656
  id, type, amountMinor, reason, voids: voids ?? null, created_at_gmt, created_by: created_by ?? null, voided_by: voided_by ?? null,
656
657
  })),
657
- transaction_count: bound.length,
658
+ transaction_count: transactions.length,
658
659
  refund_count: countedRefunds.length,
659
660
  ...(refunds !== undefined ? {
660
661
  refund_ids: countedRefunds.map((refund) => refund.id),
@@ -3,7 +3,7 @@ import { tillPayments } from '../pos-order/till-payments';
3
3
  import type { PosOrder } from '../pos-order/types';
4
4
  import type { PosRefund } from '../refund/pos-refund';
5
5
  import { roundMicrosToMinor, taxLinesByRate } from '../tax/exact';
6
- import { recordedTaxByRate, taxedLinesFor } from '../tax/taxed-lines';
6
+ import { recordedTaxByRate, storedCharges, taxedLinesFor } from '../tax/taxed-lines';
7
7
  import type { LineJoin, ReportLine, ReportOrder, ReportRefund } from './types';
8
8
 
9
9
  function joinedLine(line: { productId?: string; variantId?: string; quantity: number }, join?: LineJoin): Pick<ReportLine, 'costMinor' | 'category'> {
@@ -19,7 +19,7 @@ function joinedLine(line: { productId?: string; variantId?: string; quantity: nu
19
19
  }
20
20
 
21
21
  export function reportOrderFromPosOrder(order: PosOrder, join?: LineJoin, methodTitle?: (methodId: string) => string | undefined): ReportOrder {
22
- const taxedLines = taxedLinesFor(order.lines, [...(order.fees ?? []), ...(order.shipping ?? [])], order.pricesIncludeTax, order.taxRounding);
22
+ const taxedLines = taxedLinesFor(order.lines, storedCharges([...(order.fees ?? []), ...(order.shipping ?? [])]), order.pricesIncludeTax, order.taxRounding);
23
23
  const taxes = recordedTaxByRate(order) ?? taxLinesByRate(taxedLines, order.taxMinor, undefined, order.taxRounding);
24
24
  const till = tillPayments(order.payments);
25
25
  const amountOf = (payments: readonly { amountMinor: number }[]) => payments.reduce((total, payment) => total + payment.amountMinor, 0);
@@ -1,5 +1,13 @@
1
1
  import type { TaxRounding } from '@tallyui/core';
2
2
 
3
+ /** Undoes `finalize.ts`'s `storedCharge` for taxation: restores the entered mode and, for a generic charge, the entered net. */
4
+ export function storedCharges<C extends { entered?: { amountMinor: number; amountIncludesTax: boolean }; totalMinor?: number; netMinor?: number }>(
5
+ charges: readonly C[],
6
+ ): Array<C & { amountIncludesTax?: boolean }> {
7
+ return charges.map((charge) => charge.entered === undefined ? charge : { ...charge, amountIncludesTax: charge.entered.amountIncludesTax,
8
+ ...(charge.totalMinor === undefined ? { netMinor: charge.entered.amountMinor } : {}) });
9
+ }
10
+
3
11
  /** The lines and charges `taxLinesByRate` splits, each in the tax mode its tax was rounded in (ADR-065: the
4
12
  * receipt's figures, which every per-rate split must equal). Under `woocommerce` rounding a line with `netMicros`
5
13
  * is in the store's mode, since WooCommerce rounds every line that way. Otherwise a line keeps its own mode, and