@managesome/eposmatic-toolkit 0.2.703 → 0.2.705

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.
@@ -38,6 +38,20 @@ interface ICalculateGstForItem {
38
38
  apply_service_fee_on_original_price: boolean | undefined;
39
39
  }
40
40
  export declare function calculateGstForItem({ subtotal, totalOrderDiscount, applyGSTOnOriginalPrice, is_gst_inclusive, gst, totalItemsDiscount, grossAmount, netAmount, payablePrice, originalItemsPrice, surcharge, is_surcharge_taxable, is_surcharge_percentage, service_fee, is_service_fee_taxable, is_service_fee_percentage, apply_service_fee_on_original_price, }: ICalculateGstForItem): number;
41
+ /**
42
+ * Returns the amount of discount that was applied by the cashier/staff on the
43
+ * order (order-level + per-item `special_discount` slots, resolved to actual
44
+ * amounts) — i.e. the portion of the total discount that is subject to the
45
+ * staff role's discount cap.
46
+ *
47
+ * System-granted rewards are excluded:
48
+ * - offer discounts that live on the `loyalty_discount` / `voucher_discount`
49
+ * slots (order-level and per-item),
50
+ * - wallet cashback and gift-card rewards (which also ride the loyalty slot),
51
+ * - any reward-linked special discount (recognized via `redeemed_offers` /
52
+ * `wallet_redemptions` evidence and normalized into the loyalty slot).
53
+ */
54
+ export declare function getCashierAppliedDiscountAmount(order: Partial<IOrder> | null | undefined): number;
41
55
  export interface ICartOfferSyncContext {
42
56
  unlockedOffersApplicableTimes: Record<string, number>;
43
57
  offers: IOffer[];
@@ -13,6 +13,7 @@ exports.calculateItemTotalWithTax = calculateItemTotalWithTax;
13
13
  exports.getServiceFeeAmount = getServiceFeeAmount;
14
14
  exports.getSurchargeAmount = getSurchargeAmount;
15
15
  exports.calculateGstForItem = calculateGstForItem;
16
+ exports.getCashierAppliedDiscountAmount = getCashierAppliedDiscountAmount;
16
17
  exports.calculateCartOrderInfoAndSyncData = calculateCartOrderInfoAndSyncData;
17
18
  exports.getAddItemToCartData = getAddItemToCartData;
18
19
  exports.getSplitCartItemToTwo = getSplitCartItemToTwo;
@@ -690,6 +691,67 @@ function getSyncedItemDiscountDetailsWithoutRewardSpecialDiscount({ itemDiscount
690
691
  walletRedemptions,
691
692
  });
692
693
  }
