@pisell/pisellos 2.1.91 → 2.1.93

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 (102) 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 +497 -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 +165 -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/index.d.ts +1 -0
  30. package/dist/modules/Discount/index.js +40 -11
  31. package/dist/modules/Discount/types.d.ts +17 -0
  32. package/dist/modules/Payment/index.d.ts +2 -1
  33. package/dist/modules/Payment/index.js +10 -7
  34. package/dist/modules/Payment/utils.js +3 -0
  35. package/dist/modules/Payment/walletpass.d.ts +23 -0
  36. package/dist/modules/Payment/walletpass.js +191 -95
  37. package/dist/modules/Product/index.d.ts +1 -1
  38. package/dist/modules/Rules/index.d.ts +9 -5
  39. package/dist/modules/Rules/index.js +538 -118
  40. package/dist/modules/Rules/types.d.ts +1 -0
  41. package/dist/modules/Summary/types.d.ts +2 -0
  42. package/dist/modules/Summary/utils.d.ts +6 -0
  43. package/dist/modules/Summary/utils.js +21 -0
  44. package/dist/plugins/window.d.ts +2 -0
  45. package/dist/solution/BookingByStep/index.d.ts +2 -1
  46. package/dist/solution/Checkout/index.js +2 -0
  47. package/dist/solution/ShopDiscount/index.d.ts +2 -0
  48. package/dist/solution/ShopDiscount/index.js +146 -92
  49. package/dist/solution/ShopDiscount/types.d.ts +1 -0
  50. package/dist/solution/ShopDiscount/types.js +2 -1
  51. package/dist/solution/ShopDiscount/utils.js +26 -12
  52. package/lib/index.d.ts +1 -0
  53. package/lib/index.js +3 -1
  54. package/lib/model/index.d.ts +1 -0
  55. package/lib/model/index.js +23 -0
  56. package/lib/model/strategy/adapter/index.d.ts +3 -0
  57. package/lib/model/strategy/adapter/index.js +45 -0
  58. package/lib/model/strategy/adapter/type.d.ts +28 -0
  59. package/lib/model/strategy/adapter/type.js +17 -0
  60. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  61. package/lib/model/strategy/adapter/walletPass/evaluator.js +421 -0
  62. package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
  63. package/lib/model/strategy/adapter/walletPass/example.js +207 -0
  64. package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
  65. package/lib/model/strategy/adapter/walletPass/index.js +142 -0
  66. package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  67. package/lib/model/strategy/adapter/walletPass/locales.js +51 -0
  68. package/lib/model/strategy/adapter/walletPass/type.d.ts +165 -0
  69. package/lib/model/strategy/adapter/walletPass/type.js +17 -0
  70. package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  71. package/lib/model/strategy/adapter/walletPass/utils.js +660 -0
  72. package/lib/model/strategy/index.d.ts +94 -0
  73. package/lib/model/strategy/index.js +413 -0
  74. package/lib/model/strategy/strategy-example.d.ts +5 -0
  75. package/lib/model/strategy/strategy-example.js +318 -0
  76. package/lib/model/strategy/type.d.ts +228 -0
  77. package/lib/model/strategy/type.js +44 -0
  78. package/lib/modules/Cart/types.d.ts +2 -0
  79. package/lib/modules/Cart/utils/cartProduct.js +9 -0
  80. package/lib/modules/Discount/index.d.ts +1 -0
  81. package/lib/modules/Discount/index.js +9 -0
  82. package/lib/modules/Discount/types.d.ts +17 -0
  83. package/lib/modules/Payment/index.d.ts +2 -1
  84. package/lib/modules/Payment/index.js +1 -0
  85. package/lib/modules/Payment/utils.js +3 -0
  86. package/lib/modules/Payment/walletpass.d.ts +23 -0
  87. package/lib/modules/Payment/walletpass.js +94 -17
  88. package/lib/modules/Product/index.d.ts +1 -1
  89. package/lib/modules/Rules/index.d.ts +9 -5
  90. package/lib/modules/Rules/index.js +669 -258
  91. package/lib/modules/Rules/types.d.ts +1 -0
  92. package/lib/modules/Summary/types.d.ts +2 -0
  93. package/lib/modules/Summary/utils.d.ts +6 -0
  94. package/lib/modules/Summary/utils.js +15 -0
  95. package/lib/plugins/window.d.ts +2 -0
  96. package/lib/solution/BookingByStep/index.d.ts +2 -1
  97. package/lib/solution/Checkout/index.js +2 -0
  98. package/lib/solution/ShopDiscount/index.d.ts +2 -0
  99. package/lib/solution/ShopDiscount/index.js +36 -8
  100. package/lib/solution/ShopDiscount/types.d.ts +1 -0
  101. package/lib/solution/ShopDiscount/utils.js +10 -6
  102. package/package.json +1 -1
@@ -50,6 +50,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
50
50
  this.core = core;
51
51
  this.hooks = options == null ? void 0 : options.hooks;
52
52
  this.store = options == null ? void 0 : options.store;
53
+ this.window = core.getPlugin("window");
53
54
  }
54
55
  async setRulesList(rulesList) {
55
56
  this.store.rulesList = rulesList;
@@ -71,11 +72,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
71
72
  return false;
72
73
  return (productHolderId || orderHolderId) === discount.holder.holder_id;
73
74
  }
75
+ getWalletPassEvaluator() {
76
+ var _a, _b;
77
+ return (_b = (_a = this.window).getWalletPassEvaluator) == null ? void 0 : _b.call(_a);
78
+ }
74
79
  // 判断discountList 是否可以对当前productList生效
75
80
  isDiscountListAvailable({
76
81
  oldDiscountList,
77
82
  newDiscountList,
78
83
  productList,
84
+ orderTotalAmount,
79
85
  holders,
80
86
  isFormSubject
81
87
  }) {
@@ -108,6 +114,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
108
114
  const result = this.calcDiscount({
109
115
  discountList: mergedDiscountList,
110
116
  productList: [...productList],
117
+ orderTotalAmount,
111
118
  holders,
112
119
  isFormSubject
113
120
  }, {
@@ -180,7 +187,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
180
187
  discountList,
181
188
  productList,
182
189
  holders,
183
- isFormSubject
190
+ isFormSubject,
191
+ orderTotalAmount
184
192
  }, options) {
185
193
  const isEditModeAddNewProduct = productList.find((n) => n.booking_id) && productList.find((n) => !n.booking_id);
186
194
  const editModeDiscount = [];
@@ -222,7 +230,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
222
230
  _id: product._id,
223
231
  parentId: product._id,
224
232
  quantity: product.quantity,
225
- num: product.num
233
+ num: product.num,
234
+ booking_id: product.booking_id
226
235
  });
227
236
  if (product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0) {
228
237
  product.bundle.forEach((bundleItem, bundleIndex) => {
@@ -244,7 +253,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
244
253
  origin_total: new import_decimal.default(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
245
254
  original_price: bundleItem.original_price,
246
255
  // 继承主商品属性
247
- booking_id: product.booking_id,
256
+ booking_id: bundleItem.booking_id,
248
257
  discount_list: bundleItem.discount_list || []
249
258
  });
250
259
  });
@@ -358,7 +367,59 @@ var RulesModule = class extends import_BaseModule.BaseModule {
358
367
  }
359
368
  return priceB.minus(priceA).toNumber();
360
369
  });
370
+ const evaluator = this.getWalletPassEvaluator();
371
+ if (evaluator) {
372
+ addModeDiscount.forEach((discount) => {
373
+ var _a, _b;
374
+ const discountType = discount.tag || discount.type;
375
+ if (["good_pass", "discount_card", "product_discount_card"].includes(
376
+ discountType
377
+ )) {
378
+ const voucher = {
379
+ id: discount.id,
380
+ amount: Number(discount.par_value || 0),
381
+ balance: Number(discount.balance || 0),
382
+ type: discountType,
383
+ product_id: discount.product_id,
384
+ unified_available_status: 1,
385
+ available_product_type: (_a = discount.limited_relation_product_data) == null ? void 0 : _a.type,
386
+ available_product_ids: (_b = discount.limited_relation_product_data) == null ? void 0 : _b.product_ids
387
+ };
388
+ const productsForEvaluate = sortedFlattenedList.map((item) => ({
389
+ product_id: item.id,
390
+ price: item.price || 0,
391
+ quantity: item.quantity || item.num || 1,
392
+ selling_price: item.price || 0
393
+ }));
394
+ const result = evaluator.checkVoucherAvailability({
395
+ orderTotalAmount,
396
+ products: productsForEvaluate,
397
+ vouchers: [voucher]
398
+ });
399
+ if (result.isAvailable) {
400
+ discount.config = {
401
+ ...result.config,
402
+ isAvailable: true
403
+ };
404
+ } else {
405
+ discount.config = {
406
+ isAvailable: false
407
+ };
408
+ }
409
+ }
410
+ });
411
+ }
361
412
  const usedDiscounts = /* @__PURE__ */ new Map();
