@pisell/pisellos 3.0.60 → 3.0.62

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.
@@ -12,6 +12,7 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
12
12
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
13
  import Decimal from 'decimal.js';
14
14
  import { isNormalProduct } from "../../Product/utils";
15
+ import { getDiscountAmount } from "../../../solution/ShopDiscount/utils";
15
16
 
16
17
  /**
17
18
  * @title 处理组合商品
@@ -192,8 +193,14 @@ export var getProductTotalPrice = function getProductTotalPrice(params) {
192
193
  // 不是商品券则代表折扣卡,计算打折后的价格
193
194
  // 一个商品折扣卡只能存在于一张
194
195
  if (currentValue.type !== 'good_pass') {
195
- var discountPrice = new Decimal(100).minus(currentValue.discount.percent || 0).div(100).mul(new Decimal(price || 0)).toFixed(2);
196
- price = Number(discountPrice);
196
+ var _currentValue$discoun;
197
+ price = getDiscountAmount({
198
+ tag: currentValue.type,
199
+ par_value: currentValue.discount.percent,
200
+ metadata: {
201
+ discount_card_type: currentValue === null || currentValue === void 0 || (_currentValue$discoun = currentValue.discount) === null || _currentValue$discoun === void 0 ? void 0 : _currentValue$discoun.discount_card_type
202
+ }
203
+ }, price, price);
197
204
  }
198
205
  });
199
206
  }
@@ -20,8 +20,9 @@ export declare class DiscountModule extends BaseModule implements Module, Discou
20
20
  with_discount_card: 0 | 1;
21
21
  customer_id: number;
22
22
  }): Promise<Discount[]>;
23
- batchSearch(code: string): Promise<Discount[]>;
23
+ batchSearch(code: string, customerId?: number): Promise<Discount[]>;
24
24
  filterEnabledDiscountList(discountList: Discount[]): Discount[];
25
+ private checkUsageCreditsLimit;
25
26
  uniqueByProductId(discountList: Discount[]): Discount[];
26
27
  filterDiscountListByProductIds(discountList: Discount[], productIds: number[]): Discount[];
27
28
  calcDiscountApplicableProductTotalPrice(discount: Discount): number | undefined;
@@ -145,7 +145,7 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
145
145
  }, {
146
146
  key: "batchSearch",
147
147
  value: function () {
148
- var _batchSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(code) {
148
+ var _batchSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(code, customerId) {
149
149
  var result, resultDiscountList;
150
150
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
151
151
  while (1) switch (_context4.prev = _context4.next) {
@@ -156,7 +156,10 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
156
156
  translate_flag: 1,
157
157
  tags: ['good_pass', 'product_discount_card'],
158
158
  available: 1,
159
- relation_product: 1
159
+ relation_product: 1,
160
+ with: ['extensionData'],
161
+ order_behavior_count: 1,
162
+ order_behavior_count_customer_id: customerId || 1
160
163
  });
161
164
  case 2:
162
165
  result = _context4.sent;
@@ -168,7 +171,7 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
168
171
  }
169
172
  }, _callee4, this);
170
173
  }));
171
- function batchSearch(_x5) {
174
+ function batchSearch(_x5, _x6) {
172
175
  return _batchSearch.apply(this, arguments);
173
176
  }
174
177
  return batchSearch;
@@ -176,11 +179,71 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
176
179
  }, {
177
180
  key: "filterEnabledDiscountList",
178
181
  value: function filterEnabledDiscountList(discountList) {
182
+ var _this2 = this;
179
183
  return discountList.filter(function (discount) {
180
- return discount.limit_status === 'enable' && new Decimal((discount === null || discount === void 0 ? void 0 : discount.par_value) || 0).minus(new Decimal((discount === null || discount === void 0 ? void 0 : discount.used_par_value) || 0)).greaterThan(0);
184
+ return discount.limit_status === 'enable' && new Decimal((discount === null || discount === void 0 ? void 0 : discount.par_value) || 0).minus(new Decimal((discount === null || discount === void 0 ? void 0 : discount.used_par_value) || 0)).greaterThan(0) && _this2.checkUsageCreditsLimit(discount);
181
185
  });
182
186
  }
183
187
 
188
+ // 检查使用次数限制
189
+ }, {
190
+ key: "checkUsageCreditsLimit",
191
+ value: function checkUsageCreditsLimit(discount) {
192
+ // 如果没有 extension_data,则不需要限制
193
+ if (!discount.extension_data || discount.extension_data.length === 0) {
194
+ return true;
195
+ }
196
+
197
+ // 查找 field_key 为 usage_credits 的数据
198
+ var usageCreditsData = discount.extension_data.find(function (data) {
199
+ return data.field_key === 'usage_credits';
200
+ });
201
+
202
+ // 如果没有找到 usage_credits 数据,则不需要限制
203
+ if (!usageCreditsData) {
204
+ return true;
205
+ }
206
+ var value = usageCreditsData.value;
207
+
208
+ // 检查总次数限制
209
+ if (value.total_credits && value.total_credits > 0) {
210
+ if ((discount.total_order_behavior_count || 0) >= value.total_credits) {
211
+ return false;
212
+ }
213
+ }
214
+
215
+ // 检查单用户限制
216
+ if (value.per_user_limit && value.per_user_limit > 0) {
217
+ if ((discount.customer_order_behavior_count || 0) >= value.per_user_limit) {
218
+ return false;
219
+ }
220
+ }
221
+
222
+ // 检查单日限制
223
+ if (value.max_per_day && value.max_per_day > 0) {
224
+ if ((discount.today_order_behavior_count || 0) >= value.max_per_day) {
225
+ return false;
226
+ }
227
+ }
228
+
229
+ // 检查单周限制
230
+ if (value.max_per_week && value.max_per_week > 0) {
231
+ if ((discount.week_order_behavior_count || 0) >= value.max_per_week) {
232
+ return false;
233
+ }
234
+ }
235
+
236
+ // 检查单月限制
237
+ if (value.max_per_month && value.max_per_month > 0) {
238
+ if ((discount.month_order_behavior_count || 0) >= value.max_per_month) {
239
+ return false;
240
+ }
241
+ }
242
+
243
+ // 所有检查都通过
244
+ return true;
245
+ }
246
+
184
247
  // 根据productIds去重
185
248
  }, {
186
249
  key: "uniqueByProductId",
@@ -36,6 +36,25 @@ interface ApplicableProductDetails {
36
36
  title?: string;
37
37
  };
38
38
  }
39
+ interface UsageCreditsValue {
40
+ total_credits: number;
41
+ per_user_limit: number;
42
+ max_per_day: number;
43
+ max_per_week: number;
44
+ max_per_month: number;
45
+ }
46
+ interface ExtensionData {
47
+ id: number;
48
+ shop_id: number;
49
+ product_id: number;
50
+ field_id: number;
51
+ field_key: string;
52
+ type: string;
53
+ value: UsageCreditsValue | Record<string, any> | string | number | boolean | any[];
54
+ created_at: string | null;
55
+ updated_at: string | null;
56
+ deleted_at: string | null;
57
+ }
39
58
  export interface Discount {
40
59
  id: number;
41
60
  product_name: string;
@@ -51,6 +70,9 @@ export interface Discount {
51
70
  limited_relation_product_data: Limitedrelationproductdata;
52
71
  balance: string;
53
72
  format_title: Formattitle;
73
+ metadata?: {
74
+ discount_card_type?: 'fixed_amount' | 'percent';
75
+ };
54
76
  product: Product;
55
77
  type: "product" | 'good_pass';
56
78
  resource_id?: number;
@@ -70,6 +92,12 @@ export interface Discount {
70
92
  appliedProductDetails: ApplicableProductDetails[];
71
93
  isDisabledForProductUsed?: boolean;
72
94
  amount?: number;
95
+ extension_data?: ExtensionData[];
96
+ total_order_behavior_count?: number;
97
+ today_order_behavior_count?: number;
98
+ week_order_behavior_count?: number;
99
+ month_order_behavior_count?: number;
100
+ customer_order_behavior_count?: number;
73
101
  }
74
102
  export interface DiscountState {
75
103
  discountList: Discount[];
@@ -25,7 +25,7 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
25
25
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
26
26
  import { BaseModule } from "../BaseModule";
27
27
  import { RulesHooks } from "./types";
28
- import { uniqueById } from "../../solution/ShopDiscount/utils";
28
+ import { uniqueById, getDiscountAmount } from "../../solution/ShopDiscount/utils";
29
29
  import { getProductOriginTotalPrice, getProductTotalPrice } from "../Cart/utils";
30
30
  import Decimal from 'decimal.js';
31
31
  export var RulesModule = /*#__PURE__*/function (_BaseModule) {
@@ -188,8 +188,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
188
188
  });
