@pisell/pisellos 2.1.103 → 2.1.104

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