@managesome/eposmatic-toolkit 0.4.37 → 0.4.39

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.
Files changed (57) hide show
  1. package/esm/constants/accounting-templates.js +431 -0
  2. package/esm/constants/index.js +1 -0
  3. package/esm/helpers/store/cart-actions.js +54 -6
  4. package/esm/helpers/utils/cart-helpers.js +19 -5
  5. package/esm/helpers/utils/common.js +18 -1
  6. package/esm/helpers/utils/offer-apply-helpers.js +5 -3
  7. package/esm/helpers/utils/offers-helpers.js +92 -63
  8. package/lib/constants/accounting-templates.d.ts +8 -0
  9. package/lib/constants/accounting-templates.js +434 -0
  10. package/lib/constants/index.d.ts +1 -0
  11. package/lib/constants/index.js +1 -0
  12. package/lib/helpers/store/cart-actions.js +54 -6
  13. package/lib/helpers/utils/cart-helpers.js +19 -5
  14. package/lib/helpers/utils/common.d.ts +15 -0
  15. package/lib/helpers/utils/common.js +19 -1
  16. package/lib/helpers/utils/offer-apply-helpers.js +5 -3
  17. package/lib/helpers/utils/offers-helpers.d.ts +15 -4
  18. package/lib/helpers/utils/offers-helpers.js +92 -62
  19. package/lib/schemas/models/expense_categories.d.ts +3 -3
  20. package/lib/schemas/models/expense_details.d.ts +3 -3
  21. package/lib/schemas/models/gift_cards.d.ts +3 -3
  22. package/lib/schemas/models/halfhalf_setup.d.ts +3 -3
  23. package/lib/schemas/models/internal_invoices.d.ts +3 -3
  24. package/lib/schemas/models/items.d.ts +12 -12
  25. package/lib/schemas/models/marketing_fingerprints.d.ts +3 -3
  26. package/lib/schemas/models/modifier_extras.d.ts +3 -3
  27. package/lib/schemas/models/offers.d.ts +3 -3
  28. package/lib/schemas/models/public_holidays.d.ts +3 -3
  29. package/lib/schemas/models/specials.d.ts +3 -3
  30. package/lib/schemas/models/tenant_chart_of_accounts.d.ts +3 -3
  31. package/lib/schemas/models/tenant_hr_departments.d.ts +3 -3
  32. package/lib/schemas/models/tenant_hr_documents.d.ts +3 -3
  33. package/lib/schemas/models/tenant_hr_policies.d.ts +3 -3
  34. package/lib/schemas/models/tenant_hr_positions.d.ts +3 -3
  35. package/lib/schemas/models/tenant_journal_entries.d.ts +3 -3
  36. package/lib/schemas/models/void_order_items.d.ts +3 -3
  37. package/lib/schemas/models/vouchers.d.ts +3 -3
  38. package/lib/schemas/models/warehouse_communications.d.ts +3 -3
  39. package/lib/schemas/models/warehouse_items.d.ts +3 -3
  40. package/lib/schemas/models/warehouse_void_purchase_order_items.d.ts +3 -3
  41. package/lib/schemas/models/warehouse_void_sale_order_items.d.ts +3 -3
  42. package/lib/schemas/types/tenant_chart_of_accounts.d.ts +7 -0
  43. package/lib/tests/cart-helpers-edit-item-refs.test.d.ts +1 -0
  44. package/lib/tests/cart-helpers-edit-item-refs.test.js +99 -0
  45. package/lib/tests/code-redemption-helpers.test.d.ts +1 -0
  46. package/lib/tests/code-redemption-helpers.test.js +246 -0
  47. package/lib/tests/gift-card-helpers.test.d.ts +1 -0
  48. package/lib/tests/gift-card-helpers.test.js +100 -0
  49. package/lib/tests/offer-claim-regressions.test.d.ts +1 -0
  50. package/lib/tests/offer-claim-regressions.test.js +367 -0
  51. package/lib/tests/offers-helpers.test.js +24 -4
  52. package/lib/tests/offers-matrix.test.js +248 -20
  53. package/lib/tests/ref-exclusion-matching.test.d.ts +1 -0
  54. package/lib/tests/ref-exclusion-matching.test.js +168 -0
  55. package/lib/tests/wallet-cashback-helpers.test.d.ts +1 -0
  56. package/lib/tests/wallet-cashback-helpers.test.js +108 -0
  57. package/package.json +1 -1