189
189
 
190
190
  // 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
191
- // 商品券(n.tag=good_pass)视为最优惠(免费),折扣券(n.tag=product_discount_card)按照n.par_value排序
192
- // 如果最后拍出来商品券有多个,或者说没有商品券,但是有多个相同折扣的折扣券(比如 6 折券有 3张),则按照过期时间(n.expire_time)排序
191
+ // 1. 商品券(n.tag=good_pass)视为最优惠(免费)
192
+ // 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
193
+ // - 固定金额优先于百分比(固定金额 > 百分比)
194
+ // - 固定金额内部:金额越大越优先
195
+ // - 百分比内部:折扣越大越优先(par_value越小越优先)
196
+ // 3. 相同类型和金额的情况下,按照过期时间(n.expire_time)排序
193
197
  var sortedDiscountList = _toConsumableArray(filteredDiscountList).sort(function (a, b) {
194
198
  // 1. 商品券优先级最高
195
199
  if (a.tag === 'good_pass' && b.tag !== 'good_pass') return -1;
@@ -200,13 +204,34 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
200
204
  // 都是商品券,按照过期时间排序
201
205
  return compareByExpireTime(a, b);
202
206
  } else if (a.tag === 'product_discount_card' && b.tag === 'product_discount_card') {
203
- // 都是折扣券,按照par_value排序(折扣越大越优先,即par_value越小越优先)
204
- if (a.par_value !== b.par_value) {
205
- var valueA = new Decimal(100).minus(a.par_value || 0);
206
- var valueB = new Decimal(100).minus(b.par_value || 0);
207
- return valueA.minus(valueB).toNumber();
207
+ var _a$metadata, _b$metadata;
208
+ // 都是折扣券,按照新的优先级排序:固定金额优先于百分比
209
+ var typeA = ((_a$metadata = a.metadata) === null || _a$metadata === void 0 ? void 0 : _a$metadata.discount_card_type) || 'percent'; // 默认为百分比
210
+ var typeB = ((_b$metadata = b.metadata) === null || _b$metadata === void 0 ? void 0 : _b$metadata.discount_card_type) || 'percent'; // 默认为百分比
211
+
212
+ // 1. 固定金额优先于百分比
213
+ if (typeA === 'fixed_amount' && typeB === 'percent') return -1;
214
+ if (typeA === 'percent' && typeB === 'fixed_amount') return 1;
215
+
216
+ // 2. 都是固定金额时,金额越大越优先
217
+ if (typeA === 'fixed_amount' && typeB === 'fixed_amount') {
218
+ if (a.par_value !== b.par_value) {
219
+ var valueA = new Decimal(a.par_value || 0);
220
+ var valueB = new Decimal(b.par_value || 0);
221
+ return valueB.minus(valueA).toNumber(); // 金额大的在前
222
+ }
208
223
  }
209
- // 相同折扣的情况下,按照过期时间排序
224
+
225
+ // 3. 都是百分比时,折扣越大越优先(par_value越小越优先)
226
+ if (typeA === 'percent' && typeB === 'percent') {
227
+ if (a.par_value !== b.par_value) {
228
+ var _valueA = new Decimal(100).minus(a.par_value || 0);
229
+ var _valueB = new Decimal(100).minus(b.par_value || 0);
230
+ return _valueB.minus(_valueA).toNumber(); // 折扣大的在前
231
+ }
232
+ }
233
+
234
+ // 相同类型和金额的情况下,按照过期时间排序
210
235
  return compareByExpireTime(a, b);
211
236
  }
212
237
 
@@ -290,7 +315,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
290
315
 
291
316
  // 判断优惠券是否适用于该商品
292
317
  if (isAvailableProduct && isLimitedProduct) {
293
- var _discountApplicabilit;
318
+ var _discountApplicabilit, _discount$metadata;
294
319
  // 记录此优惠券适用的商品
295
320
  (_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
296
321
 
@@ -301,6 +326,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
301
326
  type: discount.tag || discount.type,
302
327
  tag: discount.tag || discount.type,
303
328
  discount: {
329
+ discount_card_type: discount === null || discount === void 0 || (_discount$metadata = discount.metadata) === null || _discount$metadata === void 0 ? void 0 : _discount$metadata.discount_card_type,
304
330
  resource_id: discount.id,
305
331
  title: discount.format_title,
306
332
  original_amount: product.origin_total,
@@ -427,7 +453,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
427
453
  }));
428
454
  }
429
455
  for (var _i = 0; _i < splitCount; _i++) {
430
- var _product$discount_lis11, _originProduct$_produ;
456
+ var _product$discount_lis11, _originProduct$_produ, _selectedDiscount$met;
431
457
  // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
432
458
  var _selectedDiscount = selectedDiscountCard || applicableDiscounts[_i];
433
459
  // 标记优惠券为已使用
@@ -448,11 +474,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
448
474
  }
449
475
 
450
476
  // 计算使用折扣卡/商品券以后,单个商品的总 total
451
- var targetProductTotal = _selectedDiscount.tag === 'good_pass' ? new Decimal(productOriginTotal).minus(new Decimal(product.price || 0)).toNumber() : new Decimal(100).minus(_selectedDiscount.par_value || 0).div(100).mul(new Decimal(productOriginTotal)).toNumber();
477
+ var targetProductTotal = getDiscountAmount(_selectedDiscount, productOriginTotal, product.price);
452
478
  var discountDetail = {
453
479
  amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
454
480
  type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount.tag,
455
481
  discount: {
482
+ discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
456
483
  resource_id: _selectedDiscount.id,
457
484
  title: _selectedDiscount.format_title,
458
485
  original_amount: productOriginTotal,
@@ -1273,7 +1273,7 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
1273
1273
  // 如果此时 resources 为空,视作购物车里已经有了 dateRange 数据,此时 dateList 里明确就是那一天的数据
1274
1274
  if (!allOriginResources.length) {
1275
1275
  var dateList = this.store.date.getDateList();
1276
- dateList.forEach(function (n) {
1276
+ dateList === null || dateList === void 0 || dateList.forEach(function (n) {
1277
1277
  if (n.resource) allOriginResources.push.apply(allOriginResources, _toConsumableArray(n.resource));
1278
1278
  });
1279
1279
  }
@@ -34,7 +34,7 @@ export declare class ShopDiscountImpl extends BaseModule implements Module {
34
34
  discountList: Discount[];
35
35
  };
36
36
  setProductList(productList: Record<string, any>[]): void;
37
- scanCode(code: string): Promise<{
37
+ scanCode(code: string, customerId?: number): Promise<{
38
38
  isAvailable: boolean;
39
39
  productList: Record<string, any>[];
40
40
  discountList: Discount[];
@@ -304,14 +304,14 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
304
304
  }, {
305
305
  key: "scanCode",
306
306
  value: function () {
307
- var _scanCode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(code) {
307
+ var _scanCode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(code, customerId) {
308
308
  var _this$store$discount3, resultDiscountList, rulesModule, withScanList, _ref3, newProductList, newDiscountList, isAvailable, _this$options$otherPa6;
309
309
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
310
310
  while (1) switch (_context5.prev = _context5.next) {
311
311
  case 0:
312
312
  _context5.prev = 0;
313
313
  _context5.next = 3;
314
- return (_this$store$discount3 = this.store.discount) === null || _this$store$discount3 === void 0 ? void 0 : _this$store$discount3.batchSearch(code);
314
+ return (_this$store$discount3 = this.store.discount) === null || _this$store$discount3 === void 0 ? void 0 : _this$store$discount3.batchSearch(code, customerId);
315
315
  case 3:
316
316
  _context5.t0 = _context5.sent;
317
317
  if (_context5.t0) {
@@ -393,7 +393,7 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
393
393
  }
394
394
  }, _callee5, this, [[0, 23]]);
395
395
  }));
396
- function scanCode(_x4) {
396
+ function scanCode(_x4, _x5) {
397
397
  return _scanCode.apply(this, arguments);
398
398
  }
399
399
  return scanCode;
@@ -438,7 +438,7 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
438
438
  }
439
439
  }, _callee6, this);
440
440
  }));
441
- function emitDiscountListChange(_x5) {
441
+ function emitDiscountListChange(_x6) {
442
442
  return _emitDiscountListChange.apply(this, arguments);
443
443
  }
444
444
  return emitDiscountListChange;
@@ -574,7 +574,7 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
574
574
  }
575
575
  }, _callee7, this, [[0, 11]]);
