@pisell/pisellos 2.1.177 → 2.1.179

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/model/strategy/adapter/promotion/index.js +0 -9
  2. package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +2 -0
  3. package/dist/model/strategy/adapter/walletPass/evaluator.js +4 -2
  4. package/dist/modules/Discount/availability.d.ts +7 -0
  5. package/dist/modules/Discount/availability.js +52 -0
  6. package/dist/modules/Discount/entitlementPass.d.ts +34 -0
  7. package/dist/modules/Discount/entitlementPass.js +147 -0
  8. package/dist/modules/Discount/entitlementState.d.ts +8 -0
  9. package/dist/modules/Discount/entitlementState.js +205 -0
  10. package/dist/modules/Discount/index.d.ts +6 -3
  11. package/dist/modules/Discount/index.js +125 -23
  12. package/dist/modules/Discount/types.d.ts +34 -5
  13. package/dist/modules/Rules/entitlementLines.d.ts +8 -0
  14. package/dist/modules/Rules/entitlementLines.js +157 -0
  15. package/dist/modules/Rules/index.d.ts +12 -2
  16. package/dist/modules/Rules/index.js +377 -113
  17. package/dist/modules/Rules/types.d.ts +2 -2
  18. package/dist/plugins/window.d.ts +2 -0
  19. package/dist/solution/ScanOrder/index.js +52 -20
  20. package/dist/solution/ShopDiscount/index.d.ts +3 -0
  21. package/dist/solution/ShopDiscount/index.js +161 -78
  22. package/dist/solution/ShopDiscount/utils.d.ts +1 -0
  23. package/dist/solution/ShopDiscount/utils.js +40 -1
  24. package/lib/model/strategy/adapter/promotion/index.js +0 -49
  25. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +2 -0
  26. package/lib/model/strategy/adapter/walletPass/evaluator.js +4 -2
  27. package/lib/modules/Discount/availability.d.ts +7 -0
  28. package/lib/modules/Discount/availability.js +91 -0
  29. package/lib/modules/Discount/entitlementPass.d.ts +34 -0
  30. package/lib/modules/Discount/entitlementPass.js +184 -0
  31. package/lib/modules/Discount/entitlementState.d.ts +8 -0
  32. package/lib/modules/Discount/entitlementState.js +206 -0
  33. package/lib/modules/Discount/index.d.ts +6 -3
  34. package/lib/modules/Discount/index.js +66 -16
  35. package/lib/modules/Discount/types.d.ts +34 -5
  36. package/lib/modules/Rules/entitlementLines.d.ts +8 -0
  37. package/lib/modules/Rules/entitlementLines.js +158 -0
  38. package/lib/modules/Rules/index.d.ts +12 -2
  39. package/lib/modules/Rules/index.js +303 -69
  40. package/lib/modules/Rules/types.d.ts +2 -2
  41. package/lib/plugins/window.d.ts +2 -0
  42. package/lib/solution/ScanOrder/index.js +36 -6
  43. package/lib/solution/ShopDiscount/index.d.ts +3 -0
  44. package/lib/solution/ShopDiscount/index.js +78 -28
  45. package/lib/solution/ShopDiscount/utils.d.ts +1 -0
  46. package/lib/solution/ShopDiscount/utils.js +42 -1
  47. package/package.json +1 -1
@@ -1,4 +1,8 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
3
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
5
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
2
6
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
7
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
8
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -27,6 +31,8 @@ import { uniqueById } from "../../solution/ShopDiscount/utils";
27
31
  import { BaseModule } from "../BaseModule";
28
32
  import { DiscountHooks } from "./types";
29
33
  import Decimal from 'decimal.js';
34
+ import { isEntitlementPass } from "./entitlementPass";
35
+ import { getDiscountLifecycleReason, getDiscountUsageLimitFailKey } from "./availability";
30
36
  export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
31
37
  _inherits(DiscountModule, _BaseModule);
32
38
  var _super = _createSuper(DiscountModule);
@@ -153,8 +159,8 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
153
159
  key: "loadPrepareConfig",