694
+ /**
695
+ * Keeps only the cashier-applied (special) discount slot of the given
696
+ * discount details. Loyalty and voucher slots are cleared because they
697
+ * represent system-granted rewards (offers, wallet cashback, gift cards)
698
+ * which must not count toward the staff role's discount cap.
699
+ */
700
+ function getCashierSpecialOnlyDiscountDetails(discountDetails) {
701
+ if (discountDetails == null) {
702
+ return null;
703
+ }
704
+ return clearLoyaltyDiscountFields(clearVoucherDiscountFields(discountDetails));
705
+ }
706
+ /**
707
+ * Returns the amount of discount that was applied by the cashier/staff on the
708
+ * order (order-level + per-item `special_discount` slots, resolved to actual
709
+ * amounts) — i.e. the portion of the total discount that is subject to the
710
+ * staff role's discount cap.
711
+ *
712
+ * System-granted rewards are excluded:
713
+ * - offer discounts that live on the `loyalty_discount` / `voucher_discount`
714
+ * slots (order-level and per-item),
715
+ * - wallet cashback and gift-card rewards (which also ride the loyalty slot),
716
+ * - any reward-linked special discount (recognized via `redeemed_offers` /
717
+ * `wallet_redemptions` evidence and normalized into the loyalty slot).
718
+ */
719
+ function getCashierAppliedDiscountAmount(order) {
720
+ if (order == null) {
721
+ return 0;
722
+ }
723
+ const redeemedOffers = order.redeemed_offers ?? null;
724
+ const walletRedemptions = order.wallet_redemptions ?? null;
725
+ // Resolve the order-level cashier (special) discount.
726
+ const syncedOrderDiscountDetails = getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({
727
+ orderDiscountDetails: order.order_discount_details,
728
+ redeemedOffers,
729
+ walletRedemptions,
730
+ });
731
+ const cashierOnlyOrderDiscountDetails = getCashierSpecialOnlyDiscountDetails(syncedOrderDiscountDetails ?? order.order_discount_details);
732
+ const orderDiscountBaseAmount = getOrderSubtotalFromCategoriesForOfferSync(order.categories);
733
+ let totalCashierDiscount = (0, utils_1.parseNum)(getDiscountDetails(cashierOnlyOrderDiscountDetails, orderDiscountBaseAmount)?.total_discount ?? 0);
734
+ // Resolve per-item cashier (special) discounts.
735
+ const categories = order.categories;
736
+ if (categories != null) {
737
+ for (let c = 0; c < categories.length; c++) {
738
+ const items = categories[c].items;
739
+ for (let i = 0; i < items.length; i++) {
740
+ const item = items[i];
741
+ const syncedItemDiscountDetails = getSyncedItemDiscountDetailsWithoutRewardSpecialDiscount({
742
+ itemDiscountDetails: item.item_discount_details,
743
+ redeemedOffers,
744
+ walletRedemptions,
745
+ });
746
+ const cashierOnlyItemDiscountDetails = getCashierSpecialOnlyDiscountDetails(syncedItemDiscountDetails ?? item.item_discount_details);
747
+ const itemDiscountBaseAmount = (Number(item.price ?? 0) + calculateItemModifiersPrice(item)) * Number(item.quantity ?? 0);
748
+ const itemCashierDiscount = getDiscountDetails(cashierOnlyItemDiscountDetails, Math.max(0, itemDiscountBaseAmount))?.total_discount ?? 0;
749
+ totalCashierDiscount = (0, utils_1.parseNum)(totalCashierDiscount + itemCashierDiscount);
750
+ }
751
+ }
752
+ }
753
+ return (0, utils_1.parseNum)(totalCashierDiscount);
754
+ }
693
755
  function isWalletCashbackLoyaltyDiscount(orderDiscountDetails) {
694
756
  return orderDiscountDetails?.loyalty_discount_id === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
695
757
  }
@@ -15753,6 +15753,9 @@ export declare const ORDERS_COLLECTION_NAME = "orders";
15753
15753
  * lookups filtered by is_deleted, newest first.
15754
15754
  * - `tenant_id__is_deleted__consumer.phone__createdAt`: per-tenant order
15755
15755
  * lookups by consumer phone, filtered by is_deleted, newest first.
15756
+ * - `tenant_id__is_deleted__deferred_settlement__consumer.phone__createdAt`:
15757
+ * per-tenant credit-bill (deferred settlement) order lookups by consumer
15758
+ * phone, filtered by is_deleted, newest first.
15756
15759
  */