413
+ const usedProductIdCounts = /* @__PURE__ */ new Map();
414
+ const usedDiscountCardLimitCounts = /* @__PURE__ */ new Map();
415
+ editModeDiscount.forEach((discount) => {
416
+ var _a;
417
+ const discountType = discount.tag || discount.type;
418
+ if (["discount_card", "product_discount_card"].includes(discountType)) {
419
+ const currentCount = usedDiscountCardLimitCounts.get(discount.id) || 0;
420
+ usedDiscountCardLimitCounts.set(discount.id, currentCount + (((_a = discount.metadata) == null ? void 0 : _a.num) || 1));
421
+ }
422
+ });
362
423
  const discountApplicability = /* @__PURE__ */ new Map();
363
424
  const discountApplicableProducts = /* @__PURE__ */ new Map();
364
425
  addModeDiscount.forEach((discount) => {
@@ -512,7 +573,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
512
573
  originProduct = flatItem.originProduct;
513
574
  }
514
575
  addModeDiscount.forEach((discount) => {
515
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
576
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
516
577
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
517
578
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
518
579
  const isHolderMatch = this.checkHolderMatch(
@@ -525,7 +586,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
525
586
  );
526
587
  let timeLimit = true;
527
588
  timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
528
- const isLimitedProduct = (limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
589
+ 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;
529
590
  const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a2 = product == null ? void 0 : product.discount_list) == null ? void 0 : _a2.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every(
530
591
  (discount2) => discount2.id && [
531
592
  "good_pass",
@@ -534,8 +595,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
534
595
  ].includes(discount2.tag || discount2.type)
535
596
  ))) : !((flatItem == null ? void 0 : flatItem.booking_id) && !!((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.discount_list) == null ? void 0 : _d.length) && ((_f = (_e = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _e.discount_list) == null ? void 0 : _f.every((discount2) => discount2.id)));
536
597
  const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
537
- if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable) {
538
- (_g = discountApplicability.get(discount.id)) == null ? void 0 : _g.push(product.id);
598
+ if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && ((_g = discount.config) == null ? void 0 : _g.isAvailable)) {
599
+ (_h = discountApplicability.get(discount.id)) == null ? void 0 : _h.push(product.id);
539
600
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
540
601
  const discountType = discount.tag || discount.type;
541
602
  const isGoodPass = discountType === "good_pass";
@@ -545,17 +606,19 @@ var RulesModule = class extends import_BaseModule.BaseModule {
545
606
  type: discountType,
546
607
  tag: discountType,
547
608
  discount: {
548
- discount_card_type: (_h = discount == null ? void 0 : discount.metadata) == null ? void 0 : _h.discount_card_type,
609
+ discount_card_type: (_i = discount == null ? void 0 : discount.metadata) == null ? void 0 : _i.discount_card_type,
549
610
  fixed_amount: product.price,
550
- discount_calculation_mode: (_i = discount == null ? void 0 : discount.metadata) == null ? void 0 : _i.discount_calculation_mode,
611
+ discount_calculation_mode: (_j = discount == null ? void 0 : discount.metadata) == null ? void 0 : _j.discount_calculation_mode,
551
612
  resource_id: discount.id,
552
613
  title: discount.format_title,
553
614
  original_amount: product.price || product.origin_total,
554
615
  pre_value: discount.par_value,
555
- product_id: originProduct.id
616
+ product_id: originProduct.id,
617
+ discount_product_id: discount.product_id
556
618
  },
557
619
  metadata: {
558
- num
620
+ num,
621
+ discount_rule_uncheck_flag: discount == null ? void 0 : discount.discount_rule_uncheck_flag
559
622
  }
560
623
  };
561
624
  applicableProducts.push(productData);
@@ -565,7 +628,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
565
628
  });
566
629
  const processedFlatItemsMap = /* @__PURE__ */ new Map();