154
160
  value: function () {
155
161
  var _loadPrepareConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
156
- var _prepareConfig$data, _prepareConfig$data2;
157
- var prepareConfig, goodPassList;
162
+ var _prepareConfig$data3, _prepareConfig$data4, _prepareConfig$data5;
163
+ var prepareConfig, _prepareConfig$data, _prepareConfig$data2, entitlementPassList, ids, rawDiscountList, goodPassList;
158
164
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
159
165
  while (1) switch (_context4.prev = _context4.next) {
160
166
  case 0:
@@ -162,9 +168,29 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
162
168
  return this.request.post("/order/prepare/config", params);
163
169
  case 2:
164
170
  prepareConfig = _context4.sent;
165
- goodPassList = this.filterEnabledDiscountList([].concat(_toConsumableArray((prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data = prepareConfig.data) === null || _prepareConfig$data === void 0 ? void 0 : _prepareConfig$data.good_pass_list) || []), _toConsumableArray((prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data2 = prepareConfig.data) === null || _prepareConfig$data2 === void 0 ? void 0 : _prepareConfig$data2.discount_card_list) || []))) || []; // this.setDiscountList(goodPassList);
166
- return _context4.abrupt("return", goodPassList);
171
+ if (params.with_entitlement_pass) {
172
+ _context4.next = 5;
173
+ break;
174
+ }
175
+ return _context4.abrupt("return", this.filterEnabledDiscountList([].concat(_toConsumableArray((prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data = prepareConfig.data) === null || _prepareConfig$data === void 0 ? void 0 : _prepareConfig$data.good_pass_list) || []), _toConsumableArray((prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data2 = prepareConfig.data) === null || _prepareConfig$data2 === void 0 ? void 0 : _prepareConfig$data2.discount_card_list) || [])).filter(function (item) {
176
+ return !isEntitlementPass(item);
177
+ })));
167
178
  case 5:
179
+ entitlementPassList = (prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data3 = prepareConfig.data) === null || _prepareConfig$data3 === void 0 ? void 0 : _prepareConfig$data3.entitlement_pass_list) || [];
180
+ ids = new Set(entitlementPassList.map(function (item) {
181
+ return String(item.id);
182
+ }));
183
+ rawDiscountList = [].concat(_toConsumableArray(entitlementPassList), _toConsumableArray(((prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data4 = prepareConfig.data) === null || _prepareConfig$data4 === void 0 ? void 0 : _prepareConfig$data4.good_pass_list) || []).filter(function (item) {
184
+ return !ids.has(String(item.id));
185
+ })), _toConsumableArray(((prepareConfig === null || prepareConfig === void 0 || (_prepareConfig$data5 = prepareConfig.data) === null || _prepareConfig$data5 === void 0 ? void 0 : _prepareConfig$data5.discount_card_list) || []).filter(function (item) {
186
+ return !ids.has(String(item.id));
187
+ })));
188
+ _context4.next = 10;
189
+ return this.setOriginalDiscountList(rawDiscountList);
190
+ case 10:
191
+ goodPassList = this.filterEnabledDiscountList(rawDiscountList); // this.setDiscountList(goodPassList);
192
+ return _context4.abrupt("return", goodPassList);
193
+ case 12:
168
194
  case "end":
169
195
  return _context4.stop();
170
196
  }