576
576
  }));
577
- function getCustomerWallet(_x6) {
577
+ function getCustomerWallet(_x7) {
578
578
  return _getCustomerWallet.apply(this, arguments);
579
579
  }
580
580
  return getCustomerWallet;
@@ -634,7 +634,7 @@ export var ShopDiscountImpl = /*#__PURE__*/function (_BaseModule) {
634
634
  }
635
635
  }, _callee8, this, [[0, 18]]);
636
636
  }));
637
- function loadPrepareConfig(_x7) {
637
+ function loadPrepareConfig(_x8) {
638
638
  return _loadPrepareConfig.apply(this, arguments);
639
639
  }
640
640
  return loadPrepareConfig;
@@ -1 +1,12 @@
1
+ import { Discount } from "../../modules/Discount/types";
1
2
  export declare const uniqueById: <T>(arr: T[], key?: string) => T[];
3
+ /**
4
+ * 获取折扣金额 基于折扣卡类型计算
5
+ * 商品券:直接返回商品价格
6
+ * 折扣卡:根据折扣卡类型计算 固定金额:直接返回折扣卡金额 百分比:根据折扣卡金额计算
7
+ * @param discount
8
+ * @param total
9
+ * @param price
10
+ * @returns
11
+ */
12
+ export declare const getDiscountAmount: (discount: Discount, total: number, price: number) => number;
@@ -1,7 +1,36 @@
1
+ import Decimal from "decimal.js";
1
2
  export var uniqueById = function uniqueById(arr) {
2
3
  var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id';
3
4
  var seen = new Set();
4
5
  return arr.filter(function (item) {
5
6
  return !seen.has(item[key]) && seen.add(item[key]);
6
7
  });
8
+ };
9
+
10
+ /**
11
+ * 获取折扣金额 基于折扣卡类型计算
12
+ * 商品券:直接返回商品价格
13
+ * 折扣卡:根据折扣卡类型计算 固定金额:直接返回折扣卡金额 百分比:根据折扣卡金额计算
14
+ * @param discount
15
+ * @param total
16
+ * @param price
17
+ * @returns
18
+ */
19
+ export var getDiscountAmount = function getDiscountAmount(discount, total, price) {
20
+ var _discount$metadata;
21
+ // 商品券
22
+ if (discount.tag === 'good_pass') {
23
+ return new Decimal(total).minus(new Decimal(price || 0)).toNumber();
24
+ }
25
+
26
+ // 判断是否是固定金额
27
+ var isFixedAmount = (discount === null || discount === void 0 || (_discount$metadata = discount.metadata) === null || _discount$metadata === void 0 ? void 0 : _discount$metadata.discount_card_type) === 'fixed_amount';
28
+
29
+ // 固定金额 小于0时返回0
30
+ if (isFixedAmount) {
31
+ return Math.max(new Decimal(total).minus(new Decimal(discount.par_value || 0)).toNumber(), 0);
32
+ }
33
+
34
+ // 百分比:根据折扣卡金额计算
35
+ return new Decimal(100).minus(discount.par_value || 0).div(100).mul(new Decimal(total)).toNumber();
7
36
  };
@@ -44,6 +44,7 @@ __export(cartProduct_exports, {
44
44
  module.exports = __toCommonJS(cartProduct_exports);
45
45
  var import_decimal = __toESM(require("decimal.js"));
46
46
  var import_utils = require("../../Product/utils");
47
+ var import_utils2 = require("../../../solution/ShopDiscount/utils");
47
48
  var handleVariantProduct = (product) => {
48
49
  var _a;
49
50
  if (product == null ? void 0 : product.product_variant_id) {
@@ -183,9 +184,15 @@ var getProductTotalPrice = (params) => {
183
184
  }
184
185
  if (discounts == null ? void 0 : discounts.length) {
185
186
  discounts.forEach((currentValue) => {
187
+ var _a;
186
188
  if (currentValue.type !== "good_pass") {
187
- const discountPrice = new import_decimal.default(100).minus(currentValue.discount.percent || 0).div(100).mul(new import_decimal.default(price || 0)).toFixed(2);
188
- price = Number(discountPrice);
189
+ price = (0, import_utils2.getDiscountAmount)({
190
+ tag: currentValue.type,
191
+ par_value: currentValue.discount.percent,
192
+ metadata: {
193
+ discount_card_type: (_a = currentValue == null ? void 0 : currentValue.discount) == null ? void 0 : _a.discount_card_type
194
+ }
195
+ }, price, price);
189
196
  }
190
197
  });
191
198
  }
@@ -20,8 +20,9 @@ export declare class DiscountModule extends BaseModule implements Module, Discou
20
20
  with_discount_card: 0 | 1;
21
21
  customer_id: number;
22
22
  }): Promise<Discount[]>;
23
- batchSearch(code: string): Promise<Discount[]>;
23
+ batchSearch(code: string, customerId?: number): Promise<Discount[]>;
24
24
  filterEnabledDiscountList(discountList: Discount[]): Discount[];
25
+ private checkUsageCreditsLimit;
25
26
  uniqueByProductId(discountList: Discount[]): Discount[];
26
27
  filterDiscountListByProductIds(discountList: Discount[], productIds: number[]): Discount[];
27
28
  calcDiscountApplicableProductTotalPrice(discount: Discount): number | undefined;
@@ -89,22 +89,64 @@ var DiscountModule = class extends import_BaseModule.BaseModule {
89
89
  ) || [];
90
90
  return goodPassList;
91
91
  }
92
- async batchSearch(code) {
92
+ async batchSearch(code, customerId) {
93
93
  const result = await this.request.get(`/machinecode/batch-search`, {
94
94
  code,
95
95
  translate_flag: 1,
96
96
  tags: ["good_pass", "product_discount_card"],
97
97
  available: 1,
98
- relation_product: 1
98
+ relation_product: 1,
99
+ with: ["extensionData"],
100
+ order_behavior_count: 1,
101
+ order_behavior_count_customer_id: customerId || 1
99
102
  });
100
103
  const resultDiscountList = this.filterEnabledDiscountList((result == null ? void 0 : result.data) || []) || [];
101
104
  return resultDiscountList;
102
105
  }
103
106
  filterEnabledDiscountList(discountList) {
104
107
  return discountList.filter(
105
- (discount) => discount.limit_status === "enable" && new import_decimal.default((discount == null ? void 0 : discount.par_value) || 0).minus(new import_decimal.default((discount == null ? void 0 : discount.used_par_value) || 0)).greaterThan(0)
108
+ (discount) => discount.limit_status === "enable" && new import_decimal.default((discount == null ? void 0 : discount.par_value) || 0).minus(new import_decimal.default((discount == null ? void 0 : discount.used_par_value) || 0)).greaterThan(0) && this.checkUsageCreditsLimit(discount)
106
109
  );
107
110
  }
111
+ // 检查使用次数限制
112
+ checkUsageCreditsLimit(discount) {
113
+ if (!discount.extension_data || discount.extension_data.length === 0) {
114
+ return true;
115
+ }
116
+ const usageCreditsData = discount.extension_data.find(
117
+ (data) => data.field_key === "usage_credits"
118
+ );
119
+ if (!usageCreditsData) {
120
+ return true;
121
+ }
122
+ const value = usageCreditsData.value;
123
+ if (value.total_credits && value.total_credits > 0) {
124
+ if ((discount.total_order_behavior_count || 0) >= value.total_credits) {
125
+ return false;
126
+ }
127
+ }
128
+ if (value.per_user_limit && value.per_user_limit > 0) {
129
+ if ((discount.customer_order_behavior_count || 0) >= value.per_user_limit) {
130
+ return false;
131
+ }
132
+ }
133
+ if (value.max_per_day && value.max_per_day > 0) {
134
+ if ((discount.today_order_behavior_count || 0) >= value.max_per_day) {
135
+ return false;
136
+ }
137
+ }
138
+ if (value.max_per_week && value.max_per_week > 0) {
139
+ if ((discount.week_order_behavior_count || 0) >= value.max_per_week) {
140
+ return false;
141
+ }
142
+ }
143
+ if (value.max_per_month && value.max_per_month > 0) {
144
+ if ((discount.month_order_behavior_count || 0) >= value.max_per_month) {
145
+ return false;
146
+ }
147
+ }
148
+ return true;
149
+ }
108
150
  // 根据productIds去重
109
151
  uniqueByProductId(discountList) {
110
152
  return (0, import_utils.uniqueById)(discountList, "product_id");
@@ -36,6 +36,25 @@ interface ApplicableProductDetails {
36
36
  title?: string;
37
37
  };
38
38
  }
39
+ interface UsageCreditsValue {
40
+ total_credits: number;
41
+ per_user_limit: number;
42
+ max_per_day: number;
43
+ max_per_week: number;
44
+ max_per_month: number;
45
+ }
46
+ interface ExtensionData {
47
+ id: number;
48
+ shop_id: number;
49
+ product_id: number;
50
+ field_id: number;
51
+ field_key: string;
52
+ type: string;
53
+ value: UsageCreditsValue | Record<string, any> | string | number | boolean | any[];
54
+ created_at: string | null;
55
+ updated_at: string | null;
56
+ deleted_at: string | null;
57
+ }
39
58
  export interface Discount {
40
59
  id: number;
41
60
  product_name: string;
@@ -51,6 +70,9 @@ export interface Discount {
51
70
  limited_relation_product_data: Limitedrelationproductdata;
52
71
  balance: string;
53
72
  format_title: Formattitle;
73
+ metadata?: {
74
+ discount_card_type?: 'fixed_amount' | 'percent';
75
+ };
54
76
  product: Product;
55
77
  type: "product" | 'good_pass';
56
78
  resource_id?: number;
@@ -70,6 +92,12 @@ export interface Discount {
70
92
  appliedProductDetails: ApplicableProductDetails[];
71
93
  isDisabledForProductUsed?: boolean;
72
94
  amount?: number;
95
+ extension_data?: ExtensionData[];
96
+ total_order_behavior_count?: number;
97
+ today_order_behavior_count?: number;
98
+ week_order_behavior_count?: number;
99
+ month_order_behavior_count?: number;
100
+ customer_order_behavior_count?: number;
73
101
  }
74
102
  export interface DiscountState {
75
103
  discountList: Discount[];
@@ -131,6 +131,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
131
131
  return !discount.isManualSelect;
132
132
  });
133
133
  const sortedDiscountList = [...filteredDiscountList].sort((a, b) => {
134
+ var _a, _b;
134
135
  if (a.tag === "good_pass" && b.tag !== "good_pass")
135
136
  return -1;
136
137
  if (b.tag === "good_pass" && a.tag !== "good_pass")
@@ -138,10 +139,25 @@ var RulesModule = class extends import_BaseModule.BaseModule {
138
139
  if (a.tag === "good_pass" && b.tag === "good_pass") {
139
140
  return compareByExpireTime(a, b);
140
141
  } else if (a.tag === "product_discount_card" && b.tag === "product_discount_card") {
141
- if (a.par_value !== b.par_value) {
142
- const valueA = new import_decimal.default(100).minus(a.par_value || 0);
143
- const valueB = new import_decimal.default(100).minus(b.par_value || 0);
144
- return valueA.minus(valueB).toNumber();
142
+ const typeA = ((_a = a.metadata) == null ? void 0 : _a.discount_card_type) || "percent";
143
+ const typeB = ((_b = b.metadata) == null ? void 0 : _b.discount_card_type) || "percent";
144
+ if (typeA === "fixed_amount" && typeB === "percent")
145
+ return -1;
146
+ if (typeA === "percent" && typeB === "fixed_amount")
147
+ return 1;
148
+ if (typeA === "fixed_amount" && typeB === "fixed_amount") {
149
+ if (a.par_value !== b.par_value) {
150
+ const valueA = new import_decimal.default(a.par_value || 0);
151
+ const valueB = new import_decimal.default(b.par_value || 0);
152
+ return valueB.minus(valueA).toNumber();
153
+ }
154
+ }
155
+ if (typeA === "percent" && typeB === "percent") {
156
+ if (a.par_value !== b.par_value) {
157
+ const valueA = new import_decimal.default(100).minus(a.par_value || 0);
158
+ const valueB = new import_decimal.default(100).minus(b.par_value || 0);
159
+ return valueB.minus(valueA).toNumber();
160
+ }
145
161
  }
146
162
  return compareByExpireTime(a, b);
147
163
  }
@@ -181,7 +197,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
181
197
  sortedProductList.forEach((originProduct) => {
182
198
  const product = this.hooks.getProduct(originProduct);
183
199
  addModeDiscount.forEach((discount) => {
184
- var _a, _b, _c;
200
+ var _a, _b, _c, _d;
185
201
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
186
202
  const isLimitedProduct = limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id);
187
203
  const isAvailableProduct = !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount2) => discount2.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount2.tag || discount2.type))));
@@ -193,6 +209,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
193
209
  type: discount.tag || discount.type,
194
210
  tag: discount.tag || discount.type,
195
211
  discount: {
212
+ discount_card_type: (_d = discount == null ? void 0 : discount.metadata) == null ? void 0 : _d.discount_card_type,
196
213
  resource_id: discount.id,
197
214
  title: discount.format_title,
198
215
  original_amount: product.origin_total,
@@ -207,7 +224,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
207
224
  });
208
225
  console.log(sortedProductList, "sortedProductListsortedProductList");
209
226
  sortedProductList.forEach((originProduct, i) => {
210
- var _a, _b, _c, _d, _e, _f, _g, _h;
227
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
211
228
  const product = this.hooks.getProduct(originProduct);
212
229
  if ((product == null ? void 0 : product.booking_id) && ((_a = product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
213
230
  processedProductsMap.set(product._id, [originProduct]);
@@ -308,11 +325,12 @@ var RulesModule = class extends import_BaseModule.BaseModule {
308
325
  if (Number(((_h = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _h.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
309
326
  productOriginTotal = product.total;
310
327
  }
311
- const targetProductTotal = selectedDiscount2.tag === "good_pass" ? new import_decimal.default(productOriginTotal).minus(new import_decimal.default(product.price || 0)).toNumber() : new import_decimal.default(100).minus(selectedDiscount2.par_value || 0).div(100).mul(new import_decimal.default(productOriginTotal)).toNumber();
328
+ const targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, productOriginTotal, product.price);
312
329
  const discountDetail = {
313
330
  amount: new import_decimal.default(productOriginTotal).minus(new import_decimal.default(targetProductTotal)).toNumber(),
314
331
  type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag,
315
332
  discount: {
333
+ discount_card_type: (_i = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _i.discount_card_type,
316
334
  resource_id: selectedDiscount2.id,
317
335
  title: selectedDiscount2.format_title,
318
336
  original_amount: productOriginTotal,
@@ -694,7 +694,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
694
694
  }
695
695
  if (!allOriginResources.length) {
696
696
  const dateList = this.store.date.getDateList();
697
- dateList.forEach((n) => {
697
+ dateList == null ? void 0 : dateList.forEach((n) => {
698
698
  if (n.resource)
699
699
  allOriginResources.push(...n.resource);
700
700
  });
@@ -34,7 +34,7 @@ export declare class ShopDiscountImpl extends BaseModule implements Module {
34
34
  discountList: Discount[];
35
35
  };
36
36
  setProductList(productList: Record<string, any>[]): void;
37
- scanCode(code: string): Promise<{
37
+ scanCode(code: string, customerId?: number): Promise<{
38
38
  isAvailable: boolean;
39
39
  productList: Record<string, any>[];
40
40
  discountList: Discount[];
@@ -198,10 +198,10 @@ var ShopDiscountImpl = class extends import_BaseModule.BaseModule {
198
198
  this.store.productList = productList;
199
199
  }
200
200
  // 扫码输入code
201
- async scanCode(code) {
201
+ async scanCode(code, customerId) {
202
202
  var _a, _b;
203
203
  try {
204
- const resultDiscountList = await ((_a = this.store.discount) == null ? void 0 : _a.batchSearch(code)) || [];
204
+ const resultDiscountList = await ((_a = this.store.discount) == null ? void 0 : _a.batchSearch(code, customerId)) || [];
205
205
  const rulesModule = this.store.rules;
206
206
  if (!rulesModule) {
207
207
  return {
@@ -1 +1,12 @@
1
+ import { Discount } from "../../modules/Discount/types";
1
2
  export declare const uniqueById: <T>(arr: T[], key?: string) => T[];
3
+ /**
4
+ * 获取折扣金额 基于折扣卡类型计算
5
+ * 商品券:直接返回商品价格
6
+ * 折扣卡:根据折扣卡类型计算 固定金额:直接返回折扣卡金额 百分比:根据折扣卡金额计算
7
+ * @param discount
8
+ * @param total
9
+ * @param price
10
+ * @returns
11
+ */
12
+ export declare const getDiscountAmount: (discount: Discount, total: number, price: number) => number;
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,19 +16,41 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // src/solution/ShopDiscount/utils.ts
20
30
  var utils_exports = {};
21
31
  __export(utils_exports, {
32
+ getDiscountAmount: () => getDiscountAmount,
22
33
  uniqueById: () => uniqueById
23
34
  });
24
35
  module.exports = __toCommonJS(utils_exports);
36
+ var import_decimal = __toESM(require("decimal.js"));
25
37
  var uniqueById = (arr, key = "id") => {
26
38
  const seen = /* @__PURE__ */ new Set();
27
39
  return arr.filter((item) => !seen.has(item[key]) && seen.add(item[key]));
28
40
  };
41
+ var getDiscountAmount = (discount, total, price) => {
42
+ var _a;
43
+ if (discount.tag === "good_pass") {
44
+ return new import_decimal.default(total).minus(new import_decimal.default(price || 0)).toNumber();
45
+ }
46
+ const isFixedAmount = ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.discount_card_type) === "fixed_amount";
47
+ if (isFixedAmount) {
48
+ return Math.max(new import_decimal.default(total).minus(new import_decimal.default(discount.par_value || 0)).toNumber(), 0);
49
+ }
50
+ return new import_decimal.default(100).minus(discount.par_value || 0).div(100).mul(new import_decimal.default(total)).toNumber();
51
+ };
29
52
  // Annotate the CommonJS export names for ESM import in node:
30
53
  0 && (module.exports = {
54
+ getDiscountAmount,
31
55
  uniqueById
32
56
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "3.0.60",
4
+ "version": "3.0.62",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",