@pisell/pisellos 2.1.105 → 2.1.107

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 (98) 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 +1077 -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/Rules/index.d.ts +9 -5
  36. package/dist/modules/Rules/index.js +538 -123
  37. package/dist/modules/Rules/types.d.ts +1 -0
  38. package/dist/modules/Summary/types.d.ts +2 -0
  39. package/dist/modules/Summary/utils.d.ts +6 -0
  40. package/dist/modules/Summary/utils.js +21 -0
  41. package/dist/plugins/window.d.ts +2 -0
  42. package/dist/solution/BookingByStep/index.d.ts +1 -0
  43. package/dist/solution/BookingTicket/index.d.ts +1 -1
  44. package/dist/solution/Checkout/index.js +2 -0
  45. package/dist/solution/ShopDiscount/index.d.ts +1 -0
  46. package/dist/solution/ShopDiscount/index.js +23 -7
  47. package/dist/solution/ShopDiscount/types.d.ts +1 -0
  48. package/dist/solution/ShopDiscount/types.js +2 -1
  49. package/dist/solution/ShopDiscount/utils.js +26 -12
  50. package/lib/index.d.ts +1 -0
  51. package/lib/index.js +3 -1
  52. package/lib/model/index.d.ts +1 -0
  53. package/lib/model/index.js +23 -0
  54. package/lib/model/strategy/adapter/index.d.ts +3 -0
  55. package/lib/model/strategy/adapter/index.js +45 -0
  56. package/lib/model/strategy/adapter/type.d.ts +28 -0
  57. package/lib/model/strategy/adapter/type.js +17 -0
  58. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  59. package/lib/model/strategy/adapter/walletPass/evaluator.js +422 -0
  60. package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
  61. package/lib/model/strategy/adapter/walletPass/example.js +207 -0
  62. package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
  63. package/lib/model/strategy/adapter/walletPass/index.js +142 -0
  64. package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  65. package/lib/model/strategy/adapter/walletPass/locales.js +54 -0
  66. package/lib/model/strategy/adapter/walletPass/type.d.ts +169 -0
  67. package/lib/model/strategy/adapter/walletPass/type.js +17 -0
  68. package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  69. package/lib/model/strategy/adapter/walletPass/utils.js +727 -0
  70. package/lib/model/strategy/index.d.ts +94 -0
  71. package/lib/model/strategy/index.js +413 -0
  72. package/lib/model/strategy/strategy-example.d.ts +5 -0
  73. package/lib/model/strategy/strategy-example.js +318 -0
  74. package/lib/model/strategy/type.d.ts +228 -0
  75. package/lib/model/strategy/type.js +44 -0
  76. package/lib/modules/Cart/types.d.ts +2 -0
  77. package/lib/modules/Cart/utils/cartProduct.js +9 -0
  78. package/lib/modules/Discount/types.d.ts +16 -0
  79. package/lib/modules/Payment/index.d.ts +2 -1
  80. package/lib/modules/Payment/index.js +1 -0
  81. package/lib/modules/Payment/utils.js +3 -0
  82. package/lib/modules/Payment/walletpass.d.ts +23 -0
  83. package/lib/modules/Payment/walletpass.js +94 -17
  84. package/lib/modules/Rules/index.d.ts +9 -5
  85. package/lib/modules/Rules/index.js +666 -258
  86. package/lib/modules/Rules/types.d.ts +1 -0
  87. package/lib/modules/Summary/types.d.ts +2 -0
  88. package/lib/modules/Summary/utils.d.ts +6 -0
  89. package/lib/modules/Summary/utils.js +15 -0
  90. package/lib/plugins/window.d.ts +2 -0
  91. package/lib/solution/BookingByStep/index.d.ts +1 -0
  92. package/lib/solution/BookingTicket/index.d.ts +1 -1
  93. package/lib/solution/Checkout/index.js +2 -0
  94. package/lib/solution/ShopDiscount/index.d.ts +1 -0
  95. package/lib/solution/ShopDiscount/index.js +22 -8
  96. package/lib/solution/ShopDiscount/types.d.ts +1 -0
  97. package/lib/solution/ShopDiscount/utils.js +10 -6
  98. 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,28 +812,30 @@ 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;
