@tallyui/pos 3.9.2 → 3.11.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 +290 -139
- package/dist/index.js +714 -265
- package/package.json +6 -6
- package/src/index.ts +6 -5
- package/src/order/order-builder.ts +28 -10
- package/src/order/order-drafts.ts +4 -3
- package/src/order/types.ts +8 -0
- package/src/order/woocommerce-tax.ts +1 -1
- package/src/outbox/index.ts +1 -1
- package/src/outbox/order-outbox.ts +14 -6
- package/src/pos-order/__fixtures__/order-create-v6.json +96 -0
- package/src/pos-order/command.ts +19 -7
- package/src/pos-order/finalize.ts +7 -0
- package/src/pos-order/index.ts +3 -1
- package/src/pos-order/needs-attention.ts +4 -0
- package/src/pos-order/reopen.ts +95 -0
- package/src/pos-order/reopened.ts +12 -0
- package/src/pos-order/schema.ts +18 -4
- package/src/pos-order/types.ts +18 -2
- package/src/receipt/build-receipt-data.ts +1 -0
- package/src/receipt/types.ts +2 -0
- package/src/register/session-store.ts +4 -2
- package/src/register/use-register-session.ts +6 -4
- package/src/sale/cart.ts +6 -2
- package/src/sale/discount-codes.ts +34 -0
- package/src/sale/index.ts +3 -0
- package/src/sale/use-sale.ts +78 -2
- package/src/sale/woocommerce/coupon-refetch.ts +40 -0
- package/src/sale/woocommerce/coupon-source.ts +55 -0
- package/src/sale/woocommerce/coupons.ts +41 -0
- package/src/sale/woocommerce/discount-codes.ts +40 -0
- package/src/sale/woocommerce/index.ts +5 -0
- package/src/tax/index.ts +1 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { minorUnitDigits, woocommerceCoupons } from '@tallyui/core';
|
|
2
|
+
import type { LineItem } from '../../order';
|
|
3
|
+
|
|
4
|
+
/** A WooCommerce coupon as the sale needs it: the engine's input and the connector's numeric id. */
|
|
5
|
+
export type SaleCoupon = woocommerceCoupons.CouponInput & { id: number };
|
|
6
|
+
/** Where the sale finds coupons (ADR-077 amendment 2); the app builds it over the till's coupons collection. */
|
|
7
|
+
export interface SaleCouponSource {
|
|
8
|
+
/** The published coupon with this lower-case code, its usage already counting the till's own unsent orders; null when none. */
|
|
9
|
+
find(code: string): Promise<SaleCoupon | null>;
|
|
10
|
+
/** The WooCommerce category ids of each product asked for; a product it does not know is left out. */
|
|
11
|
+
productCategories(productIds: readonly number[]): Promise<ReadonlyMap<number, readonly { id: number }[]>>;
|
|
12
|
+
}
|
|
13
|
+
/** The cashier-facing reason for a validator refusal. */
|
|
14
|
+
export function couponRefusal(code: string, rejection: woocommerceCoupons.CouponRejection): string {
|
|
15
|
+
switch (rejection.code) {
|
|
16
|
+
case 'already_applied': return `Coupon ${code} is already applied`;
|
|
17
|
+
case 'expired': return `Coupon ${code} has expired`;
|
|
18
|
+
case 'usage_limit_reached': return `Coupon ${code} has reached its usage limit`;
|
|
19
|
+
case 'usage_limit_reached_for_customer': return `This customer has used coupon ${code} as often as it allows`;
|
|
20
|
+
case 'minimum_spend_not_met': return `Coupon ${code} needs a spend of at least ${rejection.params?.amount}`;
|
|
21
|
+
case 'maximum_spend_exceeded': return `Coupon ${code} allows a spend of at most ${rejection.params?.amount}`;
|
|
22
|
+
case 'individual_use': return `Coupon ${code} cannot be used with other coupons`;
|
|
23
|
+
case 'individual_use_conflict': return `Coupon ${rejection.params?.code} cannot be used with other coupons`;
|
|
24
|
+
case 'email_required': return `Coupon ${code} needs a customer with an email address`;
|
|
25
|
+
case 'email_not_allowed': return `This customer's email address cannot use coupon ${code}`;
|
|
26
|
+
case 'not_applicable_to_cart': return `Coupon ${code} does not apply to anything in this sale`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** The sale's product lines in the validator's shape (major units); custom lines count as product 0. */
|
|
30
|
+
export function couponLineItems(lines: readonly LineItem[], currency: string,
|
|
31
|
+
categories: ReadonlyMap<number, readonly { id: number }[]>): woocommerceCoupons.CouponValidationContext['lineItems'] {
|
|
32
|
+
const digits = minorUnitDigits(currency), factor = 10 ** digits;
|
|
33
|
+
return lines.flatMap((line, lineIndex) => {
|
|
34
|
+
if (line.unitPriceMinor < 0) return [];
|
|
35
|
+
const id = Number(line.productId), product_id = !line.custom && Number.isSafeInteger(id) && id >= 0 ? id : 0;
|
|
36
|
+
return [{ product_id, quantity: line.quantity, price: line.unitPriceMinor / factor,
|
|
37
|
+
subtotal: (line.netMinor / factor).toFixed(digits), total: (line.netMinor / factor).toFixed(digits),
|
|
38
|
+
categories: [...(categories.get(product_id) ?? [])], lineIndex,
|
|
39
|
+
on_sale: line.regularUnitPriceMinor !== undefined && line.unitPriceMinor < line.regularUnitPriceMinor }];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { minorUnitDigits, woocommerceCoupons } from '@tallyui/core';
|
|
2
|
+
import type { SaleDiscountCodes } from '../discount-codes';
|
|
3
|
+
import { couponLineItems, couponRefusal, type SaleCoupon, type SaleCouponSource } from './coupons';
|
|
4
|
+
|
|
5
|
+
function wooCustomerId(customer: { id?: string } | null | undefined): number | null {
|
|
6
|
+
const id = Number(customer?.id);
|
|
7
|
+
return Number.isSafeInteger(id) && id > 0 ? id : null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** WooCommerce coupons as the sale's discount codes (ADR-077): the source's coupons, checked by woocommerceCoupons.validateCoupon. */
|
|
11
|
+
export function createDiscountCodes(source: SaleCouponSource): SaleDiscountCodes<SaleCoupon> {
|
|
12
|
+
return {
|
|
13
|
+
supports: ({ rounding }) => rounding?.granularity === 'woocommerce',
|
|
14
|
+
async find(code, sale) {
|
|
15
|
+
if (sale.accepted.has(code)) return { refusal: couponRefusal(code, { code: 'already_applied' }) };
|
|
16
|
+
const coupon = await source.find(code);
|
|
17
|
+
return coupon === null ? { refusal: `Coupon ${code} does not exist` } : { found: coupon };
|
|
18
|
+
},
|
|
19
|
+
check(code, coupon, sale) {
|
|
20
|
+
const snapshot = sale.order;
|
|
21
|
+
const validation = woocommerceCoupons.validateCoupon(coupon, {
|
|
22
|
+
lineItems: couponLineItems(snapshot.lineItems, snapshot.currency, sale.categories), appliedCoupons: [...sale.accepted.keys()],
|
|
23
|
+
appliedCouponsWithIndividualUse: [...sale.accepted].filter(([, value]) => value.individual_use).map(([key]) => key),
|
|
24
|
+
cartSubtotal: snapshot.display.subtotalMinor / 10 ** minorUnitDigits(snapshot.currency),
|
|
25
|
+
customerEmail: snapshot.customer?.email ?? '', customerId: wooCustomerId(snapshot.customer), now: Date.now(),
|
|
26
|
+
});
|
|
27
|
+
return validation.valid ? null : couponRefusal(code, validation.rejection);
|
|
28
|
+
},
|
|
29
|
+
apply(builder, sale) {
|
|
30
|
+
const context = { configs: woocommerceCoupons.toCouponConfigs([...sale.accepted.keys()], sale.accepted),
|
|
31
|
+
couponIds: new Map([...sale.accepted].map(([code, coupon]) => [code, String(coupon.id)])),
|
|
32
|
+
productCategories: sale.categories, calcDiscountsSequentially: sale.settings.calcDiscountsSequentially ?? false };
|
|
33
|
+
builder.setCoupons([...sale.accepted.keys()], context);
|
|
34
|
+
},
|
|
35
|
+
async beforeResume(sale) {
|
|
36
|
+
const ids = sale.order.lineItems.filter((line) => !line.custom).map((line) => Number(line.productId)).filter((id) => Number.isSafeInteger(id) && id >= 0);
|
|
37
|
+
for (const [id, values] of await source.productCategories(ids)) if (values.length) sale.categories.set(id, [...values]);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createDiscountCodes } from './discount-codes';
|
|
2
|
+
export { couponRefusal } from './coupons';
|
|
3
|
+
export type { SaleCoupon, SaleCouponSource } from './coupons';
|
|
4
|
+
export { createSaleCouponSource } from './coupon-source';
|
|
5
|
+
export { startCouponUsageRefetch } from './coupon-refetch';
|
package/src/tax/index.ts
CHANGED
|
@@ -2,4 +2,4 @@ export { MICROS_PER_MINOR, ratePpmFromPercent, taxMicros, roundMicrosToMinor, co
|
|
|
2
2
|
export type { TaxLineInput, OrderTaxTotals, RateTaxLine } from './exact';
|
|
3
3
|
export { TaxProvider, useTax, taxLogger } from './tax-provider';
|
|
4
4
|
export type { TaxProviderProps } from './tax-provider';
|
|
5
|
-
export type { TaxRateMap, TaxContext
|
|
5
|
+
export type { TaxRateMap, TaxContext } from './types';
|