@pisell/pisellos 2.1.53 → 2.1.54

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 (95) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/model/index.d.ts +1 -0
  4. package/dist/model/index.js +1 -0
  5. package/dist/model/strategy/adapter/index.d.ts +3 -0
  6. package/dist/model/strategy/adapter/index.js +4 -0
  7. package/dist/model/strategy/adapter/type.d.ts +28 -0
  8. package/dist/model/strategy/adapter/type.js +1 -0
  9. package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  10. package/dist/model/strategy/adapter/walletPass/evaluator.js +496 -0
  11. package/dist/model/strategy/adapter/walletPass/example.d.ts +4 -0
  12. package/dist/model/strategy/adapter/walletPass/example.js +258 -0
  13. package/dist/model/strategy/adapter/walletPass/index.d.ts +32 -0
  14. package/dist/model/strategy/adapter/walletPass/index.js +182 -0
  15. package/dist/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  16. package/dist/model/strategy/adapter/walletPass/locales.js +23 -0
  17. package/dist/model/strategy/adapter/walletPass/type.d.ts +163 -0
  18. package/dist/model/strategy/adapter/walletPass/type.js +1 -0
  19. package/dist/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  20. package/dist/model/strategy/adapter/walletPass/utils.js +965 -0
  21. package/dist/model/strategy/index.d.ts +94 -0
  22. package/dist/model/strategy/index.js +551 -0
  23. package/dist/model/strategy/strategy-example.d.ts +5 -0
  24. package/dist/model/strategy/strategy-example.js +331 -0
  25. package/dist/model/strategy/type.d.ts +228 -0
  26. package/dist/model/strategy/type.js +94 -0
  27. package/dist/modules/Cart/types.d.ts +2 -0
  28. package/dist/modules/Cart/utils/cartProduct.js +11 -0
  29. package/dist/modules/Discount/types.d.ts +15 -0
  30. package/dist/modules/Payment/index.d.ts +2 -1
  31. package/dist/modules/Payment/index.js +10 -7
  32. package/dist/modules/Payment/utils.js +3 -0
  33. package/dist/modules/Payment/walletpass.d.ts +23 -0
  34. package/dist/modules/Payment/walletpass.js +191 -95
  35. package/dist/modules/Rules/index.d.ts +8 -3
  36. package/dist/modules/Rules/index.js +155 -29
  37. package/dist/modules/Rules/types.d.ts +1 -0
  38. package/dist/modules/Summary/types.d.ts +2 -0
  39. package/dist/modules/Summary/utils.d.ts +6 -0
  40. package/dist/modules/Summary/utils.js +21 -0
  41. package/dist/plugins/window.d.ts +2 -0
  42. package/dist/solution/BookingByStep/index.d.ts +2 -1
  43. package/dist/solution/Checkout/index.js +2 -0
  44. package/dist/solution/ShopDiscount/index.d.ts +1 -0
  45. package/dist/solution/ShopDiscount/index.js +14 -4
  46. package/dist/solution/ShopDiscount/types.d.ts +1 -0
  47. package/dist/solution/ShopDiscount/utils.js +26 -11
  48. package/lib/index.d.ts +1 -0
  49. package/lib/index.js +3 -1
  50. package/lib/model/index.d.ts +1 -0
  51. package/lib/model/index.js +23 -0
  52. package/lib/model/strategy/adapter/index.d.ts +3 -0
  53. package/lib/model/strategy/adapter/index.js +45 -0
  54. package/lib/model/strategy/adapter/type.d.ts +28 -0
  55. package/lib/model/strategy/adapter/type.js +17 -0
  56. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  57. package/lib/model/strategy/adapter/walletPass/evaluator.js +420 -0
  58. package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
  59. package/lib/model/strategy/adapter/walletPass/example.js +207 -0
  60. package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
  61. package/lib/model/strategy/adapter/walletPass/index.js +142 -0
  62. package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  63. package/lib/model/strategy/adapter/walletPass/locales.js +51 -0
  64. package/lib/model/strategy/adapter/walletPass/type.d.ts +163 -0
  65. package/lib/model/strategy/adapter/walletPass/type.js +17 -0
  66. package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  67. package/lib/model/strategy/adapter/walletPass/utils.js +660 -0
  68. package/lib/model/strategy/index.d.ts +94 -0
  69. package/lib/model/strategy/index.js +413 -0
  70. package/lib/model/strategy/strategy-example.d.ts +5 -0
  71. package/lib/model/strategy/strategy-example.js +318 -0
  72. package/lib/model/strategy/type.d.ts +228 -0
  73. package/lib/model/strategy/type.js +44 -0
  74. package/lib/modules/Cart/types.d.ts +2 -0
  75. package/lib/modules/Cart/utils/cartProduct.js +9 -0
  76. package/lib/modules/Discount/types.d.ts +15 -0
  77. package/lib/modules/Payment/index.d.ts +2 -1
  78. package/lib/modules/Payment/index.js +1 -0
  79. package/lib/modules/Payment/utils.js +3 -0
  80. package/lib/modules/Payment/walletpass.d.ts +23 -0
  81. package/lib/modules/Payment/walletpass.js +94 -17
  82. package/lib/modules/Rules/index.d.ts +8 -3
  83. package/lib/modules/Rules/index.js +373 -195
  84. package/lib/modules/Rules/types.d.ts +1 -0
  85. package/lib/modules/Summary/types.d.ts +2 -0
  86. package/lib/modules/Summary/utils.d.ts +6 -0
  87. package/lib/modules/Summary/utils.js +15 -0
  88. package/lib/plugins/window.d.ts +2 -0
  89. package/lib/solution/BookingByStep/index.d.ts +2 -1
  90. package/lib/solution/Checkout/index.js +2 -0
  91. package/lib/solution/ShopDiscount/index.d.ts +1 -0
  92. package/lib/solution/ShopDiscount/index.js +9 -3
  93. package/lib/solution/ShopDiscount/types.d.ts +1 -0
  94. package/lib/solution/ShopDiscount/utils.js +10 -6
  95. package/package.json +1 -1