@@ -771,32 +872,63 @@ var RulesModule = class extends import_BaseModule.BaseModule {
771
872
  if (applicableDiscounts.length && product.booking_id && typeof selectedDiscount.isManualSelect === "undefined" && !(options == null ? void 0 : options.scan) && !isEditModeAddNewProduct) {
772
873
  return;
773
874
  }
774
- 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;
775
880
  const totalQuantity = product.quantity || product.num || 1;
776
- const availableGoodPassCount = applicableDiscounts.filter((item) => (item.tag || item.type) === "good_pass").length;
777
- 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;
778
900
  const arr = [];
779
901
  if (flatItem.type === "main") {
780
902
  if (splitCount < totalQuantity && isNeedSplit) {
781
903
  arr.push(this.hooks.setProduct(originProduct, {
782
904
  discount_list: this.filterDiscountListByType(product.discount_list, "promotion"),
783
905
  quantity: totalQuantity - splitCount,
784
- _id: product._id.split("___")[0]
906
+ _id: product._id.split("___")[0],
907
+ total: product.origin_total || product.total
785
908
  }));
786
909
  }
787
910
  for (let i = 0; i < splitCount; i++) {
788
- const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[i];
789
- usedDiscounts.set(selectedDiscount2.id, true);
790
- 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) || [];
791
923
  let productOriginTotal = product.origin_total || product.total || 0;
792
924
  if (this.filterDiscountListByType(product.discount_list, "promotion").length && product.origin_total) {
793
925
  productOriginTotal = product.origin_total;
794
926
  }
795
- 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) {
796
928
  productOriginTotal = product.total;
797
929
  }
798
- const isOrderLevel = (0, import_utils.isOrderLevelFixedAmountDiscount)(selectedDiscount2);
799
- 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;
800
932
  const productAllocation = orderLevelAllocation == null ? void 0 : orderLevelAllocation.get(flatItem._id);
801
933
  let targetProductTotal;
802
934
  let amount;
@@ -806,35 +938,38 @@ var RulesModule = class extends import_BaseModule.BaseModule {
806
938
  productDiscountDifference = productAllocation.difference;
807
939
  targetProductTotal = Math.max(new import_decimal.default(product.price).minus(amount).toNumber(), 0);
808
940
  } else {
809
- targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
941
+ targetProductTotal = (0, import_utils.getDiscountAmount)(currentSelectedDiscount, product.price, product.price);
810
942
  amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
811
943
  }
812
- const discountType = selectedDiscount2.tag || selectedDiscount2.type;
813
- 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";
814
947
  const discountDetail = {
815
- amount,
816
- type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : discountType,
948
+ amount: actualDiscountAmount,
949
+ type: currentDiscountType,
817
950
  discount: {
818
- discount_card_type: (_x = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _x.discount_card_type,
819
- fixed_amount: amount,
820
- discount_calculation_mode: (_y = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _y.discount_calculation_mode,
821
- resource_id: selectedDiscount2.id,
822
- 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,
823
956
  original_amount: product.price,
824
957
  product_id: originProduct.id,
825
- percent: selectedDiscount2.par_value
958
+ percent: currentSelectedDiscount.par_value,
959
+ discount_product_id: currentSelectedDiscount.product_id
826
960
  },
827
- // 前端使用的num数量,为了计算优惠金额
828
- _num: isGoodPass ? 1 : product.num,
961
+ // 前端使用的num数量,为了计算优惠金额(拆分时为1)
962
+ _num: isNeedSplit ? 1 : product.num,
963
+ config: currentSelectedDiscount == null ? void 0 : currentSelectedDiscount.config,
829
964
  metadata: {
830
965
  num: 1,
831
- 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,
832
967
  // 🔥 order_level 分摊差值
833
968
  ...productDiscountDifference !== void 0 && { product_discount_difference: productDiscountDifference }
834
969
  }
835
970
  };
836
971
  appliedProducts.push(discountDetail);
837
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
972
+ appliedDiscountProducts.set(currentSelectedDiscount.id, appliedProducts);
838
973
  let total = targetProductTotal;
839
974
  if (product.options) {
840
975
  total = product.options.reduce((accumulator, currentValue) => {
@@ -844,31 +979,35 @@ var RulesModule = class extends import_BaseModule.BaseModule {
844
979
  }, new import_decimal.default(total)).toNumber();
845
980
  }
846
981
  if (product.isClient) {
847
- arr.push(this.hooks.setProduct(originProduct, {
848
- discount_list: [discountDetail],
849
- price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
850
- quantity: isNeedSplit ? 1 : product.quantity,
851
- origin_total: (0, import_utils2.getProductOriginTotalPrice)({
852
- product: {
853
- original_price: product.original_price
854
- },
855
- bundle: product.bundle,
856
- options: product.options
857
- }),
858
- variant: originProduct._productInit.variant,
859
- original_price: new import_decimal.default(product.price || 0).toNumber(),
860
- total
861
- }));
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
+ );
862
999
  } else {
863
- arr.push(this.hooks.setProduct(originProduct, {
864
- discount_list: this.filterDiscountListByType(product.discount_list, "promotion").concat([discountDetail]),
865
- _id: product._id.split("___")[0] + "___" + selectedDiscount2.id + index,
866
- price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
867
- quantity: isNeedSplit ? 1 : product.quantity,
868
- total,
869
- origin_total: productOriginTotal,
870
- main_product_selling_price: targetProductTotal
871
- }));
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
+ );
872
1011
  }
873
1012
  }
874
1013
  processedProductsMap.set(product._id, arr);
@@ -878,62 +1017,298 @@ var RulesModule = class extends import_BaseModule.BaseModule {
878
1017
  const discountNum = splitCount;
879
1018
  const normalNum = totalQuantity - discountNum;
880
1019
  for (let i = 0; i < discountNum; i++) {
881
- const selectedDiscount2 = applicableDiscounts[i];
882
- 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
+ }
883
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;
884
1039
  const discountDetail = {
885
- amount: product.origin_total,
886
- type: "good_pass",
1040
+ amount: bundleDiscountAmount,
1041
+ type: bundleDiscountType,
887
1042
  discount: {
888
- fixed_amount: product.origin_total,
889
- discount_calculation_mode: (_z = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _z.discount_calculation_mode,
890
- resource_id: selectedDiscount2.id,
891
- 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,
892
1048
  original_amount: product.origin_total,
893
- product_id: product.id
1049
+ product_id: product.id,
1050
+ percent: currentBundleDiscount.par_value,
1051
+ discount_product_id: currentBundleDiscount.product_id
894
1052
  },
895
1053
  metadata: {
896
1054
  // 🔥 使用拆分后的唯一 _id
897
1055
  custom_product_bundle_map_id: uniqueId,
898
- 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,
899
1057
  num: 1
900
1058
  },
901
- _num: 1
1059
+ _num: 1,
1060
+ config: currentBundleDiscount == null ? void 0 : currentBundleDiscount.config
902
1061
  };
903
- const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
1062
+ const appliedProducts = appliedDiscountProducts.get(currentBundleDiscount.id) || [];
904
1063
  appliedProducts.push(discountDetail);
905
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
1064
+ appliedDiscountProducts.set(currentBundleDiscount.id, appliedProducts);
906
1065
  processedItems.push({
907
1066
  ...flatItem,
908
1067
  // 🔥 使用唯一的 _id
909
1068
  _id: uniqueId,
910
1069
  num: 1,
911
1070
  quantity: 1,
912
- price: 0,
913
- // 商品券价格为0
914
- total: 0,
1071
+ price: discountedPrice,
1072
+ // 折扣后的价格
1073
+ total: discountedPrice,
915
1074
  discount_list: this.filterDiscountListByType(flatItem.discount_list, "promotion").concat([discountDetail]),
916
1075
  processed: true,
917
- _discountId: selectedDiscount2.id
1076
+ _discountId: currentBundleDiscount.id
918
1077
  });
919
1078
  }
920
- if (normalNum > 0) {
921
- processedItems.push({
922
- ...flatItem,
923
- // 🔥 为剩余商品生成唯一的 _id
924
- _id: `${flatItem._id}_split_rest`,
925
- num: normalNum,
926
- quantity: normalNum,
927
- discount_list: this.filterDiscountListByType(flatItem.discount_list, "promotion"),
928
- 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;
929
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;
930
1295
  }
931
1296
  } else {
932
- const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[0];
933
- 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
+ }
934
1309
  const productOriginTotal = product.original_price || product.price || 0;
935
- const isOrderLevel = (0, import_utils.isOrderLevelFixedAmountDiscount)(selectedDiscount2);
936
- 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;
937
1312
  const productAllocation = orderLevelAllocation == null ? void 0 : orderLevelAllocation.get(flatItem._id);
938
1313
  let targetProductTotal;
939
1314
  let fixedAmountPerItem;
@@ -944,39 +1319,42 @@ var RulesModule = class extends import_BaseModule.BaseModule {
944
1319
  targetProductTotal = Math.max(new import_decimal.default(productOriginTotal).minus(fixedAmountPerItem).toNumber(), 0);
945
1320
  } else {
946
1321
  targetProductTotal = (0, import_utils.getDiscountAmount)(
947
- selectedDiscount2,
1322
+ currentBundleDiscount,
948
1323
  productOriginTotal,
949
1324
  productOriginTotal
950
1325
  );
951
1326
  fixedAmountPerItem = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
952
1327
  }
1328
+ const bundleActualDiscountAmount = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
953
1329
  const uniqueId = flatItem._id;
954
1330
  const discountDetail = {
955
- amount: fixedAmountPerItem * (product.num || 1),
956
- 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,
957
1333
  discount: {
958
- discount_card_type: (_A = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _A.discount_card_type,
959
- fixed_amount: fixedAmountPerItem,
960
- discount_calculation_mode: (_B = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _B.discount_calculation_mode,
961
- resource_id: selectedDiscount2.id,
962
- 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,
963
1339
  original_amount: product.original_price,
964
1340
  product_id: product.id,
965
- percent: selectedDiscount2.par_value
1341
+ percent: currentBundleDiscount.par_value,
1342
+ discount_product_id: currentBundleDiscount.product_id
966
1343
  },
967
1344
  metadata: {
968
1345
  // 🔥 使用唯一的 _id
969
1346
  custom_product_bundle_map_id: uniqueId,
970
- 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,
971
1348
  num: product.num || 1,
972
1349
  // 🔥 order_level 分摊差值
973
1350
  ...productDiscountDifference !== void 0 && { product_discount_difference: productDiscountDifference }
974
1351
  },
975
- _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)
976
1354
  };
977
- const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
1355
+ const appliedProducts = appliedDiscountProducts.get(currentBundleDiscount.id) || [];
978
1356
  appliedProducts.push(discountDetail);
979
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
1357
+ appliedDiscountProducts.set(currentBundleDiscount.id, appliedProducts);
980
1358
  processedItems.push({
981
1359
  ...flatItem,
982
1360
  total: targetProductTotal,
@@ -1061,80 +1439,96 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1061
1439
  const mainProduct = mainProductArr[0];
1062
1440
  const mainProductData = this.hooks.getProduct(mainProduct);
1063
1441
  const newBundleWithDiscount = [];
1064
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
1065
- const processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
1066
- if (processedItems.length > 1) {
1067
- processedItems.forEach((item) => {
1068
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1069
- var _a;
1070
- return {
1071
- ...discount,
1072
- metadata: {
1073
- ...discount.metadata,
1074
- num: item.num,
1075
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
1076
- }
1077
- // num: item.num, // 使用拆分后的 num
1078
- };
1079
- });
1080
- newBundleWithDiscount.push({
1081
- ...bundleItem,
1082
- _id: item._id,
1083
- product_id: bundleItem.product_id,
1084
- price: item.price,
1085
- num: item.num,
1086
- discount_list: updatedDiscountList2
1087
- });
1088
- });
1089
- } else {
1090
- const item = processedItems[0];
1091
- if (item.processed) {
1092
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1093
- var _a;
1094
- return {
1095
- ...discount,
1096
- metadata: {
1097
- ...discount.metadata,
1098
- num: item.num,
1099
- 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
+ };
1100
1461
  }
1101
- // num: item.num, // 使用当前的 num
1102
- };
1103
- });
1104
- newBundleWithDiscount.push({
1105
- ...bundleItem,
1106
- _id: item._id,
1107
- product_id: bundleItem.product_id,
1108
- price: item.price,
1109
- num: item.num,
1110
- 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
+ });
1111
1472
  });
1112
1473
  } else {
1113
- 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
+ }
1114
1502
  }
