@pisell/pisellos 0.0.396 → 0.0.397

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.
@@ -542,86 +542,178 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
542
542
  discountApplicableProducts.set(discount.id, []);
543
543
  });
544
544
 
545
- // 🔥 预处理 order_level 固定金额折扣卡的分摊信息
545
+ // 预处理 order_level 固定金额折扣卡的分摊信息
546
546
  // Map<discountId, Map<productId, { discountAmount, difference }>>
547
547
  var orderLevelDiscountAllocations = new Map();
548
548
 
549
- // 识别 order_level 折扣卡并预计算分摊
549
+ // 第一步:收集所有被勾选的 order_level 折扣卡及其适用商品
550
+ // Map<discountId, Set<flatItemId>> - 记录每个折扣卡适用的商品
551
+ var orderLevelDiscountApplicableItems = new Map();
552
+ // Map<flatItemId, Set<discountId>> - 记录每个商品可以被哪些折扣卡使用
553
+ var itemApplicableDiscounts = new Map();
554
+
555
+ // 辅助函数:检查商品是否对某个折扣卡可用
556
+ var checkItemApplicableForDiscount = function checkItemApplicableForDiscount(flatItem, discount) {
557
+ var _product, _product2, _product3, _flatItem$bundleItem, _flatItem$bundleItem2, _product4;
558
+ var product;
559
+ if (flatItem.type === 'main') {
560
+ product = flatItem.product;
561
+ } else {
562
+ var _flatItem$parentProdu;
563
+ product = {
564
+ _id: flatItem._id,
565
+ id: flatItem.id,
566
+ price: flatItem.price,
567
+ quantity: flatItem.quantity,
568
+ num: flatItem.num,
569
+ booking_id: flatItem.booking_id,
570
+ discount_list: flatItem.discount_list || [],
571
+ startDate: (_flatItem$parentProdu = flatItem.parentProduct) === null || _flatItem$parentProdu === void 0 ? void 0 : _flatItem$parentProdu.startDate
572
+ };
573
+ }
574
+
575
+ // 编辑的商品使用了优惠券不可用
576
+ var isAvailableProduct = flatItem.type === 'main' ? !((_product = product) !== null && _product !== void 0 && _product.booking_id && (_product2 = product) !== null && _product2 !== void 0 && (_product2 = _product2.discount_list) !== null && _product2 !== void 0 && _product2.length && (_product3 = product) !== null && _product3 !== void 0 && (_product3 = _product3.discount_list) !== null && _product3 !== void 0 && _product3.every(function (d) {
577
+ return d.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(d.tag || d.type);
578
+ })) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && !!(flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem = flatItem.bundleItem) !== null && _flatItem$bundleItem !== void 0 && (_flatItem$bundleItem = _flatItem$bundleItem.discount_list) !== null && _flatItem$bundleItem !== void 0 && _flatItem$bundleItem.length) && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem2 = flatItem.bundleItem) !== null && _flatItem$bundleItem2 !== void 0 && (_flatItem$bundleItem2 = _flatItem$bundleItem2.discount_list) !== null && _flatItem$bundleItem2 !== void 0 && _flatItem$bundleItem2.every(function (d) {
579
+ return d.id;
580
+ }));
581
+ if (!isAvailableProduct) {
582
+ return false;
583
+ }
584
+
585
+ // vouchersApplicable 为 false 的商品不参与订单级别折扣均摊
586
+ if (isBoolean(product.vouchersApplicable) && !product.vouchersApplicable) {
587
+ return false;
588
+ }
589
+
590
+ // 商品价格为0时不可用
591
+ if (Number(product.price) <= 0 || !product.price) {
592
+ return false;
593
+ }
594
+ var limitedData = discount.limited_relation_product_data;
595
+
596
+ // 时间限制检查
597
+ var timeLimit = !!filterDiscountListByBookingTime([discount], (((_product4 = product) === null || _product4 === void 0 ? void 0 : _product4.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
598
+ if (!timeLimit) {
599
+ return false;
600
+ }
601
+
602
+ // 判断商品是否适用
603
+ var isLimitedProduct = false;
604
+ if (limitedData.type === 'product_all') {
605
+ var _limitedData$exclude_;
606
+ // 全品类,但需要检查是否被排除
607
+ if (limitedData.filter === 1 && (_limitedData$exclude_ = limitedData.exclude_product_ids) !== null && _limitedData$exclude_ !== void 0 && _limitedData$exclude_.includes(product.id)) {
608
+ isLimitedProduct = false;
609
+ } else {
610
+ isLimitedProduct = true;
611
+ }
612
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
613
+ isLimitedProduct = true;
614
+ }
615
+
616
+ // 套餐规则检查
617
+ var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
618
+ return isLimitedProduct && isBundleAvailable;
619
+ };
620
+
621
+ // 收集所有被勾选的 order_level 折扣卡及其适用商品
622
+ var selectedOrderLevelDiscounts = sortedDiscountList.filter(function (discount) {
623
+ return isOrderLevelFixedAmountDiscount(discount) && discount.isSelected !== false;
624
+ });
625
+
626
+ // 遍历所有被勾选的 order_level 折扣卡,建立双向映射
627
+ selectedOrderLevelDiscounts.forEach(function (discount) {
628
+ var applicableItemIds = new Set();
629
+ sortedFlattenedList.forEach(function (flatItem) {
630
+ if (checkItemApplicableForDiscount(flatItem, discount)) {
631
+ applicableItemIds.add(flatItem._id);
632
+
633
+ // 记录商品可以被哪些折扣卡使用
634
+ if (!itemApplicableDiscounts.has(flatItem._id)) {
635
+ itemApplicableDiscounts.set(flatItem._id, new Set());
636
+ }
637
+ itemApplicableDiscounts.get(flatItem._id).add(discount.id);
638
+ }
639
+ });
640
+ orderLevelDiscountApplicableItems.set(discount.id, applicableItemIds);
641
+ });
642
+
643
+ // 专属折扣卡定义:限定了特定商品(product_ids),而不是全品类(product_all)的折扣卡
644
+ // 如果专属折扣卡被勾选,它适用的商品会被占用,其他折扣卡均摊时应排除这些商品
645
+ // Map<flatItemId, discountId> - 记录被占用的商品及其对应的折扣卡
646
+ var occupiedItems = new Map();
647
+
648
+ // 按排序顺序遍历折扣卡,判断哪些是专属折扣卡
649
+ selectedOrderLevelDiscounts.forEach(function (discount) {
650
+ var limitedData = discount.limited_relation_product_data;
651
+
652
+ // 判断是否是专属折扣卡(只能用于特定商品,而不是全品类)
653
+ var isExclusiveDiscount = limitedData.type !== 'product_all';
654
+ if (isExclusiveDiscount) {
655
+ // 专属折扣卡:它适用的所有商品都会被它占用
656
+ var applicableItems = orderLevelDiscountApplicableItems.get(discount.id);
657
+ if (applicableItems) {
658
+ applicableItems.forEach(function (itemId) {
659
+ // 如果商品还没被占用,则标记为被当前折扣卡占用
660
+ if (!occupiedItems.has(itemId)) {
661
+ occupiedItems.set(itemId, discount.id);
662
+ }
663
+ });
664
+ }
665
+ }
666
+ });
667
+ console.log('occupiedItems', occupiedItems, 'orderLevelDiscountApplicableItems', orderLevelDiscountApplicableItems);
668
+
669
+ // 🔥 第三步:识别 order_level 折扣卡并预计算分摊(排除被其他专属折扣卡占用的商品)
550
670
  sortedDiscountList.forEach(function (discount) {
551
671
  if (!isOrderLevelFixedAmountDiscount(discount)) {
552
672
  return;
553
673
  }
554
674
 
555
- // 收集该折扣卡适用的商品
675
+ // 收集该折扣卡适用的商品(排除被其他专属折扣卡占用的商品)
556
676
  var applicableProducts = [];
557
677
  sortedFlattenedList.forEach(function (flatItem) {
558
- var _product, _product2, _product3, _flatItem$bundleItem, _product4;
678
+ var _flatItem$parentProdu2;
679
+ // 🔥 检查该商品是否被其他专属折扣卡占用
680
+ var occupyingDiscountId = occupiedItems.get(flatItem._id);
681
+ if (occupyingDiscountId !== undefined && occupyingDiscountId !== discount.id) {
682
+ // 该商品被其他专属折扣卡占用,跳过
683
+ return;
684
+ }
685
+ if (!checkItemApplicableForDiscount(flatItem, discount)) {
686
+ return;
687
+ }
559
688
  var product;
560
689
  if (flatItem.type === 'main') {
561
690
  product = flatItem.product;
562
691
  } else {
563
- var _flatItem$parentProdu;
564
692
  product = {
565
693
  _id: flatItem._id,
566
694
  id: flatItem.id,
567
695
  price: flatItem.price,
568
696
  quantity: flatItem.quantity,
569
- num: flatItem.num,
570
- booking_id: flatItem.booking_id,
571
- discount_list: flatItem.discount_list || [],
572
- startDate: (_flatItem$parentProdu = flatItem.parentProduct) === null || _flatItem$parentProdu === void 0 ? void 0 : _flatItem$parentProdu.startDate
697
+ num: flatItem.num
573
698
  };
574
699
  }
575
700
 
576
- // 编辑的商品使用了优惠券不可用
577
- var isAvailableProduct = flatItem.type === 'main' ? !((_product = product) !== null && _product !== void 0 && _product.booking_id && (_product2 = product) !== null && _product2 !== void 0 && (_product2 = _product2.discount_list) !== null && _product2 !== void 0 && _product2.length && (_product3 = product) !== null && _product3 !== void 0 && (_product3 = _product3.discount_list) !== null && _product3 !== void 0 && _product3.every(function (discount) {
578
- return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
579
- })) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem = flatItem.bundleItem) !== null && _flatItem$bundleItem !== void 0 && (_flatItem$bundleItem = _flatItem$bundleItem.metadata) !== null && _flatItem$bundleItem !== void 0 && _flatItem$bundleItem.custom_product_bundle_map_id);
580
- if (!isAvailableProduct) {
581
- return;
582
- }
583
-
584
- // 🔥 vouchersApplicable 为 false 的商品不参与订单级别折扣均摊
585
- if (isBoolean(product.vouchersApplicable) && !product.vouchersApplicable) {
586
- return;
587
- }
588
-
589
- // 商品价格为0时不可用
590
- if (Number(product.price) <= 0 || !product.price) {
591
- return;
592
- }
593
- var limitedData = discount.limited_relation_product_data;
594
-
595
- // 时间限制检查
596
- var timeLimit = !!filterDiscountListByBookingTime([discount], (((_product4 = product) === null || _product4 === void 0 ? void 0 : _product4.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
597
- if (!timeLimit) {
598
- return;
599
- }
600
-
601
- // 判断商品是否适用
602
- var isLimitedProduct = limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id);
701
+ // 对于 bundle 子商品,quantity 需要乘以主商品的购买数量
702
+ var quantity = flatItem.type === 'main' ? product.quantity || 1 : (product.num || 1) * (((_flatItem$parentProdu2 = flatItem.parentProduct) === null || _flatItem$parentProdu2 === void 0 ? void 0 : _flatItem$parentProdu2.quantity) || 1);
603
703
 
604
- // 套餐规则检查
605
- var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
606
- if (isLimitedProduct && isBundleAvailable) {
607
- var _flatItem$parentProdu2;
608
- // 🔥 对于 bundle 子商品,quantity 需要乘以主商品的购买数量
609
- var quantity = flatItem.type === 'main' ? product.quantity || 1 : (product.num || 1) * (((_flatItem$parentProdu2 = flatItem.parentProduct) === null || _flatItem$parentProdu2 === void 0 ? void 0 : _flatItem$parentProdu2.quantity) || 1);
610
-
611
- // 🔥 传递 parentQuantity 用于差值处理时判断是否是真正的"数量为1"
612
- var productData = {
613
- productId: flatItem._id,
614
- amount: Number(product.price || 0),
615
- quantity: quantity
616
- };
704
+ // 传递 parentQuantity 用于差值处理时判断是否是真正的"数量为1"
705
+ var productData = {
706
+ productId: flatItem._id,
707
+ amount: Number(product.price || 0),
708
+ quantity: quantity
709
+ };
617
710
 
618
- // 子商品需要传递主商品数量
619
- if (flatItem.type === 'bundle') {
620
- var _flatItem$parentProdu3;
621
- productData.parentQuantity = ((_flatItem$parentProdu3 = flatItem.parentProduct) === null || _flatItem$parentProdu3 === void 0 ? void 0 : _flatItem$parentProdu3.quantity) || 1;
622
- }
623
- applicableProducts.push(productData);
711
+ // 子商品需要传递主商品数量
712
+ if (flatItem.type === 'bundle') {
713
+ var _flatItem$parentProdu3;
714
+ productData.parentQuantity = ((_flatItem$parentProdu3 = flatItem.parentProduct) === null || _flatItem$parentProdu3 === void 0 ? void 0 : _flatItem$parentProdu3.quantity) || 1;
624
715
  }
716
+ applicableProducts.push(productData);
625
717
  });
626
718
 
627
719
  // 计算分摊
@@ -663,7 +755,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
663
755
  originProduct = flatItem.originProduct;
664
756
  }
665
757
  addModeDiscount.forEach(function (discount) {
666
- var _product5, _product6, _product7, _product8, _flatItem$bundleItem2, _discount$config;
758
+ var _product5, _product6, _product7, _product8, _flatItem$bundleItem3, _flatItem$bundleItem4, _discount$config;
667
759
  var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
668
760
  // 拿到discount配置的holder信息 product信息 product.holder 加在 isLimitedProduct
669
761
  var _tempVar = (flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'bundle' ? flatItem === null || flatItem === void 0 ? void 0 : flatItem.parentProduct : flatItem === null || flatItem === void 0 ? void 0 : flatItem.product;
@@ -679,7 +771,9 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
679
771
  // 编辑的商品 使用了优惠券不可用
680
772
  var isAvailableProduct = flatItem.type === 'main' ? !((_product6 = product) !== null && _product6 !== void 0 && _product6.booking_id && (_product7 = product) !== null && _product7 !== void 0 && (_product7 = _product7.discount_list) !== null && _product7 !== void 0 && _product7.length && (_product8 = product) !== null && _product8 !== void 0 && (_product8 = _product8.discount_list) !== null && _product8 !== void 0 && _product8.every(function (discount) {
681
773
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
682
- })) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem2 = flatItem.bundleItem) !== null && _flatItem$bundleItem2 !== void 0 && (_flatItem$bundleItem2 = _flatItem$bundleItem2.metadata) !== null && _flatItem$bundleItem2 !== void 0 && _flatItem$bundleItem2.custom_product_bundle_map_id);
774
+ })) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && !!(flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem3 = flatItem.bundleItem) !== null && _flatItem$bundleItem3 !== void 0 && (_flatItem$bundleItem3 = _flatItem$bundleItem3.discount_list) !== null && _flatItem$bundleItem3 !== void 0 && _flatItem$bundleItem3.length) && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem4 = flatItem.bundleItem) !== null && _flatItem$bundleItem4 !== void 0 && (_flatItem$bundleItem4 = _flatItem$bundleItem4.discount_list) !== null && _flatItem$bundleItem4 !== void 0 && _flatItem$bundleItem4.every(function (discount) {
775
+ return discount.id;
776
+ }));
683
777
 
684
778
  // 套餐是否可用判断
685
779
  var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
@@ -735,7 +829,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
735
829
  product = flatItem.product;
736
830
  originProduct = flatItem.originProduct;
737
831
  } else {
738
- var _flatItem$parentProdu5, _flatItem$bundleItem3, _flatItem$bundleItem4, _flatItem$bundleItem5;
832
+ var _flatItem$parentProdu5, _flatItem$bundleItem5, _flatItem$bundleItem6, _flatItem$bundleItem7;
739
833
  // bundle子商品
740
834
  product = {
741
835
  startDate: flatItem === null || flatItem === void 0 || (_flatItem$parentProdu5 = flatItem.parentProduct) === null || _flatItem$parentProdu5 === void 0 ? void 0 : _flatItem$parentProdu5.startDate,
@@ -745,10 +839,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
745
839
  quantity: flatItem.quantity,
746
840
  num: flatItem.num,
747
841
  total: flatItem.total,
748
- original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem3 = flatItem.bundleItem) === null || _flatItem$bundleItem3 === void 0 ? void 0 : _flatItem$bundleItem3.original_price,
749
- origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem4 = flatItem.bundleItem) === null || _flatItem$bundleItem4 === void 0 ? void 0 : _flatItem$bundleItem4.original_price,
842
+ original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.original_price,
843
+ origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.original_price,
750
844
  booking_id: flatItem.booking_id,
751
- discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.discount_list) || []
845
+ discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.discount_list) || []
752
846
  };
753
847
  originProduct = flatItem.originProduct;
754
848
  }
@@ -1758,10 +1852,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1758
1852
  return true; // 单独购买时可用
1759
1853
  }
1760
1854
  if (isBundleItem) {
1761
- var _flatItem$bundleItem6, _flatItem$bundleItem7;
1855
+ var _flatItem$bundleItem8, _flatItem$bundleItem9;
1762
1856
  // 套餐中购买时,判断是否为原价
1763
- var priceType = (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.price_type;
1764
- var priceTypeExt = (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.price_type_ext;
1857
+ var priceType = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type;
1858
+ var priceTypeExt = (_flatItem$bundleItem9 = flatItem.bundleItem) === null || _flatItem$bundleItem9 === void 0 ? void 0 : _flatItem$bundleItem9.price_type_ext;
1765
1859
  // original_price 对应:
1766
1860
  // 1. price_type: "markup" && price_type_ext: "product_price"
1767
1861
  // markup_price 对应:
@@ -1799,10 +1893,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1799
1893
  return false; // 单独购买时不可用
1800
1894
  }
1801
1895
  if (isBundleItem) {
1802
- var _flatItem$bundleItem8, _flatItem$bundleItem9;
1896
+ var _flatItem$bundleItem10, _flatItem$bundleItem11;
1803
1897
  // 套餐中购买时,判断是否为原价
1804
- var _priceType = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type;
1805
- var _priceTypeExt = (_flatItem$bundleItem9 = flatItem.bundleItem) === null || _flatItem$bundleItem9 === void 0 ? void 0 : _flatItem$bundleItem9.price_type_ext;
1898
+ var _priceType = (_flatItem$bundleItem10 = flatItem.bundleItem) === null || _flatItem$bundleItem10 === void 0 ? void 0 : _flatItem$bundleItem10.price_type;
1899
+ var _priceTypeExt = (_flatItem$bundleItem11 = flatItem.bundleItem) === null || _flatItem$bundleItem11 === void 0 ? void 0 : _flatItem$bundleItem11.price_type_ext;
1806
1900
 
1807
1901
  // original_price 对应:
1808
1902
  // 1. price_type: "markup" && price_type_ext: "product_price"
@@ -381,13 +381,99 @@ var RulesModule = class extends import_BaseModule.BaseModule {
381
381
  discountApplicableProducts.set(discount.id, []);
382
382
  });
383
383
  const orderLevelDiscountAllocations = /* @__PURE__ */ new Map();
384
+ const orderLevelDiscountApplicableItems = /* @__PURE__ */ new Map();
385
+ const itemApplicableDiscounts = /* @__PURE__ */ new Map();
386
+ const checkItemApplicableForDiscount = (flatItem, discount) => {
387
+ var _a, _b, _c, _d, _e, _f, _g, _h;
388
+ let product;
389
+ if (flatItem.type === "main") {
390
+ product = flatItem.product;
391
+ } else {
392
+ product = {
393
+ _id: flatItem._id,
394
+ id: flatItem.id,
395
+ price: flatItem.price,
396
+ quantity: flatItem.quantity,
397
+ num: flatItem.num,
398
+ booking_id: flatItem.booking_id,
399
+ discount_list: flatItem.discount_list || [],
400
+ startDate: (_a = flatItem.parentProduct) == null ? void 0 : _a.startDate
401
+ };
402
+ }
403
+ const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.length) && ((_c = product == null ? void 0 : product.discount_list) == null ? void 0 : _c.every((d) => d.id && ["good_pass", "discount_card", "product_discount_card"].includes(d.tag || d.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && !!((_e = (_d = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _d.discount_list) == null ? void 0 : _e.length) && ((_g = (_f = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _f.discount_list) == null ? void 0 : _g.every((d) => d.id)));
404
+ if (!isAvailableProduct) {
405
+ return false;
406
+ }
407
+ if ((0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
408
+ return false;
409
+ }
410
+ if (Number(product.price) <= 0 || !product.price) {
411
+ return false;
412
+ }
413
+ const limitedData = discount.limited_relation_product_data;
414
+ const timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
415
+ if (!timeLimit) {
416
+ return false;
417
+ }
418
+ let isLimitedProduct = false;
419
+ if (limitedData.type === "product_all") {
420
+ if (limitedData.filter === 1 && ((_h = limitedData.exclude_product_ids) == null ? void 0 : _h.includes(product.id))) {
421
+ isLimitedProduct = false;
422
+ } else {
423
+ isLimitedProduct = true;
424
+ }
425
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
426
+ isLimitedProduct = true;
427
+ }
428
+ const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
429
+ return isLimitedProduct && isBundleAvailable;
430
+ };
431
+ const selectedOrderLevelDiscounts = sortedDiscountList.filter((discount) => {
432
+ return (0, import_utils.isOrderLevelFixedAmountDiscount)(discount) && discount.isSelected !== false;
433
+ });
434
+ selectedOrderLevelDiscounts.forEach((discount) => {
435
+ const applicableItemIds = /* @__PURE__ */ new Set();
436
+ sortedFlattenedList.forEach((flatItem) => {
437
+ if (checkItemApplicableForDiscount(flatItem, discount)) {
438
+ applicableItemIds.add(flatItem._id);
439
+ if (!itemApplicableDiscounts.has(flatItem._id)) {
440
+ itemApplicableDiscounts.set(flatItem._id, /* @__PURE__ */ new Set());
441
+ }
442
+ itemApplicableDiscounts.get(flatItem._id).add(discount.id);
443
+ }
444
+ });
445
+ orderLevelDiscountApplicableItems.set(discount.id, applicableItemIds);
446
+ });
447
+ const occupiedItems = /* @__PURE__ */ new Map();
448
+ selectedOrderLevelDiscounts.forEach((discount) => {
449
+ const limitedData = discount.limited_relation_product_data;
450
+ const isExclusiveDiscount = limitedData.type !== "product_all";
451
+ if (isExclusiveDiscount) {
452
+ const applicableItems = orderLevelDiscountApplicableItems.get(discount.id);
453
+ if (applicableItems) {
454
+ applicableItems.forEach((itemId) => {
455
+ if (!occupiedItems.has(itemId)) {
456
+ occupiedItems.set(itemId, discount.id);
457
+ }
458
+ });
459
+ }
460
+ }
461
+ });
462
+ console.log("occupiedItems", occupiedItems, "orderLevelDiscountApplicableItems", orderLevelDiscountApplicableItems);
384
463
  sortedDiscountList.forEach((discount) => {
385
464
  if (!(0, import_utils.isOrderLevelFixedAmountDiscount)(discount)) {
386
465
  return;
387
466
  }
388
467
  const applicableProducts = [];
389
468
  sortedFlattenedList.forEach((flatItem) => {
390
- var _a, _b, _c, _d, _e, _f, _g;
469
+ var _a, _b;
470
+ const occupyingDiscountId = occupiedItems.get(flatItem._id);
471
+ if (occupyingDiscountId !== void 0 && occupyingDiscountId !== discount.id) {
472
+ return;
473
+ }
474
+ if (!checkItemApplicableForDiscount(flatItem, discount)) {
475
+ return;
476
+ }
391
477
  let product;
392
478
  if (flatItem.type === "main") {
393
479
  product = flatItem.product;
@@ -397,41 +483,19 @@ var RulesModule = class extends import_BaseModule.BaseModule {
397
483
  id: flatItem.id,
398
484
  price: flatItem.price,
399
485
  quantity: flatItem.quantity,
400
- num: flatItem.num,
401
- booking_id: flatItem.booking_id,
402
- discount_list: flatItem.discount_list || [],
403
- startDate: (_a = flatItem.parentProduct) == null ? void 0 : _a.startDate
486
+ num: flatItem.num
404
487
  };
405
488
  }
406
- const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.length) && ((_c = product == null ? void 0 : product.discount_list) == null ? void 0 : _c.every((discount2) => discount2.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount2.tag || discount2.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_e = (_d = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _d.metadata) == null ? void 0 : _e.custom_product_bundle_map_id));
407
- if (!isAvailableProduct) {
408
- return;
409
- }
410
- if ((0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
411
- return;
412
- }
413
- if (Number(product.price) <= 0 || !product.price) {
414
- return;
415
- }
416
- const limitedData = discount.limited_relation_product_data;
417
- const timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
418
- if (!timeLimit) {
419
- return;
420
- }
421
- const isLimitedProduct = limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id);
422
- const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
423
- if (isLimitedProduct && isBundleAvailable) {
424
- const quantity = flatItem.type === "main" ? product.quantity || 1 : (product.num || 1) * (((_f = flatItem.parentProduct) == null ? void 0 : _f.quantity) || 1);
425
- const productData = {
426
- productId: flatItem._id,
427
- amount: Number(product.price || 0),
428
- quantity
429
- };
430
- if (flatItem.type === "bundle") {
431
- productData.parentQuantity = ((_g = flatItem.parentProduct) == null ? void 0 : _g.quantity) || 1;
432
- }
433
- applicableProducts.push(productData);
489
+ const quantity = flatItem.type === "main" ? product.quantity || 1 : (product.num || 1) * (((_a = flatItem.parentProduct) == null ? void 0 : _a.quantity) || 1);
490
+ const productData = {
491
+ productId: flatItem._id,
492
+ amount: Number(product.price || 0),
493
+ quantity
494
+ };
495
+ if (flatItem.type === "bundle") {
496
+ productData.parentQuantity = ((_b = flatItem.parentProduct) == null ? void 0 : _b.quantity) || 1;
434
497
  }
498
+ applicableProducts.push(productData);
435
499
  });
436
500
  if (applicableProducts.length > 0) {
437
501
  const allocation = (0, import_utils.calculateOrderLevelDiscountAllocation)(discount, applicableProducts);
@@ -462,7 +526,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
462
526
  originProduct = flatItem.originProduct;
463
527
  }
464
528
  addModeDiscount.forEach((discount) => {
465
- var _a2, _b, _c, _d, _e, _f, _g, _h;
529
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
466
530
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
467
531
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
468
532
  const isHolderMatch = this.checkHolderMatch(
@@ -482,10 +546,10 @@ var RulesModule = class extends import_BaseModule.BaseModule {
482
546
  "discount_card",
483
547
  "product_discount_card"
484
548
  ].includes(discount2.tag || discount2.type)
485
- ))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
549
+ ))) : !((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)));
486
550
  const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
487
- if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && ((_e = discount.config) == null ? void 0 : _e.isAvailable)) {
488
- (_f = discountApplicability.get(discount.id)) == null ? void 0 : _f.push(product.id);
551
+ if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && ((_g = discount.config) == null ? void 0 : _g.isAvailable)) {
552
+ (_h = discountApplicability.get(discount.id)) == null ? void 0 : _h.push(product.id);
489
553
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
490
554
  const discountType = discount.tag || discount.type;
491
555
  const isGoodPass = discountType === "good_pass";
@@ -495,9 +559,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
495
559
  type: discountType,
496
560
  tag: discountType,
497
561
  discount: {
498
- discount_card_type: (_g = discount == null ? void 0 : discount.metadata) == null ? void 0 : _g.discount_card_type,
562
+ discount_card_type: (_i = discount == null ? void 0 : discount.metadata) == null ? void 0 : _i.discount_card_type,
499
563
  fixed_amount: product.price,
500
- discount_calculation_mode: (_h = discount == null ? void 0 : discount.metadata) == null ? void 0 : _h.discount_calculation_mode,
564
+ discount_calculation_mode: (_j = discount == null ? void 0 : discount.metadata) == null ? void 0 : _j.discount_calculation_mode,
501
565
  resource_id: discount.id,
502
566
  title: discount.format_title,
503
567
  original_amount: product.price || product.origin_total,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.396",
4
+ "version": "0.0.397",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",