@@ -0,0 +1,431 @@
1
+ import { ACCOUNT_TYPE_ENUM } from './enums/accounting';
2
+ /**
3
+ * Default restaurant chart of accounts (Pakistan-first, PKR) — the seed used by
4
+ * `POST /tenant-chart-of-accounts/seed-template` and the admin panel's
5
+ * "Load Restaurant Template" action. Entries are flat posting accounts; tenants
6
+ * nest their own sub-accounts afterwards, and seeding never overwrites existing codes.
7
+ */
8
+ export const RESTAURANT_ACCOUNTS_TEMPLATE = [
9
+ // ===================== Assets =====================
10
+ {
11
+ code: '1001',
12
+ name: 'Cash on Hand',
13
+ type: ACCOUNT_TYPE_ENUM.ASSET,
14
+ description: 'Physical cash held in cash drawers.',
15
+ },
16
+ {
17
+ code: '1011',
18
+ name: 'Bank – Main Operating Account',
19
+ type: ACCOUNT_TYPE_ENUM.ASSET,
20
+ },
21
+ {
22
+ code: '1012',
23
+ name: 'Bank – Payouts / Settlement Account',
24
+ type: ACCOUNT_TYPE_ENUM.ASSET,
25
+ },
26
+ {
27
+ code: '1021',
28
+ name: 'Card Settlement Clearing',
29
+ type: ACCOUNT_TYPE_ENUM.ASSET,
30
+ },
31
+ {
32
+ code: '1022',
33
+ name: 'JazzCash Clearing',
34
+ type: ACCOUNT_TYPE_ENUM.ASSET,
35
+ },
36
+ {
37
+ code: '1023',
38
+ name: 'EasyPaisa Clearing',
39
+ type: ACCOUNT_TYPE_ENUM.ASSET,
40
+ },
41
+ {
42
+ code: '1024',
43
+ name: 'Raast Clearing',
44
+ type: ACCOUNT_TYPE_ENUM.ASSET,
45
+ },
46
+ {
47
+ code: '1025',
48
+ name: 'Terminal Devices Clearing',
49
+ type: ACCOUNT_TYPE_ENUM.ASSET,
50
+ },
51
+ {
52
+ code: '1031',
53
+ name: 'Foodpanda Receivable',
54
+ type: ACCOUNT_TYPE_ENUM.ASSET,
55
+ },
56
+ {
57
+ code: '1032',
58
+ name: 'Own App / Web Receivable',
59
+ type: ACCOUNT_TYPE_ENUM.ASSET,
60
+ },
61
+ {
62
+ code: '1040',
63
+ name: 'Accounts Receivable – Customers',
64
+ type: ACCOUNT_TYPE_ENUM.ASSET,
65
+ description: 'Credit bills / pay-later orders and B2B clients.',
66
+ },
67
+ {
68
+ code: '1051',
69
+ name: 'Inventory – Raw Materials',
70
+ type: ACCOUNT_TYPE_ENUM.ASSET,
71
+ },
72
+ {
73
+ code: '1052',
74
+ name: 'Inventory – Packed / Retail Items',
75
+ type: ACCOUNT_TYPE_ENUM.ASSET,
76
+ },
77
+ {
78
+ code: '1053',
79
+ name: 'Inventory – Packaging & Consumables',
80
+ type: ACCOUNT_TYPE_ENUM.ASSET,
81
+ },
82
+ {
83
+ code: '1060',
84
+ name: 'Input Tax Receivable (GST)',
85
+ type: ACCOUNT_TYPE_ENUM.ASSET,
86
+ description: 'GST paid on purchases, claimable as credit.',
87
+ },
88
+ {
89
+ code: '1070',
90
+ name: 'Prepaid Expenses',
91
+ type: ACCOUNT_TYPE_ENUM.ASSET,
92
+ },
93
+ {
94
+ code: '1081',
95
+ name: 'Kitchen Equipment',
96
+ type: ACCOUNT_TYPE_ENUM.ASSET,
97
+ },
98
+ {
99
+ code: '1082',
100
+ name: 'Furniture & Fittings',
101
+ type: ACCOUNT_TYPE_ENUM.ASSET,
102
+ },
103
+ {
104
+ code: '1083',
105
+ name: 'Computers & POS Hardware',
106
+ type: ACCOUNT_TYPE_ENUM.ASSET,
107
+ },
108
+ {
109
+ code: '1085',
110
+ name: 'Accumulated Depreciation',
111
+ type: ACCOUNT_TYPE_ENUM.ASSET,
112
+ description: 'Depreciation charged to date (a contra-asset account).',
113
+ },
114
+ {
115
+ code: '1090',
116
+ name: 'Security Deposits',
117
+ type: ACCOUNT_TYPE_ENUM.ASSET,
118
+ },
119
+ // ===================== Liabilities =====================
120
+ {
121
+ code: '2001',
122
+ name: 'Accounts Payable – Vendors',
123
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
124
+ },
125
+ {
126
+ code: '2011',
127
+ name: 'Accrued Salaries & Wages',
128
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
129
+ },
130
+ {
131
+ code: '2012',
132
+ name: 'Accrued Utilities',
133
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
134
+ },
135
+ {
136
+ code: '2021',
137
+ name: 'Net Wages Payable',
138
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
139
+ },
140
+ {
141
+ code: '2022',
142
+ name: 'Salary Tax Withheld Payable',
143
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
144
+ },
145
+ {
146
+ code: '2023',
147
+ name: 'EOBI / Social Security Payable',
148
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
149
+ },
150
+ {
151
+ code: '2030',
152
+ name: 'Tips Payable',
153
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
154
+ description: 'Tips collected but not yet distributed to staff.',
155
+ },
156
+ {
157
+ code: '2041',
158
+ name: 'GST Payable – Card Sales',
159
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
160
+ },
161
+ {
162
+ code: '2042',
163
+ name: 'GST Payable – Cash Sales',
164
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
165
+ },
166
+ {
167
+ code: '2043',
168
+ name: 'Provincial Services Tax Payable',
169
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
170
+ description: 'PRA / SRB / KPRA services tax collected.',
171
+ },
172
+ {
173
+ code: '2044',
174
+ name: 'FBR POS Invoice Tax Payable',
175
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
176
+ },
177
+ {
178
+ code: '2050',
179
+ name: 'Gift Card & Voucher Liability',
180
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
181
+ description: 'Prepaid gift cards / vouchers not yet redeemed.',
182
+ },
183
+ {
184
+ code: '2060',
185
+ name: 'Customer Advances / Deposits',
186
+ type: ACCOUNT_TYPE_ENUM.LIABILITY,
187
+ },
188
+ // ===================== Equity =====================
189
+ {
190
+ code: '3001',
191
+ name: "Owner's Capital",
192
+ type: ACCOUNT_TYPE_ENUM.EQUITY,
193
+ },
194
+ {
195
+ code: '3002',
196
+ name: "Owner's Drawings",
197
+ type: ACCOUNT_TYPE_ENUM.EQUITY,
198
+ },
199
+ {
200
+ code: '3003',
201
+ name: 'Retained Earnings',
202
+ type: ACCOUNT_TYPE_ENUM.EQUITY,
203
+ },
204
+ {
205
+ code: '3099',
206
+ name: 'Opening Balance Equity',
207
+ type: ACCOUNT_TYPE_ENUM.EQUITY,
208
+ description: 'Balancing account used by opening balances entry.',
209
+ },
210
+ // ===================== Revenue =====================
211
+ {
212
+ code: '4001',
213
+ name: 'Food Sales',
214
+ type: ACCOUNT_TYPE_ENUM.INCOME,
215
+ },
216
+ {
217
+ code: '4002',
218
+ name: 'Beverage Sales',
219
+ type: ACCOUNT_TYPE_ENUM.INCOME,
220
+ },
221
+ {
222
+ code: '4003',
223
+ name: 'Delivery Sales – Own',
224
+ type: ACCOUNT_TYPE_ENUM.INCOME,
225
+ },
226
+ {
227
+ code: '4004',
228
+ name: 'Delivery Sales – Platforms',
229
+ type: ACCOUNT_TYPE_ENUM.INCOME,
230
+ },
231
+ {
232
+ code: '4005',
233
+ name: 'Catering & Events Sales',
234
+ type: ACCOUNT_TYPE_ENUM.INCOME,
235
+ },
236
+ {
237
+ code: '4010',
238
+ name: 'Service Fee Income',
239
+ type: ACCOUNT_TYPE_ENUM.INCOME,
240
+ },
241
+ {
242
+ code: '4011',
243
+ name: 'Delivery & Rider Income',
244
+ type: ACCOUNT_TYPE_ENUM.INCOME,
245
+ },
246
+ {
247
+ code: '4012',
248
+ name: 'Surcharge Income',
249
+ type: ACCOUNT_TYPE_ENUM.INCOME,
250
+ },
251
+ {
252
+ code: '4090',
253
+ name: 'Sales Discounts',
254
+ type: ACCOUNT_TYPE_ENUM.INCOME,
255
+ description: 'Discounts given — a contra-revenue account.',
256
+ },
257
+ {
258
+ code: '4091',
259
+ name: 'Complimentary / Comps',
260
+ type: ACCOUNT_TYPE_ENUM.INCOME,
261
+ description: 'Complimentary meals — a contra-revenue account.',
262
+ },
263
+ {
264
+ code: '4092',
265
+ name: 'Sales Returns & Refunds',
266
+ type: ACCOUNT_TYPE_ENUM.INCOME,
267
+ description: 'Refunds and returns — a contra-revenue account.',
268
+ },
269
+ // ===================== Cost of Sales =====================
270
+ {
271
+ code: '5001',
272
+ name: 'Food Cost',
273
+ type: ACCOUNT_TYPE_ENUM.COGS,
274
+ },
275
+ {
276
+ code: '5002',
277
+ name: 'Beverage Cost',
278
+ type: ACCOUNT_TYPE_ENUM.COGS,
279
+ },
280
+ {
281
+ code: '5003',
282
+ name: 'Packaging Cost',
283
+ type: ACCOUNT_TYPE_ENUM.COGS,
284
+ },
285
+ {
286
+ code: '5010',
287
+ name: 'Inventory Wastage & Spoilage',
288
+ type: ACCOUNT_TYPE_ENUM.COGS,
289
+ },
290
+ {
291
+ code: '5011',
292
+ name: 'Inventory Shrinkage',
293
+ type: ACCOUNT_TYPE_ENUM.COGS,
294
+ },
295
+ {
296
+ code: '5012',
297
+ name: 'Inventory Adjustment Gain / Loss',
298
+ type: ACCOUNT_TYPE_ENUM.COGS,
299
+ },
300
+ // ===================== Expenses =====================
301
+ {
302
+ code: '6001',
303
+ name: 'Salaries & Wages',
304
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
305
+ },
306
+ {
307
+ code: '6002',
308
+ name: 'Overtime',
309
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
310
+ },
311
+ {
312
+ code: '6003',
313
+ name: 'Staff Meals & Amenities',
314
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
315
+ },
316
+ {
317
+ code: '6010',
318
+ name: 'Rent & Rentals',
319
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
320
+ },
321
+ {
322
+ code: '6020',
323
+ name: 'Utilities',
324
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
325
+ description: 'Electricity, gas, water, internet.',
326
+ },
327
+ {
328
+ code: '6031',
329
+ name: 'Platform Commissions',
330
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
331
+ description: 'Commissions deducted by delivery platforms.',
332
+ },
333
+ {
334
+ code: '6032',
335
+ name: 'Ads & Social Media',
336
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
337
+ },
338
+ {
339
+ code: '6033',
340
+ name: 'Promotions & Campaigns',
341
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
342
+ },
343
+ {
344
+ code: '6034',
345
+ name: 'Loyalty & Rewards Cost',
346
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
347
+ },
348
+ {
349
+ code: '6041',
350
+ name: 'Rider Payouts',
351
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
352
+ },
353
+ {
354
+ code: '6042',
355
+ name: 'Fuel & Vehicle',
356
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
357
+ },
358
+ {
359
+ code: '6050',
360
+ name: 'Repairs & Maintenance',
361
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
362
+ },
363
+ {
364
+ code: '6060',
365
+ name: 'Cleaning & Housekeeping Supplies',
366
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
367
+ },
368
+ {
369
+ code: '6070',
370
+ name: 'Licenses, Permits & Government Fees',
371
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
372
+ },
373
+ {
374
+ code: '6080',
375
+ name: 'Insurance',
376
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
377
+ },
378
+ {
379
+ code: '6091',
380
+ name: 'Payment Processing Fees',
381
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
382
+ },
383
+ {
384
+ code: '6092',
385
+ name: 'Bank Charges',
386
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
387
+ },
388
+ {
389
+ code: '6100',
390
+ name: 'Software & Subscriptions',
391
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
392
+ },
393
+ {
394
+ code: '6110',
395
+ name: 'Professional Fees',
396
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
397
+ description: 'Accounting, legal and consultancy fees.',
398
+ },
399
+ {
400
+ code: '6120',
401
+ name: 'Training & Recruitment',
402
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
403
+ },
404
+ {
405
+ code: '6130',
406
+ name: 'Office & Miscellaneous Expenses',
407
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
408
+ },
409
+ {
410
+ code: '6140',
411
+ name: 'Depreciation Expense',
412
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
413
+ },
414
+ // ================ Other Income & Expenses ================
415
+ {
416
+ code: '7001',
417
+ name: 'Cash Over / Short',
418
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
419
+ description: 'Day-close cash drawer variances.',
420
+ },
421
+ {
422
+ code: '7002',
423
+ name: 'Interest Expense',
424
+ type: ACCOUNT_TYPE_ENUM.EXPENSE,
425
+ },
426
+ {
427
+ code: '7003',
428
+ name: 'Other Income',
429
+ type: ACCOUNT_TYPE_ENUM.INCOME,
430
+ },
431
+ ];
@@ -1,3 +1,4 @@
1
+ export * from './accounting-templates';
1
2
  export * from './ANALYTICS';