1115
1503
  }
1116
- });
1504
+ );
1117
1505
  let newTotalWithDiscount = Number(mainProductData.price || 0);
1118
- let newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
1119
- const updatedMainDiscountList = mainProductData.discount_list.map((discount) => {
1120
- var _a, _b;
1121
- if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
1122
- return discount;
1123
- }
1124
- return {
1125
- ...discount,
1126
- // num: 1,
1127
- metadata: {
1128
- ...discount.metadata,
1129
- custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id,
1130
- 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;
1131
1514
  }
1132
- };
1133
- });
1134
- const mainDiscountList = updatedMainDiscountList.filter((item) => {
1135
- var _a;
1136
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1137
- });
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
+ );
1138
1532
  if (mainDiscountList && mainDiscountList.length > 0) {
1139
1533
  const allDiscountAmount = (0, import_utils.getDiscountListAmountTotal)(mainDiscountList);
1140
1534
  newTotalWithDiscount = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalWithDiscount;
@@ -1164,20 +1558,24 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1164
1558
  quantity: 1,
1165
1559
  discount_list: updatedMainDiscountList,
1166
1560
  bundle: newBundleWithDiscount,
1167
- total: new import_decimal.default(newTotalWithDiscount).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber(),
1168
- origin_total: new import_decimal.default(newOriginTotalWithDiscount).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber()
1561
+ total: newTotalWithDiscount,
1562
+ origin_total: newOriginTotalWithDiscount
1169
1563
  })
1170
1564
  );
1171
1565
  if (mainProductQuantity > 1) {
1172
1566
  const newBundleOriginal = [];
1173
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
1174
- newBundleOriginal.push({
1175
- ...bundleItem,
1176
- discount_list: []
1177
- });
1178
- });
1567
+ (product.bundle || []).forEach(
1568
+ (bundleItem, bundleIndex) => {
1569
+ newBundleOriginal.push({
1570
+ ...bundleItem,
1571
+ discount_list: []
1572
+ });
1573
+ }
1574
+ );
1179
1575
  let newTotalOriginal = Number(mainProductData.price || 0);
1180
- let newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
1576
+ let newOriginTotalOriginal = Number(
1577
+ mainProductData.original_price || mainProductData.price || 0
1578
+ );
1181
1579
  const updatedMainDiscountListOriginal = mainProductData.discount_list.map((discount) => {
1182
1580
  var _a, _b;
1183
1581
  if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
@@ -1193,12 +1591,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1193
1591
  // num: mainProductQuantity - 1,
1194
1592
  };
1195
1593
  });
1196
- const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter((item) => {
1197
- var _a;
1198
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1199
- });
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
+ );
1200
1600
  if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
1201
- const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountListOriginal);
1601
+ const allDiscountAmount = (0, import_utils.getDiscountListAmount)(
1602
+ mainDiscountListOriginal
1603
+ );
1202
1604
  newTotalOriginal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalOriginal;
