@managesome/eposmatic-toolkit 0.2.284 → 0.2.285

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.
@@ -32,6 +32,7 @@ export interface IOrderEngineItem {
32
32
  quantity: number;
33
33
  image_url: string;
34
34
  special_instructions: string;
35
+ excluded_discount_refs?: string[];
35
36
  item_discount_details: IDiscountDetails | null;
36
37
  modifiers: IOrderEngineModifier[];
37
38
  error_message?: string;
@@ -28,6 +28,7 @@ export interface ICatalogItemOptionsDrawerProps<I extends ISimplifiedCatalogItem
28
28
  orderItemId?: string;
29
29
  defaultOrderItem?: IOrderItem;
30
30
  excludeRefs?: string[];
31
+ excludedDiscountRefs?: string[];
31
32
  zeroPriceRefs?: string[];
32
33
  maxQuantity?: number;
33
34
  applyLoyalty?: IApplyLoyalty;
@@ -52,7 +52,8 @@ function getEditItemProps({ item, orderItemCategoryId, orderItem, offers, isLoya
52
52
  let applyLoyalty;
53
53
  let applyVoucher;
54
54
  const excludeRefs = undefined;
55
- let zeroPriceRefs = undefined;
55
+ let excludedDiscountRefs = undefined;
56
+ const zeroPriceRefs = undefined;
56
57
  // Step 1: Check if loyalty discount or voucher discount is applied
57
58
  if (isLoyaltyDiscountApplied) {
58
59
  const offer = offers.find((o) => o._id === orderItem.item_discount_details?.loyalty_discount_id);
@@ -60,20 +61,20 @@ function getEditItemProps({ item, orderItemCategoryId, orderItem, offers, isLoya
60
61
  return;
61
62
  // const rewardItems = offer?.reward_items;
62
63
  let refs = [];
63
- let offerZeroPriceRefs = [];
64
+ let offerExcludedDiscountRefs = [];
64
65
  let applicable_quantity = 1;
65
66
  if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
66
67
  if (offer.reward_items == null)
67
68
  return;
68
69
  refs = offer.reward_items.exclusive_refs.concat(item._id) ?? [];
69
- offerZeroPriceRefs = offer.reward_items.excluded_refs;
70
+ offerExcludedDiscountRefs = offer.reward_items.excluded_refs;
70
71
  applicable_quantity = offer.reward_items.num_of_items || 1;
71
72
  }
72
73
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
73
74
  if (offer.reward_discount_items == null)
74
75
  return;
75
76
  refs = offer.reward_discount_items.exclusive_refs.concat(item._id) ?? [];
76
- offerZeroPriceRefs = offer.reward_discount_items.excluded_refs;
77
+ offerExcludedDiscountRefs = offer.reward_discount_items.excluded_refs;
77
78
  applicable_quantity = offer.reward_discount_items.num_of_items || 1;
78
79
  }
79
80
  let discount_details = null;
@@ -92,7 +93,7 @@ function getEditItemProps({ item, orderItemCategoryId, orderItem, offers, isLoya
92
93
  discount_details,
93
94
  applicable_quantity,
94
95
  };
95
- zeroPriceRefs = offerZeroPriceRefs;
96
+ excludedDiscountRefs = offerExcludedDiscountRefs;
96
97
  }
97
98
  if (isVoucherDiscountApplied) {
98
99
  const offer = offers.find((o) => o._id === orderItem.item_discount_details?.voucher_discount_id);
@@ -100,20 +101,20 @@ function getEditItemProps({ item, orderItemCategoryId, orderItem, offers, isLoya
100
101
  return;
101
102
  // const rewardItems = offer?.reward_items;
102
103
  let refs = [];
103
- let offerZeroPriceRefs = [];
104
+ let offerExcludedDiscountRefs = [];
104
105
  let applicable_quantity = 1;
105
106
  if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
106
107
  if (offer.reward_items == null)
107
108
  return;
108
109
  refs = offer.reward_items.exclusive_refs.concat(item._id) ?? [];
109
- offerZeroPriceRefs = offer.reward_items.excluded_refs;
110
+ offerExcludedDiscountRefs = offer.reward_items.excluded_refs;
110
111
  applicable_quantity = offer.reward_items.num_of_items || 1;
111
112
  }
112
113
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
113
114
  if (offer.reward_discount_items == null)
114
115
  return;
115
116
  refs = offer.reward_discount_items.exclusive_refs.concat(item._id) ?? [];
116
- offerZeroPriceRefs = offer.reward_discount_items.excluded_refs;
117
+ offerExcludedDiscountRefs = offer.reward_discount_items.excluded_refs;
117
118
  applicable_quantity = offer.reward_discount_items.num_of_items || 1;
118
119
  }
119
120
  let discount_details = null;
@@ -132,7 +133,7 @@ function getEditItemProps({ item, orderItemCategoryId, orderItem, offers, isLoya
132
133
  discount_details,
133
134
  applicable_quantity,
134
135
  };
135
- zeroPriceRefs = offerZeroPriceRefs;
136
+ excludedDiscountRefs = offerExcludedDiscountRefs;
136
137
  }
137
138
  // TODO: We need to add use case for HalfHalf and Specials
138
139
  const props = {
@@ -142,6 +143,7 @@ function getEditItemProps({ item, orderItemCategoryId, orderItem, offers, isLoya
142
143
  applyLoyalty,
143
144
  applyVoucher,
144
145
  excludeRefs,
146
+ excludedDiscountRefs,
145
147
  zeroPriceRefs,
146
148
  };
147
149
  return props;
@@ -33,6 +33,7 @@ interface IConvertCatalogItemToOrderEngineItemParams {
33
33
  tenantId: string;
34
34
  orderItemType: ORDER_ITEM_TYPE_ENUM;
35
35
  excludeRefs?: string[];
36
+ excludedDiscountRefs?: string[];
36
37
  zeroPriceRefs?: string[];
37
38
  applyLoyalty?: IApplyLoyalty;
38
39
  applyVoucher?: IApplyVoucher;
@@ -40,7 +41,7 @@ interface IConvertCatalogItemToOrderEngineItemParams {
40
41
  defaultMenuId: string | null;
41
42
  soldoutModifiers: string[];
42
43
  }
43
- export declare function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenantId, excludeRefs, zeroPriceRefs, orderItemType, applyLoyalty, applyVoucher, keepOptionalModifiersAtBottom, defaultMenuId, soldoutModifiers, }: IConvertCatalogItemToOrderEngineItemParams): IOrderEngineItem;
44
+ export declare function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenantId, excludeRefs, excludedDiscountRefs, zeroPriceRefs, orderItemType, applyLoyalty, applyVoucher, keepOptionalModifiersAtBottom, defaultMenuId, soldoutModifiers, }: IConvertCatalogItemToOrderEngineItemParams): IOrderEngineItem;
44
45
  export declare function hasRequiredModifier<T extends IModifier | IOrderEngineModifier>(currentModifiers: T[] | null): boolean;
45
46
  export declare function convertItemToOrderItem(item: IItem | ISimplifiedCatalogItem, menu_id: string | null, platform: PLATFORMS_ENUM | undefined, tenantId: string, orderItemType?: ORDER_ITEM_TYPE_ENUM): IOrderItem;
46
47
  export declare function setCheckFieldInOrderEngineItem(orderEngineItem: IOrderEngineItem, identifier: string, shouldCheck: boolean): IOrderEngineItem;
@@ -61,6 +61,92 @@ function hasMissedRequiredItemModifier(currentModifiers, platform) {
61
61
  findMissedRequiredModifiers(currentModifiers);
62
62
  return missedModifier !== null;
63
63
  }
64
+ /* ==========================================================><==================================================== */
65
+ function getSelectedModifiersPriceSplit(modifiers, excludedRefSet, inheritedExcluded = false) {
66
+ let eligible = 0;
67
+ let excluded = 0;
68
+ for (let i = 0; i < modifiers.length; i++) {
69
+ const modifier = modifiers[i];
70
+ if (!modifier.default || !modifier.active)
71
+ continue;
72
+ const modifierExcluded = inheritedExcluded || excludedRefSet.has(modifier.ref);
73
+ for (let j = 0; j < modifier.options.length; j++) {
74
+ const option = modifier.options[j];
75
+ if (!option.default || !option.active)
76
+ continue;
77
+ const optionExcluded = modifierExcluded || excludedRefSet.has(option.ref);
78
+ const optionTotal = (option.price ?? 0) * (option.quantity ?? 1);
79
+ if (optionExcluded) {
80
+ excluded += optionTotal;
81
+ }
82
+ else {
83
+ eligible += optionTotal;
84
+ }
85
+ if (option.modifiers?.length > 0) {
86
+ const nested = getSelectedModifiersPriceSplit(option.modifiers, excludedRefSet, optionExcluded);
87
+ eligible += nested.eligible;
88
+ excluded += nested.excluded;
89
+ }
90
+ }
91
+ }
92
+ return {
93
+ eligible: (0, common_1.parseNum)(eligible),
94
+ excluded: (0, common_1.parseNum)(excluded),
95
+ };
96
+ }
97
+ function getEligibleDiscountBaseForItem(item, fullTotal) {
98
+ const excludedDiscountRefs = item.excluded_discount_refs ?? [];
99
+ if (excludedDiscountRefs.length === 0)
100
+ return fullTotal;
101
+ const excludedRefSet = new Set(excludedDiscountRefs);
102
+ const itemExcluded = excludedRefSet.has(item.item_id);
103
+ const { eligible: eligibleModifiersTotal, excluded: excludedModifiersTotal } = getSelectedModifiersPriceSplit(item.modifiers, excludedRefSet);
104
+ if (!itemExcluded && excludedModifiersTotal === 0)
105
+ return fullTotal;
106
+ const eligibleItemBase = itemExcluded ? 0 : item.price;
107
+ return (0, common_1.parseNum)((eligibleItemBase + eligibleModifiersTotal) * item.quantity);
108
+ }
109
+ /**
110
+ * Resolve percentage discounts to fixed amounts for excluded-ref items.
111
+ * Used when converting to IOrderItem so cart-actions (which doesn't know about
112
+ * excluded refs) can recalculate correctly.
113
+ */
114
+ function resolveDiscountDetailsForExcludedRefs(orderItem) {
115
+ const details = orderItem.item_discount_details;
116
+ if (!details)
117
+ return null;
118
+ if (!orderItem.excluded_discount_refs?.length)
119
+ return details;
120
+ const excludedRefSet = new Set(orderItem.excluded_discount_refs);
121
+ const itemExcluded = excludedRefSet.has(orderItem.item_id);
122
+ const { eligible: eligibleModifiersTotal, excluded: excludedModifiersTotal } = getSelectedModifiersPriceSplit(orderItem.modifiers, excludedRefSet);
123
+ if (!itemExcluded && excludedModifiersTotal === 0)
124
+ return details;
125
+ const eligibleItemBase = itemExcluded ? 0 : orderItem.price;
126
+ const eligibleSubtotal = (0, common_1.parseNum)((eligibleItemBase + eligibleModifiersTotal) * orderItem.quantity);
127
+ const resolved = { ...details };
128
+ if (resolved.loyalty_discount_id != null && resolved.loyalty_discount != null) {
129
+ if (resolved.is_loyalty_discount_percentage) {
130
+ resolved.loyalty_discount =
131
+ eligibleSubtotal > 0 ? (0, common_1.parseNum)((0, common_1.getPercentageAmount)(eligibleSubtotal, resolved.loyalty_discount)) : 0;
132
+ resolved.is_loyalty_discount_percentage = false;
133
+ }
134
+ else {
135
+ resolved.loyalty_discount = (0, common_1.parseNum)(Math.min(resolved.loyalty_discount, eligibleSubtotal));
136
+ }
137
+ }
138
+ if (resolved.voucher_discount_id != null && resolved.voucher_discount != null) {
139
+ if (resolved.is_voucher_discount_percentage) {
140
+ resolved.voucher_discount =
141
+ eligibleSubtotal > 0 ? (0, common_1.parseNum)((0, common_1.getPercentageAmount)(eligibleSubtotal, resolved.voucher_discount)) : 0;
142
+ resolved.is_voucher_discount_percentage = false;
143
+ }
144
+ else {
145
+ resolved.voucher_discount = (0, common_1.parseNum)(Math.min(resolved.voucher_discount, eligibleSubtotal));
146
+ }
147
+ }
148
+ return resolved;
149
+ }
64
150
  function syncOrderEngineItem(orderEngineItem) {
65
151
  // Calculate modifiers total and find errors
66
152
  let error = null;
@@ -145,7 +231,10 @@ function syncOrderEngineItem(orderEngineItem) {
145
231
  // Item discount details
146
232
  // discount is 50%
147
233
  // 50% of 20 is 10
148
- const item_discount_details = (0, store_1.getDiscountDetails)(orderEngineItem.item_discount_details, itemTotalWithoutTaxAndWithoutDiscount);
234
+ // When excluded_discount_refs exist, discount is computed against the eligible subtotal only.
235
+ // Original percentage flags are preserved so re-syncs (option toggles, qty changes) stay correct.
236
+ const discountBase = getEligibleDiscountBaseForItem(orderEngineItem, itemTotalWithoutTaxAndWithoutDiscount);
237
+ const item_discount_details = (0, store_1.getDiscountDetails)(orderEngineItem.item_discount_details, discountBase);
149
238
  // Item total without tax amount and with discount amount
150
239
  // 20 - 10 = 10
151
240
  // 10 is the item total without tax amount and with discount amount
@@ -349,6 +438,9 @@ function convertOrderEngineModifiersToOrderItemModifiers(modifiers, tenantId) {
349
438
  return newModifiers;
350
439
  }
351
440
  function convertOrderEngineItemToOrderItem(orderItem, tenantId) {
441
+ // When excluded_discount_refs exist, resolve percentage discounts to fixed amounts
442
+ // so that cart-actions (which doesn't know about exclusions) recalculates correctly.
443
+ const item_discount_details = resolveDiscountDetailsForExcludedRefs(orderItem);
352
444
  return {
353
445
  _id: orderItem._id,
354
446
  item_id: orderItem.item_id,
@@ -359,7 +451,7 @@ function convertOrderEngineItemToOrderItem(orderItem, tenantId) {
359
451
  name: orderItem.name,
360
452
  price: orderItem.price,
361
453
  subtotal: orderItem.price,
362
- item_discount_details: orderItem.item_discount_details,
454
+ item_discount_details,
363
455
  quantity: orderItem.quantity,
364
456
  tax_rate: orderItem.tax_rate,
365
457
  special_instructions: orderItem.special_instructions,
@@ -392,7 +484,7 @@ function convertItemDiscountToOrderItemDiscount(discount_amount, is_special_disc
392
484
  }
393
485
  return defaultItemDiscountDetails;
394
486
  }
395
- function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenantId, excludeRefs, zeroPriceRefs, orderItemType, applyLoyalty, applyVoucher, keepOptionalModifiersAtBottom, defaultMenuId, soldoutModifiers, }) {
487
+ function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenantId, excludeRefs, excludedDiscountRefs, zeroPriceRefs, orderItemType, applyLoyalty, applyVoucher, keepOptionalModifiersAtBottom, defaultMenuId, soldoutModifiers, }) {
396
488
  // Default values will be used in case of new order
397
489
  // we need to find the applicable schedule for the item
398
490
  const applicableSchedule = (0, platform_catalog_1.findApplicableSchedule)(item.item_special_hours, platform);
@@ -480,6 +572,7 @@ function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenant
480
572
  loyalty_applied: loyaltyApplied,
481
573
  voucher_applied: voucherApplied,
482
574
  special_instructions,
575
+ excluded_discount_refs: excludedDiscountRefs,
483
576
  item_discount_details: mergedItemDiscountDetails,
484
577
  modifiers: orderEngineModifiers,
485
578
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.284",
3
+ "version": "0.2.285",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",