@@ -49,6 +49,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
49
49
  this.core = core;
50
50
  this.hooks = options == null ? void 0 : options.hooks;
51
51
  this.store = options == null ? void 0 : options.store;
52
+ this.window = core.getPlugin("window");
52
53
  }
53
54
  async setRulesList(rulesList) {
54
55
  this.store.rulesList = rulesList;
@@ -57,6 +58,10 @@ var RulesModule = class extends import_BaseModule.BaseModule {
57
58
  getRulesList() {
58
59
  return this.store.rulesList;
59
60
  }
61
+ getWalletPassEvaluator() {
62
+ var _a, _b;
63
+ return (_b = (_a = this.window).getWalletPassEvaluator) == null ? void 0 : _b.call(_a);
64
+ }
60
65
  // 商品不需要holder,则不需要判断,直接返回true,商品需要holder但是还没填写,那么暂时不使用带有holder的券,直到填写才去匹配
61
66
  checkHolderMatch(discount, product, holders) {
62
67
  var _a;
@@ -75,7 +80,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
75
80
  oldDiscountList,
76
81
  newDiscountList,
77
82
  productList,
78
- holders
83
+ holders,
84
+ orderTotalAmount
79
85
  }) {
80
86
  if (!newDiscountList || newDiscountList.length === 0) {
81
87
  return {
@@ -95,15 +101,18 @@ var RulesModule = class extends import_BaseModule.BaseModule {
95
101
  productList
96
102
  };
97
103
  }
98
- const filteredOldDiscountList = oldDiscountList.filter((discount) => !discount.isEditMode && discount.tag !== "good_pass");
99
- const mergedDiscountList = (0, import_utils.uniqueById)((0, import_utils.uniqueById)([
100
- ...filteredOldDiscountList,
101
- ...newDiscountList
102
- ]), "product_id");
104
+ const filteredOldDiscountList = oldDiscountList.filter(
105
+ (discount) => !discount.isEditMode && discount.tag !== "good_pass"
106
+ );
107
+ const mergedDiscountList = (0, import_utils.uniqueById)(
108
+ (0, import_utils.uniqueById)([...filteredOldDiscountList, ...newDiscountList]),
109
+ "product_id"
110
+ );
103
111
  const result = this.calcDiscount({
104
112
  discountList: mergedDiscountList,
105
113
  productList: [...productList],
106
- holders
114
+ holders,
115
+ orderTotalAmount
107
116
  }, {
108
117
  scan: true
109
118
  });
@@ -136,7 +145,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
136
145
  calcDiscount({
137
146
  discountList,
138
147
  productList,
139
- holders
148
+ holders,
149
+ orderTotalAmount
140
150
  }, options) {
141
151
  const isEditModeAddNewProduct = productList.find((n) => n.booking_id) && productList.find((n) => !n.booking_id);
142
152
  const editModeDiscount = [];
@@ -256,7 +266,50 @@ var RulesModule = class extends import_BaseModule.BaseModule {
256
266
  }
257
267
  return priceB.minus(priceA).toNumber();
258
268
  });
269
+ const evaluator = this.getWalletPassEvaluator();
270
+ if (evaluator) {
271
+ addModeDiscount.forEach((discount) => {
272
+ var _a, _b;
273
+ const discountType = discount.tag || discount.type;
274
+ if (["good_pass", "discount_card", "product_discount_card"].includes(
275
+ discountType
276
+ )) {
277
+ const voucher = {
278
+ id: discount.id,
279
+ amount: Number(discount.par_value || 0),
280
+ balance: Number(discount.balance || 0),
281
+ type: discountType,
282
+ product_id: discount.product_id,
283
+ unified_available_status: 1,
284
+ available_product_type: (_a = discount.limited_relation_product_data) == null ? void 0 : _a.type,
285
+ available_product_ids: (_b = discount.limited_relation_product_data) == null ? void 0 : _b.product_ids
286
+ };
287
+ const productsForEvaluate = sortedFlattenedList.map((item) => ({
288
+ product_id: item.id,
289
+ price: item.price || 0,
290
+ quantity: item.quantity || item.num || 1,
291
+ selling_price: item.price || 0
292
+ }));
293
+ const result = evaluator.checkVoucherAvailability({
294
+ orderTotalAmount,
295
+ products: productsForEvaluate,
296
+ vouchers: [voucher]
297
+ });
298
+ if (result.isAvailable) {
299
+ discount.config = {
300
+ ...result.config,
301
+ isAvailable: true
302
+ };
303
+ } else {
304
+ discount.config = {
305
+ isAvailable: false
306
+ };
307
+ }
308
+ }
309
+ });
310
+ }
259
311
  const usedDiscounts = /* @__PURE__ */ new Map();
312
+ const usedProductIdCounts = /* @__PURE__ */ new Map();
260
313
  const discountApplicability = /* @__PURE__ */ new Map();
261
314
  const discountApplicableProducts = /* @__PURE__ */ new Map();
262
315
  addModeDiscount.forEach((discount) => {
@@ -285,7 +338,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
285
338
  originProduct = flatItem.originProduct;
286
339
  }
287
340
  addModeDiscount.forEach((discount) => {
288
- var _a, _b, _c, _d, _e, _f;
341
+ var _a, _b, _c, _d, _e, _f, _g;
289
342
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
290
343
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
291
344
  const isHolderMatch = this.checkHolderMatch(
@@ -298,11 +351,17 @@ var RulesModule = class extends import_BaseModule.BaseModule {
298
351
  );
299
352
  let timeLimit = true;
300
353
  timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
301
- const isLimitedProduct = (limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
302
- const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount2) => discount2.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount2.tag || discount2.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
354
+ const isLimitedProduct = (limitedData.type === "product_all" && limitedData.filter !== 1 || limitedData.type === "product_all" && limitedData.filter === 1 && !limitedData.exclude_product_ids.includes(product.id) || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
355
+ const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every(
356
+ (discount2) => discount2.id && [
357
+ "good_pass",
358
+ "discount_card",
359
+ "product_discount_card"
360
+ ].includes(discount2.tag || discount2.type)
361
+ ))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
303
362
  const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
304
- if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable) {
305
- (_e = discountApplicability.get(discount.id)) == null ? void 0 : _e.push(product.id);
363
+ if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && ((_e = discount.config) == null ? void 0 : _e.isAvailable)) {
364
+ (_f = discountApplicability.get(discount.id)) == null ? void 0 : _f.push(product.id);
306
365
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
307
366
  const discountType = discount.tag || discount.type;
308
367
  const isGoodPass = discountType === "good_pass";
@@ -312,13 +371,14 @@ var RulesModule = class extends import_BaseModule.BaseModule {
312
371
  type: discountType,
313
372
  tag: discountType,
314
373
  discount: {
315
- discount_card_type: (_f = discount == null ? void 0 : discount.metadata) == null ? void 0 : _f.discount_card_type,
374
+ discount_card_type: (_g = discount == null ? void 0 : discount.metadata) == null ? void 0 : _g.discount_card_type,
316
375
  fixed_amount: product.price,
317
376
  resource_id: discount.id,
318
377
  title: discount.format_title,
319
378
  original_amount: product.price || product.origin_total,
320
379
  pre_value: discount.par_value,
321
- product_id: originProduct.id
380
+ product_id: originProduct.id,
381
+ discount_product_id: discount.product_id
322
382
  },
323
383
  metadata: {
324
384
  num
@@ -351,25 +411,39 @@ var RulesModule = class extends import_BaseModule.BaseModule {
351
411
  };
352
412
  originProduct = flatItem.originProduct;
353
413
  }
354
- if ((product == null ? void 0 : product.booking_id) && ((_d = product.discount_list) == null ? void 0 : _d.length) && ((_e = product == null ? void 0 : product.discount_list) == null ? void 0 : _e.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
414
+ if ((product == null ? void 0 : product.booking_id) && ((_d = product.discount_list) == null ? void 0 : _d.length) && ((_e = product == null ? void 0 : product.discount_list) == null ? void 0 : _e.every(
415
+ (discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(
416
+ discount.tag || discount.type
417
+ )
418
+ ))) {
355
419
  if (flatItem.type === "main") {
356
420
  processedProductsMap.set(product._id, [originProduct]);
357
421
  } else {
358
- processedFlatItemsMap.set(flatItem._id, [{
359
- ...flatItem,
360
- processed: true
361
- }]);
422
+ processedFlatItemsMap.set(flatItem._id, [
423
+ {
424
+ ...flatItem,
425
+ processed: true
426
+ }
427
+ ]);
362
428
  }
363
429
  return;
364
430
  }
365
431
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
366
- var _a2, _b2;
367
- if ((Number(product.price) <= 0 || !product.price) && !((_a2 = product.discount_list) == null ? void 0 : _a2.find((n) => {
432
+ var _a2, _b2, _c2, _d2;
433
+ const discountType = discount.tag || discount.type;
434
+ if (["good_pass", "discount_card", "product_discount_card"].includes(
435
+ discountType
436
+ )) {
437
+ if (discount.config === void 0 || !((_a2 = discount == null ? void 0 : discount.config) == null ? void 0 : _a2.isAvailable)) {
438
+ return false;
439
+ }
440
+ }
441
+ if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
368
442
  var _a3;
369
443
  return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
370
444
  })) && (discount.tag || discount.type) === "good_pass")
371
445
  return false;
372
- if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
446
+ if ((Number(product.price) <= 0 || !product.price) && !((_c2 = product.discount_list) == null ? void 0 : _c2.find((n) => {
373
447
  var _a3;
374
448
  return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
375
449
  })) && (discount.tag || discount.type) !== "good_pass")
@@ -377,6 +451,15 @@ var RulesModule = class extends import_BaseModule.BaseModule {
377
451
  const targetUsedDiscounts = usedDiscounts.get(discount.id);
378
452
  if (targetUsedDiscounts && (discount.tag || discount.type) === "good_pass")
379
453
  return false;
454
+ if ((discount.tag || discount.type) === "good_pass") {
455
+ const maxUsagePerOrder = (_d2 = discount.config) == null ? void 0 : _d2.maxUsagePerOrder;
456
+ if (maxUsagePerOrder && maxUsagePerOrder > 0) {
457
+ const currentUsedCount = usedProductIdCounts.get(discount.product_id) || 0;
458
+ if (currentUsedCount >= maxUsagePerOrder) {
459
+ return false;
460
+ }
461
+ }
462
+ }
380
463
  const limitedData = discount.limited_relation_product_data;
381
464
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
382
465
  const isHolderMatch = this.checkHolderMatch(
@@ -395,6 +478,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
395
478
  return false;
396
479
  }
397
480
  if (limitedData.type === "product_all") {
481
+ if (limitedData.filter === 1 && limitedData.exclude_product_ids.includes(product.id)) {
482
+ return false;
483
+ }
398
484
  if (!this.checkPackageSubItemUsageRules(discount, flatItem)) {
399
485
  return false;
400
486
  }
@@ -407,66 +493,96 @@ var RulesModule = class extends import_BaseModule.BaseModule {
407
493
  }
408
494
  return false;
409
495
  });
410
- const selectedDiscountCard = applicableDiscounts.find((n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass");
496
+ const selectedDiscountCard = applicableDiscounts.find(
497
+ (n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass"
498
+ );
411
499
  const selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
412
500
  let isManualDiscount = false;
413
501
  if (flatItem.type === "main") {
414
- isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every((item) => {
415
- var _a2;
416
- return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
417
- }) && (!((_f = product.discount_list) == null ? void 0 : _f.length) || ((_h = (_g = product == null ? void 0 : product.discount_list) == null ? void 0 : _g.every) == null ? void 0 : _h.call(_g, (item) => item.type === "product")));
502
+ isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every(
503
+ (item) => {
504
+ var _a2;
505
+ return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
506
+ }
507
+ ) && (!((_f = product.discount_list) == null ? void 0 : _f.length) || ((_h = (_g = product == null ? void 0 : product.discount_list) == null ? void 0 : _g.every) == null ? void 0 : _h.call(
508
+ _g,
509
+ (item) => item.type === "product"
510
+ )));
418
511
  } else {
419
512
  const parentProduct = flatItem.parentProduct;
420
513
  if (parentProduct) {
421
- isManualDiscount = typeof parentProduct.isManualDiscount === "boolean" ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every((item) => {
422
- var _a2;
423
- return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
424
- }) && (!((_i = parentProduct.discount_list) == null ? void 0 : _i.length) || ((_k = (_j = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _j.every) == null ? void 0 : _k.call(_j, (item) => item.type === "product")));
514
+ isManualDiscount = typeof parentProduct.isManualDiscount === "boolean" ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every(
515
+ (item) => {
516
+ var _a2;
517
+ return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
518
+ }
519
+ ) && (!((_i = parentProduct.discount_list) == null ? void 0 : _i.length) || ((_k = (_j = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _j.every) == null ? void 0 : _k.call(
520
+ _j,
521
+ (item) => item.type === "product"
522
+ )));
425
523
  }
426
524
  }
427
525
  if (options == null ? void 0 : options.discountId) {
428
- if (flatItem.type === "main" && ((_l = product.discount_list) == null ? void 0 : _l.some((item) => {
429
- var _a2;
430
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
431
- }))) {
526
+ if (flatItem.type === "main" && ((_l = product.discount_list) == null ? void 0 : _l.some(
527
+ (item) => {
528
+ var _a2;
529
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
530
+ }
531
+ ))) {
432
532
  isManualDiscount = false;
433
533
  }
434
534
  if (flatItem.type === "bundle") {
435
- if (((_m = product.discount_list) == null ? void 0 : _m.some((item) => {
436
- var _a2;
437
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
438
- })) || ((_o = (_n = flatItem.parentProduct) == null ? void 0 : _n.discount_list) == null ? void 0 : _o.some((item) => {
439
- var _a2;
440
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
441
- }))) {
535
+ if (((_m = product.discount_list) == null ? void 0 : _m.some(
536
+ (item) => {
537
+ var _a2;
538
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
539
+ }
540
+ )) || ((_o = (_n = flatItem.parentProduct) == null ? void 0 : _n.discount_list) == null ? void 0 : _o.some(
541
+ (item) => {
542
+ var _a2;
543
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
544
+ }
545
+ ))) {
442
546
  isManualDiscount = false;
443
547
  }
444
548
  }
445
549
  }
446
550
  if (options == null ? void 0 : options.selectedList) {
447
- if (flatItem.type === "main" && ((_p = product.discount_list) == null ? void 0 : _p.some((item) => {
448
- var _a2;
449
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
450
- var _a3;
451
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
452
- });
453
- }))) {
551
+ if (flatItem.type === "main" && ((_p = product.discount_list) == null ? void 0 : _p.some(
552
+ (item) => {
553
+ var _a2;
554
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some(
555
+ (n) => {
556
+ var _a3;
557
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
558
+ }
559
+ );
560
+ }
561
+ ))) {
454
562
  isManualDiscount = false;
455
563
  }
456
564
  if (flatItem.type === "bundle") {
457
- if (((_q = product.discount_list) == null ? void 0 : _q.some((item) => {
458
- var _a2;
459
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
460
- var _a3;
461
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
462
- });
463
- })) || ((_s = (_r = flatItem.parentProduct) == null ? void 0 : _r.discount_list) == null ? void 0 : _s.some((item) => {
464
- var _a2;
465
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
466
- var _a3;
467
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
468
- });
469
- }))) {
565
+ if (((_q = product.discount_list) == null ? void 0 : _q.some(
566
+ (item) => {
567
+ var _a2;
568
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some(
569
+ (n) => {
570
+ var _a3;
571
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
572
+ }
573
+ );
574
+ }
575
+ )) || ((_s = (_r = flatItem.parentProduct) == null ? void 0 : _r.discount_list) == null ? void 0 : _s.some(
576
+ (item) => {
577
+ var _a2;
578
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some(
579
+ (n) => {
580
+ var _a3;
581
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
582
+ }
583
+ );
584
+ }
585
+ ))) {
470
586
  isManualDiscount = false;
471
587
  }
472
588
  }
@@ -474,9 +590,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
474
590
  if (applicableDiscounts.length === 0 || isManualDiscount || (0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
475
591
  if (flatItem.type === "main") {
476
592
  if (product.isClient) {
477
- processedProductsMap.set(
478
- product._id,
479
- [this.hooks.setProduct(originProduct, {
593
+ processedProductsMap.set(product._id, [
594
+ this.hooks.setProduct(originProduct, {
480
595
  ...isManualDiscount ? {} : {
481
596
  origin_total: (0, import_utils2.getProductOriginTotalPrice)({
482
597
  product: {
@@ -497,12 +612,11 @@ var RulesModule = class extends import_BaseModule.BaseModule {
497
612
  price: product.price
498
613
  },
499
614
  discount_list: []
500
- })]
501
- );
615
+ })
616
+ ]);
502
617
  } else {
503
- processedProductsMap.set(
504
- product._id,
505
- [this.hooks.setProduct(originProduct, {
618
+ processedProductsMap.set(product._id, [
619
+ this.hooks.setProduct(originProduct, {
506
620
  ...isManualDiscount ? {
507
621
  price: product.price,
508
622
  main_product_selling_price: product.price
@@ -513,8 +627,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
513
627
  main_product_selling_price: product.price
514
628
  },
515
629
  discount_list: []
516
- })]
517
- );
630
+ })
631
+ ]);
518
632
  }
519
633
  } else {
520
634
  processedFlatItemsMap.set(flatItem._id, [{
@@ -531,20 +645,36 @@ var RulesModule = class extends import_BaseModule.BaseModule {
531
645
  }
532
646
  const isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === "good_pass";
533
647
  const totalQuantity = product.quantity || product.num || 1;
534
- const availableGoodPassCount = applicableDiscounts.filter((item) => (item.tag || item.type) === "good_pass").length;
535
- const splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
648
+ const availableGoodPassCount = applicableDiscounts.filter(
649
+ (item) => (item.tag || item.type) === "good_pass"
650
+ ).length;
651
+ let maxUsageLimit = availableGoodPassCount;
652
+ if (selectedDiscount.config && selectedDiscount.config.maxUsagePerOrder) {
653
+ maxUsageLimit = Math.min(
654
+ availableGoodPassCount,
655
+ selectedDiscount.config.maxUsagePerOrder
656
+ );
657
+ }
658
+ const splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, maxUsageLimit) : 1;
536
659
  const arr = [];
537
660
  if (flatItem.type === "main") {
538
661
  if (splitCount < totalQuantity && isNeedSplit) {
539
- arr.push(this.hooks.setProduct(originProduct, {
540
- discount_list: [],
541
- quantity: totalQuantity - splitCount,
542
- _id: product._id.split("___")[0]
543
- }));
662
+ arr.push(
663
+ this.hooks.setProduct(originProduct, {
664
+ discount_list: [],
665
+ quantity: totalQuantity - splitCount,
666
+ _id: product._id.split("___")[0],
667
+ total: product.origin_total || product.total
668
+ })
669
+ );
544
670
  }
545
671
  for (let i = 0; i < splitCount; i++) {
546
672
  const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[i];
547
673
  usedDiscounts.set(selectedDiscount2.id, true);
674
+ if ((selectedDiscount2.tag || selectedDiscount2.type) === "good_pass") {
675
+ const currentCount = usedProductIdCounts.get(selectedDiscount2.product_id) || 0;
676
+ usedProductIdCounts.set(selectedDiscount2.product_id, currentCount + 1);
677
+ }
548
678
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
549
679
  let productOriginTotal = product.origin_total || product.total || 0;
550
680
  if (((_t = product.discount_list) == null ? void 0 : _t.length) && product.origin_total) {
@@ -553,24 +683,30 @@ var RulesModule = class extends import_BaseModule.BaseModule {
553
683
  if (Number(((_u = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _u.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
554
684
  productOriginTotal = product.total;
555
685
  }
556
- const targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
557
- const discountType = selectedDiscount2.tag || selectedDiscount2.type;
686
+ const targetProductTotal = (0, import_utils.getDiscountAmount)(
687
+ selectedDiscount2,
688
+ product.price,
689
+ product.price
690
+ );
691
+ const discountType = selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag;
692
+ const actualDiscountAmount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
558
693
  const isGoodPass = discountType === "good_pass";
559
- const amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
560
694
  const discountDetail = {
561
- amount,
562
- type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : discountType,
695
+ amount: actualDiscountAmount,
696
+ type: discountType,
563
697
  discount: {
564
698
  discount_card_type: (_v = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _v.discount_card_type,
565
- fixed_amount: amount,
699
+ fixed_amount: actualDiscountAmount,
566
700
  resource_id: selectedDiscount2.id,
567
701
  title: selectedDiscount2.format_title,
568
702
  original_amount: product.price,
569
703
  product_id: originProduct.id,
570
- percent: selectedDiscount2.par_value
704
+ percent: selectedDiscount2.par_value,
705
+ discount_product_id: selectedDiscount2.product_id
571
706
  },
572
707
  // 前端使用的num数量,为了计算优惠金额
573
708
  _num: isGoodPass ? 1 : product.num,
709
+ config: selectedDiscount2 == null ? void 0 : selectedDiscount2.config,
574
710
  metadata: {
575
711
  num: 1
576
712
  }
@@ -586,7 +722,6 @@ var RulesModule = class extends import_BaseModule.BaseModule {
586
722
  }, new import_decimal.default(total)).toNumber();
587
723
  }
588
724
  if (product.isClient) {
589
- debugger;
590
725
  arr.push(this.hooks.setProduct(originProduct, {
591
726
  discount_list: [discountDetail],
592
727
  price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
@@ -623,23 +758,35 @@ var RulesModule = class extends import_BaseModule.BaseModule {
623
758
  for (let i = 0; i < discountNum; i++) {
624
759
  const selectedDiscount2 = applicableDiscounts[i];
625
760
  usedDiscounts.set(selectedDiscount2.id, true);
761
+ if ((selectedDiscount2.tag || selectedDiscount2.type) === "good_pass") {
762
+ const currentCount = usedProductIdCounts.get(selectedDiscount2.product_id) || 0;
763
+ usedProductIdCounts.set(selectedDiscount2.product_id, currentCount + 1);
764
+ }
626
765
  const uniqueId = `${flatItem._id}_split_${i}`;
766
+ const discountedPrice = (0, import_utils.getDiscountAmount)(
767
+ selectedDiscount2,
768
+ product.origin_total,
769
+ product.origin_total
770
+ );
771
+ const bundleDiscountAmount = new import_decimal.default(product.origin_total || 0).minus(discountedPrice).toNumber();
627
772
  const discountDetail = {
628
- amount: product.origin_total,
773
+ amount: bundleDiscountAmount,
629
774
  type: "good_pass",
630
775
  discount: {
631
776
  fixed_amount: product.origin_total,
632
777
  resource_id: selectedDiscount2.id,
633
778
  title: selectedDiscount2.format_title,
634
779
  original_amount: product.origin_total,
635
- product_id: product.id
780
+ product_id: product.id,
781
+ discount_product_id: selectedDiscount2.product_id
636
782
  },
637
783
  metadata: {
638
784
  // 🔥 使用拆分后的唯一 _id
639
785
  custom_product_bundle_map_id: uniqueId,
640
786
  num: 1
641
787
  },
642
- _num: 1
788
+ _num: 1,
789
+ config: selectedDiscount2 == null ? void 0 : selectedDiscount2.config
643
790
  };
644
791
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
645
792
  appliedProducts.push(discountDetail);
@@ -650,9 +797,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
650
797
  _id: uniqueId,
651
798
  num: 1,
652
799
  quantity: 1,
653
- price: 0,
654
- // 商品券价格为0
655
- total: 0,
800
+ price: discountedPrice,
801
+ // 折扣后的价格
802
+ total: discountedPrice,
656
803
  discount_list: [discountDetail],
657
804
  processed: true,
658
805
  _discountId: selectedDiscount2.id
@@ -672,30 +819,37 @@ var RulesModule = class extends import_BaseModule.BaseModule {
672
819
  } else {
673
820
  const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[0];
674
821
  usedDiscounts.set(selectedDiscount2.id, true);
822
+ if ((selectedDiscount2.tag || selectedDiscount2.type) === "good_pass") {
823
+ const currentCount = usedProductIdCounts.get(selectedDiscount2.product_id) || 0;
824
+ usedProductIdCounts.set(selectedDiscount2.product_id, currentCount + 1);
825
+ }
675
826
  const productOriginTotal = product.original_price || product.price || 0;
676
827
  const targetProductTotal = (0, import_utils.getDiscountAmount)(
677
828
  selectedDiscount2,
678
829
  productOriginTotal,
679
830
  productOriginTotal
680
831
  );
832
+ const bundleActualDiscountAmount = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
681
833
  const uniqueId = flatItem._id;
682
834
  const discountDetail = {
683
- amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber() * (product.num || 1),
835
+ amount: bundleActualDiscountAmount * (product.num || 1),
684
836
  type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag,
685
837
  discount: {
686
838
  discount_card_type: (_w = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _w.discount_card_type,
687
- fixed_amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber(),
839
+ fixed_amount: bundleActualDiscountAmount,
688
840
  resource_id: selectedDiscount2.id,
689
841
  title: selectedDiscount2.format_title,
690
842
  original_amount: product.original_price,
691
843
  product_id: product.id,
692
- percent: selectedDiscount2.par_value
844
+ percent: selectedDiscount2.par_value,
845
+ discount_product_id: selectedDiscount2.product_id
693
846
  },
694
847
  metadata: {
695
848
  // 🔥 使用唯一的 _id
696
849
  custom_product_bundle_map_id: uniqueId,
697
850
  num: product.num || 1
698
851
  },
852
+ config: selectedDiscount2 == null ? void 0 : selectedDiscount2.config,
699
853
  _num: (product.num || 1) * (((_x = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _x.num) || 1)
700
854
  };
701
855
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
@@ -753,7 +907,6 @@ var RulesModule = class extends import_BaseModule.BaseModule {
753
907
  return;
754
908
  }
755
909
  const hasBundle = product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0;
756
- debugger;
757
910
  if (!hasBundle) {
758
911
  result.push(...mainProductArr);
759
912
  } else {
@@ -786,59 +939,65 @@ var RulesModule = class extends import_BaseModule.BaseModule {
786
939
  const mainProduct = mainProductArr[0];
787
940
  const mainProductData = this.hooks.getProduct(mainProduct);
788
941
  const newBundleWithDiscount = [];
789
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
790
- const processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
791
- if (processedItems.length > 1) {
792
- processedItems.forEach((item) => {
793
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
794
- var _a;
795
- return {
796
- ...discount,
797
- metadata: {
798
- num: item.num,
799
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
800
- }
801
- // num: item.num, // 使用拆分后的 num
802
- };
803
- });
804
- newBundleWithDiscount.push({
805
- ...bundleItem,
806
- _id: item._id,
807
- product_id: bundleItem.product_id,
808
- price: item.price,
809
- num: item.num,
810
- discount_list: updatedDiscountList2
811
- });
812
- });
813
- } else {
814
- const item = processedItems[0];
815
- if (item.processed) {
816
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
817
- var _a;
818
- return {
819
- ...discount,
820
- metadata: {
821
- num: item.num,
822
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
823
- }
824
- // num: item.num, // 使用当前的 num
825
- };
826
- });
827
- newBundleWithDiscount.push({
828
- ...bundleItem,
829
- _id: item._id,
830
- product_id: bundleItem.product_id,
831
- price: item.price,
832
- num: item.num,
833
- discount_list: updatedDiscountList2
942
+ (product.bundle || []).forEach(
943
+ (bundleItem, bundleIndex) => {
944
+ const processedItems = bundleProcessingInfo.get(
945
+ bundleIndex
946
+ ) || [bundleItem];
947
+ if (processedItems.length > 1) {
948
+ processedItems.forEach((item) => {
949
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
950
+ var _a;
951
+ return {
952
+ ...discount,
953
+ metadata: {
954
+ num: item.num,
955
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
956
+ }
957
+ // num: item.num, // 使用拆分后的 num
958
+ };
959
+ });
960
+ newBundleWithDiscount.push({
961
+ ...bundleItem,
962
+ _id: item._id,
963
+ product_id: bundleItem.product_id,
964
+ price: item.price,
965
+ num: item.num,
966
+ discount_list: updatedDiscountList2
967
+ });
834
968
  });
835
969
  } else {
836
- newBundleWithDiscount.push(item);
970
+ const item = processedItems[0];
971
+ if (item.processed) {
972
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
973
+ var _a;
974
+ return {
975
+ ...discount,
976
+ metadata: {
977
+ num: item.num,
978
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
979
+ }
980
+ // num: item.num, // 使用当前的 num
981
+ };
982
+ });
983
+ newBundleWithDiscount.push({
984
+ ...bundleItem,
985
+ _id: item._id,
986
+ product_id: bundleItem.product_id,
987
+ price: item.price,
988
+ num: item.num,
989
+ discount_list: updatedDiscountList2
990
+ });
991
+ } else {
992
+ newBundleWithDiscount.push(item);
993
+ }
837
994
  }
838
995
  }
839
- });
996
+ );
840
997
  let newTotalWithDiscount = Number(mainProductData.price || 0);
841
- let newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
998
+ let newOriginTotalWithDiscount = Number(
999
+ mainProductData.original_price || mainProductData.price || 0
1000
+ );
842
1001
  const updatedMainDiscountList = mainProductData.discount_list.map((discount) => {
843
1002
  var _a, _b;
844
1003
  if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
@@ -853,10 +1012,12 @@ var RulesModule = class extends import_BaseModule.BaseModule {
853
1012
  }
854
1013
  };
855
1014
  });
856
- const mainDiscountList = updatedMainDiscountList.filter((item) => {
857
- var _a;
858
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
859
- });
1015
+ const mainDiscountList = updatedMainDiscountList.filter(
1016
+ (item) => {
1017
+ var _a;
1018
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1019
+ }
1020
+ );
860
1021
  if (mainDiscountList && mainDiscountList.length > 0) {
861
1022
  const allDiscountAmount = (0, import_utils.getDiscountListAmountTotal)(mainDiscountList);
862
1023
  newTotalWithDiscount = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalWithDiscount;
@@ -892,14 +1053,18 @@ var RulesModule = class extends import_BaseModule.BaseModule {
892
1053
  );
893
1054
  if (mainProductQuantity > 1) {
894
1055
  const newBundleOriginal = [];
895
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
896
- newBundleOriginal.push({
897
- ...bundleItem,
898
- discount_list: []
899
- });
900
- });
1056
+ (product.bundle || []).forEach(
1057
+ (bundleItem, bundleIndex) => {
1058
+ newBundleOriginal.push({
1059
+ ...bundleItem,
1060
+ discount_list: []
1061
+ });
1062
+ }
1063
+ );
901
1064
  let newTotalOriginal = Number(mainProductData.price || 0);
902
- let newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
1065
+ let newOriginTotalOriginal = Number(
1066
+ mainProductData.original_price || mainProductData.price || 0
1067
+ );
903
1068
  const updatedMainDiscountListOriginal = mainProductData.discount_list.map((discount) => {
904
1069
  var _a, _b;
905
1070
  if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
@@ -914,12 +1079,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
914
1079
  // num: mainProductQuantity - 1,
915
1080
  };
916
1081
  });
917
- const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter((item) => {
918
- var _a;
919
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
920
- });
1082
+ const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter(
1083
+ (item) => {
1084
+ var _a;
1085
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1086
+ }
1087
+ );
921
1088
  if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
922
- const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountListOriginal);
1089
+ const allDiscountAmount = (0, import_utils.getDiscountListAmount)(
1090
+ mainDiscountListOriginal
1091
+ );
923
1092
  newTotalOriginal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalOriginal;
924
1093
  newOriginTotalOriginal = mainProductData.origin_total ?? newOriginTotalOriginal;
925
1094
  }
@@ -954,47 +1123,56 @@ var RulesModule = class extends import_BaseModule.BaseModule {
954
1123
  const mainProductData = this.hooks.getProduct(mainProduct);
955
1124
  const newBundle = [];
956
1125
  if (product.bundle && Array.isArray(product.bundle)) {
957
- product.bundle.forEach((bundleItem, bundleIndex) => {
958
- const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
959
- const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
960
- if (!processedBundleItems || processedBundleItems.length === 0) {
961
- newBundle.push(bundleItem);
962
- } else {
963
- processedBundleItems.forEach((item) => {
964
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
965
- var _a2;
966
- return {
967
- ...discount,
968
- metadata: {
969
- num: item.num,
970
- custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
971
- }
972
- // num: item.num, // 使用拆分后的 num
973
- };
1126
+ product.bundle.forEach(
1127
+ (bundleItem, bundleIndex) => {
1128
+ const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
1129
+ const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
1130
+ if (!processedBundleItems || processedBundleItems.length === 0) {
1131
+ newBundle.push(bundleItem);
1132
+ } else {
1133
+ processedBundleItems.forEach((item) => {
1134
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1135
+ var _a2;
1136
+ return {
1137
+ ...discount,
1138
+ metadata: {
1139
+ num: item.num,
1140
+ custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
1141
+ }
1142
+ // num: item.num, // 使用拆分后的 num
1143
+ };
1144
+ });
1145
+ newBundle.push({
1146
+ ...bundleItem,
1147
+ _id: item._id,
1148
+ product_id: bundleItem.product_id,
1149
+ price: item.price,
1150
+ num: item.num,
1151
+ discount_list: updatedDiscountList2
1152
+ });
974
1153
  });
975
- newBundle.push({
976
- ...bundleItem,
977
- _id: item._id,
978
- product_id: bundleItem.product_id,
979
- price: item.price,
980
- num: item.num,
981
- discount_list: updatedDiscountList2
982
- });
983
- });
1154
+ }
984
1155
  }
985
- });
1156
+ );
986
1157
  }
987
1158
  let newTotal = Number(mainProductData.price || 0);
988
- let newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
989
- const isManualDiscount = typeof mainProductData.isManualDiscount === "boolean" ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_a = mainProductData.discount_list) == null ? void 0 : _a.length) || ((_c = (_b = mainProductData == null ? void 0 : mainProductData.discount_list) == null ? void 0 : _b.every) == null ? void 0 : _c.call(_b, (item) => item.type === "product")));
1159
+ let newOriginTotal = Number(
1160
+ mainProductData.original_price || mainProductData.price || 0
1161
+ );
1162
+ const isManualDiscount = typeof mainProductData.isManualDiscount === "boolean" ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_a = mainProductData.discount_list) == null ? void 0 : _a.length) || ((_c = (_b = mainProductData == null ? void 0 : mainProductData.discount_list) == null ? void 0 : _b.every) == null ? void 0 : _c.call(
1163
+ _b,
1164
+ (item) => item.type === "product"
1165
+ )));
990
1166
  if (isManualDiscount) {
991
1167
  newTotal = mainProductData.total ?? newTotal;
992
1168
  newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
993
1169
  } else {
994
- const mainDiscountList = mainProductData.discount_list.filter((item) => {
995
- var _a2;
996
- return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
997
- });
1170
+ const mainDiscountList = mainProductData.discount_list.filter(
1171
+ (item) => {
1172
+ var _a2;
1173
+ return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
1174
+ }
1175
+ );
998
1176
  if (mainDiscountList && mainDiscountList.length > 0) {
999
1177
  const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountList);
1000
1178
  newTotal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotal;