@pisell/pisellos 2.2.101 → 2.2.103

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.
@@ -0,0 +1,9 @@
1
+ // 导出评估器
2
+ export { PromotionEvaluator } from "./evaluator";
3
+
4
+ // 导出适配器
5
+ export { PromotionAdapter } from "./adapter";
6
+ export { default } from "./adapter";
7
+
8
+ // 导出策略配置示例常量
9
+ export { X_ITEMS_FOR_Y_PRICE_STRATEGY, BUY_X_GET_Y_FREE_STRATEGY } from "./examples";
@@ -20,7 +20,8 @@ var defaultStrategyMetadataCustom = {
20
20
  allowCrossProduct: true,
21
21
  applicableProductLimit: 0,
22
22
  deductTaxAndFee: true,
23
- maxPassesPerItem: 0
23
+ maxPassesPerItem: 0,
24
+ deductOptionPrice: false
24
25
  };
25
26
 
26
27
  /**
@@ -41,8 +41,10 @@ export interface Voucher {
41
41
  allowCrossProduct: boolean;
42
42
  /** 可用商品数量上限 (仅多商品) 默认为0,表示不限制商品数量。 */
43
43
  applicableProductLimit: number;
44
- /** 单商品可用卡券上限(同一 Wallet Pass 商品生成的卡券对同一商品的每个 unit 最多抵扣次数,总上限 = maxPassesPerItem × quantity)。默认为 0,表示不限制。 */
44
+ /** 单订单行每单位可用同一 Wallet Pass 券次数;该行总券次上限 = maxPassesPerItem × 该行 quantity(按行唯一键区分)。0 表示不限制。 */
45
45
  maxPassesPerItem: number;
46
+ /** 是否抵扣单规格价格 (Option Price),默认 false。开启后折扣范围包含 option 的 add_price */
47
+ deductOptionPrice: boolean;
46
48
  };
47
49
  }
48
50
  /**
@@ -50,6 +52,10 @@ export interface Voucher {
50
52
  */
51
53
  export interface Product {
52
54
  product_id: number;
55
+ /** 订单明细 id,参与行唯一键兜底解析 */
56
+ id?: number;
57
+ /** 行级唯一串,参与行唯一键解析 */
58
+ product_unique_string?: string;
53
59
  price: number;
54
60
  quantity: number;
55
61
  name?: string;
@@ -58,9 +64,19 @@ export interface Product {
58
64
  main_product_original_price?: number;
59
65
  /** 主商品折扣后金额,不包含套餐子商品 */
60
66
  main_product_selling_price?: number;
67
+ /** 单规格(Option)列表 */
68
+ product_options?: {
69
+ id: number;
70
+ add_price: number | string;
71
+ num: number;
72
+ [key: string]: any;
73
+ }[];
61
74
  /** 主商品税费 */
62
75
  tax_fee: number;
63
76
  metadata: {
77
+ /** 行唯一标识,优先用于 Wallet Pass 按行配额 */
78
+ product_unique?: string;
79
+ unique_identification_number?: string;
64
80
  main_product_attached_bundle_tax_fee?: number;
65
81
  main_product_attached_bundle_surcharge_fee?: number;
66
82
  surcharge_rounding_remainder?: number;
@@ -129,8 +145,10 @@ export interface EvaluatorInput {
129
145
  allowCrossProduct: boolean;
130
146
  /** 可用商品数量上限 (仅多商品) 默认为0,表示不限制商品数量。 */
131
147
  applicableProductLimit: number;
132
- /** 单商品可用卡券上限(同一 Wallet Pass 商品生成的卡券对同一商品的每个 unit 最多抵扣次数,总上限 = maxPassesPerItem × quantity)。默认为 0,表示不限制。 */
148
+ /** 单订单行每单位可用同一 Wallet Pass 券次数;行总上限 = maxPassesPerItem × 该行 quantity(按行唯一键计)。0 表示不限制。 */
133
149
  maxPassesPerItem: number;
150
+ /** 是否抵扣单规格价格 (Option Price),默认 false */
151
+ deductOptionPrice: boolean;
134
152
  }>[];
135
153
  }
136
154
  /**
@@ -1,4 +1,10 @@
1
1
  import { Product, Voucher } from './type';
2
+ /** 订单商品数量 */
3
+ export declare const getProductQuantity: (product: any) => any;
4
+ /**
5
+ * 订单商品行唯一键,用于 maxPassesPerItem 按行、按件配额(同 SPU 不同规格视为不同行)。
6
+ */
7
+ export declare function resolveWalletPassLineKey(product: any, indexInOrder: number): string;
2
8
  export declare const getApplicableProductIds: (voucher: Voucher) => number[] | null;
3
9
  /**
4
10
  * 优惠券处理函数
@@ -24,26 +30,20 @@ export declare function recalculateVouchers(allVouchers: any[], selectedVouchers
24
30
  selectedWithDetails: any[];
25
31
  };
26
32
  /**
27
- * 获取主商品价格
33
+ * 获取主商品价格(单价,不含舍入余数)
28
34
  * @param product 商品
29
35
  * @param isDeductTaxAndFee 是否抵扣税费与附加费
30
- * @returns 商品价格
36
+ * @returns 商品单价
31
37
  */
32
38
  export declare const getMainProductPrice: (product: Product, isDeductTaxAndFee: boolean) => number;
33
39
  /**
34
- * 获取套餐子商品价格
40
+ * 获取套餐子商品价格(不含舍入余数)
35
41
  * @param bundleItem 套餐子商品
36
42
  * @param parentQuantity 父商品数量
37
43
  * @param isDeductTaxAndFee 是否抵扣税费与附加费
38
- * @returns 子商品总价格
44
+ * @returns 子商品总价格(不含舍入余数)
39
45
  */
40
46
  export declare const getBundleItemPrice: (bundleItem: any, parentQuantity: number, isDeductTaxAndFee: boolean) => number;
41
- /**
42
- * 获取商品数量
43
- * @param product 商品
44
- * @returns 商品数量
45
- */
46
- export declare const getProductQuantity: (product: any) => any;
47
47
  export declare const getBundleItemIsOriginalPrice: (item: any) => boolean;
48
48
  export declare const getBundleItemIsMarkupPrice: (item: any) => boolean;
49
49
  export declare const getBundleItemIsDiscountPrice: (item: any) => boolean;
@@ -12,6 +12,63 @@ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symb
12
12
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
13
13
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
14
14
  import Decimal from 'decimal.js';
15
+ /** 订单商品数量 */
16
+ export var getProductQuantity = function getProductQuantity(product) {
17
+ return product.quantity || product.product_quantity || 1;
18
+ };
19
+
20
+ /**
21
+ * 订单商品行唯一键,用于 maxPassesPerItem 按行、按件配额(同 SPU 不同规格视为不同行)。
22
+ */
23
+ export function resolveWalletPassLineKey(product, indexInOrder) {
24
+ var m = (product === null || product === void 0 ? void 0 : product.metadata) || {};
25
+ var u1 = m.product_unique;
26
+ if (u1 != null && String(u1).length > 0) return String(u1);
27
+ var u2 = m.unique_identification_number;
28
+ if (u2 != null && String(u2).length > 0) return String(u2);
29
+ if ((product === null || product === void 0 ? void 0 : product.id) != null && String(product.id).length > 0) return "order_item:".concat(product.id);
30
+ if ((product === null || product === void 0 ? void 0 : product.product_unique_string) != null && String(product.product_unique_string).length > 0) {
31
+ return String(product.product_unique_string);
32
+ }
33
+ return "order_line_".concat(indexInOrder);
34
+ }
35
+ function getExpandedOrderLineQuantity(p) {
36
+ if ((p === null || p === void 0 ? void 0 : p._orderLineQuantity) != null && p._orderLineQuantity > 0) return p._orderLineQuantity;
37
+ return getProductQuantity(p);
38
+ }
39
+
40
+ /** 该行允许消耗的「券次」上限:maxPassesPerItem × 订单行件数 */
41
+ function getMaxPassSlotsForExpandedLine(maxPassesPerItem, p) {
42
+ if (maxPassesPerItem <= 0) return Infinity;
43
+ return maxPassesPerItem * getExpandedOrderLineQuantity(p);
44
+ }
45
+
46
+ /**
47
+ * 本次抵扣占用多少「券次」配额:有抵扣时至少占 1,并与按金额折算的件数挂钩,不超过剩余额度。
48
+ */
49
+ function computePassSlotsIncrement(deductAmount, deductQty, maxPassesPerItem, orderLineQuantity, currentUsage) {
50
+ if (maxPassesPerItem <= 0 || !deductAmount.greaterThan(0)) return 0;
51
+ var cap = maxPassesPerItem * orderLineQuantity;
52
+ var room = Math.max(0, cap - currentUsage);
53
+ if (room <= 0) return 0;
54
+ var raw = Math.max(1, Math.ceil(Number(deductQty)) || 1);
55
+ return Math.min(raw, room);
56
+ }
57
+ function applyMaxPassUsageIncrements(usageMap, walletPassProductId, maxPassesPerItem, deductionDetails) {
58
+ deductionDetails.forEach(function (detail) {
59
+ var _usageMap$get;
60
+ var lineKey = detail.lineKey;
61
+ if (!lineKey || maxPassesPerItem <= 0) return;
62
+ var orderLineQty = detail.orderLineQuantity != null && detail.orderLineQuantity > 0 ? detail.orderLineQuantity : 1;
63
+ var cur = ((_usageMap$get = usageMap.get(walletPassProductId)) === null || _usageMap$get === void 0 ? void 0 : _usageMap$get.get(lineKey)) || 0;
64
+ var inc = computePassSlotsIncrement(new Decimal(detail.deductAmount), detail.deductQuantity, maxPassesPerItem, orderLineQty, cur);
65
+ if (inc <= 0) return;
66
+ if (!usageMap.has(walletPassProductId)) usageMap.set(walletPassProductId, new Map());
67
+ var inner = usageMap.get(walletPassProductId);
68
+ inner.set(lineKey, cur + inc);
69
+ });
70
+ }
71
+
15
72
  // 辅助函数:根据 deductTaxAndFee 配置获取推荐使用金额
16
73
  var getRecommendedAmount = function getRecommendedAmount(voucher) {
17
74
  var _config$deductTaxAndF;
@@ -101,18 +158,6 @@ var getApplicableProducts = function getApplicableProducts(voucher, productsData
101
158
  });
102
159
  };
103
160
 
104
- /**
105
- * 计算指定 product_id 在展开后的商品列表中的总 quantity
106
- * 处理同一 product_id 可能分散在多条记录中的情况(如3个独立的 quantity=1 条目)
107
- */
108
- var getTotalQuantityByProductId = function getTotalQuantityByProductId(allProducts, productId) {
109
- return allProducts.filter(function (p) {
110
- return p.product_id === productId;
111
- }).reduce(function (sum, p) {
112
- return sum + getProductQuantity(p);
113
- }, 0);
114
- };
115
-
116
161
  /**
117
162
  * 优惠券处理函数
118
163
  * @param applicableVouchers 可用的券列表
@@ -130,29 +175,19 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
130
175
  var productsCopy = expandProductsWithBundleItems(products, true);
131
176
  var remainingOrderAmount = new Decimal(orderTotalAmount); // 订单剩余应付金额
132
177
 
133
- // ========== 辅助工具:maxPassesPerItem(单商品可用卡券上限)追踪 ==========
134
- // 获取指定 Wallet Pass 商品对指定订单商品行的已用卡券次数
135
- var getItemPassUsage = function getItemPassUsage(usageMap, walletPassProductId, orderItemProductId) {
136
- var _usageMap$get;
137
- return ((_usageMap$get = usageMap.get(walletPassProductId)) === null || _usageMap$get === void 0 ? void 0 : _usageMap$get.get(orderItemProductId)) || 0;
178
+ // ========== 辅助工具:maxPassesPerItem(按订单行 + 件数)追踪 ==========
179
+ var getItemPassUsage = function getItemPassUsage(usageMap, walletPassProductId, lineKey) {
180
+ var _usageMap$get2;
181
+ return ((_usageMap$get2 = usageMap.get(walletPassProductId)) === null || _usageMap$get2 === void 0 ? void 0 : _usageMap$get2.get(lineKey)) || 0;
138
182
  };
139
183
 
140
- // 更新指定 Wallet Pass 商品对指定订单商品行的已用卡券次数
141
- var incrementItemPassUsage = function incrementItemPassUsage(usageMap, walletPassProductId, orderItemProductId) {
142
- var count = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
143
- if (!usageMap.has(walletPassProductId)) {
144
- usageMap.set(walletPassProductId, new Map());
145
- }
146
- var innerMap = usageMap.get(walletPassProductId);
147
- innerMap.set(orderItemProductId, (innerMap.get(orderItemProductId) || 0) + count);
148
- };
149
-
150
- // 按 maxPassesPerItem 过滤商品:排除已达到单商品可用卡券上限的商品
151
- // maxPassesPerItem 是 per-unit 的限制,总上限 = maxPassesPerItem × 该 product_id 的总 quantity
152
- var filterByMaxPassesPerItem = function filterByMaxPassesPerItem(products, allProducts, usageMap, walletPassProductId, maxPassesPerItem) {
153
- if (maxPassesPerItem <= 0) return products; // 0 = 不限制
184
+ // maxPassesPerItem 过滤:排除已达到该行「券次」上限的展开行(按 _walletPassLineKey)
185
+ var filterByMaxPassesPerItem = function filterByMaxPassesPerItem(products, usageMap, walletPassProductId, maxPassesPerItem) {
186
+ if (maxPassesPerItem <= 0) return products;
154
187
  return products.filter(function (p) {
155
- return getItemPassUsage(usageMap, walletPassProductId, p.product_id) < maxPassesPerItem * getTotalQuantityByProductId(allProducts, p.product_id);
188
+ var lineKey = p._walletPassLineKey;
189
+ if (!lineKey) return true;
190
+ return getItemPassUsage(usageMap, walletPassProductId, lineKey) < getMaxPassSlotsForExpandedLine(maxPassesPerItem, p);
156
191
  });
157
192
  };
158
193
  // ================================================================
@@ -189,7 +224,7 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
189
224
 
190
225
  // 按 maxPassesPerItem 过滤:排除已达到单商品可用卡券上限的商品
191
226
  if (itemPassUsage) {
192
- applicableProducts = filterByMaxPassesPerItem(applicableProducts, productsData, itemPassUsage, voucher.product_id, maxPassesPerItem);
227
+ applicableProducts = filterByMaxPassesPerItem(applicableProducts, itemPassUsage, voucher.product_id, maxPassesPerItem);
193
228
  }
194
229
  if (applicableProducts.length === 0) {
195
230
  return new Decimal(0);
@@ -301,7 +336,9 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
301
336
  var availableAfterPassLimit = getApplicableProducts(voucher, productsData).filter(function (p) {
302
337
  return p[amountField].greaterThan(0);
303
338
  }).filter(function (p) {
304
- return getItemPassUsage(itemPassUsage, product_id, p.product_id) < maxPassesPerItem * getTotalQuantityByProductId(productsData, p.product_id);
339
+ var lineKey = p._walletPassLineKey;
340
+ if (!lineKey) return true;
341
+ return getItemPassUsage(itemPassUsage, product_id, lineKey) < getMaxPassSlotsForExpandedLine(maxPassesPerItem, p);
305
342
  });
306
343
  if (availableAfterPassLimit.length === 0) {
307
344
  return {
@@ -337,7 +374,7 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
337
374
  // 重置商品余额追踪(同时维护含税和不含税两个金额池)
338
375
  var productsForRecommendation = expandProductsWithBundleItems(products, true);
339
376
  remainingOrderAmount = new Decimal(orderTotalAmount);
340
- // 追踪推荐阶段每个 Wallet Pass 商品对每个订单商品行的已使用卡券次数
377
+ // 追踪推荐阶段每个 Wallet Pass 商品对每个订单商品行的已使用卡券次数(按行唯一键)
341
378
  var itemPassUsageMap = new Map();
342
379
 
343
380
  /**
@@ -398,7 +435,7 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
398
435
  });
399
436
 
400
437
  // 按 maxPassesPerItem 过滤:排除已达到单商品可用卡券上限的商品
401
- applicableProducts = filterByMaxPassesPerItem(applicableProducts, productsForRecommendation, itemPassUsageMap, product_id, maxPassesPerItem);
438
+ applicableProducts = filterByMaxPassesPerItem(applicableProducts, itemPassUsageMap, product_id, maxPassesPerItem);
402
439
  if (applicableProducts.length === 0) return false;
403
440
 
404
441
  // ========== 关键修改:在应用券之前,基于当前剩余金额计算 _available_max_amount ==========
@@ -492,6 +529,8 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
492
529
  product_id: _product.product_id,
493
530
  parent_product_id: _product.parent_product_id || null,
494
531
  is_bundle_item: _product.is_bundle_item || false,
532
+ lineKey: _product._walletPassLineKey,
533
+ orderLineQuantity: getExpandedOrderLineQuantity(_product),
495
534
  deductAmount: actualDeductAmount.toNumber(),
496
535
  // 转换为数字
497
536
  deductQuantity: actualDeductQty // 抵扣涉及的数量(用于记录)
@@ -525,6 +564,8 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
525
564
  product_id: targetProduct.product_id,
526
565
  parent_product_id: targetProduct.parent_product_id || null,
527
566
  is_bundle_item: targetProduct.is_bundle_item || false,
567
+ lineKey: targetProduct._walletPassLineKey,
568
+ orderLineQuantity: getExpandedOrderLineQuantity(targetProduct),
528
569
  deductAmount: _actualDeductAmount.toNumber(),
529
570
  deductQuantity: _actualDeductQty
530
571
  });
@@ -536,11 +577,7 @@ export function processVouchers(applicableVouchers, orderTotalAmount, products)
536
577
 
537
578
  // 更新券使用次数(按 product_id 统计)
538
579
  usedVoucherCounts.set(product_id, (usedVoucherCounts.get(product_id) || 0) + 1);
539
-
540
- // 更新 maxPassesPerItem 追踪:按实际抵扣数量递增
541
- deductionDetails.forEach(function (detail) {
542
- incrementItemPassUsage(itemPassUsageMap, product_id, detail.product_id, detail.deductQuantity);
543
- });
580
+ applyMaxPassUsageIncrements(itemPassUsageMap, product_id, maxPassesPerItem, deductionDetails);
544
581
 
545
582
  // 添加到推荐列表(包含基于当前剩余金额计算的 available_max_amount)
546
583
  recommendedVouchers.push(_objectSpread(_objectSpread({}, voucher), {}, {
@@ -623,25 +660,18 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
623
660
  var remainingOrderAmount = new Decimal(orderTotalAmount);
624
661
  var selectedWithDetails = [];
625
662
 
626
- // 追踪每个 Wallet Pass 商品对每个订单商品行的已使用卡券次数
627
- // Map<walletPassProductId, Map<orderItemProductId, usedCount>>
663
+ // 追踪每个 Wallet Pass 商品对每个订单商品行的已使用卡券次数(按行唯一键)
628
664
  var itemPassUsageMap = new Map();
629
- var getItemPassUsage = function getItemPassUsage(walletPassProductId, orderItemProductId) {
665
+ var getItemPassUsageRecalc = function getItemPassUsageRecalc(walletPassProductId, lineKey) {
630
666
  var _itemPassUsageMap$get;
631
- return ((_itemPassUsageMap$get = itemPassUsageMap.get(walletPassProductId)) === null || _itemPassUsageMap$get === void 0 ? void 0 : _itemPassUsageMap$get.get(orderItemProductId)) || 0;
632
- };
633
- var incrementItemPassUsage = function incrementItemPassUsage(walletPassProductId, orderItemProductId) {
634
- var count = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
635
- if (!itemPassUsageMap.has(walletPassProductId)) {
636
- itemPassUsageMap.set(walletPassProductId, new Map());
637
- }
638
- var innerMap = itemPassUsageMap.get(walletPassProductId);
639
- innerMap.set(orderItemProductId, (innerMap.get(orderItemProductId) || 0) + count);
667
+ return ((_itemPassUsageMap$get = itemPassUsageMap.get(walletPassProductId)) === null || _itemPassUsageMap$get === void 0 ? void 0 : _itemPassUsageMap$get.get(lineKey)) || 0;
640
668
  };
641
- var filterByMaxPassesPerItem = function filterByMaxPassesPerItem(products, walletPassProductId, maxPassesPerItem) {
642
- if (maxPassesPerItem <= 0) return products; // 0 = 不限制
669
+ var filterByMaxPassesPerItemRecalc = function filterByMaxPassesPerItemRecalc(products, walletPassProductId, maxPassesPerItem) {
670
+ if (maxPassesPerItem <= 0) return products;
643
671
  return products.filter(function (p) {
644
- return getItemPassUsage(walletPassProductId, p.product_id) < maxPassesPerItem * getTotalQuantityByProductId(productsForCalc, p.product_id);
672
+ var lineKey = p._walletPassLineKey;
673
+ if (!lineKey) return true;
674
+ return getItemPassUsageRecalc(walletPassProductId, lineKey) < getMaxPassSlotsForExpandedLine(maxPassesPerItem, p);
645
675
  });
646
676
  };
647
677
 
@@ -666,8 +696,8 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
666
696
  return p[amountField].greaterThan(0);
667
697
  });
668
698
 
669
- // 按 maxPassesPerItem 过滤:排除已达到单商品可用卡券上限的商品
670
- applicableProducts = filterByMaxPassesPerItem(applicableProducts, selectedVoucher.product_id, maxPassesPerItem);
699
+ // 按 maxPassesPerItem 过滤:排除已达到该行券次上限的展开行
700
+ applicableProducts = filterByMaxPassesPerItemRecalc(applicableProducts, selectedVoucher.product_id, maxPassesPerItem);
671
701
  if (applicableProducts.length === 0) {
672
702
  // 无适用商品,跳过
673
703
  selectedWithDetails.push(_objectSpread(_objectSpread({}, selectedVoucher), {}, {
@@ -718,6 +748,8 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
718
748
  product_id: product.product_id,
719
749
  parent_product_id: product.parent_product_id || null,
720
750
  is_bundle_item: product.is_bundle_item || false,
751
+ lineKey: product._walletPassLineKey,
752
+ orderLineQuantity: getExpandedOrderLineQuantity(product),
721
753
  deductAmount: actualDeductAmount.toNumber(),
722
754
  // 转换为数字
723
755
  deductQuantity: actualDeductQty // 抵扣涉及的数量(用于记录)
@@ -751,6 +783,8 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
751
783
  product_id: targetProduct.product_id,
752
784
  parent_product_id: targetProduct.parent_product_id || null,
753
785
  is_bundle_item: targetProduct.is_bundle_item || false,
786
+ lineKey: targetProduct._walletPassLineKey,
787
+ orderLineQuantity: getExpandedOrderLineQuantity(targetProduct),
754
788
  deductAmount: _actualDeductAmount2.toNumber(),
755
789
  deductQuantity: _actualDeductQty2
756
790
  });
@@ -759,11 +793,7 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
759
793
 
760
794
  // 更新订单剩余金额
761
795
  remainingOrderAmount = remainingOrderAmount.minus(totalDeducted);
762
-
763
- // 更新 maxPassesPerItem 追踪:按实际抵扣数量递增
764
- deductionDetails.forEach(function (detail) {
765
- incrementItemPassUsage(selectedVoucher.product_id, detail.product_id, detail.deductQuantity);
766
- });
796
+ applyMaxPassUsageIncrements(itemPassUsageMap, selectedVoucher.product_id, maxPassesPerItem, deductionDetails);
767
797
  selectedWithDetails.push(_objectSpread(_objectSpread({}, selectedVoucher), {}, {
768
798
  actualDeduction: totalDeducted.toNumber(),
769
799
  // 转换为数字
@@ -836,7 +866,7 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
836
866
  });
837
867
 
838
868
  // 按 maxPassesPerItem 过滤:排除已达到单商品可用卡券上限的商品
839
- applicableProducts = filterByMaxPassesPerItem(applicableProducts, product_id, maxPassesPerItem);
869
+ applicableProducts = filterByMaxPassesPerItemRecalc(applicableProducts, product_id, maxPassesPerItem);
840
870
  if (applicableProducts.length === 0) {
841
871
  isAvailable = false;
842
872
  reasonCode = 'not_meet_the_required_conditions';
@@ -910,10 +940,10 @@ export function recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmo
910
940
  }
911
941
 
912
942
  /**
913
- * 获取主商品价格
943
+ * 获取主商品价格(单价,不含舍入余数)
914
944
  * @param product 商品
915
945
  * @param isDeductTaxAndFee 是否抵扣税费与附加费
916
- * @returns 商品价格
946
+ * @returns 商品单价
917
947
  */
918
948
  export var getMainProductPrice = function getMainProductPrice(product, isDeductTaxAndFee) {
919
949
  var _product$metadata, _product$metadata2, _product$metadata3, _product$metadata4, _product$metadata5;
@@ -956,11 +986,11 @@ export var getMainProductPrice = function getMainProductPrice(product, isDeductT
956
986
  };
957
987
 
958
988
  /**
959
- * 获取套餐子商品价格
989
+ * 获取套餐子商品价格(不含舍入余数)
960
990
  * @param bundleItem 套餐子商品
961
991
  * @param parentQuantity 父商品数量
962
992
  * @param isDeductTaxAndFee 是否抵扣税费与附加费
963
- * @returns 子商品总价格
993
+ * @returns 子商品总价格(不含舍入余数)
964
994
  */
965
995
  export var getBundleItemPrice = function getBundleItemPrice(bundleItem, parentQuantity, isDeductTaxAndFee) {
966
996
  var _bundleItem$bundle_se2;
@@ -991,8 +1021,9 @@ export var getBundleItemPrice = function getBundleItemPrice(bundleItem, parentQu
991
1021
  */
992
1022
  var expandProductsWithBundleItems = function expandProductsWithBundleItems(products, deductTaxAndFee) {
993
1023
  var expandedProducts = [];
994
- products.forEach(function (product) {
1024
+ products.forEach(function (product, indexInOrder) {
995
1025
  var productQuantity = getProductQuantity(product);
1026
+ var parentLineKey = resolveWalletPassLineKey(product, indexInOrder);
996
1027
 
997
1028
  // 计算主商品单价(含税和不含税)
998
1029
  var unitPriceWithTax = getMainProductPrice(product, true);
@@ -1002,6 +1033,8 @@ var expandProductsWithBundleItems = function expandProductsWithBundleItems(produ
1002
1033
  expandedProducts.push(_objectSpread(_objectSpread({}, product), {}, {
1003
1034
  is_bundle_item: false,
1004
1035
  parent_product_id: null,
1036
+ _walletPassLineKey: parentLineKey,
1037
+ _orderLineQuantity: productQuantity,
1005
1038
  // 单价(用于按 quantity 计算抵扣)
1006
1039
  unitPriceWithTax: new Decimal(unitPriceWithTax),
1007
1040
  unitPricePure: new Decimal(unitPricePure),
@@ -1018,6 +1051,7 @@ var expandProductsWithBundleItems = function expandProductsWithBundleItems(produ
1018
1051
  product.product_bundle.forEach(function (bundleItem) {
1019
1052
  if (getBundleItemIsOriginalPrice(bundleItem)) {
1020
1053
  var bundleQuantity = bundleItem.num * productQuantity;
1054
+ var bundleLineKey = "".concat(parentLineKey, "#bundle:").concat(bundleItem.bundle_id, ":").concat(bundleItem.bundle_product_id);
1021
1055
  // 计算子商品单价(注意:getBundleItemPrice 返回的是总价,需要除以数量得到单价)
1022
1056
  var bundleUnitPriceWithTax = new Decimal(getBundleItemPrice(bundleItem, 1, true)).dividedBy(bundleItem.num);
1023
1057
  var bundleUnitPricePure = new Decimal(getBundleItemPrice(bundleItem, 1, false)).dividedBy(bundleItem.num);
@@ -1030,6 +1064,8 @@ var expandProductsWithBundleItems = function expandProductsWithBundleItems(produ
1030
1064
  parent_product_id: product.product_id,
1031
1065
  quantity: bundleQuantity,
1032
1066
  // 子商品数量 * 主商品数量
1067
+ _walletPassLineKey: bundleLineKey,
1068
+ _orderLineQuantity: bundleQuantity,
1033
1069
  // 单价(用于按 quantity 计算抵扣)
1034
1070
  unitPriceWithTax: bundleUnitPriceWithTax,
1035
1071
  unitPricePure: bundleUnitPricePure,
@@ -1047,15 +1083,6 @@ var expandProductsWithBundleItems = function expandProductsWithBundleItems(produ
1047
1083
  return expandedProducts;
1048
1084
  };
1049
1085
 
1050
- /**
1051
- * 获取商品数量
1052
- * @param product 商品
1053
- * @returns 商品数量
1054
- */
1055
- export var getProductQuantity = function getProductQuantity(product) {
1056
- return product.quantity || product.product_quantity || 1;
1057
- };
1058
-
1059
1086
  // bundle商品是否是原价
1060
1087
  export var getBundleItemIsOriginalPrice = function getBundleItemIsOriginalPrice(item) {
1061
1088
  return (item === null || item === void 0 ? void 0 : item.price_type) === 'markup' && (item === null || item === void 0 ? void 0 : item.price_type_ext) === 'product_price';
@@ -14,6 +14,20 @@ import Decimal from 'decimal.js';
14
14
  import { isNormalProduct } from "../../Product/utils";
15
15
  import { getDiscountAmount } from "../../../solution/ShopDiscount/utils";
16
16
 
17
+ /** 与 Rules / ShopDiscount 一致的折扣入参,用于主价与 option 单价 */
18
+ var toDiscountPayload = function toDiscountPayload(discountItem) {
19
+ var _discountItem$discoun, _discountItem$discoun2;
20
+ return {
21
+ amount: discountItem.amount,
22
+ tag: discountItem.type,
23
+ par_value: (_discountItem$discoun = discountItem.discount) === null || _discountItem$discoun === void 0 ? void 0 : _discountItem$discoun.percent,
24
+ config: discountItem.config,
25
+ metadata: {
26
+ discount_card_type: discountItem === null || discountItem === void 0 || (_discountItem$discoun2 = discountItem.discount) === null || _discountItem$discoun2 === void 0 ? void 0 : _discountItem$discoun2.discount_card_type
27
+ }
28
+ };
29
+ };
30
+
17
31
  /**
18
32
  * @title 处理组合商品
19
33
  * @description 组合商品需要直接修改 product 里的价格和 is_charge_tax
@@ -189,6 +203,7 @@ export var formatProductToCartItemOrigin = function formatProductToCartItemOrigi
189
203
  * @returns 商品总价
190
204
  */
191
205
  export var getProductTotalPrice = function getProductTotalPrice(params) {
206
+ var _options$map;
192
207
  var product = params.product,
193
208
  bundle = params.bundle,
194
209
  options = params.options,
@@ -196,23 +211,29 @@ export var getProductTotalPrice = function getProductTotalPrice(params) {
196
211
  // const num = params.num || 1;
197
212
  var price = Number(product.price);
198
213
 
214
+ // option 行:unit 折后价在循环中与主商品使用同一套 getDiscountAmount(含 deductOptionPrice)
215
+ var optionRows = (_options$map = options === null || options === void 0 ? void 0 : options.map(function (currentValue) {
216
+ var _ref, _currentValue$price, _ref2, _currentValue$num;
217
+ return {
218
+ unit: Number((_ref = (_currentValue$price = currentValue.price) !== null && _currentValue$price !== void 0 ? _currentValue$price : currentValue.add_price) !== null && _ref !== void 0 ? _ref : 0),
219
+ num: Number((_ref2 = (_currentValue$num = currentValue.num) !== null && _currentValue$num !== void 0 ? _currentValue$num : currentValue.quantity) !== null && _ref2 !== void 0 ? _ref2 : 1)
220
+ };
221
+ })) !== null && _options$map !== void 0 ? _options$map : [];
222
+
199
223
  // 如果商品有折扣,则计算折扣
200
224
  if (discounts !== null && discounts !== void 0 && discounts.length) {
201
225
  discounts.forEach(function (currentValue) {
202
- var _currentValue$discoun;
203
- // 不是商品券则代表折扣卡,计算打折后的价格
204
- // 一个商品折扣卡只能存在于一张
205
- // if (currentValue.type !== 'good_pass') {
206
- price = getDiscountAmount({
207
- amount: currentValue.amount,
208
- tag: currentValue.type,
209
- par_value: currentValue.discount.percent,
210
- config: currentValue.config,
211
- metadata: {
212
- discount_card_type: currentValue === null || currentValue === void 0 || (_currentValue$discoun = currentValue.discount) === null || _currentValue$discoun === void 0 ? void 0 : _currentValue$discoun.discount_card_type
226
+ var _currentValue$config;
227
+ var payload = toDiscountPayload(currentValue);
228
+ price = getDiscountAmount(payload, price, price);
229
+ if (currentValue !== null && currentValue !== void 0 && (_currentValue$config = currentValue.config) !== null && _currentValue$config !== void 0 && _currentValue$config.deductOptionPrice && optionRows.length) {
230
+ for (var i = 0; i < optionRows.length; i++) {
231
+ var row = optionRows[i];
232
+ optionRows[i] = _objectSpread(_objectSpread({}, row), {}, {
233
+ unit: getDiscountAmount(payload, row.unit, row.unit)
234
+ });
213
235
  }
214
- }, price, price);
215
- // }
236
+ }
216
237
  });
217
238
  }
218
239
  if (bundle !== null && bundle !== void 0 && bundle.length) {
@@ -221,10 +242,10 @@ export var getProductTotalPrice = function getProductTotalPrice(params) {
221
242
  }, price);
222
243
  }
223
244
 
224
- // 单规格
225
- if (options !== null && options !== void 0 && options.length) {
226
- price = options.reduce(function (accumulator, currentValue) {
227
- return accumulator + Number(currentValue.price) * Number(currentValue.num);
245
+ // 单规格 / 加购 option
246
+ if (optionRows.length) {
247
+ price = optionRows.reduce(function (accumulator, row) {
248
+ return accumulator + row.unit * row.num;
228
249
  }, price);
229
250
  }
230
251
  return Math.max(0, price);
@@ -369,11 +390,11 @@ export var getProductDeposit = function getProductDeposit(params) {
369
390
  // 判定商品是否有定金规则
370
391
  if ((product === null || product === void 0 || (_product$custom_depos2 = product.custom_deposit_data) === null || _product$custom_depos2 === void 0 ? void 0 : _product$custom_depos2.has_deposit) == 1) {
371
392
  var total = new Decimal(0);
372
- var _ref = (product === null || product === void 0 ? void 0 : product.custom_deposit_data) || {},
373
- deposit_fixed = _ref.deposit_fixed,
374
- deposit_percentage = _ref.deposit_percentage,
375
- deposit_policy_data = _ref.deposit_policy_data,
376
- self_deposit_policy_ids = _ref.self_deposit_policy_ids;
393
+ var _ref3 = (product === null || product === void 0 ? void 0 : product.custom_deposit_data) || {},
394
+ deposit_fixed = _ref3.deposit_fixed,
395
+ deposit_percentage = _ref3.deposit_percentage,
396
+ deposit_policy_data = _ref3.deposit_policy_data,
397
+ self_deposit_policy_ids = _ref3.self_deposit_policy_ids;
377
398
  // 所有协议数据
378
399
  var allProtocols = deposit_policy_data || [];
379
400
  // 是否存在定金,主商品has_deposit为1,但是自身没有定金设置,需要判定子商品是否存在定金,如果不存在最终判定也是不存在定金
@@ -467,9 +488,9 @@ export var handleProductDeposit = function handleProductDeposit(params) {
467
488
  depositTotal: new Decimal(0)
468
489
  };
469
490
  }
470
- var _ref2 = depositData || {},
471
- deposit_fixed = _ref2.deposit_fixed,
472
- deposit_percentage = _ref2.deposit_percentage;
491
+ var _ref4 = depositData || {},
492
+ deposit_fixed = _ref4.deposit_fixed,
493
+ deposit_percentage = _ref4.deposit_percentage;
473
494
  if (typeof deposit_fixed === 'string' && typeof deposit_percentage === 'string') {
474
495
  var depositFixed = Decimal(deposit_fixed || 0);
475
496
  var depositPercent = Decimal(deposit_percentage || 0);
@@ -119,6 +119,8 @@ export interface Discount {
119
119
  allowCrossProduct?: boolean;
120
120
  /** 可用商品数量上限 */
121
121
  applicableProductLimit?: number;
122
+ /** 是否抵扣单规格价格 (Option Price) */
123
+ deductOptionPrice?: boolean;
122
124
  };
123
125
  applicableProductIds?: number[];
124
126
  applicableProductDetails: ApplicableProductDetails[];
@@ -23,7 +23,7 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
23
23
  */
24
24
  private logError;
25
25
  createOrder(params: CommitOrderParams['query']): {
26
- type: "appointment_booking" | "virtual";
26
+ type: "virtual" | "appointment_booking";
27
27
  platform: string;
28
28
  sales_channel: string;
29
29
  order_sales_channel: string;
@@ -49,5 +49,5 @@ export declare class Product extends BaseModule implements Module {
49
49
  getCategories(): ProductCategory[];
50
50
  setOtherParams(key: string, value: any): void;
51
51
  getOtherParams(): any;
52
- getProductType(): "duration" | "session" | "normal";
52
+ getProductType(): "normal" | "duration" | "session";
53
53
  }