1203
1605
  newOriginTotalOriginal = mainProductData.origin_total ?? newOriginTotalOriginal;
1204
1606
  }
@@ -1235,59 +1637,67 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1235
1637
  const mainProductData = this.hooks.getProduct(mainProduct);
1236
1638
  const newBundle = [];
1237
1639
  if (product.bundle && Array.isArray(product.bundle)) {
1238
- product.bundle.forEach((bundleItem, bundleIndex) => {
1239
- const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
1240
- const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
1241
- if (!processedBundleItems || processedBundleItems.length === 0) {
1242
- newBundle.push(bundleItem);
1243
- } else {
1244
- processedBundleItems.forEach((item) => {
1245
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1246
- var _a2;
1247
- return {
1248
- ...discount,
1249
- metadata: {
1250
- ...discount.metadata,
1251
- num: item.num,
1252
- custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
1253
- }
1254
- // num: item.num, // 使用拆分后的 num
1255
- };
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
+ });
1256
1669
  });
1257
- newBundle.push({
1258
- ...bundleItem,
1259
- _id: item._id,
1260
- product_id: bundleItem.product_id,
1261
- price: item.price,
1262
- num: item.num,
1263
- discount_list: updatedDiscountList2
1264
- });
1265
- });
1670
+ }
1266
1671
  }
1267
- });
1672
+ );
1268
1673
  }
1269
1674
  let newTotal = Number(mainProductData.price || 0);
1270
- let newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
1271
- 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
+ )));
1272
1682
  if (isManualDiscount) {
1273
1683
  newTotal = mainProductData.total ?? newTotal;
1274
1684
  newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
1275
1685
  } else {
1276
- const mainDiscountList = mainProductData.discount_list.filter((item) => {
1277
- var _a2;
1278
- return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
1279
- });
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
+ );
1280
1692
  if (mainDiscountList && mainDiscountList.length > 0) {
1281
1693
  const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountList);
1282
1694
  newTotal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotal;
1283
1695
  newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
1284
1696
  }
1285
1697
  if (newBundle.length > 0) {
1286
- newBundle.forEach((item) => {
1287
- newTotal += Number(item.price) * Number(item.num);
1288
- });
1289
1698
  newBundle.forEach((item) => {
1290
1699
  var _a2, _b2, _c2;
1700
+ newTotal += Number(item.price) * Number(item.num);
1291
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;
1292
1702
  newOriginTotal += Number(originalPrice) * Number(item.num);
1293
1703
  });
@@ -1300,8 +1710,6 @@ var RulesModule = class extends import_BaseModule.BaseModule {
1300
1710
  return accumulator.add(currentPrice.mul(currentNum));
1301
1711
  }, new import_decimal.default(newTotal)).toNumber();
1302
1712
  }
1303
- newTotal = new import_decimal.default(newTotal).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
1304
- newOriginTotal = new import_decimal.default(newOriginTotal).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
1305
1713
  result.push(
1306
1714
  this.hooks.setProduct(mainProduct, {
1307
1715
  ...mainProductData,