@@ -178,18 +204,22 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
178
204
  }, {
179
205
  key: "batchSearch",
180
206
  value: function () {
181
- var _batchSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(code, options) {
182
- var _ref, customerId, orderId, result, resultDiscountWithReason;
207
+ var _batchSearch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(code, customerIdOrOptions) {
208
+ var _this2 = this,
209
+ _entitlement$value,
210
+ _legacy$value;
211
+ var _ref, customerId, orderId, withEntitlementPass, params, _result, search, _yield$Promise$all, _yield$Promise$all2, legacy, entitlement, entitlementList, legacyList, ids, result;
183
212
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
184
213
  while (1) switch (_context5.prev = _context5.next) {
185
214
  case 0:
186
- _ref = options || {}, customerId = _ref.customerId, orderId = _ref.orderId;
187
- _context5.next = 3;
188
- return this.request.get("/machinecode/batch-search", _objectSpread(_objectSpread({
215
+ _ref = typeof customerIdOrOptions === 'number' ? {
216
+ customerId: customerIdOrOptions,
217
+ orderId: undefined,
218
+ withEntitlementPass: false
219
+ } : customerIdOrOptions || {}, customerId = _ref.customerId, orderId = _ref.orderId, withEntitlementPass = _ref.withEntitlementPass;
220
+ params = _objectSpread(_objectSpread({
189
221
  code: code,
190
222
  translate_flag: 1,
191
- tags: ['good_pass', 'product_discount_card'],
192
- // available: 1,
193
223
  relation_product: 1,
194
224
  with: ['extensionData', 'customScheduleSnapshot', 'holder.detail'],
195
225
  order_behavior_count: 1,
@@ -199,12 +229,75 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
199
229
  } : {}), {}, {
200
230
  unified_status_info_flag: 1,
201
231
  request_timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
232
+ });
233
+ if (withEntitlementPass) {
234
+ _context5.next = 7;
235
+ break;
236
+ }
237
+ _context5.next = 5;
238
+ return this.request.get("/machinecode/batch-search", _objectSpread(_objectSpread({}, params), {}, {
239
+ tags: ['good_pass', 'product_discount_card']
202
240
  }));
203
- case 3:
204
- result = _context5.sent;
205
- resultDiscountWithReason = this.filterEnabledDiscountListWithReason((result === null || result === void 0 ? void 0 : result.data) || []) || [];
206
- return _context5.abrupt("return", resultDiscountWithReason);
207
- case 6:
241
+ case 5:
242
+ _result = _context5.sent;
243
+ return _context5.abrupt("return", this.filterEnabledDiscountListWithReason(((_result === null || _result === void 0 ? void 0 : _result.data) || []).filter(function (item) {
244
+ return !isEntitlementPass(item);
245
+ })));
246
+ case 7:
247
+ // 旧券沿用统一状态校验;多次卡单独查询,保留暂不可用的卡供展示。
248
+ search = function search(query) {
249
+ return _this2.request.get("/machinecode/batch-search", query).then(function (value) {
250
+ return {
251
+ status: 'fulfilled',
252
+ value: value
253
+ };
254
+ }, function (reason) {
255
+ return {
256
+ status: 'rejected',
257
+ reason: reason
258
+ };
259
+ });
260
+ };
261
+ _context5.next = 10;
262
+ return Promise.all([search(_objectSpread(_objectSpread({}, params), {}, {
263
+ tags: ['good_pass', 'product_discount_card']
264
+ })), search(_objectSpread(_objectSpread({}, params), {}, {
265
+ tags: ['entitlement_pass'],
266
+ with_franchisor_data: 1,
267
+ unified_status_info_flag: 1,
268
+ with: [].concat(_toConsumableArray(params.with), ['franchiseeWalletPass:shop_id,source_shop_id,product_id,use'])
269
+ }))]);
270
+ case 10:
271
+ _yield$Promise$all = _context5.sent;
272
+ _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
273
+ legacy = _yield$Promise$all2[0];
274
+ entitlement = _yield$Promise$all2[1];
275
+ entitlementList = (entitlement.status === 'fulfilled' ? ((_entitlement$value = entitlement.value) === null || _entitlement$value === void 0 ? void 0 : _entitlement$value.data) || [] : []).filter(isEntitlementPass);
276
+ legacyList = legacy.status === 'fulfilled' ? ((_legacy$value = legacy.value) === null || _legacy$value === void 0 ? void 0 : _legacy$value.data) || [] : [];
277
+ ids = new Set(entitlementList.map(function (item) {
278
+ return String(item.id);
279
+ }));
280
+ result = this.filterEnabledDiscountListWithReason([].concat(_toConsumableArray(entitlementList), _toConsumableArray(legacyList.filter(function (item) {
281
+ return !isEntitlementPass(item) && !ids.has(String(item.id));
282
+ })))); // 任一类型查到可用卡即可继续,另一类型的查询失败不阻断该卡。
283
+ if (result.discountList.length) {
284
+ _context5.next = 23;
285
+ break;
286
+ }
287
+ if (!(legacy.status === 'rejected')) {
288
+ _context5.next = 21;
289
+ break;
290
+ }
291
+ throw legacy.reason;
292
+ case 21:
293
+ if (!(entitlement.status === 'rejected')) {
294
+ _context5.next = 23;
295
+ break;
296
+ }
297
+ throw entitlement.reason;
298
+ case 23:
299
+ return _context5.abrupt("return", result);
300
+ case 24:
208
301
  case "end":
209
302
  return _context5.stop();
210
303
  }
@@ -247,9 +340,9 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
247
340
  }, {
248
341
  key: "filterEnabledDiscountList",
249
342
  value: function filterEnabledDiscountList(discountList) {
250
- var _this2 = this;
343
+ var _this3 = this;
251
344
  return discountList.filter(function (discount) {
252
- 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);
345
+ return isEntitlementPass(discount) ? !getDiscountLifecycleReason(discount) : 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) && _this3.checkUsageCreditsLimit(discount);
253
346
  });
254
347
  }
255
348
 
@@ -316,14 +409,19 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
316
409
  }, {
317
410
  key: "filterEnabledDiscountListWithReason",
318
411
  value: function filterEnabledDiscountListWithReason(discountList) {
319
- var _this3 = this;
320
- var lists = discountList.filter(function (discount) {
412
+ var _this4 = this;
413
+ var entitlementPasses = discountList.filter(function (item) {
414
+ return isEntitlementPass(item) && !getDiscountLifecycleReason(item);
415
+ });
416
+ var lists = discountList.filter(function (item) {
417
+ return !isEntitlementPass(item);
418
+ }).filter(function (discount) {
321
419
  return discount.limit_status === 'enable';
322
420
  }).filter(function (discount) {
323
421
  var remaining = 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);
324
422
  return remaining;
325
423
  }).map(function (discount) {
326
- var limitFailKey = _this3.checkUsageCreditsLimitWithFailKey(discount);
424
+ var limitFailKey = _this4.checkUsageCreditsLimitWithFailKey(discount);
327
425
  return _objectSpread(_objectSpread({}, discount), {}, {
328
426
  usageLimitFailKey: limitFailKey
329
427
  });
@@ -338,8 +436,12 @@ export var DiscountModule = /*#__PURE__*/function (_BaseModule) {
338
436
  return item.usageLimitFailKey;
339
437
  });
340
438
  return {
341
- discountList: availableLists,
342
- unavailableReasonKey: currentFailDiscount === null || currentFailDiscount === void 0 ? void 0 : currentFailDiscount.usageLimitFailKey
439
+ discountList: [].concat(_toConsumableArray(entitlementPasses.map(function (item) {
440
+ return _objectSpread(_objectSpread({}, item), {}, {
441
+ usageLimitFailKey: getDiscountUsageLimitFailKey(item)
442
+ });
443
+ })), _toConsumableArray(availableLists)),
444
+ unavailableReasonKey: (currentFailDiscount === null || currentFailDiscount === void 0 ? void 0 : currentFailDiscount.usageLimitFailKey) || discountList.filter(isEntitlementPass).map(getDiscountLifecycleReason).find(Boolean) || null
343
445
  };
344
446
  }
345
447
 
@@ -45,6 +45,8 @@ interface ApplicableProductDetails {
45
45
  num: number;
46
46
  metadata?: {
47
47
  product_discount_difference?: number;
48
+ num?: number;
49
+ custom_product_bundle_map_id?: string;
48
50
  };
49
51
  discount?: {
50
52
  fixed_amount?: number;
@@ -63,10 +65,17 @@ interface UsageCreditsValue {
63
65
  max_per_week: number;
64
66
  max_per_month: number;
65
67
  }
68
+ export type IncludedUses = {
69
+ type: 'limited';
70
+ value: number;
71
+ } | {
72
+ type: 'unlimited';
73
+ value: null;
74
+ };
66
75
  export type DiscountFilterRejectKey = 'total_credits' | 'per_user_limit' | 'max_per_day' | 'max_per_week' | 'max_per_month';
67
76
  export interface DiscountWithReason {
68
77
  discountList: Discount[];
69
- unavailableReasonKey: DiscountFilterRejectKey | null;
78
+ unavailableReasonKey: DiscountFilterRejectKey | string | null;
70
79
  }
71
80
  interface ExtensionData {
72
81
  id: number;
@@ -93,19 +102,39 @@ export interface Discount {
93
102
  used_par_value: string;
94
103
  limit_status: string;
95
104
  limited_relation_product_data: Limitedrelationproductdata;
96
- balance: string;
105
+ balance?: string | number | null;
97
106
  format_title: Formattitle;
98
107
  metadata?: {
99
108
  discount_card_type?: 'fixed_amount' | 'percent';
100
109
  custom_product_bundle_map_id?: string;
101
110
  validity_type?: "custom_schedule_validity" | "fixed_validity";
102
- discount_calculation_mode: 'item_level' | 'order_level';
103
- holder: {
111
+ included_uses?: IncludedUses;
112
+ discount_calculation_mode?: 'item_level' | 'order_level';
113
+ holder?: {
104
114
  type: "customer" | "custom";
105
115
  };
106
116
  };
107
117
  product: Product;
108
- type: "product" | 'good_pass';
118
+ type?: 'product' | 'good_pass' | 'entitlement_pass' | 'discount_card' | 'product_discount_card';
119
+ shop_id?: number;
120
+ customer_id?: number | string | null;
121
+ holder?: {
122
+ holder_type?: string;
123
+ holder_id?: number | string | Array<number | string>;
124
+ [key: string]: any;
125
+ };
126
+ config?: {
127
+ isAvailable?: boolean;
128
+ maxUsagePerOrder?: number;
129
+ [key: string]: any;
130
+ };
131
+ reason?: string;
132
+ reasonCode?: string;
133
+ is_franchisor_pass?: number;
134
+ franchisee_wallet_pass?: {
135
+ use?: string;
136
+ };
137
+ committedAppliedProductDetails?: ApplicableProductDetails[];
109
138
  resource_id?: number;
110
139
  savedAmount?: number;
111
140
  isEditMode?: boolean;
@@ -0,0 +1,8 @@
1
+ import type { RulesParamsHooks } from './types';
2
+ export declare function isEntitlementRuntimeLine(product: any, mappedProduct?: any): boolean;
3
+ /** 仅同步本次多次卡行的数量与价格,沿用 Rules 的主商品、套餐计算结果。 */
4
+ export declare function rebuildEntitlementLine(origin: any, bundle: any[], hooks: RulesParamsHooks, count?: number): any;
5
+ /** 重算只还原本次多次卡;套餐和多 Holder 按父商品份数计算,不再合并商品行。 */
6
+ export declare function prepareEntitlementLines(products: any[], hooks: RulesParamsHooks, canUseEntitlement: (product: any) => boolean): any[];
7
+ /** 客户端钩子可能保留原 _id,只给新增的重复行补标识,保留拆分结果。 */
8
+ export declare function finalizeEntitlementLines(products: any[], hooks: RulesParamsHooks): any[];
@@ -0,0 +1,157 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ 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); }
7
+ import { cloneDeep } from 'lodash-es';
8
+ import Decimal from 'decimal.js';
9
+ import { isEntitlementPass } from "../Discount/entitlementPass";
10
+ import { hasEntitlementDiscount, isPersistedEntitlementLine } from "../Discount/entitlementState";
11
+ var SOURCE = 'entitlement_source_identity';
12
+ function isManualDiscount(data) {
13
+ var _data$isManualDiscoun, _data$discount_list;
14
+ return (_data$isManualDiscoun = data.isManualDiscount) !== null && _data$isManualDiscoun !== void 0 ? _data$isManualDiscoun : data.total != data.origin_total && (data.bundle || []).every(function (item) {
15
+ var _item$discount_list;
16
+ return !((_item$discount_list = item.discount_list) !== null && _item$discount_list !== void 0 && _item$discount_list.length);
17
+ }) && (!((_data$discount_list = data.discount_list) !== null && _data$discount_list !== void 0 && _data$discount_list.length) || data.discount_list.every(function (item) {
18
+ return item.type === 'product';
19
+ }));
20
+ }
21
+ export function isEntitlementRuntimeLine(product, mappedProduct) {
22
+ var _product$metadata;
23
+ return ((_product$metadata = product.metadata) === null || _product$metadata === void 0 ? void 0 : _product$metadata[SOURCE]) != null && !isPersistedEntitlementLine(product, mappedProduct) && !isManualDiscount(mappedProduct !== null && mappedProduct !== void 0 ? mappedProduct : product);
24
+ }
25
+
26
+ /** 仅同步本次多次卡行的数量与价格,沿用 Rules 的主商品、套餐计算结果。 */
27
+ export function rebuildEntitlementLine(origin, bundle, hooks, count) {
28
+ var _ref, _ref2, _ref3, _data$original_price;
29
+ var data = hooks.getProduct(origin);
30
+ var quantity = (_ref = (_ref2 = count !== null && count !== void 0 ? count : data.quantity) !== null && _ref2 !== void 0 ? _ref2 : data.num) !== null && _ref !== void 0 ? _ref : 1;
31
+ var discounts = (data.discount_list || []).map(function (item) {
32
+ var _item$metadata;
33
+ return isEntitlementPass(item) && !((_item$metadata = item.metadata) !== null && _item$metadata !== void 0 && _item$metadata.custom_product_bundle_map_id) ? _objectSpread(_objectSpread({}, item), {}, {
34
+ _num: quantity,
35
+ metadata: _objectSpread(_objectSpread({}, item.metadata), {}, {
36
+ num: quantity
37
+ })
38
+ }) : item;
39
+ });
40
+ var mainDiscount = discounts.filter(function (item) {
41
+ var _item$metadata2;
42
+ return !((_item$metadata2 = item.metadata) !== null && _item$metadata2 !== void 0 && _item$metadata2.custom_product_bundle_map_id);
43
+ }).reduce(function (sum, item) {
44
+ return sum.plus(item.amount || 0);
45
+ }, new Decimal(0));
46
+ var originalPrice = new Decimal((_ref3 = (_data$original_price = data.original_price) !== null && _data$original_price !== void 0 ? _data$original_price : data.price) !== null && _ref3 !== void 0 ? _ref3 : 0);
47
+ var sourceSellingPrice = originalPrice.minus(mainDiscount).toNumber();
48
+ var optionPrice = (data.options || []).reduce(function (sum, item) {
49
+ return sum.plus(new Decimal(item.price || 0).times(item.num || 0));
50
+ }, new Decimal(0));
51
+ var bundleTotal = bundle.reduce(function (sum, item) {
52
+ return sum.plus(new Decimal(item.price || 0).times(item.num || 1));
53
+ }, new Decimal(0));
54
+ var bundleOriginalTotal = bundle.reduce(function (sum, item) {
55
+ var _ref4, _item$original_price;
56
+ return sum.plus(new Decimal((_ref4 = (_item$original_price = item.original_price) !== null && _item$original_price !== void 0 ? _item$original_price : item.price) !== null && _ref4 !== void 0 ? _ref4 : 0).times(item.num || 1));
57
+ }, new Decimal(0));
58
+ return hooks.setProduct(origin, {
59
+ quantity: quantity,
60
+ discount_list: discounts,
61
+ bundle: bundle,
62
+ price: sourceSellingPrice,
63
+ main_product_selling_price: sourceSellingPrice,
64
+ total: new Decimal(sourceSellingPrice).plus(optionPrice).plus(bundleTotal).toNumber(),
65
+ origin_total: originalPrice.plus(optionPrice).plus(bundleOriginalTotal).toNumber()
66
+ });
67
+ }
68
+ function setLineIdentity(row, id) {
69
+ row._id = id;
70
+ row.metadata = _objectSpread(_objectSpread({}, row.metadata), {}, {
71
+ unique_identification_number: id
72
+ });
73
+ if (row._extend) row._extend.unique_identification_number = id;
74
+ return row;
75
+ }
76
+
77
+ /** 重算只还原本次多次卡;套餐和多 Holder 按父商品份数计算,不再合并商品行。 */
78
+ export function prepareEntitlementLines(products, hooks, canUseEntitlement) {
79
+ return products.flatMap(function (original) {
80
+ var _original$metadata, _origin$metadata$SOUR, _origin$metadata, _data$discount_list2, _ref8, _data$original_price2, _mainEntitlement$disc;
81
+ var data = hooks.getProduct(original);
82
+ if (isPersistedEntitlementLine(original, data) || isManualDiscount(data)) return [original];
83
+ var applicable = canUseEntitlement(original);
84
+ if (!((_original$metadata = original.metadata) !== null && _original$metadata !== void 0 && _original$metadata[SOURCE]) && !hasEntitlementDiscount(data) && !applicable) return [original];
85
+ var origin = cloneDeep(original);
86
+ origin.metadata = _objectSpread(_objectSpread({}, origin.metadata), {}, _defineProperty({}, SOURCE, (_origin$metadata$SOUR = (_origin$metadata = origin.metadata) === null || _origin$metadata === void 0 ? void 0 : _origin$metadata[SOURCE]) !== null && _origin$metadata$SOUR !== void 0 ? _origin$metadata$SOUR : data._id));
87
+ var bundle = (data.bundle || []).map(function (item) {
88
+ var _ref5, _ref6, _ref7, _item$original_price2, _entitlement$discount;
89
+ var entitlement = (item.discount_list || []).find(isEntitlementPass);
90
+ if (!entitlement) return item;
91
+ var discounts = (item.discount_list || []).filter(function (discount) {
92
+ return !isEntitlementPass(discount);
93
+ });
94
+ var originalPrice = (_ref5 = (_ref6 = (_ref7 = (_item$original_price2 = item.original_price) !== null && _item$original_price2 !== void 0 ? _item$original_price2 : (_entitlement$discount = entitlement.discount) === null || _entitlement$discount === void 0 ? void 0 : _entitlement$discount.original_amount) !== null && _ref7 !== void 0 ? _ref7 : item.product_price) !== null && _ref6 !== void 0 ? _ref6 : item.price) !== null && _ref5 !== void 0 ? _ref5 : 0;
95
+ var amount = discounts.reduce(function (sum, discount) {
96
+ return sum.plus(discount.amount || 0);
97
+ }, new Decimal(0));
98
+ var price = new Decimal(originalPrice).minus(amount.div(item.num || 1)).toNumber();
99
+ return _objectSpread(_objectSpread({}, item), {}, {
100
+ original_price: originalPrice,
101
+ price: price,
102
+ bundle_selling_price: price,
103
+ discount_list: discounts
104
+ });
105
+ });
106
+ var mainEntitlement = (_data$discount_list2 = data.discount_list) === null || _data$discount_list2 === void 0 ? void 0 : _data$discount_list2.find(isEntitlementPass);
107
+ var restored = hooks.setProduct(origin, {
108
+ original_price: (_ref8 = (_data$original_price2 = data.original_price) !== null && _data$original_price2 !== void 0 ? _data$original_price2 : mainEntitlement === null || mainEntitlement === void 0 || (_mainEntitlement$disc = mainEntitlement.discount) === null || _mainEntitlement$disc === void 0 ? void 0 : _mainEntitlement$disc.original_amount) !== null && _ref8 !== void 0 ? _ref8 : data.price,
109
+ discount_list: (data.discount_list || []).filter(function (item) {
110
+ return !isEntitlementPass(item);
111
+ }),
112
+ bundle: bundle
113
+ });
114
+ var row = rebuildEntitlementLine(restored, bundle, hooks);
115
+ var count = Math.max(1, Math.floor(Number(data.quantity || data.num || 1)));
116
+ if (!applicable || count <= 1 || !bundle.length && !Array.isArray(data.holder_id)) return [row];
117
+ return Array.from({
118
+ length: count
119
+ }, function (_, index) {
120
+ var _unit$_origin;
121
+ var unit = cloneDeep(row);
122
+ // booking 使用 _extend.holder_id,appointmentBooking 使用购物车 Holder。
123
+ for (var _i = 0, _arr = [unit, unit._extend]; _i < _arr.length; _i++) {
124
+ var source = _arr[_i];
125
+ if (Array.isArray(source === null || source === void 0 ? void 0 : source.holder_id)) source.holder_id = source.holder_id.slice(index, index + 1);
126
+ }
127
+ if (Array.isArray((_unit$_origin = unit._origin) === null || _unit$_origin === void 0 || (_unit$_origin = _unit$_origin.holder) === null || _unit$_origin === void 0 ? void 0 : _unit$_origin.form_record)) {
128
+ var records = unit._origin.holder.form_record;
129
+ // 单 Holder 的多份商品仍归属同一人;多个 Holder 才按份对应。
130
+ if (records.length > 1) unit._origin.holder.form_record = records.slice(index, index + 1);
131
+ }
132
+ return setLineIdentity(rebuildEntitlementLine(unit, hooks.getProduct(unit).bundle || [], hooks, 1), "".concat(data._id, ":entitlement-unit:").concat(index));
133
+ });
134
+ });
135
+ }
136
+
137
+ /** 客户端钩子可能保留原 _id,只给新增的重复行补标识,保留拆分结果。 */
138
+ export function finalizeEntitlementLines(products, hooks) {
139
+ var runtime = function runtime(item) {
140
+ return isEntitlementRuntimeLine(item, hooks.getProduct(item));
141
+ };
142
+ var seen = new Set(products.filter(function (item) {
143
+ return !runtime(item);
144
+ }).map(function (item) {
145
+ return String(item._id);
146
+ }));
147
+ return products.map(function (origin, index) {
148
+ var _origin$_id;
149
+ if (!runtime(origin)) return origin;
150
+ var base = String((_origin$_id = origin._id) !== null && _origin$_id !== void 0 ? _origin$_id : hooks.getProduct(origin)._id);
151
+ var id = base;
152
+ var ordinal = index;
153
+ while (seen.has(id)) id = "".concat(base, ":entitlement-split:").concat(ordinal++);
154
+ seen.add(id);
155
+ return id === String(origin._id) ? origin : setLineIdentity(cloneDeep(origin), id);
156
+ });
157
+ }
@@ -8,17 +8,26 @@ export declare class RulesModule extends BaseModule implements Module, RulesModu
8
8
  protected defaultVersion: string;
9
9
  private store;
10
10
  private hooks;
11
+ private entitlementPassEnabled;
12
+ private defaultWalletPassEvaluator?;
13
+ setEntitlementPassEnabled(enabled: boolean): void;
14
+ private getWalletPassEvaluator;
11
15
  constructor(name?: string, version?: string);
12
16
  initialize(core: PisellCore, options?: ModuleOptions): Promise<void>;
13
17
  setRulesList(rulesList: Rules[]): Promise<void>;
14
18
  getRulesList(): Rules[];
19
+ private normalizeHolderRecordIds;
20
+ /**
21
+ * 商品未开启 Holder 选择时跳过匹配;已开启时,特定 form Holder 卡
22
+ * 只适用于同一 Holder Record,是否必填不影响匹配规则。
23
+ */
15
24
  private checkHolderMatch;
16
25
  isDiscountListAvailable({ oldDiscountList, newDiscountList, productList, holders, isFormSubject, }: {
17
26
  oldDiscountList: Discount[];
18
27
  newDiscountList: Discount[];
19
28
  productList: any[];
20
29
  holders: {
21
- form_record_id: number;
30
+ form_record_id: number | string;
22
31
  }[];
23
32
  isFormSubject: boolean;
24
33
  }): {
@@ -26,6 +35,7 @@ export declare class RulesModule extends BaseModule implements Module, RulesModu
26
35
  discountList: Discount[];
27
36
  productList: any[];
28
37
  unavailableReason?: UnavailableReason;
38
+ evaluatedDiscountList?: Discount[];
29
39
  };
30
40
  filterDiscountListByType(discountList: Discount[], type: string): Discount[];
31
41
  private getUnavailableReason;
@@ -33,7 +43,7 @@ export declare class RulesModule extends BaseModule implements Module, RulesModu
33
43
  discountList: Discount[];
34
44
  productList: any[];
35
45
  holders: {
36
- form_record_id: number;
46
+ form_record_id: number | string;
37
47
  }[];
38
48
  isFormSubject: boolean;
39
49
  }, options?: {