@pisell/pisellos 3.0.75 → 3.0.77

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.
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // src/solution/ShopDiscount/utils.ts
30
30
  var utils_exports = {};
31
31
  __export(utils_exports, {
32
+ calculateOrderLevelDiscountAllocation: () => calculateOrderLevelDiscountAllocation,
32
33
  filterDiscountListByBookingTime: () => filterDiscountListByBookingTime,
33
34
  getDateIsInSchedule: () => getDateIsInSchedule,
34
35
  getDiscountAmount: () => getDiscountAmount,
@@ -36,6 +37,7 @@ __export(utils_exports, {
36
37
  getDiscountListAmountTotal: () => getDiscountListAmountTotal,
37
38
  isAllNormalProduct: () => isAllNormalProduct,
38
39
  isNormalProductByDurationSchedule: () => isNormalProductByDurationSchedule,
40
+ isOrderLevelFixedAmountDiscount: () => isOrderLevelFixedAmountDiscount,
39
41
  uniqueById: () => uniqueById
40
42
  });
41
43
  module.exports = __toCommonJS(utils_exports);
@@ -59,7 +61,7 @@ var getDiscountAmount = (discount, total, price) => {
59
61
  }
60
62
  const isFixedAmount = ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.discount_card_type) === "fixed_amount";
61
63
  if (isFixedAmount) {
62
- return Math.max(new import_decimal.default(price).minus(new import_decimal.default(discount.par_value || 0)).toNumber(), 0);
64
+ return Math.max(new import_decimal.default(price).minus(new import_decimal.default((discount.amount ?? discount.par_value) || 0)).toNumber(), 0);
63
65
  }
64
66
  return new import_decimal.default(100).minus(discount.par_value || 0).div(100).mul(new import_decimal.default(price)).toNumber();
65
67
  };
@@ -306,8 +308,92 @@ var filterDiscountListByBookingTime = (discountList, bookingTime) => {
306
308
  }
307
309
  });
308
310
  };
311
+ var isOrderLevelFixedAmountDiscount = (discount) => {
312
+ var _a, _b;
313
+ return discount.tag === "product_discount_card" && ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.discount_card_type) === "fixed_amount" && ((_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.discount_calculation_mode) === "order_level";
314
+ };
315
+ var calculateOrderLevelDiscountAllocation = (discount, applicableProducts) => {
316
+ const result = /* @__PURE__ */ new Map();
317
+ if (applicableProducts.length === 0) {
318
+ return result;
319
+ }
320
+ const totalAmount = applicableProducts.reduce((acc, product) => {
321
+ return new import_decimal.default(acc).plus(new import_decimal.default(product.amount).mul(product.quantity)).toNumber();
322
+ }, 0);
323
+ if (totalAmount <= 0) {
324
+ return result;
325
+ }
326
+ const fixedAmount = new import_decimal.default(discount.par_value || 0).toNumber();
327
+ const actualDiscountTotal = Math.min(fixedAmount, totalAmount);
328
+ let allocatedTotal = new import_decimal.default(0);
329
+ applicableProducts.forEach((product) => {
330
+ const rawDiscountPerItem = new import_decimal.default(product.amount).div(totalAmount).mul(actualDiscountTotal);
331
+ const discountAmountPerItem = rawDiscountPerItem.toDecimalPlaces(2, import_decimal.default.ROUND_DOWN).toNumber();
332
+ result.set(product.productId, {
333
+ discountAmount: discountAmountPerItem,
334
+ // 单价折扣金额
335
+ difference: 0
336
+ // 默认差值为0
337
+ });
338
+ allocatedTotal = allocatedTotal.plus(
339
+ new import_decimal.default(discountAmountPerItem).mul(product.quantity)
340
+ );
341
+ });
342
+ const totalDifference = new import_decimal.default(actualDiscountTotal).minus(allocatedTotal).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
343
+ if (totalDifference > 0) {
344
+ const isSingleQuantity = (product) => {
345
+ if (product.parentQuantity !== void 0) {
346
+ return product.quantity === 1 && product.parentQuantity === 1;
347
+ }
348
+ return product.quantity === 1;
349
+ };
350
+ const singleQuantityProducts = applicableProducts.filter(isSingleQuantity);
351
+ if (singleQuantityProducts.length > 0) {
352
+ const productsWithEnoughSpace = singleQuantityProducts.filter((product) => {
353
+ const allocation = result.get(product.productId);
354
+ if (!allocation)
355
+ return false;
356
+ const remainingSpace = new import_decimal.default(product.amount).minus(allocation.discountAmount).toNumber();
357
+ return remainingSpace >= totalDifference;
358
+ });
359
+ if (productsWithEnoughSpace.length > 0) {
360
+ productsWithEnoughSpace.sort((a, b) => b.amount - a.amount);
361
+ const targetProduct = productsWithEnoughSpace[0];
362
+ const targetAllocation = result.get(targetProduct.productId);
363
+ if (targetAllocation) {
364
+ result.set(targetProduct.productId, {
365
+ discountAmount: new import_decimal.default(targetAllocation.discountAmount).plus(totalDifference).toNumber(),
366
+ difference: 0
367
+ // 不需要存储差值
368
+ });
369
+ }
370
+ } else {
371
+ singleQuantityProducts.sort((a, b) => b.amount - a.amount);
372
+ const targetProduct = singleQuantityProducts[0];
373
+ const targetAllocation = result.get(targetProduct.productId);
374
+ if (targetAllocation) {
375
+ result.set(targetProduct.productId, {
376
+ ...targetAllocation,
377
+ difference: totalDifference
378
+ });
379
+ }
380
+ }
381
+ } else {
382
+ const lastProduct = applicableProducts[applicableProducts.length - 1];
383
+ const lastProductAllocation = result.get(lastProduct.productId);
384
+ if (lastProductAllocation) {
385
+ result.set(lastProduct.productId, {
386
+ ...lastProductAllocation,
387
+ difference: totalDifference
388
+ });
389
+ }
390
+ }
391
+ }
392
+ return result;
393
+ };
309
394
  // Annotate the CommonJS export names for ESM import in node:
310
395
  0 && (module.exports = {
396
+ calculateOrderLevelDiscountAllocation,
311
397
  filterDiscountListByBookingTime,
312
398
  getDateIsInSchedule,
313
399
  getDiscountAmount,
@@ -315,5 +401,6 @@ var filterDiscountListByBookingTime = (discountList, bookingTime) => {
315
401
  getDiscountListAmountTotal,
316
402
  isAllNormalProduct,
317
403
  isNormalProductByDurationSchedule,
404
+ isOrderLevelFixedAmountDiscount,
318
405
  uniqueById
319
406
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "3.0.75",
4
+ "version": "3.0.77",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",