2
3
  export * from './API_ERRORS';
3
4
  export * from './COMMON';
@@ -987,7 +987,12 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
987
987
  subtotal: orderLevelDiscountBaseAmount,
988
988
  hasExcludedRefs: false,
989
989
  };
990
- if (hasExcludedRefs && !is_discount_percentage) {
990
+ // A fixed discount with exclusions is legitimate: the builder already
991
+ // normalized it against the eligible subtotal. Only clear when there
992
+ // is nothing left to discount - the capping branch below re-derives
993
+ // the amount from the same base. (The voucher copy never had this
994
+ // early clear, which is why the two drifted.)
995
+ if (hasExcludedRefs && !is_discount_percentage && eligibleSubtotal <= 0) {
991
996
  clearOrderLevelDiscount('loyalty');
992
997
  }
993
998
  else {
@@ -1000,10 +1005,7 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
1000
1005
  Number.isFinite(max_discount_amount) &&
1001
1006
  max_discount_amount >= 0;
1002
1007
  const hasFlatMaxPercentage = !is_discount_percentage && hasRelevantPercentageCap;
1003
- if (hasExcludedRefs && !is_discount_percentage && !hasFlatMaxPercentage) {
1004
- clearOrderLevelDiscount('loyalty');
1005
- }
1006
- else if (hasExcludedRefs) {
1008
+ if (hasExcludedRefs) {
1007
1009
  // Excluded items present: must apply percentage against eligible subtotal only.
1008
1010
  const eligibleBase = hasFlatMaxPercentage
1009
1011
  ? parseNum(getNetItemsValueAfterItemDiscount({
@@ -1131,7 +1133,10 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
1131
1133
  Number.isFinite(max_discount_amount) &&
1132
1134
  max_discount_amount >= 0;
1133
1135
  const hasFlatMaxPercentage = !is_discount_percentage && hasRelevantPercentageCap;
1134
- if (hasExcludedRefs && !is_discount_percentage && !hasFlatMaxPercentage) {
1136
+ // Same rule as the loyalty slot: a fixed discount with exclusions is
1137
+ // valid (the builder already normalized it against the eligible
1138
+ // subtotal), so only clear when nothing is left to discount.
1139
+ if (hasExcludedRefs && !is_discount_percentage && eligibleSubtotal <= 0) {
1135
1140
  clearOrderLevelDiscount('voucher');
1136
1141
  }
1137
1142
  else if (hasExcludedRefs) {
@@ -2482,6 +2487,7 @@ export function getUpdateItemInCartData(data, payload, courses, listnerSettings,
2482
2487
  ...data,
2483
2488
  ...(iterations.length > 0 && { iterations }),
2484
2489
  categories,
2490
+ ...getItemRemovalRedeemedOffersPatch({ data, categories, removedItemId: payload._id }),
2485
2491
  };
2486
2492
  const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2487
2493
  if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
@@ -2534,6 +2540,47 @@ export function getUpdateCartInfoData(data, payload, courses, listnerSettings, s
2534
2540
  };
2535
2541
  }
2536
2542
  /* ======================><====================== */
2543
+ /**
2544
+ * Drop the claim for any offer whose last stamped item was just removed.
2545
+ *
2546
+ * An item reward lives both in `redeemed_offers` and on the item's own
2547
+ * `item_discount_details`. The offers sync is parked while an order is edited, so
2548
+ * removing a reward item used to leave its claim behind: the order could still be
2549
+ * settled against a reward that is no longer in it. This reconciles the one case
2550
+ * that needs no offer definition - nothing stamped is left.
2551
+ */
2552
+ function getItemRemovalRedeemedOffersPatch({ data, categories, removedItemId, }) {
2553
+ if (removedItemId == null)
2554
+ return {};
2555
+ const removedOfferIds = [];
2556
+ for (let i = 0; i < (data.categories ?? []).length; i++) {
2557
+ const items = data.categories?.[i].items ?? [];
2558
+ for (let j = 0; j < items.length; j++) {
2559
+ if (items[j]._id !== removedItemId)
2560
+ continue;
2561
+ const discountDetails = items[j].item_discount_details;
2562
+ const offerId = discountDetails?.loyalty_discount_id ?? discountDetails?.voucher_discount_id;
2563
+ if (offerId != null)
2564
+ removedOfferIds.push(offerId);
2565
+ }
2566
+ }
2567
+ if (removedOfferIds.length === 0)
2568
+ return {};
2569
+ const nextRedeemedOffers = { ...(getRedeemedOffersMap(data.redeemed_offers) ?? {}) };
2570
+ for (let i = 0; i < removedOfferIds.length; i++) {
2571
+ const offerId = getNormalizedOfferId(removedOfferIds[i]);
2572
+ if (offerId == null)
2573
+ continue;
2574
+ const stillStamped = (categories ?? []).some((category) => (category.items ?? []).some((item) => {
2575
+ const details = item.item_discount_details;
2576
+ return (getNormalizedOfferId(details?.loyalty_discount_id) === offerId ||
2577
+ getNormalizedOfferId(details?.voucher_discount_id) === offerId);
2578
+ }));
2579
+ if (!stillStamped)
2580
+ delete nextRedeemedOffers[offerId];
2581
+ }
2582
+ return { redeemed_offers: getRedeemedOffersMap(nextRedeemedOffers) };
2583
+ }
2537
2584
  export function getRemoveItemFromCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2538
2585
  const orderItemIdsMap = {};
2539
2586
  let categories;
@@ -2612,6 +2659,7 @@ export function getRemoveItemFromCartData(data, payload, courses, listnerSetting
2612
2659
  ...data,
2613
2660
  ...(iterations.length > 0 && { iterations }),
2614
2661
  categories,
2662
+ ...getItemRemovalRedeemedOffersPatch({ data, categories, removedItemId: payload._id }),
2615
2663
  };
2616
2664
  const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2617
2665
  if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
@@ -43,7 +43,11 @@ export function getEditItemProps({ item, orderItemCategoryId, orderItem, offers,
43
43
  return;
44
44
  let applyLoyalty;
45
45
  let applyVoucher;
46
- const excludeRefs = undefined;
46
+ // "hide these options" and "this option is not discountable" are two different
47
+ // lists and were previously crossed here: the hide list was never populated and
48
+ // the no-discount slot received the hide list, so editing a reward item hid
49
+ // nothing and price-protected the wrong options.
50
+ let excludeRefs = undefined;
47
51
  let excludedDiscountRefs = undefined;
48
52
  const zeroPriceRefs = undefined;
49
53
  // Step 1: Check if loyalty discount or voucher discount is applied
@@ -53,20 +57,24 @@ export function getEditItemProps({ item, orderItemCategoryId, orderItem, offers,
53
57
  return;
54
58
  // const rewardItems = offer?.reward_items;
55
59
  let refs = [];
60
+ let offerHideRefs = [];
56
61
  let offerExcludedDiscountRefs = [];
57
62
  let applicable_quantity = 1;
58
63
  if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.ITEMS) {
59
64
  if (offer.reward_items == null)
60
65
  return;
61
66
  refs = offer.reward_items.exclusive_refs.concat(item._id) ?? [];
62
- offerExcludedDiscountRefs = offer.reward_items.excluded_refs;
67
+ // excluded_refs hides options; exclusive_refs marks options as not discountable.
68
+ offerHideRefs = offer.reward_items.excluded_refs;
69
+ offerExcludedDiscountRefs = offer.reward_items.exclusive_refs;
63
70
  applicable_quantity = offer.reward_items.num_of_items || 1;
64
71
  }
65
72
  else if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
66
73
  if (offer.reward_discount_items == null)
67
74
  return;
68
75
  refs = offer.reward_discount_items.exclusive_refs.concat(item._id) ?? [];
69
- offerExcludedDiscountRefs = offer.reward_discount_items.excluded_refs;
76
+ offerHideRefs = offer.reward_discount_items.excluded_refs;
77
+ offerExcludedDiscountRefs = offer.reward_discount_items.exclusive_refs;
70
78
  applicable_quantity = offer.reward_discount_items.num_of_items || 1;
71
79
  }
72
80
  let discount_details = null;
@@ -85,6 +93,7 @@ export function getEditItemProps({ item, orderItemCategoryId, orderItem, offers,
85
93
  discount_details,
86
94
  applicable_quantity,
87
95
  };
96
+ excludeRefs = offerHideRefs;
88
97
  excludedDiscountRefs = offerExcludedDiscountRefs;
89
98
  }
90
99
  if (isVoucherDiscountApplied) {
@@ -93,20 +102,24 @@ export function getEditItemProps({ item, orderItemCategoryId, orderItem, offers,
93
102
  return;
94
103
  // const rewardItems = offer?.reward_items;
95
104
  let refs = [];
105
+ let offerHideRefs = [];
96
106
  let offerExcludedDiscountRefs = [];
97
107
  let applicable_quantity = 1;
98
108
  if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.ITEMS) {
99
109
  if (offer.reward_items == null)
100
110
  return;
101
111
  refs = offer.reward_items.exclusive_refs.concat(item._id) ?? [];
102
- offerExcludedDiscountRefs = offer.reward_items.excluded_refs;
112
+ // excluded_refs hides options; exclusive_refs marks options as not discountable.
113
+ offerHideRefs = offer.reward_items.excluded_refs;
114
+ offerExcludedDiscountRefs = offer.reward_items.exclusive_refs;
103
115
  applicable_quantity = offer.reward_items.num_of_items || 1;
104
116
  }
105
117
  else if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
106
118
  if (offer.reward_discount_items == null)
107
119
  return;
108
120
  refs = offer.reward_discount_items.exclusive_refs.concat(item._id) ?? [];
109
- offerExcludedDiscountRefs = offer.reward_discount_items.excluded_refs;
121
+ offerHideRefs = offer.reward_discount_items.excluded_refs;
122
+ offerExcludedDiscountRefs = offer.reward_discount_items.exclusive_refs;
110
123
  applicable_quantity = offer.reward_discount_items.num_of_items || 1;
111
124
  }
112
125
  let discount_details = null;
@@ -125,6 +138,7 @@ export function getEditItemProps({ item, orderItemCategoryId, orderItem, offers,
125
138
  discount_details,
126
139
  applicable_quantity,
127
140
  };
141
+ excludeRefs = offerHideRefs;
128
142
  excludedDiscountRefs = offerExcludedDiscountRefs;
129
143
  }
130
144
  // TODO: We need to add use case for HalfHalf and Specials
@@ -291,11 +291,28 @@ export function getCappedOfferDiscountAmount({ baseAmount, discountValue, isDisc
291
291
  }
292
292
  return Math.min(amount, normalizedBaseAmount);
293
293
  }
294
+ /**
295
+ * Does this order line match any of the given refs?
296
+ *
297
+ * A ref is an **item-level** id: an item id, and - because a SPECIAL or
298
+ * HALF_HALF order line stores its own document id in `item_id` - a special or
299
+ * half-half id too. Modifier/option refs never appear here: those belong to the
300
+ * reward's "Hide Options" / "Discount Not Applicable" lists, which act on the
301
+ * modifier tree at selection time, not on spend qualification.
302
+ */
303
+ export function doesOrderItemMatchRefs(item, refs) {
304
+ if (refs.length === 0)
305
+ return false;
306
+ return item.item_id != null && refs.includes(item.item_id);
307
+ }
294
308
  /**
295
309
  * Calculate the eligible subtotal by excluding items/categories in excluded_refs.
296
310
  *
297
311
  * Shared by the cart engine (discount application/re-sync) and the offer-stats
298
312
  * engine (spend qualification) so both use the exact same exclusion math.
313
+ *
314
+ * A ref matches a whole category (`category_id`) or an item - where "item" means
315
+ * the item id, one of its modifier refs, or one of its option refs.
299
316
  */
300
317
  export function calculateEligibleOfferDiscountDetails(categories, excluded_refs) {
301
318
  if (!categories?.length)
@@ -310,7 +327,7 @@ export function calculateEligibleOfferDiscountDetails(categories, excluded_refs)
310
327
  }
311
328
  // Otherwise, check each item
312
329
  for (const item of category.items ?? []) {
313
- if (excluded_refs.includes(item.item_id)) {
330
+ if (doesOrderItemMatchRefs(item, excluded_refs)) {
314
331
  hasExcludedRefs = true;
315
332
  }
316
333
  else {
@@ -88,12 +88,14 @@ export function buildOfferDiscountUpdate({ offer, orderDiscountDetails, categori
88
88
  if (excluded_refs != null && excluded_refs.length > 0) {
89
89
  const { subtotal: eligibleSubtotal, hasExcludedRefs } = calculateEligibleOfferDiscountDetails(categories, excluded_refs);
90
90
  if (hasExcludedRefs) {
91
- if (!is_discount_percentage) {
92
- return { success: false, discountDetails: null, errorMessage: null };
93
- }
94
91
  if (eligibleSubtotal <= 0) {
95
92
  return { success: false, discountDetails: null, errorMessage: null };
96
93
  }
94
+ // Both shapes resolve against the eligible subtotal. A fixed amount used to
95
+ // bail out here while the eligibility engine still reported the offer as
96
+ // claimable, so the claim button always failed with a generic error. Capping
97
+ // is the same contract the gift-card path already uses: a discount may never
98
+ // exceed the amount it is allowed to discount.
97
99
  discount_value = parseNum(getCappedOfferDiscountAmount({
98
100
  baseAmount: eligibleSubtotal,
99
101
  discountValue: discount_value,