15757
15760
  export declare const ORDERS_COLLECTION_INDEXES: {
15758
15761
  _id: {
@@ -15781,4 +15784,11 @@ export declare const ORDERS_COLLECTION_INDEXES: {
15781
15784
  'consumer.phone': number;
15782
15785
  createdAt: number;
15783
15786
  };
15787
+ tenant_id__is_deleted__deferred_settlement__consumer_phone__createdAt: {
15788
+ tenant_id: number;
15789
+ is_deleted: number;
15790
+ deferred_settlement: number;
15791
+ 'consumer.phone': number;
15792
+ createdAt: number;
15793
+ };
15784
15794
  };
@@ -1002,6 +1002,9 @@ exports.ORDERS_COLLECTION_NAME = 'orders';
1002
1002
  * lookups filtered by is_deleted, newest first.
1003
1003
  * - `tenant_id__is_deleted__consumer.phone__createdAt`: per-tenant order
1004
1004
  * lookups by consumer phone, filtered by is_deleted, newest first.
1005
+ * - `tenant_id__is_deleted__deferred_settlement__consumer.phone__createdAt`:
1006
+ * per-tenant credit-bill (deferred settlement) order lookups by consumer
1007
+ * phone, filtered by is_deleted, newest first.
1005
1008
  */
1006
1009
  exports.ORDERS_COLLECTION_INDEXES = {
1007
1010
  _id: { _id: 1 }, // Unique Index
@@ -1028,4 +1031,11 @@ exports.ORDERS_COLLECTION_INDEXES = {
1028
1031
  'consumer.phone': 1,
1029
1032
  createdAt: -1,
1030
1033
  }, // per-tenant order lookups by consumer phone, filtered by is_deleted, newest first
1034
+ tenant_id__is_deleted__deferred_settlement__consumer_phone__createdAt: {
1035
+ tenant_id: 1,
1036
+ is_deleted: 1,
1037
+ deferred_settlement: 1,
1038
+ 'consumer.phone': 1,
1039
+ createdAt: -1,
1040
+ }, // per-tenant credit-bill (deferred settlement) order lookups by consumer phone, filtered by is_deleted, newest first
1031
1041
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cart_actions_1 = require("../helpers/store/cart-actions");
4
+ const OFFER_ID = 'offer-loyalty-reward';
5
+ const VOUCHER_ID = 'offer-voucher-reward';
6
+ const GIFT_CARD_ID = 'gift-card-reward';
7
+ function makeDiscountDetails(partial) {
8
+ return {
9
+ total_discount: 0,
10
+ ...partial,
11
+ };
12
+ }
13
+ function makeItem(params) {
14
+ return {
15
+ _id: 'cart-item',
16
+ item_id: 'item-1',
17
+ name: 'Item',
18
+ price: params.price ?? 100,
19
+ quantity: params.quantity ?? 1,
20
+ subtotal: params.subtotal ?? 100,
21
+ items_price: params.price ?? 100,
22
+ modifiers: params.modifiers ?? [],
23
+ item_discount_details: params.discount ?? null,
24
+ };
25
+ }
26
+ function makeOrder(partial) {
27
+ return {
28
+ categories: [],
29
+ order_discount_details: null,
30
+ redeemed_offers: null,
31
+ wallet_redemptions: null,
32
+ ...partial,
33
+ };
34
+ }
35
+ describe('getCashierAppliedDiscountAmount', () => {
36
+ it('returns 0 for an order without any discount', () => {
37
+ const order = makeOrder({
38
+ categories: [{ category_id: 'c1', items: [makeItem({})] }],
39
+ });
40
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(0);
41
+ });
42
+ it('returns the resolved order-level special discount (percentage)', () => {
43
+ const order = makeOrder({
44
+ order_discount_details: makeDiscountDetails({
45
+ special_discount_id: 'manual-discount',
46
+ special_discount: 10,
47
+ is_special_discount_percentage: true,
48
+ }),
49
+ categories: [{ category_id: 'c1', items: [makeItem({ price: 250, quantity: 2, subtotal: 500 })] }],
50
+ });
51
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(50);
52
+ });
53
+ it('returns the order-level special discount in fixed amounts', () => {
54
+ const order = makeOrder({
55
+ order_discount_details: makeDiscountDetails({
56
+ special_discount_id: 'manual-discount',
57
+ special_discount: 35,
58
+ }),
59
+ categories: [{ category_id: 'c1', items: [makeItem({ price: 100, quantity: 1, subtotal: 100 })] }],
60
+ });
61
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(35);
62
+ });
63
+ it('excludes order-level loyalty and voucher reward discounts', () => {
64
+ const order = makeOrder({
65
+ order_discount_details: makeDiscountDetails({
66
+ loyalty_discount_id: OFFER_ID,
67
+ loyalty_discount: 100,
68
+ voucher_discount_id: VOUCHER_ID,
69
+ voucher_discount: 200,
70
+ }),
71
+ redeemed_offers: { [OFFER_ID]: 1, [VOUCHER_ID]: 1 },
72
+ categories: [{ category_id: 'c1', items: [makeItem({ price: 1000, quantity: 1, subtotal: 700 })] }],
73
+ });
74
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(0);
75
+ });
76
+ it('counts only the special portion when rewards and cashier discount coexist', () => {
77
+ const order = makeOrder({
78
+ order_discount_details: makeDiscountDetails({
79
+ special_discount_id: 'manual-discount',
80
+ special_discount: 50,
81
+ loyalty_discount_id: OFFER_ID,
82
+ loyalty_discount: 150,
83
+ }),
84
+ redeemed_offers: { [OFFER_ID]: 1 },
85
+ categories: [{ category_id: 'c1', items: [makeItem({ price: 1000, quantity: 1, subtotal: 800 })] }],
86
+ });
87
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(50);
88
+ });
89
+ it('excludes reward-linked special discounts (offer redemption evidence)', () => {
90
+ const order = makeOrder({
91
+ order_discount_details: makeDiscountDetails({
92
+ special_discount_id: OFFER_ID,
93
+ special_discount: 120,
94
+ }),
95
+ redeemed_offers: { [OFFER_ID]: 1 },
96
+ categories: [{ category_id: 'c1', items: [makeItem({ price: 1000, quantity: 1, subtotal: 880 })] }],
97
+ });
98
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(0);
99
+ });
100
+ it('excludes gift-card reward special discounts', () => {
101
+ const order = makeOrder({
102
+ order_discount_details: makeDiscountDetails({
103
+ special_discount_id: GIFT_CARD_ID,
104
+ special_discount: 120,
105
+ }),
106
+ wallet_redemptions: {
107
+ gift_cards: [{ gift_card_id: GIFT_CARD_ID, redeemed_times: 1 }],
108
+ },
109
+ categories: [{ category_id: 'c1', items: [makeItem({ price: 1000, quantity: 1, subtotal: 880 })] }],
110
+ });
111
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(0);
112
+ });
113
+ it('counts cashier item special discounts and excludes rewarded free items', () => {
114
+ const order = makeOrder({
115
+ categories: [
116
+ {
117
+ category_id: 'c1',
118
+ items: [
119
+ // Cashier applied a 50% item discount on a 200 item.
120
+ makeItem({
121
+ price: 200,
122
+ quantity: 1,
123
+ subtotal: 100,
124
+ discount: makeDiscountDetails({
125
+ special_discount_id: 'manual-item-discount',
126
+ special_discount: 50,
127
+ is_special_discount_percentage: true,
128
+ }),
129
+ }),
130
+ // Free item granted by a loyalty offer (100% reward).
131
+ makeItem({
132
+ price: 150,
133
+ quantity: 1,
134
+ subtotal: 0,
135
+ discount: makeDiscountDetails({
136
+ loyalty_discount_id: OFFER_ID,
137
+ loyalty_discount: 100,
138
+ is_loyalty_discount_percentage: true,
139
+ }),
140
+ }),
141
+ ],
142
+ },
143
+ ],
144
+ redeemed_offers: { [OFFER_ID]: 1 },
145
+ });
146
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(order)).toBe(100);
147
+ });
148
+ it('treats null/undefined order as no cashier discount', () => {
149
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(null)).toBe(0);
150
+ expect((0, cart_actions_1.getCashierAppliedDiscountAmount)(undefined)).toBe(0);
151
+ });
152
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.703",
3
+ "version": "0.2.705",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",