@pisell/pisellos 2.2.22 → 2.2.24

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 (31) hide show
  1. package/dist/modules/Cart/utils/cartProduct.js +21 -4
  2. package/dist/modules/Customer/index.js +1 -1
  3. package/dist/modules/Discount/index.js +3 -3
  4. package/dist/modules/Discount/types.d.ts +9 -0
  5. package/dist/modules/Order/utils.js +1 -1
  6. package/dist/modules/Rules/index.js +401 -102
  7. package/dist/modules/Summary/utils.d.ts +6 -1
  8. package/dist/modules/Summary/utils.js +36 -1
  9. package/dist/server/modules/products/index.js +1 -0
  10. package/dist/solution/BookingByStep/index.d.ts +1 -0
  11. package/dist/solution/BookingByStep/index.js +12 -1
  12. package/dist/solution/Checkout/index.js +181 -108
  13. package/dist/solution/ShopDiscount/index.js +4 -3
  14. package/dist/solution/ShopDiscount/utils.d.ts +24 -0
  15. package/dist/solution/ShopDiscount/utils.js +135 -1
  16. package/lib/modules/Cart/utils/cartProduct.js +22 -6
  17. package/lib/modules/Customer/index.js +1 -1
  18. package/lib/modules/Discount/index.js +3 -3
  19. package/lib/modules/Discount/types.d.ts +9 -0
  20. package/lib/modules/Order/utils.js +1 -1
  21. package/lib/modules/Rules/index.js +290 -63
  22. package/lib/modules/Summary/utils.d.ts +6 -1
  23. package/lib/modules/Summary/utils.js +27 -3
  24. package/lib/server/modules/products/index.js +1 -0
  25. package/lib/solution/BookingByStep/index.d.ts +1 -0
  26. package/lib/solution/BookingByStep/index.js +12 -1
  27. package/lib/solution/Checkout/index.js +45 -6
  28. package/lib/solution/ShopDiscount/index.js +6 -6
  29. package/lib/solution/ShopDiscount/utils.d.ts +24 -0
  30. package/lib/solution/ShopDiscount/utils.js +88 -1
  31. package/package.json +1 -1
@@ -38,6 +38,8 @@ var import_utils2 = require("../Cart/utils");
38
38
  var import_decimal = __toESM(require("decimal.js"));
39
39
  var import_dayjs = __toESM(require("dayjs"));
40
40
  var import_lodash_es = require("lodash-es");
41
+ var import_utils3 = require("../../solution/ShopDiscount/utils");
42
+ var import_utils4 = require("../../solution/ShopDiscount/utils");
41
43
  var RulesModule = class extends import_BaseModule.BaseModule {
42
44
  constructor(name, version) {
43
45
  super(name, version);
@@ -151,6 +153,20 @@ var RulesModule = class extends import_BaseModule.BaseModule {
151
153
  addModeDiscount.push(discount);
152
154
  }
153
155
  });
156
+ const editModeOrderLevelProductIds = /* @__PURE__ */ new Set();
157
+ editModeDiscount.forEach((discount) => {
158
+ var _a, _b;
159
+ if (((_a = discount.discount) == null ? void 0 : _a.discount_calculation_mode) === "order_level") {
160
+ editModeOrderLevelProductIds.add((_b = discount == null ? void 0 : discount.discount) == null ? void 0 : _b.discount_product_id);
161
+ }
162
+ });
163
+ if (editModeOrderLevelProductIds.size > 0) {
164
+ addModeDiscount.forEach((discount) => {
165
+ if (editModeOrderLevelProductIds.has(discount.product_id)) {
166
+ discount.isDisabled = true;
167
+ }
168
+ });
169
+ }
154
170
  const filteredDiscountList = addModeDiscount.filter((discount) => {
155
171
  return !discount.isManualSelect;
156
172
  });
@@ -198,51 +214,95 @@ var RulesModule = class extends import_BaseModule.BaseModule {
198
214
  return flattened;
199
215
  };