567
630
  sortedFlattenedList.forEach((flatItem, index) => {
568
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
631
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I;
569
632
  let product, originProduct;
570
633
  if (flatItem.type === "main") {
571
634
  product = flatItem.product;
@@ -586,22 +649,36 @@ var RulesModule = class extends import_BaseModule.BaseModule {
586
649
  };
587
650
  originProduct = flatItem.originProduct;
588
651
  }
589
- if ((product == null ? void 0 : product.booking_id) && ((_e = product.discount_list) == null ? void 0 : _e.length) && ((_f = product == null ? void 0 : product.discount_list) == null ? void 0 : _f.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
652
+ if ((product == null ? void 0 : product.booking_id) && ((_e = product.discount_list) == null ? void 0 : _e.length) && ((_f = product == null ? void 0 : product.discount_list) == null ? void 0 : _f.every(
653
+ (discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(
654
+ discount.tag || discount.type
655
+ )
656
+ ))) {
590
657
  if (flatItem.type === "main") {
591
658
  processedProductsMap.set(product._id, [originProduct]);
592
659
  } else {
593
- processedFlatItemsMap.set(flatItem._id, [{
594
- ...flatItem,
595
- processed: true
596
- }]);
660
+ processedFlatItemsMap.set(flatItem._id, [
661
+ {
662
+ ...flatItem,
663
+ processed: true
664
+ }
665
+ ]);
597
666
  }
598
667
  return;
599
668
  }
600
669
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
601
- var _a2, _b2;
602
- if ((Number(product.price) <= 0 || !product.price) && !((_a2 = product.discount_list) == null ? void 0 : _a2.length) && (discount.tag || discount.type) === "good_pass")
670
+ var _a2, _b2, _c2, _d2, _e2;
671
+ const discountType2 = discount.tag || discount.type;
672
+ if (["good_pass", "discount_card", "product_discount_card"].includes(
673
+ discountType2
674
+ )) {
675
+ if (discount.config === void 0 || !((_a2 = discount == null ? void 0 : discount.config) == null ? void 0 : _a2.isAvailable)) {
676
+ return false;
677
+ }
678
+ }
679
+ if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.length) && (discount.tag || discount.type) === "good_pass")
603
680
  return false;
604
- if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
681
+ if ((Number(product.price) <= 0 || !product.price) && !((_c2 = product.discount_list) == null ? void 0 : _c2.find((n) => {
605
682
  var _a3;
606
683
  return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
607
684
  })) && (discount.tag || discount.type) !== "good_pass")
@@ -609,7 +686,31 @@ var RulesModule = class extends import_BaseModule.BaseModule {
609
686
  const targetUsedDiscounts = usedDiscounts.get(discount.id);
610
687
  if (targetUsedDiscounts && (discount.tag || discount.type) === "good_pass")
611
688
  return false;
689
+ if ((discount.tag || discount.type) === "good_pass") {
690
+ const maxUsagePerOrder = (_d2 = discount.config) == null ? void 0 : _d2.maxUsagePerOrder;
691
+ if (maxUsagePerOrder && maxUsagePerOrder > 0) {
692
+ const currentUsedCount = usedProductIdCounts.get(discount.product_id) || 0;
693
+ if (currentUsedCount >= maxUsagePerOrder) {
694
+ return false;
695
+ }
696
+ }
697
+ }
698
+ const discountTypeForLimit = discount.tag || discount.type;
699
+ if (["discount_card", "product_discount_card"].includes(discountTypeForLimit)) {
700
+ const applicableProductLimitConfig = ((_e2 = discount.config) == null ? void 0 : _e2.applicableProductLimit) || 0;
701
+ if (applicableProductLimitConfig > 0) {
702
+ const currentUsedLimitCount = usedDiscountCardLimitCounts.get(discount.id) || 0;
703
+ if (currentUsedLimitCount >= applicableProductLimitConfig) {
704
+ return false;
705
+ }
706
+ }
707
+ }
612
708
  const limitedData = discount.limited_relation_product_data;
709
+ let timeLimit = true;
710
+ timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], (product.startDate || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
711
+ if (!timeLimit) {
712
+ return false;
713
+ }
613
714
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
614
715
  const isHolderMatch = this.checkHolderMatch(
615
716
  discount,
@@ -621,12 +722,10 @@ var RulesModule = class extends import_BaseModule.BaseModule {
621
722
  );
622
723
  if (!isHolderMatch)
623
724
  return false;
624
- let timeLimit = true;
625
- timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], (product.startDate || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
626
- if (!timeLimit) {
627
- return false;
628
- }
629
725
  if (limitedData.type === "product_all") {
726
+ if (limitedData.filter === 1 && limitedData.exclude_product_ids.includes(product.id)) {
727
+ return false;
728
+ }
630
729
  if (!this.checkPackageSubItemUsageRules(discount, flatItem)) {
631
730
  return false;
632
731
  }
@@ -639,7 +738,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
639
738
  }
640
739
  return false;
641
740
  });
642
- const selectedDiscountCard = applicableDiscounts.find((n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass");
741
+ const selectedDiscountCard = applicableDiscounts.find(
742
+ (n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass"
743
+ );
643
744
  const selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
644
745
  let isManualDiscount = false;
645
746
  if (flatItem.type === "main") {
@@ -706,9 +807,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
706
807
  if (applicableDiscounts.length === 0 || isManualDiscount || (0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
707
808
  if (flatItem.type === "main") {
708
809
  if (product.isClient) {
709
- processedProductsMap.set(
710
- product._id,
711
- [this.hooks.setProduct(originProduct, {
810
+ processedProductsMap.set(product._id, [
811
+ this.hooks.setProduct(originProduct, {
712
812
  ...isManualDiscount ? {} : {
713
813
  origin_total: (0, import_utils2.getProductOriginTotalPrice)({
714
814
  product: {
@@ -729,12 +829,11 @@ var RulesModule = class extends import_BaseModule.BaseModule {
729
829
  price: product.price
730
830
  },
731
831
  discount_list: []
732
- })]
733
- );
832
+ })
833
+ ]);
734
834
  } else {
735
- processedProductsMap.set(
736
- product._id,
737
- [this.hooks.setProduct(originProduct, {
835
+ processedProductsMap.set(product._id, [
836
+ this.hooks.setProduct(originProduct, {
738
837
  ...isManualDiscount ? {
739
838
  price: product.price,
740
839
  main_product_selling_price: product.price
@@ -745,48 +844,83 @@ var RulesModule = class extends import_BaseModule.BaseModule {
745
844
  main_product_selling_price: product.price
746
845
  },
747
846
  discount_list: []
748
- })]
749
- );
847
+ })
848
+ ]);
750
849
  }
751
850
  } else {
752
- processedFlatItemsMap.set(flatItem._id, [{
753
- ...flatItem,
754
- discount_list: [],
755
- price: isManualDiscount ? flatItem.bundleItem.price : flatItem.bundleItem.original_price,
756
- processed: true
757
- }]);
851
+ processedFlatItemsMap.set(flatItem._id, [
852
+ {
853
+ ...flatItem,
854
+ discount_list: [],
855
+ price: isManualDiscount ? flatItem.bundleItem.price : flatItem.bundleItem.original_price,
856
+ processed: true
857
+ }
858
+ ]);
758
859
  }
759
860
  return;
760
861
  }
761
862
  if (applicableDiscounts.length && product.booking_id && typeof selectedDiscount.isManualSelect === "undefined" && !(options == null ? void 0 : options.scan) && !isEditModeAddNewProduct) {
762
863
  return;
763
864
  }
764
- const isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === "good_pass";
865
+ const discountType = selectedDiscount.tag || selectedDiscount.type;
866
+ const isGoodPass = discountType === "good_pass";
867
+ const isDiscountCard = ["discount_card", "product_discount_card"].includes(discountType);
868
+ const applicableProductLimit = ((_u = selectedDiscount.config) == null ? void 0 : _u.applicableProductLimit) || 0;
869
+ const isNeedSplit = isGoodPass || isDiscountCard && applicableProductLimit > 0;
765
870
  const totalQuantity = product.quantity || product.num || 1;
766
- const availableGoodPassCount = applicableDiscounts.filter((item) => (item.tag || item.type) === "good_pass").length;
767
- const splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
871
+ const availableGoodPassCount = applicableDiscounts.filter(
872
+ (item) => (item.tag || item.type) === "good_pass"
873
+ ).length;
874
+ let maxUsageLimit;
875
+ if (isGoodPass) {
876
+ maxUsageLimit = availableGoodPassCount;
877
+ if (selectedDiscount.config && selectedDiscount.config.maxUsagePerOrder) {
878
+ maxUsageLimit = Math.min(
879
+ availableGoodPassCount,
880
+ selectedDiscount.config.maxUsagePerOrder
881
+ );
882
+ }
883
+ } else if (isDiscountCard && applicableProductLimit > 0) {
884
+ const usedLimitCount = usedDiscountCardLimitCounts.get(selectedDiscount.id) || 0;
885
+ maxUsageLimit = Math.max(0, applicableProductLimit - usedLimitCount);
886
+ } else {
887
+ maxUsageLimit = 1;
888
+ }
889
+ const splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, maxUsageLimit) : 1;
768
890
  const arr = [];
769
891
  if (flatItem.type === "main") {
770
892
  if (splitCount < totalQuantity && isNeedSplit) {
771
- arr.push(this.hooks.setProduct(originProduct, {
772
- discount_list: [],
773
- quantity: totalQuantity - splitCount,
774
- _id: product._id.split("___")[0]
775
- }));
893
+ arr.push(
894
+ this.hooks.setProduct(originProduct, {
895
+ discount_list: [],
896
+ quantity: totalQuantity - splitCount,
897
+ _id: product._id.split("___")[0],
898
+ total: product.origin_total || product.total
899
+ })
900
+ );
776
901
  }
777
902
  for (let i = 0; i < splitCount; i++) {
778
- const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[i];
779
- usedDiscounts.set(selectedDiscount2.id, true);
780
- const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
903
+ const currentSelectedDiscount = isGoodPass ? selectedDiscountCard || applicableDiscounts[i] : selectedDiscountCard || applicableDiscounts[0];
904
+ usedDiscounts.set(currentSelectedDiscount.id, true);
905
+ if ((currentSelectedDiscount.tag || currentSelectedDiscount.type) === "good_pass") {
906
+ const currentCount = usedProductIdCounts.get(currentSelectedDiscount.product_id) || 0;
907
+ usedProductIdCounts.set(currentSelectedDiscount.product_id, currentCount + 1);
908
+ }
909
+ const currentDiscountTypeForCount = currentSelectedDiscount.tag || currentSelectedDiscount.type;
910
+ if (["discount_card", "product_discount_card"].includes(currentDiscountTypeForCount)) {
911
+ const currentLimitCount = usedDiscountCardLimitCounts.get(currentSelectedDiscount.id) || 0;
912
+ usedDiscountCardLimitCounts.set(currentSelectedDiscount.id, currentLimitCount + 1);
913
+ }
914
+ const appliedProducts = appliedDiscountProducts.get(currentSelectedDiscount.id) || [];
781
915
  let productOriginTotal = product.origin_total || product.total || 0;
782
- if (((_u = product.discount_list) == null ? void 0 : _u.length) && product.origin_total) {
916
+ if (((_v = product.discount_list) == null ? void 0 : _v.length) && product.origin_total) {
783
917
  productOriginTotal = product.origin_total;
784
918
  }
785
- if (Number(((_v = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _v.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
919
+ if (Number(((_w = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _w.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
786
920
  productOriginTotal = product.total;
787
921
  }
788
- const isOrderLevel = (0, import_utils.isOrderLevelFixedAmountDiscount)(selectedDiscount2);
789
- const orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount2.id) : null;
922
+ const isOrderLevel = (0, import_utils.isOrderLevelFixedAmountDiscount)(selectedDiscount);
923
+ const orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount.id) : null;
790
924
  const productAllocation = orderLevelAllocation == null ? void 0 : orderLevelAllocation.get(flatItem._id);
791
925
  let targetProductTotal;
792
926
  let amount;
@@ -796,34 +930,38 @@ var RulesModule = class extends import_BaseModule.BaseModule {
796
930
  productDiscountDifference = productAllocation.difference;
797
931
  targetProductTotal = Math.max(new import_decimal.default(product.price).minus(amount).toNumber(), 0);
798
932
  } else {
799
- targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
933
+ targetProductTotal = (0, import_utils.getDiscountAmount)(currentSelectedDiscount, product.price, product.price);
800
934
  amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
801
935
  }
802
- const discountType = selectedDiscount2.tag || selectedDiscount2.type;
803
- const isGoodPass = discountType === "good_pass";
936
+ const currentDiscountType = currentSelectedDiscount.tag === "product_discount_card" ? "discount_card" : currentSelectedDiscount.tag;
937
+ const actualDiscountAmount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
938
+ const currentIsGoodPass = currentDiscountType === "good_pass";
804
939
  const discountDetail = {
805
- amount,
806
- type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : discountType,
940
+ amount: actualDiscountAmount,
941
+ type: currentDiscountType,
807
942
  discount: {
808
- discount_card_type: (_w = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _w.discount_card_type,
809
- fixed_amount: amount,
810
- discount_calculation_mode: (_x = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _x.discount_calculation_mode,
811
- resource_id: selectedDiscount2.id,
812
- title: selectedDiscount2.format_title,
943
+ discount_card_type: (_x = currentSelectedDiscount == null ? void 0 : currentSelectedDiscount.metadata) == null ? void 0 : _x.discount_card_type,
944
+ fixed_amount: actualDiscountAmount,
945
+ discount_calculation_mode: (_y = currentSelectedDiscount == null ? void 0 : currentSelectedDiscount.metadata) == null ? void 0 : _y.discount_calculation_mode,
946
+ resource_id: currentSelectedDiscount.id,
947
+ title: currentSelectedDiscount.format_title,
813
948
  original_amount: product.price,
814
949
  product_id: originProduct.id,
815
- percent: selectedDiscount2.par_value
950
+ percent: currentSelectedDiscount.par_value,
951
+ discount_product_id: currentSelectedDiscount.product_id
816
952
  },
817
- // 前端使用的num数量,为了计算优惠金额
818
- _num: isGoodPass ? 1 : product.num,
953
+ // 前端使用的num数量,为了计算优惠金额(拆分时为1)
954
+ _num: isNeedSplit ? 1 : product.num,
955
+ config: currentSelectedDiscount == null ? void 0 : currentSelectedDiscount.config,
819
956
  metadata: {
820
957
  num: 1,
958
+ discount_rule_uncheck_flag: selectedDiscount == null ? void 0 : selectedDiscount.discount_rule_uncheck_flag,
821
959
  // 🔥 order_level 分摊差值
822
960
  ...productDiscountDifference !== void 0 && { product_discount_difference: productDiscountDifference }
823
961
  }
824
962
  };
825
963
  appliedProducts.push(discountDetail);
826
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
964
+ appliedDiscountProducts.set(currentSelectedDiscount.id, appliedProducts);
827
965
  let total = targetProductTotal;
828
966
  if (product.options) {
829
967
  total = product.options.reduce((accumulator, currentValue) => {
@@ -833,32 +971,35 @@ var RulesModule = class extends import_BaseModule.BaseModule {
833
971
  }, new import_decimal.default(total)).toNumber();
834
972
  }
835
973
  if (product.isClient) {
836
- debugger;
837
- arr.push(this.hooks.setProduct(originProduct, {
838
- discount_list: [discountDetail],
839
- price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
840
- quantity: isNeedSplit ? 1 : product.quantity,
841
- origin_total: (0, import_utils2.getProductOriginTotalPrice)({
842
- product: {
843
- original_price: product.original_price
844
- },
845
- bundle: product.bundle,
846
- options: product.options
847
- }),
848
- variant: originProduct._productInit.variant,
849
- original_price: new import_decimal.default(product.price || 0).toNumber(),
850
- total
851
- }));
974
+ arr.push(
975
+ this.hooks.setProduct(originProduct, {
976
+ discount_list: [discountDetail],
977
+ price: currentIsGoodPass ? product.price - discountDetail.amount : product.price,
978
+ quantity: isNeedSplit ? 1 : product.quantity,
979
+ origin_total: (0, import_utils2.getProductOriginTotalPrice)({
980
+ product: {
981
+ original_price: product.original_price
982
+ },
983
+ bundle: product.bundle,
984
+ options: product.options
985
+ }),
986
+ variant: originProduct._productInit.variant,
987
+ original_price: new import_decimal.default(product.price || 0).toNumber(),
988
+ total: targetProductTotal
989
+ })
990
+ );
852
991
  } else {
853
- arr.push(this.hooks.setProduct(originProduct, {
854
- discount_list: [discountDetail],
855
- _id: product._id.split("___")[0] + "___" + selectedDiscount2.id + index,
856
- price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
857
- quantity: isNeedSplit ? 1 : product.quantity,
858
- total,
859
- origin_total: productOriginTotal,
860
- main_product_selling_price: targetProductTotal
861
- }));
992
+ arr.push(
993
+ this.hooks.setProduct(originProduct, {
994
+ discount_list: [discountDetail],
995
+ _id: product._id.split("___")[0] + "___" + currentSelectedDiscount.id + "_" + i + "_" + index,
996
+ price: currentIsGoodPass ? 0 : product.price,
997
+ quantity: isNeedSplit ? 1 : product.quantity,
998
+ total: targetProductTotal,
999
+ origin_total: productOriginTotal,
1000
+ main_product_selling_price: targetProductTotal
1001
+ })
1002
+ );
862
1003
  }
863
1004
  }
864
1005
  processedProductsMap.set(product._id, arr);
@@ -868,61 +1009,298 @@ var RulesModule = class extends import_BaseModule.BaseModule {
868
1009
  const discountNum = splitCount;
869
1010
  const normalNum = totalQuantity - discountNum;
870
1011
  for (let i = 0; i < discountNum; i++) {
871
- const selectedDiscount2 = applicableDiscounts[i];
872
- usedDiscounts.set(selectedDiscount2.id, true);
1012
+ const currentBundleDiscount = isGoodPass ? selectedDiscountCard || applicableDiscounts[i] : selectedDiscountCard || applicableDiscounts[0];
1013
+ usedDiscounts.set(currentBundleDiscount.id, true);
1014
+ if ((currentBundleDiscount.tag || currentBundleDiscount.type) === "good_pass") {
1015
+ const currentCount = usedProductIdCounts.get(currentBundleDiscount.product_id) || 0;
1016
+ usedProductIdCounts.set(currentBundleDiscount.product_id, currentCount + 1);
1017
+ }
1018
+ const bundleDiscountTypeForCount = currentBundleDiscount.tag || currentBundleDiscount.type;
1019
+ if (["discount_card", "product_discount_card"].includes(bundleDiscountTypeForCount)) {
1020
+ const currentBundleLimitCount = usedDiscountCardLimitCounts.get(currentBundleDiscount.id) || 0;
1021
+ usedDiscountCardLimitCounts.set(currentBundleDiscount.id, currentBundleLimitCount + 1);
1022
+ }
873
1023
  const uniqueId = `${flatItem._id}_split_${i}`;
1024
+ const discountedPrice = (0, import_utils.getDiscountAmount)(
1025
+ currentBundleDiscount,
1026
+ product.origin_total,
1027
+ product.origin_total
1028
+ );
1029
+ const bundleDiscountAmount = new import_decimal.default(product.origin_total || 0).minus(discountedPrice).toNumber();
1030
+ const bundleDiscountType = currentBundleDiscount.tag === "product_discount_card" ? "discount_card" : currentBundleDiscount.tag;
874
1031
  const discountDetail = {
875
- amount: product.origin_total,
876
- type: "good_pass",
1032
+ amount: bundleDiscountAmount,
1033
+ type: bundleDiscountType,
877
1034
  discount: {
878
- fixed_amount: product.origin_total,
879
- discount_calculation_mode: (_y = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _y.discount_calculation_mode,
880
- resource_id: selectedDiscount2.id,
881
- title: selectedDiscount2.format_title,
1035
+ discount_card_type: (_z = currentBundleDiscount == null ? void 0 : currentBundleDiscount.metadata) == null ? void 0 : _z.discount_card_type,
1036
+ discount_calculation_mode: (_A = currentBundleDiscount == null ? void 0 : currentBundleDiscount.metadata) == null ? void 0 : _A.discount_calculation_mode,
1037
+ fixed_amount: bundleDiscountAmount,
1038
+ resource_id: currentBundleDiscount.id,
1039
+ title: currentBundleDiscount.format_title,
882
1040
  original_amount: product.origin_total,
883
- product_id: product.id
1041
+ product_id: product.id,
1042
+ percent: currentBundleDiscount.par_value,
1043
+ discount_product_id: currentBundleDiscount.product_id
884
1044
  },
885
1045
  metadata: {
886
1046
  // 🔥 使用拆分后的唯一 _id
887
1047
  custom_product_bundle_map_id: uniqueId,
1048
+ discount_rule_uncheck_flag: selectedDiscount == null ? void 0 : selectedDiscount.discount_rule_uncheck_flag,
888
1049
  num: 1
889
1050
  },
890
- _num: 1
1051
+ _num: 1,
1052
+ config: currentBundleDiscount == null ? void 0 : currentBundleDiscount.config
891
1053
  };
892
- const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
1054
+ const appliedProducts = appliedDiscountProducts.get(currentBundleDiscount.id) || [];
893
1055
  appliedProducts.push(discountDetail);
894
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
1056
+ appliedDiscountProducts.set(currentBundleDiscount.id, appliedProducts);
895
1057
  processedItems.push({
896
1058
  ...flatItem,
897
1059
  // 🔥 使用唯一的 _id
898
1060
  _id: uniqueId,
899
1061
  num: 1,
900
1062
  quantity: 1,
901
- price: 0,
902
- // 商品券价格为0
903
- total: 0,
1063
+ price: discountedPrice,
1064
+ // 折扣后的价格
1065
+ total: discountedPrice,
904
1066
  discount_list: [discountDetail],
905
1067
  processed: true,
906
- _discountId: selectedDiscount2.id
1068
+ _discountId: currentBundleDiscount.id
907
1069
  });
908
1070
  }
909
- if (normalNum > 0) {
910
- processedItems.push({
911
- ...flatItem,
912
- // 🔥 为剩余商品生成唯一的 _id
913
- _id: `${flatItem._id}_split_rest`,
914
- num: normalNum,
915
- quantity: normalNum,
916
- discount_list: [],
917
- processed: true
1071
+ let remainingNum = normalNum;
1072
+ let continueSplitIndex = discountNum;
1073
+ while (remainingNum > 0) {
1074
+ const nextApplicableDiscounts = sortedDiscountList.filter((nextDiscount) => {
1075
+ var _a2, _b2, _c2, _d2;
1076
+ const nextDiscountType = nextDiscount.tag || nextDiscount.type;
1077
+ if (["good_pass", "discount_card", "product_discount_card"].includes(nextDiscountType)) {
1078
+ if (nextDiscount.config === void 0 || !((_a2 = nextDiscount == null ? void 0 : nextDiscount.config) == null ? void 0 : _a2.isAvailable)) {
1079
+ return false;
1080
+ }
1081
+ }
1082
+ if ((Number(product.total) <= 0 || !product.total) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find(
1083
+ (n) => {
1084
+ var _a3;
1085
+ return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === nextDiscount.id;
1086
+ }
1087
+ )) && nextDiscountType !== "good_pass")
1088
+ return false;
1089
+ if (usedDiscounts.get(nextDiscount.id) && nextDiscountType === "good_pass")
1090
+ return false;
1091
+ if (nextDiscountType === "good_pass") {
1092
+ const maxUsagePerOrder = (_c2 = nextDiscount.config) == null ? void 0 : _c2.maxUsagePerOrder;
1093
+ if (maxUsagePerOrder && maxUsagePerOrder > 0) {
1094
+ const currentUsedCount = usedProductIdCounts.get(nextDiscount.product_id) || 0;
1095
+ if (currentUsedCount >= maxUsagePerOrder) {
1096
+ return false;
1097
+ }
1098
+ }
1099
+ }
1100
+ if (["discount_card", "product_discount_card"].includes(nextDiscountType)) {
1101
+ const limitConfig = ((_d2 = nextDiscount.config) == null ? void 0 : _d2.applicableProductLimit) || 0;
1102
+ if (limitConfig > 0) {
1103
+ const currentUsedLimitCount = usedDiscountCardLimitCounts.get(nextDiscount.id) || 0;
1104
+ if (currentUsedLimitCount >= limitConfig) {
1105
+ return false;
1106
+ }
1107
+ }
1108
+ }
1109
+ const limitedData = nextDiscount.limited_relation_product_data;
1110
+ if (limitedData.type === "product_all") {
1111
+ if (limitedData.filter === 1 && limitedData.exclude_product_ids.includes(product.id)) {
1112
+ return false;
1113
+ }
1114
+ if (!this.checkPackageSubItemUsageRules(nextDiscount, flatItem)) {
1115
+ return false;
1116
+ }
1117
+ return true;
1118
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
1119
+ if (!this.checkPackageSubItemUsageRules(nextDiscount, flatItem)) {
1120
+ return false;
1121
+ }
1122
+ return true;
1123
+ }
1124
+ return false;
918
1125
  });
1126
+ if (nextApplicableDiscounts.length === 0) {
1127
+ processedItems.push({
1128
+ ...flatItem,
1129
+ _id: `${flatItem._id}_split_rest`,
1130
+ num: remainingNum,
1131
+ quantity: remainingNum,
1132
+ discount_list: [],
1133
+ processed: true
1134
+ });
1135
+ break;
1136
+ }
1137
+ const nextSelectedDiscountCard = nextApplicableDiscounts.find(
1138
+ (n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass"
1139
+ );
1140
+ const nextSelectedDiscount = nextSelectedDiscountCard || nextApplicableDiscounts[0];
1141
+ const nextDiscType = nextSelectedDiscount.tag || nextSelectedDiscount.type;
1142
+ const nextIsGoodPass = nextDiscType === "good_pass";
1143
+ const nextIsDiscountCard = ["discount_card", "product_discount_card"].includes(nextDiscType);
1144
+ const nextApplicableProductLimit = ((_B = nextSelectedDiscount.config) == null ? void 0 : _B.applicableProductLimit) || 0;
1145
+ const nextIsNeedSplit = nextIsGoodPass || nextIsDiscountCard && nextApplicableProductLimit > 0;
1146
+ if (!nextIsNeedSplit) {
1147
+ const nextBundleDiscount = nextSelectedDiscountCard || nextApplicableDiscounts[0];
1148
+ usedDiscounts.set(nextBundleDiscount.id, true);
1149
+ if ((nextBundleDiscount.tag || nextBundleDiscount.type) === "good_pass") {
1150
+ const curCount = usedProductIdCounts.get(nextBundleDiscount.product_id) || 0;
1151
+ usedProductIdCounts.set(nextBundleDiscount.product_id, curCount + 1);
1152
+ }
1153
+ const nextNoSplitType = nextBundleDiscount.tag || nextBundleDiscount.type;
1154
+ if (["discount_card", "product_discount_card"].includes(nextNoSplitType)) {
1155
+ const curCount = usedDiscountCardLimitCounts.get(nextBundleDiscount.id) || 0;
1156
+ usedDiscountCardLimitCounts.set(nextBundleDiscount.id, curCount + remainingNum);
1157
+ }
1158
+ const nextOriginTotal = product.original_price || product.price || 0;
1159
+ const nextTargetTotal = (0, import_utils.getDiscountAmount)(nextBundleDiscount, nextOriginTotal, nextOriginTotal);
1160
+ const nextDiscountAmount = new import_decimal.default(nextOriginTotal).minus(nextTargetTotal).toNumber();
1161
+ const nextUniqueId = `${flatItem._id}_split_${continueSplitIndex}`;
1162
+ const nextBundleDiscType = nextBundleDiscount.tag === "product_discount_card" ? "discount_card" : nextBundleDiscount.tag;
1163
+ const nextDiscountDetail = {
1164
+ amount: nextDiscountAmount * remainingNum,
1165
+ type: nextBundleDiscType,
1166
+ discount: {
1167
+ discount_card_type: (_C = nextBundleDiscount == null ? void 0 : nextBundleDiscount.metadata) == null ? void 0 : _C.discount_card_type,
1168
+ fixed_amount: nextDiscountAmount,
1169
+ resource_id: nextBundleDiscount.id,
1170
+ title: nextBundleDiscount.format_title,
1171
+ original_amount: product.original_price,
1172
+ product_id: product.id,
1173
+ percent: nextBundleDiscount.par_value,
1174
+ discount_product_id: nextBundleDiscount.product_id
1175
+ },
1176
+ metadata: {
1177
+ custom_product_bundle_map_id: nextUniqueId,
1178
+ num: remainingNum
1179
+ },
1180
+ config: nextBundleDiscount == null ? void 0 : nextBundleDiscount.config,
1181
+ _num: remainingNum * (((_D = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _D.num) || 1)
1182
+ };
1183
+ const nextAppliedProducts = appliedDiscountProducts.get(nextBundleDiscount.id) || [];
1184
+ nextAppliedProducts.push(nextDiscountDetail);
1185
+ appliedDiscountProducts.set(nextBundleDiscount.id, nextAppliedProducts);
1186
+ processedItems.push({
1187
+ ...flatItem,
1188
+ _id: nextUniqueId,
1189
+ num: remainingNum,
1190
+ quantity: remainingNum,
1191
+ total: nextTargetTotal,
1192
+ price: new import_decimal.default(nextOriginTotal || 0).minus(nextDiscountDetail.discount.fixed_amount).toNumber(),
1193
+ discount_list: [nextDiscountDetail],
1194
+ processed: true
1195
+ });
1196
+ remainingNum = 0;
1197
+ break;
1198
+ }
1199
+ let nextMaxUsageLimit;
1200
+ if (nextIsGoodPass) {
1201
+ const nextGoodPassCount = nextApplicableDiscounts.filter(
1202
+ (item) => (item.tag || item.type) === "good_pass"
1203
+ ).length;
1204
+ nextMaxUsageLimit = nextGoodPassCount;
1205
+ if ((_E = nextSelectedDiscount.config) == null ? void 0 : _E.maxUsagePerOrder) {
1206
+ const usedCount = usedProductIdCounts.get(nextSelectedDiscount.product_id) || 0;
1207
+ nextMaxUsageLimit = Math.min(
1208
+ nextGoodPassCount,
1209
+ Math.max(0, nextSelectedDiscount.config.maxUsagePerOrder - usedCount)
1210
+ );
1211
+ }
1212
+ } else if (nextIsDiscountCard && nextApplicableProductLimit > 0) {
1213
+ const usedLimitCount = usedDiscountCardLimitCounts.get(nextSelectedDiscount.id) || 0;
1214
+ nextMaxUsageLimit = Math.max(0, nextApplicableProductLimit - usedLimitCount);
1215
+ } else {
1216
+ nextMaxUsageLimit = 1;
1217
+ }
1218
+ const nextSplitCount = Math.min(remainingNum, nextMaxUsageLimit);
1219
+ if (nextSplitCount === 0) {
1220
+ processedItems.push({
1221
+ ...flatItem,
1222
+ _id: `${flatItem._id}_split_rest`,
1223
+ num: remainingNum,
1224
+ quantity: remainingNum,
1225
+ discount_list: [],
1226
+ processed: true
1227
+ });
1228
+ break;
1229
+ }
1230
+ for (let j = 0; j < nextSplitCount; j++) {
1231
+ const nextBundleDiscount = nextIsGoodPass ? nextSelectedDiscountCard || nextApplicableDiscounts[j] : nextSelectedDiscountCard || nextApplicableDiscounts[0];
1232
+ usedDiscounts.set(nextBundleDiscount.id, true);
1233
+ if ((nextBundleDiscount.tag || nextBundleDiscount.type) === "good_pass") {
1234
+ const curCount = usedProductIdCounts.get(nextBundleDiscount.product_id) || 0;
1235
+ usedProductIdCounts.set(nextBundleDiscount.product_id, curCount + 1);
1236
+ }
1237
+ const nextBundleDiscTypeForCount = nextBundleDiscount.tag || nextBundleDiscount.type;
1238
+ if (["discount_card", "product_discount_card"].includes(nextBundleDiscTypeForCount)) {
1239
+ const curLimitCount = usedDiscountCardLimitCounts.get(nextBundleDiscount.id) || 0;
1240
+ usedDiscountCardLimitCounts.set(nextBundleDiscount.id, curLimitCount + 1);
1241
+ }
1242
+ const nextUniqueId = `${flatItem._id}_split_${continueSplitIndex}`;
1243
+ const nextDiscountedPrice = (0, import_utils.getDiscountAmount)(
1244
+ nextBundleDiscount,
1245
+ product.origin_total,
1246
+ product.origin_total
1247
+ );
1248
+ const nextBundleDiscAmount = new import_decimal.default(product.origin_total || 0).minus(nextDiscountedPrice).toNumber();
1249
+ const nextBundleDiscTypeStr = nextBundleDiscount.tag === "product_discount_card" ? "discount_card" : nextBundleDiscount.tag;
1250
+ const nextDiscountDetail = {
1251
+ amount: nextBundleDiscAmount,
1252
+ type: nextBundleDiscTypeStr,
1253
+ discount: {
1254
+ discount_card_type: (_F = nextBundleDiscount == null ? void 0 : nextBundleDiscount.metadata) == null ? void 0 : _F.discount_card_type,
1255
+ fixed_amount: nextBundleDiscAmount,
1256
+ resource_id: nextBundleDiscount.id,
1257
+ title: nextBundleDiscount.format_title,
1258
+ original_amount: product.origin_total,
1259
+ product_id: product.id,
1260
+ percent: nextBundleDiscount.par_value,
1261
+ discount_product_id: nextBundleDiscount.product_id
1262
+ },
1263
+ metadata: {
1264
+ custom_product_bundle_map_id: nextUniqueId,
1265
+ num: 1
1266
+ },
1267
+ _num: 1,
1268
+ config: nextBundleDiscount == null ? void 0 : nextBundleDiscount.config
1269
+ };
1270
+ const nextAppliedProducts = appliedDiscountProducts.get(nextBundleDiscount.id) || [];
1271
+ nextAppliedProducts.push(nextDiscountDetail);
1272
+ appliedDiscountProducts.set(nextBundleDiscount.id, nextAppliedProducts);
1273
+ processedItems.push({
1274
+ ...flatItem,
1275
+ _id: nextUniqueId,
1276
+ num: 1,
1277
+ quantity: 1,
1278
+ price: nextDiscountedPrice,
1279
+ total: nextDiscountedPrice,
1280
+ discount_list: [nextDiscountDetail],
1281
+ processed: true,
1282
+ _discountId: nextBundleDiscount.id
1283
+ });
1284
+ continueSplitIndex++;
1285
+ }
1286
+ remainingNum -= nextSplitCount;
919
1287
  }
920
1288
  } else {
921
- const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[0];
922
- usedDiscounts.set(selectedDiscount2.id, true);
1289
+ const currentBundleDiscount = selectedDiscountCard || applicableDiscounts[0];
1290
+ usedDiscounts.set(currentBundleDiscount.id, true);
1291
+ if ((currentBundleDiscount.tag || currentBundleDiscount.type) === "good_pass") {
1292
+ const currentCount = usedProductIdCounts.get(currentBundleDiscount.product_id) || 0;
1293
+ usedProductIdCounts.set(currentBundleDiscount.product_id, currentCount + 1);
1294
+ }
1295
+ const noSplitDiscountType = currentBundleDiscount.tag || currentBundleDiscount.type;
1296
+ if (["discount_card", "product_discount_card"].includes(noSplitDiscountType)) {
1297
+ const bundleQuantity = product.num || 1;
1298
+ const currentNoSplitCount = usedDiscountCardLimitCounts.get(currentBundleDiscount.id) || 0;
1299
+ usedDiscountCardLimitCounts.set(currentBundleDiscount.id, currentNoSplitCount + bundleQuantity);
1300
+ }
923
1301
  const productOriginTotal = product.original_price || product.price || 0;
924
- const isOrderLevel = (0, import_utils.isOrderLevelFixedAmountDiscount)(selectedDiscount2);
925
- const orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount2.id) : null;
1302
+ const isOrderLevel = (0, import_utils.isOrderLevelFixedAmountDiscount)(selectedDiscount);
1303
+ const orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount.id) : null;
926
1304
  const productAllocation = orderLevelAllocation == null ? void 0 : orderLevelAllocation.get(flatItem._id);
927
1305
  let targetProductTotal;
928
1306
  let fixedAmountPerItem;
@@ -933,39 +1311,42 @@ var RulesModule = class extends import_BaseModule.BaseModule {
933
1311
  targetProductTotal = Math.max(new import_decimal.default(productOriginTotal).minus(fixedAmountPerItem).toNumber(), 0);
934
1312
  } else {
935
1313
  targetProductTotal = (0, import_utils.getDiscountAmount)(
936
- selectedDiscount2,
1314
+ currentBundleDiscount,
937
1315
  productOriginTotal,
938
1316
  productOriginTotal
939
1317
  );
940
1318
  fixedAmountPerItem = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
941
1319
  }
942
- debugger;
1320
+ const bundleActualDiscountAmount = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
943
1321
  const uniqueId = flatItem._id;
944
1322
  const discountDetail = {
945
- amount: fixedAmountPerItem * (product.num || 1),
946
- type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag,
1323
+ amount: bundleActualDiscountAmount * (product.num || 1),
1324
+ type: currentBundleDiscount.tag === "product_discount_card" ? "discount_card" : currentBundleDiscount.tag,
947
1325
  discount: {
948
- discount_card_type: (_z = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _z.discount_card_type,
949
- fixed_amount: fixedAmountPerItem,
950
- discount_calculation_mode: (_A = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _A.discount_calculation_mode,
951
- resource_id: selectedDiscount2.id,
952
- title: selectedDiscount2.format_title,
1326
+ discount_card_type: (_G = currentBundleDiscount == null ? void 0 : currentBundleDiscount.metadata) == null ? void 0 : _G.discount_card_type,
1327
+ fixed_amount: bundleActualDiscountAmount,
1328
+ discount_calculation_mode: (_H = currentBundleDiscount == null ? void 0 : currentBundleDiscount.metadata) == null ? void 0 : _H.discount_calculation_mode,
1329
+ resource_id: currentBundleDiscount.id,
1330
+ title: currentBundleDiscount.format_title,
953
1331
  original_amount: product.original_price,
954
1332
  product_id: product.id,
955
- percent: selectedDiscount2.par_value
1333
+ percent: currentBundleDiscount.par_value,
1334
+ discount_product_id: currentBundleDiscount.product_id
956
1335
  },
957
1336
  metadata: {
958
1337
  // 🔥 使用唯一的 _id
959
1338
  custom_product_bundle_map_id: uniqueId,
1339
+ discount_rule_uncheck_flag: selectedDiscount == null ? void 0 : selectedDiscount.discount_rule_uncheck_flag,
960
1340
  num: product.num || 1,
961
1341
  // 🔥 order_level 分摊差值
962
1342
  ...productDiscountDifference !== void 0 && { product_discount_difference: productDiscountDifference }
963
1343
  },
964
- _num: (product.num || 1) * (((_B = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _B.num) || 1)
1344
+ config: currentBundleDiscount == null ? void 0 : currentBundleDiscount.config,
1345
+ _num: (product.num || 1) * (((_I = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _I.num) || 1)
965
1346
  };
966
- const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
1347
+ const appliedProducts = appliedDiscountProducts.get(currentBundleDiscount.id) || [];
967
1348
  appliedProducts.push(discountDetail);
968
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
1349
+ appliedDiscountProducts.set(currentBundleDiscount.id, appliedProducts);
969
1350
  processedItems.push({
970
1351
  ...flatItem,
971
1352
  total: targetProductTotal,
@@ -1050,80 +1431,96 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1050
1431
  const mainProduct = mainProductArr[0];
1051
1432
  const mainProductData = this.hooks.getProduct(mainProduct);
1052
1433
  const newBundleWithDiscount = [];
1053
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
1054
- const processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
1055
- if (processedItems.length > 1) {
1056
- processedItems.forEach((item) => {
1057
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1058
- var _a;
1059
- return {
1060
- ...discount,
1061
- metadata: {
1062
- ...discount.metadata,
1063
- num: item.num,
1064
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
1065
- }
1066
- // num: item.num, // 使用拆分后的 num
1067
- };
1068
- });
1069
- newBundleWithDiscount.push({
1070
- ...bundleItem,
1071
- _id: item._id,
1072
- product_id: bundleItem.product_id,
1073
- price: item.price,
1074
- num: item.num,
1075
- discount_list: updatedDiscountList2
1076
- });
1077
- });
1078
- } else {
1079
- const item = processedItems[0];
1080
- if (item.processed) {
1081
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1082
- var _a;
1083
- return {
1084
- ...discount,
1085
- metadata: {
1086
- ...discount.metadata,
1087
- num: item.num,
1088
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
1434
+ (product.bundle || []).forEach(
1435
+ (bundleItem, bundleIndex) => {
1436
+ const processedItems = bundleProcessingInfo.get(
1437
+ bundleIndex
1438
+ ) || [bundleItem];
1439
+ if (processedItems.length > 1) {
1440
+ processedItems.forEach((item) => {
1441
+ const updatedDiscountList2 = (item.discount_list || []).map(
1442
+ (discount) => {
1443
+ var _a;
1444
+ return {
1445
+ ...discount,
1446
+ metadata: {
1447
+ ...discount.metadata,
1448
+ num: item.num,
1449
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
1450
+ }
1451
+ // num: item.num, // 使用拆分后的 num
1452
+ };
1089
1453
  }
1090
- // num: item.num, // 使用当前的 num
1091
- };
1092
- });
1093
- newBundleWithDiscount.push({
1094
- ...bundleItem,
1095
- _id: item._id,
1096
- product_id: bundleItem.product_id,
1097
- price: item.price,
1098
- num: item.num,
1099
- discount_list: updatedDiscountList2
1454
+ );
1455
+ newBundleWithDiscount.push({
1456
+ ...bundleItem,
1457
+ _id: item._id,
1458
+ product_id: bundleItem.product_id,
1459
+ price: item.price,
1460
+ num: item.num,
1461
+ quantity: item.num,
1462
+ discount_list: updatedDiscountList2
1463
+ });
1100
1464
  });
1101
1465
  } else {
1102
- newBundleWithDiscount.push(item);
1466
+ const item = processedItems[0];
1467
+ if (item.processed) {
1468
+ const updatedDiscountList2 = (item.discount_list || []).map(
1469
+ (discount) => {
1470
+ var _a;
1471
+ return {
1472
+ ...discount,
1473
+ metadata: {
1474
+ ...discount.metadata,
1475
+ num: item.num,
1476
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
1477
+ }
1478
+ // num: item.num, // 使用当前的 num
1479
+ };
1480
+ }
1481
+ );
1482
+ newBundleWithDiscount.push({
1483
+ ...bundleItem,
1484
+ _id: item._id,
1485
+ product_id: bundleItem.product_id,
1486
+ price: item.price,
1487
+ num: item.num,
1488
+ quantity: item.num,
1489
+ discount_list: updatedDiscountList2
1490
+ });
1491
+ } else {
1492
+ newBundleWithDiscount.push(item);
1493
+ }
1103
1494
  }
1104
1495
  }
1105
- });
1496
+ );
1106
1497
  let newTotalWithDiscount = Number(mainProductData.price || 0);
1107
- let newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
1108
- const updatedMainDiscountList = mainProductData.discount_list.map((discount) => {
1109
- var _a, _b;
1110
- if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
1111
- return discount;
1112
- }
1113
- return {
1114
- ...discount,
1115
- // num: 1,
1116
- metadata: {
1117
- ...discount.metadata,
1118
- custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id,
1119
- num: 1
1498
+ let newOriginTotalWithDiscount = Number(
1499
+ mainProductData.original_price || mainProductData.price || 0
1500
+ );
1501
+ const updatedMainDiscountList = mainProductData.discount_list.map(
1502
+ (discount) => {
1503
+ var _a, _b;
1504
+ if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
1505
+ return discount;
1120
1506
  }
1121
- };
1122
- });
1123
- const mainDiscountList = updatedMainDiscountList.filter((item) => {
1124
- var _a;
1125
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1126
- });
1507
+ return {
1508
+ ...discount,
1509
+ // num: 1,
1510
+ metadata: {
1511
+ ...discount.metadata,
1512
+ custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id,
1513
+ num: 1
1514
+ }
1515
+ };
1516
+ }
1517
+ );
1518
+ const mainDiscountList = updatedMainDiscountList.filter(
1519
+ (item) => {
1520
+ var _a;
1521
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1522
+ }
1523
+ );
1127
1524
  if (mainDiscountList && mainDiscountList.length > 0) {
1128
1525
  const allDiscountAmount = (0, import_utils.getDiscountListAmountTotal)(mainDiscountList);
1129
1526
  newTotalWithDiscount = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalWithDiscount;
@@ -1153,20 +1550,24 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1153
1550
  quantity: 1,
1154
1551
  discount_list: updatedMainDiscountList,
1155
1552
  bundle: newBundleWithDiscount,
1156
- total: new import_decimal.default(newTotalWithDiscount).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber(),
1157
- origin_total: new import_decimal.default(newOriginTotalWithDiscount).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber()
1553
+ total: newTotalWithDiscount,
1554
+ origin_total: newOriginTotalWithDiscount
1158
1555
  })
1159
1556
  );
1160
1557
  if (mainProductQuantity > 1) {
1161
1558
  const newBundleOriginal = [];
1162
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
1163
- newBundleOriginal.push({
1164
- ...bundleItem,
1165
- discount_list: []
1166
- });
1167
- });
1559
+ (product.bundle || []).forEach(
1560
+ (bundleItem, bundleIndex) => {
1561
+ newBundleOriginal.push({
1562
+ ...bundleItem,
1563
+ discount_list: []
1564
+ });
1565
+ }
1566
+ );
1168
1567
  let newTotalOriginal = Number(mainProductData.price || 0);
1169
- let newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
1568
+ let newOriginTotalOriginal = Number(
1569
+ mainProductData.original_price || mainProductData.price || 0
1570
+ );
1170
1571
  const updatedMainDiscountListOriginal = mainProductData.discount_list.map((discount) => {
1171
1572
  var _a, _b;
1172
1573
  if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
@@ -1182,12 +1583,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1182
1583
  // num: mainProductQuantity - 1,
1183
1584
  };
1184
1585
  });
1185
- const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter((item) => {
1186
- var _a;
1187
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1188
- });
1586
+ const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter(
1587
+ (item) => {
1588
+ var _a;
1589
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1590
+ }
1591
+ );
1189
1592
  if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
1190
- const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountListOriginal);
1593
+ const allDiscountAmount = (0, import_utils.getDiscountListAmount)(
1594
+ mainDiscountListOriginal
1595
+ );
1191
1596
  newTotalOriginal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalOriginal;
1192
1597
  newOriginTotalOriginal = mainProductData.origin_total ?? newOriginTotalOriginal;
1193
1598
  }
@@ -1224,59 +1629,67 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1224
1629
  const mainProductData = this.hooks.getProduct(mainProduct);
1225
1630
  const newBundle = [];
1226
1631
  if (product.bundle && Array.isArray(product.bundle)) {
1227
- product.bundle.forEach((bundleItem, bundleIndex) => {
1228
- const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
1229
- const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
1230
- if (!processedBundleItems || processedBundleItems.length === 0) {
1231
- newBundle.push(bundleItem);
1232
- } else {
1233
- processedBundleItems.forEach((item) => {
1234
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1235
- var _a2;
1236
- return {
1237
- ...discount,
1238
- metadata: {
1239
- ...discount.metadata,
1240
- num: item.num,
1241
- custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
1242
- }
1243
- // num: item.num, // 使用拆分后的 num
1244
- };
1632
+ product.bundle.forEach(
1633
+ (bundleItem, bundleIndex) => {
1634
+ const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
1635
+ const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
1636
+ if (!processedBundleItems || processedBundleItems.length === 0) {
1637
+ newBundle.push(bundleItem);
1638
+ } else {
1639
+ processedBundleItems.forEach((item) => {
1640
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1641
+ var _a2;
1642
+ return {
1643
+ ...discount,
1644
+ metadata: {
1645
+ ...discount.metadata,
1646
+ num: item.num,
1647
+ custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
1648
+ }
1649
+ // num: item.num, // 使用拆分后的 num
1650
+ };
1651
+ });
1652
+ newBundle.push({
1653
+ ...bundleItem,
1654
+ _id: item._id,
1655
+ product_id: bundleItem.product_id,
1656
+ price: item.price,
1657
+ num: item.num,
1658
+ quantity: item.num,
1659
+ discount_list: updatedDiscountList2
1660
+ });
1245
1661
  });
1246
- newBundle.push({
1247
- ...bundleItem,
1248
- _id: item._id,
1249
- product_id: bundleItem.product_id,
1250
- price: item.price,
1251
- num: item.num,
1252
- discount_list: updatedDiscountList2
1253
- });
1254
- });
1662
+ }
1255
1663
  }
1256
- });
1664
+ );
1257
1665
  }
1258
1666
  let newTotal = Number(mainProductData.price || 0);
1259
- let newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
1260
- 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")));
1667
+ let newOriginTotal = Number(
1668
+ mainProductData.original_price || mainProductData.price || 0
1669
+ );
1670
+ 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(
1671
+ _b,
1672
+ (item) => item.type === "product"
1673
+ )));
1261
1674
  if (isManualDiscount) {
1262
1675
  newTotal = mainProductData.total ?? newTotal;
1263
1676
  newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
1264
1677
  } else {
1265
- const mainDiscountList = mainProductData.discount_list.filter((item) => {
1266
- var _a2;
1267
- return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
1268
- });
1678
+ const mainDiscountList = mainProductData.discount_list.filter(
1679
+ (item) => {
1680
+ var _a2;
1681
+ return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
1682
+ }
1683
+ );
1269
1684
  if (mainDiscountList && mainDiscountList.length > 0) {
1270
1685
  const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountList);
1271
1686
  newTotal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotal;
1272
1687
  newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
1273
1688
  }
1274
1689
  if (newBundle.length > 0) {
1275
- newBundle.forEach((item) => {
1276
- newTotal += Number(item.price) * Number(item.num);
1277
- });
1278
1690
  newBundle.forEach((item) => {
1279
1691
  var _a2, _b2, _c2;
1692
+ newTotal += Number(item.price) * Number(item.num);
1280
1693
  const originalPrice = ((_c2 = (_b2 = (_a2 = item.discount_list) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.discount) == null ? void 0 : _c2.original_amount) || item.price;
1281
1694
  newOriginTotal += Number(originalPrice) * Number(item.num);
1282
1695
  });
@@ -1289,8 +1702,6 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1289
1702
  return accumulator.add(currentPrice.mul(currentNum));
1290
1703
  }, new import_decimal.default(newTotal)).toNumber();
1291
1704
  }
1292
- newTotal = new import_decimal.default(newTotal).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
1293
- newOriginTotal = new import_decimal.default(newOriginTotal).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
1294
1705
  result.push(
1295
1706
  this.hooks.setProduct(mainProduct, {
1296
1707
  ...mainProductData,