200
216
  const sortedDiscountList = [...filteredDiscountList].sort((a, b) => {
201
- var _a, _b;
202
- if (a.tag === "good_pass" && b.tag !== "good_pass")
203
- return -1;
204
- if (b.tag === "good_pass" && a.tag !== "good_pass")
205
- return 1;
206
- if (a.tag === "good_pass" && b.tag === "good_pass") {
207
- return compareByExpireTime(a, b);
208
- } else if (a.tag === "product_discount_card" && b.tag === "product_discount_card") {
209
- const typeA = ((_a = a.metadata) == null ? void 0 : _a.discount_card_type) || "percent";
210
- const typeB = ((_b = b.metadata) == null ? void 0 : _b.discount_card_type) || "percent";
217
+ const isHolderDiscount = (discount) => {
218
+ var _a, _b;
219
+ return ((_b = (_a = discount.metadata) == null ? void 0 : _a.holder) == null ? void 0 : _b.type) === "custom";
220
+ };
221
+ const isGoodPass = (discount) => discount.tag === "good_pass";
222
+ const isOrderLevelDiscount = (discount) => {
223
+ var _a;
224
+ return discount.tag === "product_discount_card" && ((_a = discount.metadata) == null ? void 0 : _a.discount_calculation_mode) === "order_level";
225
+ };
226
+ const isItemLevelDiscount = (discount) => {
227
+ var _a;
228
+ return discount.tag === "product_discount_card" && ((_a = discount.metadata) == null ? void 0 : _a.discount_calculation_mode) !== "order_level";
229
+ };
230
+ const getPriority = (discount) => {
231
+ const isHolder = isHolderDiscount(discount);
232
+ if (isHolder) {
233
+ if (isGoodPass(discount))
234
+ return 1;
235
+ if (isItemLevelDiscount(discount))
236
+ return 2;
237
+ if (isOrderLevelDiscount(discount))
238
+ return 3;
239
+ } else {
240
+ if (isGoodPass(discount))
241
+ return 4;
242
+ if (isItemLevelDiscount(discount))
243
+ return 5;
244
+ if (isOrderLevelDiscount(discount))
245
+ return 6;
246
+ }
247
+ return 7;
248
+ };
249
+ function compareByExpireTime(itemA, itemB) {
250
+ if (itemA.expire_time && itemB.expire_time) {
251
+ const timeA = new Date(itemA.expire_time).getTime();
252
+ const timeB = new Date(itemB.expire_time).getTime();
253
+ return timeA - timeB;
254
+ }
255
+ return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
256
+ }
257
+ function compareDiscountCardValue(itemA, itemB) {
258
+ var _a, _b;
259
+ const typeA = ((_a = itemA.metadata) == null ? void 0 : _a.discount_card_type) || "percent";
260
+ const typeB = ((_b = itemB.metadata) == null ? void 0 : _b.discount_card_type) || "percent";
211
261
  if (typeA === "fixed_amount" && typeB === "percent")
212
262
  return -1;
213
263
  if (typeA === "percent" && typeB === "fixed_amount")
214
264
  return 1;
215
265
  if (typeA === "fixed_amount" && typeB === "fixed_amount") {
216
- if (a.par_value !== b.par_value) {
217
- const valueA = new import_decimal.default(a.par_value || 0);
218
- const valueB = new import_decimal.default(b.par_value || 0);
266
+ if (itemA.par_value !== itemB.par_value) {
267
+ const valueA = new import_decimal.default(itemA.par_value || 0);
268
+ const valueB = new import_decimal.default(itemB.par_value || 0);
219
269
  return valueB.minus(valueA).toNumber();
220
270
  }
221
271
  }
222
272
  if (typeA === "percent" && typeB === "percent") {
223
- if (a.par_value !== b.par_value) {
224
- const valueA = new import_decimal.default(100).minus(a.par_value || 0);
225
- const valueB = new import_decimal.default(100).minus(b.par_value || 0);
273
+ if (itemA.par_value !== itemB.par_value) {
274
+ const valueA = new import_decimal.default(100).minus(itemA.par_value || 0);
275
+ const valueB = new import_decimal.default(100).minus(itemB.par_value || 0);
226
276
  return valueA.minus(valueB).toNumber();
227
277
  }
228
278
  }
279
+ return 0;
280
+ }
281
+ const priorityA = getPriority(a);
282
+ const priorityB = getPriority(b);
283
+ if (priorityA !== priorityB) {
284
+ return priorityA - priorityB;
285
+ }
286
+ if (isGoodPass(a) && isGoodPass(b)) {
229
287
  return compareByExpireTime(a, b);
230
288
  }
231
- return compareByExpireTime(a, b);
232
- function compareByExpireTime(itemA, itemB) {
233
- if (itemA.expire_time && itemB.expire_time) {
234
- const timeA = new Date(itemA.expire_time).getTime();
235
- const timeB = new Date(itemB.expire_time).getTime();
236
- return timeA - timeB;
237
- }
238
- return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
289
+ if (a.tag === "product_discount_card" && b.tag === "product_discount_card") {
290
+ const valueCompare = compareDiscountCardValue(a, b);
291
+ if (valueCompare !== 0)
292
+ return valueCompare;
293
+ return compareByExpireTime(a, b);
239
294
  }
295
+ return compareByExpireTime(a, b);
240
296
  });
241
297
  const flattenedList = flattenProductsWithBundle(productList);
242
298
  const sortedFlattenedList = flattenedList.sort((a, b) => {
243
299
  var _a, _b;
244
- const priceA = new import_decimal.default(a.price || "0");
245
- const priceB = new import_decimal.default(b.price || "0");
300
+ const priceA = new import_decimal.default(
301
+ a.type === "bundle" ? a.original_price ?? a.price ?? "0" : a.price || "0"
302
+ );
303
+ const priceB = new import_decimal.default(
304
+ b.type === "bundle" ? b.original_price ?? b.price ?? "0" : b.price || "0"
305
+ );
246
306
  if (priceA.equals(priceB)) {
247
307
  if (a.type !== b.type) {
248
308
  return a.type === "main" ? -1 : 1;
@@ -266,9 +326,132 @@ var RulesModule = class extends import_BaseModule.BaseModule {
266
326
  discountApplicability.set(discount.id, []);
267
327
  discountApplicableProducts.set(discount.id, []);
268
328
  });
329
+ const orderLevelDiscountAllocations = /* @__PURE__ */ new Map();
330
+ const orderLevelDiscountApplicableItems = /* @__PURE__ */ new Map();
331
+ const itemApplicableDiscounts = /* @__PURE__ */ new Map();
332
+ const checkItemApplicableForDiscount = (flatItem, discount) => {
333
+ var _a, _b, _c, _d, _e, _f, _g, _h;
334
+ let product;
335
+ if (flatItem.type === "main") {
336
+ product = flatItem.product;
337
+ } else {
338
+ product = {
339
+ _id: flatItem._id,
340
+ id: flatItem.id,
341
+ price: flatItem.price,
342
+ quantity: flatItem.quantity,
343
+ num: flatItem.num,
344
+ booking_id: flatItem.booking_id,
345
+ discount_list: flatItem.discount_list || [],
346
+ startDate: (_a = flatItem.parentProduct) == null ? void 0 : _a.startDate
347
+ };
348
+ }
349
+ const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.length) && ((_c = product == null ? void 0 : product.discount_list) == null ? void 0 : _c.every((d) => d.id && ["good_pass", "discount_card", "product_discount_card"].includes(d.tag || d.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && !!((_e = (_d = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _d.discount_list) == null ? void 0 : _e.length) && ((_g = (_f = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _f.discount_list) == null ? void 0 : _g.every((d) => d.id)));
350
+ if (!isAvailableProduct) {
351
+ return false;
352
+ }
353
+ if ((0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
354
+ return false;
355
+ }
356
+ if (Number(product.price) <= 0 || !product.price) {
357
+ return false;
358
+ }
359
+ const limitedData = discount.limited_relation_product_data;
360
+ const timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
361
+ if (!timeLimit) {
362
+ return false;
363
+ }
364
+ let isLimitedProduct = false;
365
+ if (limitedData.type === "product_all") {
366
+ if (limitedData.filter === 1 && ((_h = limitedData.exclude_product_ids) == null ? void 0 : _h.includes(product.id))) {
367
+ isLimitedProduct = false;
368
+ } else {
369
+ isLimitedProduct = true;
370
+ }
371
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
372
+ isLimitedProduct = true;
373
+ }
374
+ const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
375
+ return isLimitedProduct && isBundleAvailable;
376
+ };
377
+ const selectedOrderLevelDiscounts = sortedDiscountList.filter((discount) => {
378
+ return (0, import_utils3.isOrderLevelFixedAmountDiscount)(discount) && discount.isSelected !== false;
379
+ });
380
+ selectedOrderLevelDiscounts.forEach((discount) => {
381
+ const applicableItemIds = /* @__PURE__ */ new Set();
382
+ sortedFlattenedList.forEach((flatItem) => {
383
+ if (checkItemApplicableForDiscount(flatItem, discount)) {
384
+ applicableItemIds.add(flatItem._id);
385
+ if (!itemApplicableDiscounts.has(flatItem._id)) {
386
+ itemApplicableDiscounts.set(flatItem._id, /* @__PURE__ */ new Set());
387
+ }
388
+ itemApplicableDiscounts.get(flatItem._id).add(discount.id);
389
+ }
390
+ });
391
+ orderLevelDiscountApplicableItems.set(discount.id, applicableItemIds);
392
+ });
393
+ const occupiedItems = /* @__PURE__ */ new Map();
394
+ selectedOrderLevelDiscounts.forEach((discount) => {
395
+ const limitedData = discount.limited_relation_product_data;
396
+ const isExclusiveDiscount = limitedData.type !== "product_all";
397
+ if (isExclusiveDiscount) {
398
+ const applicableItems = orderLevelDiscountApplicableItems.get(discount.id);
399
+ if (applicableItems) {
400
+ applicableItems.forEach((itemId) => {
401
+ if (!occupiedItems.has(itemId)) {
402
+ occupiedItems.set(itemId, discount.id);
403
+ }
404
+ });
405
+ }
406
+ }
407
+ });
408
+ console.log("occupiedItems", occupiedItems, "orderLevelDiscountApplicableItems", orderLevelDiscountApplicableItems);
409
+ sortedDiscountList.forEach((discount) => {
410
+ if (!(0, import_utils3.isOrderLevelFixedAmountDiscount)(discount)) {
411
+ return;
412
+ }
413
+ const applicableProducts = [];
414
+ sortedFlattenedList.forEach((flatItem) => {
415
+ var _a, _b;
416
+ const occupyingDiscountId = occupiedItems.get(flatItem._id);
417
+ if (occupyingDiscountId !== void 0 && occupyingDiscountId !== discount.id) {
418
+ return;
419
+ }
420
+ if (!checkItemApplicableForDiscount(flatItem, discount)) {
421
+ return;
422
+ }
423
+ let product;
424
+ if (flatItem.type === "main") {
425
+ product = flatItem.product;
426
+ } else {
427
+ product = {
428
+ _id: flatItem._id,
429
+ id: flatItem.id,
430
+ price: flatItem.price,
431
+ quantity: flatItem.quantity,
432
+ num: flatItem.num
433
+ };
434
+ }
435
+ const quantity = flatItem.type === "main" ? product.quantity || 1 : (product.num || 1) * (((_a = flatItem.parentProduct) == null ? void 0 : _a.quantity) || 1);
436
+ const productData = {
437
+ productId: flatItem._id,
438
+ amount: Number(product.price || 0),
439
+ quantity
440
+ };
441
+ if (flatItem.type === "bundle") {
442
+ productData.parentQuantity = ((_b = flatItem.parentProduct) == null ? void 0 : _b.quantity) || 1;
443
+ }
444
+ applicableProducts.push(productData);
445
+ });
446
+ if (applicableProducts.length > 0) {
447
+ const allocation = (0, import_utils4.calculateOrderLevelDiscountAllocation)(discount, applicableProducts);
448
+ orderLevelDiscountAllocations.set(discount.id, allocation);
449
+ }
450
+ });
269
451
  const processedProductsMap = /* @__PURE__ */ new Map();
270
452
  const appliedDiscountProducts = /* @__PURE__ */ new Map();
271
453
  sortedFlattenedList.forEach((flatItem) => {
454
+ var _a;
272
455
  let product, originProduct;
273
456
  if (flatItem.type === "main") {
274
457
  product = flatItem.product;
@@ -283,12 +466,13 @@ var RulesModule = class extends import_BaseModule.BaseModule {
283
466
  total: flatItem.total,
284
467
  origin_total: flatItem.origin_total,
285
468
  booking_id: flatItem.booking_id,
286
- discount_list: flatItem.discount_list || []
469
+ discount_list: flatItem.discount_list || [],
470
+ startDate: (_a = flatItem.parentProduct) == null ? void 0 : _a.startDate
287
471
  };
288
472
  originProduct = flatItem.originProduct;
289
473
  }
290
474
  addModeDiscount.forEach((discount) => {
291
- var _a, _b, _c, _d, _e, _f;
475
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
292
476
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
293
477
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
294
478
  const isHolderMatch = this.checkHolderMatch(
@@ -302,10 +486,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
302
486
  let timeLimit = true;
303
487
  timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
304
488
  const isLimitedProduct = (limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
305
- const isAvailableProduct = flatItem.type === "main" ? !((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)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
489
+ const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a2 = product == null ? void 0 : product.discount_list) == null ? void 0 : _a2.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every(
490
+ (discount2) => discount2.id && [
491
+ "good_pass",
492
+ "discount_card",
493
+ "product_discount_card"
494
+ ].includes(discount2.tag || discount2.type)
495
+ ))) : !((flatItem == null ? void 0 : flatItem.booking_id) && !!((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.discount_list) == null ? void 0 : _d.length) && ((_f = (_e = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _e.discount_list) == null ? void 0 : _f.every((discount2) => discount2.id)));
306
496
  const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
307
497
  if (isAvailableProduct && isLimitedProduct && isBundleAvailable && timeLimit) {
308
- (_e = discountApplicability.get(discount.id)) == null ? void 0 : _e.push(product.id);
498
+ (_g = discountApplicability.get(discount.id)) == null ? void 0 : _g.push(product.id);
309
499
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
310
500
  const discountType = discount.tag || discount.type;
311
501
  const isGoodPass = discountType === "good_pass";
@@ -315,8 +505,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
315
505
  type: discountType,
316
506
  tag: discountType,
317
507
  discount: {
318
- discount_card_type: (_f = discount == null ? void 0 : discount.metadata) == null ? void 0 : _f.discount_card_type,
508
+ discount_card_type: (_h = discount == null ? void 0 : discount.metadata) == null ? void 0 : _h.discount_card_type,
319
509
  fixed_amount: product.price,
510
+ discount_calculation_mode: (_i = discount == null ? void 0 : discount.metadata) == null ? void 0 : _i.discount_calculation_mode,
320
511
  resource_id: discount.id,
321
512
  title: discount.format_title,
322
513
  original_amount: product.price || product.origin_total,
@@ -334,7 +525,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
334
525
  });
335
526
  const processedFlatItemsMap = /* @__PURE__ */ new Map();
336
527
  sortedFlattenedList.forEach((flatItem, index) => {
337
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
528
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
338
529
  let product, originProduct;
339
530
  if (flatItem.type === "main") {
340
531
  product = flatItem.product;
@@ -350,11 +541,12 @@ var RulesModule = class extends import_BaseModule.BaseModule {
350
541
  original_price: (_a = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _a.original_price,
351
542
  origin_total: (_b = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _b.original_price,
352
543
  booking_id: flatItem.booking_id,
353
- discount_list: ((_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.discount_list) || []
544
+ discount_list: ((_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.discount_list) || [],
545
+ startDate: (_d = flatItem.parentProduct) == null ? void 0 : _d.startDate
354
546
  };
355
547
  originProduct = flatItem.originProduct;
356
548
  }
357
- if ((product == null ? void 0 : product.booking_id) && ((_d = product.discount_list) == null ? void 0 : _d.length) && ((_e = product == null ? void 0 : product.discount_list) == null ? void 0 : _e.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
549
+ if ((product == null ? void 0 : product.booking_id) && ((_e = product.discount_list) == null ? void 0 : _e.length) && ((_f = product == null ? void 0 : product.discount_list) == null ? void 0 : _f.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
358
550
  if (flatItem.type === "main") {
359
551
  processedProductsMap.set(product._id, [originProduct]);
360
552
  } else {
@@ -367,10 +559,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
367
559
  }
368
560
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
369
561
  var _a2, _b2;
370
- if ((Number(product.price) <= 0 || !product.price) && !((_a2 = product.discount_list) == null ? void 0 : _a2.find((n) => {
371
- var _a3;
372
- return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
373
- })) && (discount.tag || discount.type) === "good_pass")
562
+ if ((Number(product.price) <= 0 || !product.price) && !((_a2 = product.discount_list) == null ? void 0 : _a2.length) && (discount.tag || discount.type) === "good_pass")
374
563
  return false;
375
564
  if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
376
565
  var _a3;
@@ -417,28 +606,28 @@ var RulesModule = class extends import_BaseModule.BaseModule {
417
606
  isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every((item) => {
418
607
  var _a2;
419
608
  return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
420
- }) && (!((_f = product.discount_list) == null ? void 0 : _f.length) || ((_h = (_g = product == null ? void 0 : product.discount_list) == null ? void 0 : _g.every) == null ? void 0 : _h.call(_g, (item) => item.type === "product")));
609
+ }) && (!((_g = product.discount_list) == null ? void 0 : _g.length) || ((_i = (_h = product == null ? void 0 : product.discount_list) == null ? void 0 : _h.every) == null ? void 0 : _i.call(_h, (item) => item.type === "product")));
421
610
  } else {
422
611
  const parentProduct = flatItem.parentProduct;
423
612
  if (parentProduct) {
424
613
  isManualDiscount = typeof parentProduct.isManualDiscount === "boolean" ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every((item) => {
425
614
  var _a2;
426
615
  return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
427
- }) && (!((_i = parentProduct.discount_list) == null ? void 0 : _i.length) || ((_k = (_j = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _j.every) == null ? void 0 : _k.call(_j, (item) => item.type === "product")));
616
+ }) && (!((_j = parentProduct.discount_list) == null ? void 0 : _j.length) || ((_l = (_k = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _k.every) == null ? void 0 : _l.call(_k, (item) => item.type === "product")));
428
617
  }
429
618
  }
430
619
  if (options == null ? void 0 : options.discountId) {
431
- if (flatItem.type === "main" && ((_l = product.discount_list) == null ? void 0 : _l.some((item) => {
620
+ if (flatItem.type === "main" && ((_m = product.discount_list) == null ? void 0 : _m.some((item) => {
432
621
  var _a2;
433
622
  return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
434
623
  }))) {
435
624
  isManualDiscount = false;
436
625
  }
437
626
  if (flatItem.type === "bundle") {
438
- if (((_m = product.discount_list) == null ? void 0 : _m.some((item) => {
627
+ if (((_n = product.discount_list) == null ? void 0 : _n.some((item) => {
439
628
  var _a2;
440
629
  return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
441
- })) || ((_o = (_n = flatItem.parentProduct) == null ? void 0 : _n.discount_list) == null ? void 0 : _o.some((item) => {
630
+ })) || ((_p = (_o = flatItem.parentProduct) == null ? void 0 : _o.discount_list) == null ? void 0 : _p.some((item) => {
442
631
  var _a2;
443
632
  return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
444
633
  }))) {
@@ -447,7 +636,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
447
636
  }
448
637
  }
449
638
  if (options == null ? void 0 : options.selectedList) {
450
- if (flatItem.type === "main" && ((_p = product.discount_list) == null ? void 0 : _p.some((item) => {
639
+ if (flatItem.type === "main" && ((_q = product.discount_list) == null ? void 0 : _q.some((item) => {
451
640
  var _a2;
452
641
  return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
453
642
  var _a3;
@@ -457,13 +646,13 @@ var RulesModule = class extends import_BaseModule.BaseModule {
457
646
  isManualDiscount = false;
458
647
  }
459
648
  if (flatItem.type === "bundle") {
460
- if (((_q = product.discount_list) == null ? void 0 : _q.some((item) => {
649
+ if (((_r = product.discount_list) == null ? void 0 : _r.some((item) => {
461
650
  var _a2;
462
651
  return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
463
652
  var _a3;
464
653
  return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
465
654
  });
466
- })) || ((_s = (_r = flatItem.parentProduct) == null ? void 0 : _r.discount_list) == null ? void 0 : _s.some((item) => {
655
+ })) || ((_t = (_s = flatItem.parentProduct) == null ? void 0 : _s.discount_list) == null ? void 0 : _t.some((item) => {
467
656
  var _a2;
468
657
  return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
469
658
  var _a3;
@@ -550,22 +739,35 @@ var RulesModule = class extends import_BaseModule.BaseModule {
550
739
  usedDiscounts.set(selectedDiscount2.id, true);
551
740
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
552
741
  let productOriginTotal = product.origin_total || product.total || 0;
553
- if (((_t = product.discount_list) == null ? void 0 : _t.length) && product.origin_total) {
742
+ if (((_u = product.discount_list) == null ? void 0 : _u.length) && product.origin_total) {
554
743
  productOriginTotal = product.origin_total;
555
744
  }
556
- if (Number(((_u = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _u.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
745
+ if (Number(((_v = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _v.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
557
746
  productOriginTotal = product.total;
558
747
  }
559
- const targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
748
+ const isOrderLevel = (0, import_utils3.isOrderLevelFixedAmountDiscount)(selectedDiscount2);
749
+ const orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount2.id) : null;
750
+ const productAllocation = orderLevelAllocation == null ? void 0 : orderLevelAllocation.get(flatItem._id);
751
+ let targetProductTotal;
752
+ let amount;
753
+ let productDiscountDifference;
754
+ if (isOrderLevel && productAllocation) {
755
+ amount = productAllocation.discountAmount;
756
+ productDiscountDifference = productAllocation.difference;
757
+ targetProductTotal = Math.max(new import_decimal.default(product.price).minus(amount).toNumber(), 0);
758
+ } else {
759
+ targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
760
+ amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
761
+ }
560
762
  const discountType = selectedDiscount2.tag || selectedDiscount2.type;
561
763
  const isGoodPass = discountType === "good_pass";
562
- const amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
563
764
  const discountDetail = {
564
765
  amount,
565
766
  type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : discountType,
566
767
  discount: {
567
- discount_card_type: (_v = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _v.discount_card_type,
768
+ discount_card_type: (_w = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _w.discount_card_type,
568
769
  fixed_amount: amount,
770
+ discount_calculation_mode: (_x = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _x.discount_calculation_mode,
569
771
  resource_id: selectedDiscount2.id,
570
772
  title: selectedDiscount2.format_title,
571
773
  original_amount: product.price,
@@ -575,7 +777,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
575
777
  // 前端使用的num数量,为了计算优惠金额
576
778
  _num: isGoodPass ? 1 : product.num,
577
779
  metadata: {
578
- num: 1
780
+ num: 1,
781
+ // 🔥 order_level 分摊差值
782
+ ...productDiscountDifference !== void 0 && { product_discount_difference: productDiscountDifference }
579
783
  }
580
784
  };
581
785
  appliedProducts.push(discountDetail);
@@ -632,6 +836,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
632
836
  type: "good_pass",
633
837
  discount: {
634
838
  fixed_amount: product.origin_total,
839
+ discount_calculation_mode: (_y = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _y.discount_calculation_mode,
635
840
  resource_id: selectedDiscount2.id,
636
841
  title: selectedDiscount2.format_title,
637
842
  original_amount: product.origin_total,
@@ -676,18 +881,33 @@ var RulesModule = class extends import_BaseModule.BaseModule {
676
881
  const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[0];
677
882
  usedDiscounts.set(selectedDiscount2.id, true);
678
883
  const productOriginTotal = product.original_price || product.price || 0;
679
- const targetProductTotal = (0, import_utils.getDiscountAmount)(
680
- selectedDiscount2,
681
- productOriginTotal,
682
- productOriginTotal
683
- );
884
+ const isOrderLevel = (0, import_utils3.isOrderLevelFixedAmountDiscount)(selectedDiscount2);
885
+ const orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount2.id) : null;
886
+ const productAllocation = orderLevelAllocation == null ? void 0 : orderLevelAllocation.get(flatItem._id);
887
+ let targetProductTotal;
888
+ let fixedAmountPerItem;
889
+ let productDiscountDifference;
890
+ if (isOrderLevel && productAllocation) {
891
+ fixedAmountPerItem = productAllocation.discountAmount;
892
+ productDiscountDifference = productAllocation.difference;
893
+ targetProductTotal = Math.max(new import_decimal.default(productOriginTotal).minus(fixedAmountPerItem).toNumber(), 0);
894
+ } else {
895
+ targetProductTotal = (0, import_utils.getDiscountAmount)(
896
+ selectedDiscount2,
897
+ productOriginTotal,
898
+ productOriginTotal
899
+ );
900
+ fixedAmountPerItem = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
901
+ }
902
+ debugger;
684
903
  const uniqueId = flatItem._id;
685
904
  const discountDetail = {
686
- amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber() * (product.num || 1),
905
+ amount: fixedAmountPerItem * (product.num || 1),
687
906
  type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag,
688
907
  discount: {
689
- discount_card_type: (_w = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _w.discount_card_type,
690
- fixed_amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber(),
908
+ discount_card_type: (_z = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _z.discount_card_type,
909
+ fixed_amount: fixedAmountPerItem,
910
+ discount_calculation_mode: (_A = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _A.discount_calculation_mode,
691
911
  resource_id: selectedDiscount2.id,
692
912
  title: selectedDiscount2.format_title,
693
913
  original_amount: product.original_price,
@@ -697,9 +917,11 @@ var RulesModule = class extends import_BaseModule.BaseModule {
697
917
  metadata: {
698
918
  // 🔥 使用唯一的 _id
699
919
  custom_product_bundle_map_id: uniqueId,
700
- num: product.num || 1
920
+ num: product.num || 1,
921
+ // 🔥 order_level 分摊差值
922
+ ...productDiscountDifference !== void 0 && { product_discount_difference: productDiscountDifference }
701
923
  },
702
- _num: (product.num || 1) * (((_x = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _x.num) || 1)
924
+ _num: (product.num || 1) * (((_B = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _B.num) || 1)
703
925
  };
704
926
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
705
927
  appliedProducts.push(discountDetail);
@@ -707,7 +929,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
707
929
  processedItems.push({
708
930
  ...flatItem,
709
931
  total: targetProductTotal,
710
- price: new import_decimal.default(productOriginTotal || 0).minus(discountDetail.discount.fixed_amount).toNumber(),
932
+ price: new import_decimal.default(productOriginTotal || 0).minus(fixedAmountPerItem).toNumber(),
711
933
  discount_list: [discountDetail],
712
934
  processed: true
713
935
  });
@@ -797,6 +1019,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
797
1019
  return {
798
1020
  ...discount,
799
1021
  metadata: {
1022
+ ...discount.metadata,
800
1023
  num: item.num,
801
1024
  custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
802
1025
  }
@@ -820,6 +1043,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
820
1043
  return {
821
1044
  ...discount,
822
1045
  metadata: {
1046
+ ...discount.metadata,
823
1047
  num: item.num,
824
1048
  custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
825
1049
  }
@@ -850,6 +1074,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
850
1074
  ...discount,
851
1075
  // num: 1,
852
1076
  metadata: {
1077
+ ...discount.metadata,
853
1078
  custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id,
854
1079
  num: 1
855
1080
  }
@@ -910,6 +1135,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
910
1135
  return {
911
1136
  ...discount,
912
1137
  metadata: {
1138
+ ...discount.metadata,
913
1139
  num: mainProductQuantity - 1,
914
1140
  custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id
915
1141
  }
@@ -968,6 +1194,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
968
1194
  return {
969
1195
  ...discount,
970
1196
  metadata: {
1197
+ ...discount.metadata,
971
1198
  num: item.num,
972
1199
  custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
973
1200
  }
@@ -2,6 +2,11 @@ import Decimal from 'decimal.js';
2
2
  import { CartItem } from '../Cart/types';
3
3
  import { ISummaryState } from './types';
4
4
  export declare const calculatePriceDetails: (shopInfo: any, items: CartItem[]) => ISummaryState['summary'];
5
+ /**
6
+ * 获取子商品折扣信息
7
+ * @param item
8
+ */
9
+ export declare const getBundleDiscountList: (bundle: any[]) => any[];
5
10
  /**
6
11
  * 计算商品小计(不含其他费用)
7
12
  * @param items - 购物车商品数组
@@ -16,7 +21,7 @@ export declare const calculateSubtotal: (items: CartItem[]) => string;
16
21
  * @return {*}
17
22
  * @Author: xiangfeng.xue
18
23
  */
19
- export declare const calculateTaxFee: (shopInfo: any, items: CartItem[]) => Decimal | "0.00";
24
+ export declare const calculateTaxFee: (shopInfo: any, items: CartItem[]) => "0.00" | Decimal;
20
25
  /**
21
26
  * @title: 计算定金
22
27
  * @param items - 购物车商品数组