@pisell/pisellos 2.2.11 → 2.2.12
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.
- package/dist/modules/Cart/utils/cartProduct.js +41 -26
- package/dist/modules/Discount/index.d.ts +2 -0
- package/dist/modules/Discount/index.js +69 -36
- package/dist/modules/Discount/types.d.ts +16 -0
- package/dist/modules/Order/index.js +4 -1
- package/dist/modules/Order/utils.d.ts +1 -0
- package/dist/modules/Order/utils.js +9 -0
- package/dist/modules/Rules/index.d.ts +7 -0
- package/dist/modules/Rules/index.js +1032 -195
- package/dist/modules/Rules/types.d.ts +4 -1
- package/dist/solution/BookingByStep/index.d.ts +1 -1
- package/dist/solution/BookingByStep/index.js +1 -0
- package/dist/solution/BookingTicket/index.d.ts +1 -1
- package/dist/solution/ShopDiscount/index.d.ts +2 -0
- package/dist/solution/ShopDiscount/index.js +80 -19
- package/dist/solution/ShopDiscount/types.d.ts +4 -1
- package/dist/solution/ShopDiscount/utils.d.ts +55 -0
- package/dist/solution/ShopDiscount/utils.js +432 -3
- package/lib/modules/Cart/utils/cartProduct.js +35 -22
- package/lib/modules/Discount/index.d.ts +2 -0
- package/lib/modules/Discount/index.js +19 -4
- package/lib/modules/Discount/types.d.ts +16 -0
- package/lib/modules/Order/index.js +2 -0
- package/lib/modules/Order/utils.d.ts +1 -0
- package/lib/modules/Order/utils.js +11 -0
- package/lib/modules/Rules/index.d.ts +7 -0
- package/lib/modules/Rules/index.js +809 -177
- package/lib/modules/Rules/types.d.ts +4 -1
- package/lib/solution/BookingByStep/index.d.ts +1 -1
- package/lib/solution/BookingByStep/index.js +4 -0
- package/lib/solution/BookingTicket/index.d.ts +1 -1
- package/lib/solution/ShopDiscount/index.d.ts +2 -0
- package/lib/solution/ShopDiscount/index.js +55 -9
- package/lib/solution/ShopDiscount/types.d.ts +4 -1
- package/lib/solution/ShopDiscount/utils.d.ts +55 -0
- package/lib/solution/ShopDiscount/utils.js +266 -3
- package/package.json +1 -1
|
@@ -24,10 +24,10 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
|
|
|
24
24
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
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
|
-
import { uniqueById, getDiscountAmount } from "../../solution/ShopDiscount/utils";
|
|
27
|
+
import { uniqueById, getDiscountAmount, getDiscountListAmountTotal, getDiscountListAmount, filterDiscountListByBookingTime } from "../../solution/ShopDiscount/utils";
|
|
28
28
|
import { getProductOriginTotalPrice, getProductTotalPrice } from "../Cart/utils";
|
|
29
29
|
import Decimal from 'decimal.js';
|
|
30
|
-
import
|
|
30
|
+
import dayjs from 'dayjs';
|
|
31
31
|
// 临时变量
|
|
32
32
|
var flatItem;
|
|
33
33
|
export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
@@ -162,10 +162,15 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
162
162
|
// 遍历处理后的产品列表,检查是否有产品使用了新折扣
|
|
163
163
|
result.productList.forEach(function (product) {
|
|
164
164
|
var _this2$hooks$getProdu = _this2.hooks.getProduct(product),
|
|
165
|
-
discount_list = _this2$hooks$getProdu.discount_list
|
|
166
|
-
|
|
165
|
+
discount_list = _this2$hooks$getProdu.discount_list,
|
|
166
|
+
bundle = _this2$hooks$getProdu.bundle;
|
|
167
|
+
var allDiscountList = _toConsumableArray(discount_list || []);
|
|
168
|
+
(bundle || []).forEach(function (item) {
|
|
169
|
+
allDiscountList.push.apply(allDiscountList, _toConsumableArray((item === null || item === void 0 ? void 0 : item.discount_list) || []));
|
|
170
|
+
});
|
|
171
|
+
if (allDiscountList && allDiscountList.length > 0) {
|
|
167
172
|
// 检查是否有使用新折扣的情况
|
|
168
|
-
var usedNewDiscount =
|
|
173
|
+
var usedNewDiscount = allDiscountList.some(function (discount) {
|
|
169
174
|
var _discount$discount;
|
|
170
175
|
return newDiscountIds.includes(discount === null || discount === void 0 || (_discount$discount = discount.discount) === null || _discount$discount === void 0 ? void 0 : _discount$discount.resource_id);
|
|
171
176
|
});
|
|
@@ -209,6 +214,55 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
209
214
|
return !discount.isManualSelect;
|
|
210
215
|
});
|
|
211
216
|
|
|
217
|
+
// 🔥 扁平化商品列表:将 bundle 子商品展开为虚拟商品
|
|
218
|
+
var flattenProductsWithBundle = function flattenProductsWithBundle(productList) {
|
|
219
|
+
var flattened = [];
|
|
220
|
+
productList.forEach(function (originProduct) {
|
|
221
|
+
var product = _this3.hooks.getProduct(originProduct);
|
|
222
|
+
|
|
223
|
+
// 1. 添加主商品
|
|
224
|
+
flattened.push({
|
|
225
|
+
type: 'main',
|
|
226
|
+
originProduct: originProduct,
|
|
227
|
+
product: product,
|
|
228
|
+
price: Number(product.price || 0),
|
|
229
|
+
id: product.id,
|
|
230
|
+
_id: product._id,
|
|
231
|
+
parentId: product._id,
|
|
232
|
+
quantity: product.quantity,
|
|
233
|
+
num: product.num
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// 2. 展开 bundle 子商品
|
|
237
|
+
if (product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0) {
|
|
238
|
+
product.bundle.forEach(function (bundleItem, bundleIndex) {
|
|
239
|
+
flattened.push({
|
|
240
|
+
type: 'bundle',
|
|
241
|
+
originProduct: originProduct,
|
|
242
|
+
parentProduct: product,
|
|
243
|
+
bundleItem: bundleItem,
|
|
244
|
+
bundleIndex: bundleIndex,
|
|
245
|
+
// 虚拟商品属性
|
|
246
|
+
price: Number(bundleItem.price || 0),
|
|
247
|
+
id: bundleItem._bundle_product_id,
|
|
248
|
+
// 🔥 使用 _bundle_product_id
|
|
249
|
+
_id: "".concat(product._id, "_bundle_").concat(bundleIndex),
|
|
250
|
+
parentId: product._id,
|
|
251
|
+
num: bundleItem.num || 1,
|
|
252
|
+
quantity: bundleItem.num || 1,
|
|
253
|
+
total: new Decimal(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
|
|
254
|
+
origin_total: new Decimal(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
|
|
255
|
+
original_price: bundleItem.original_price,
|
|
256
|
+
// 继承主商品属性
|
|
257
|
+
booking_id: product.booking_id,
|
|
258
|
+
discount_list: bundleItem.discount_list || []
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
return flattened;
|
|
264
|
+
};
|
|
265
|
+
|
|
212
266
|
// 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
|
|
213
267
|
// 1. 商品券(n.tag=good_pass)视为最优惠(免费)
|
|
214
268
|
// 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
|
|
@@ -244,12 +298,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
244
298
|
}
|
|
245
299
|
}
|
|
246
300
|
|
|
247
|
-
// 3. 都是百分比时,折扣越大越优先(par_value
|
|
301
|
+
// 3. 都是百分比时,折扣越大越优先(par_value越大越优先)
|
|
248
302
|
if (typeA === 'percent' && typeB === 'percent') {
|
|
249
303
|
if (a.par_value !== b.par_value) {
|
|
250
304
|
var _valueA = new Decimal(100).minus(a.par_value || 0);
|
|
251
305
|
var _valueB = new Decimal(100).minus(b.par_value || 0);
|
|
252
|
-
return
|
|
306
|
+
return _valueA.minus(_valueB).toNumber(); // 折扣大的在前
|
|
253
307
|
}
|
|
254
308
|
}
|
|
255
309
|
|
|
@@ -283,21 +337,50 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
283
337
|
// }
|
|
284
338
|
// })
|
|
285
339
|
|
|
340
|
+
// 🔥 扁平化商品列表(包含主商品和bundle子商品)
|
|
341
|
+
var flattenedList = flattenProductsWithBundle(productList);
|
|
342
|
+
|
|
343
|
+
// 对扁平化后的列表按价格降序排序(用于应用优惠券时优先选择高价商品)
|
|
344
|
+
var sortedFlattenedList = flattenedList.sort(function (a, b) {
|
|
345
|
+
var priceA = new Decimal(a.price || '0');
|
|
346
|
+
var priceB = new Decimal(b.price || '0');
|
|
347
|
+
if (priceA.equals(priceB)) {
|
|
348
|
+
// 价格相同时,主商品优先
|
|
349
|
+
if (a.type !== b.type) {
|
|
350
|
+
return a.type === 'main' ? -1 : 1;
|
|
351
|
+
}
|
|
352
|
+
// 都是主商品时,按原有逻辑排序
|
|
353
|
+
if (a.type === 'main' && b.type === 'main') {
|
|
354
|
+
if (a.product.quantity === b.product.quantity) {
|
|
355
|
+
var _b$product$discount_l, _a$product$discount_l;
|
|
356
|
+
return (((_b$product$discount_l = b.product.discount_list) === null || _b$product$discount_l === void 0 ? void 0 : _b$product$discount_l.length) || 0) - (((_a$product$discount_l = a.product.discount_list) === null || _a$product$discount_l === void 0 ? void 0 : _a$product$discount_l.length) || 0);
|
|
357
|
+
}
|
|
358
|
+
return a.product.quantity - b.product.quantity;
|
|
359
|
+
}
|
|
360
|
+
// 都是bundle时,按数量排序
|
|
361
|
+
if (a.type === 'bundle' && b.type === 'bundle') {
|
|
362
|
+
return (a.num || 1) - (b.num || 1);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return priceB.minus(priceA).toNumber();
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
/**
|
|
286
369
|
// 对productList按价格降序排序(用于应用优惠券时优先选择高价商品) 价格相同时使用quantity 排序
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
370
|
+
const sortedProductList = [...productList].sort((a, b) => {
|
|
371
|
+
const aProduct = this.hooks.getProduct(a);
|
|
372
|
+
const bProduct = this.hooks.getProduct(b);
|
|
373
|
+
const priceA = new Decimal((aProduct.price as string) || '0');
|
|
374
|
+
const priceB = new Decimal((bProduct.price as string) || '0');
|
|
292
375
|
if (priceA.toNumber() === priceB.toNumber()) {
|
|
293
376
|
if (aProduct.quantity === bProduct.quantity) {
|
|
294
|
-
|
|
295
|
-
return ((_bProduct$discount_li = bProduct.discount_list) === null || _bProduct$discount_li === void 0 ? void 0 : _bProduct$discount_li.length) - ((_aProduct$discount_li = aProduct.discount_list) === null || _aProduct$discount_li === void 0 ? void 0 : _aProduct$discount_li.length);
|
|
377
|
+
return bProduct.discount_list?.length - aProduct.discount_list?.length;
|
|
296
378
|
}
|
|
297
379
|
return aProduct.quantity - bProduct.quantity;
|
|
298
380
|
}
|
|
299
381
|
return priceB.toNumber() - priceA.toNumber();
|
|
300
382
|
});
|
|
383
|
+
*/
|
|
301
384
|
|
|
302
385
|
// 标记已使用的优惠券
|
|
303
386
|
var usedDiscounts = new Map();
|
|
@@ -321,26 +404,50 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
321
404
|
var appliedDiscountProducts = new Map();
|
|
322
405
|
|
|
323
406
|
// 首先遍历所有商品和所有优惠券,确定每个优惠券的适用范围,包括isSelected为false的优惠券
|
|
324
|
-
|
|
325
|
-
|
|
407
|
+
// 🔥 使用扁平化后的列表,包含主商品和bundle子商品
|
|
408
|
+
sortedFlattenedList.forEach(function (flatItem) {
|
|
409
|
+
// 获取商品数据
|
|
410
|
+
var product, originProduct;
|
|
411
|
+
if (flatItem.type === 'main') {
|
|
412
|
+
product = flatItem.product;
|
|
413
|
+
originProduct = flatItem.originProduct;
|
|
414
|
+
} else {
|
|
415
|
+
// bundle子商品:构造虚拟商品对象
|
|
416
|
+
product = {
|
|
417
|
+
_id: flatItem._id,
|
|
418
|
+
id: flatItem.id,
|
|
419
|
+
price: flatItem.price,
|
|
420
|
+
quantity: flatItem.quantity,
|
|
421
|
+
num: flatItem.num,
|
|
422
|
+
total: flatItem.total,
|
|
423
|
+
origin_total: flatItem.origin_total,
|
|
424
|
+
booking_id: flatItem.booking_id,
|
|
425
|
+
discount_list: flatItem.discount_list || []
|
|
426
|
+
};
|
|
427
|
+
originProduct = flatItem.originProduct;
|
|
428
|
+
}
|
|
326
429
|
addModeDiscount.forEach(function (discount) {
|
|
327
|
-
var _flatItem$parentProdu, _flatItem$product, _product
|
|
430
|
+
var _flatItem$parentProdu, _flatItem$product, _product, _product2, _product3, _product4, _flatItem$bundleItem;
|
|
328
431
|
var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
|
|
329
432
|
// 拿到discount配置的holder信息 product信息 product.holder 加在 isLimitedProduct
|
|
330
433
|
var isHolderMatch = _this3.checkHolderMatch(discount, {
|
|
331
434
|
holder_id: ((flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'bundle' ? flatItem === null || flatItem === void 0 || (_flatItem$parentProdu = flatItem.parentProduct) === null || _flatItem$parentProdu === void 0 ? void 0 : _flatItem$parentProdu.holder_id : flatItem === null || flatItem === void 0 || (_flatItem$product = flatItem.product) === null || _flatItem$product === void 0 ? void 0 : _flatItem$product.holder_id) || product.holder_id
|
|
332
435
|
}, holders);
|
|
333
|
-
|
|
436
|
+
var timeLimit = true;
|
|
437
|
+
timeLimit = !!filterDiscountListByBookingTime([discount], (((_product = product) === null || _product === void 0 ? void 0 : _product.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
|
|
334
438
|
// 是符合折扣的商品
|
|
335
439
|
var isLimitedProduct = (limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
|
|
336
440
|
|
|
337
441
|
// 编辑的商品 使用了优惠券不可用
|
|
338
|
-
var isAvailableProduct = !(product !== null &&
|
|
442
|
+
var isAvailableProduct = flatItem.type === 'main' ? !((_product2 = product) !== null && _product2 !== void 0 && _product2.booking_id && (_product3 = product) !== null && _product3 !== void 0 && (_product3 = _product3.discount_list) !== null && _product3 !== void 0 && _product3.length && (_product4 = product) !== null && _product4 !== void 0 && (_product4 = _product4.discount_list) !== null && _product4 !== void 0 && _product4.every(function (discount) {
|
|
339
443
|
return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
|
|
340
|
-
}));
|
|
444
|
+
})) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem = flatItem.bundleItem) !== null && _flatItem$bundleItem !== void 0 && (_flatItem$bundleItem = _flatItem$bundleItem.metadata) !== null && _flatItem$bundleItem !== void 0 && _flatItem$bundleItem.custom_product_bundle_map_id);
|
|
445
|
+
|
|
446
|
+
// 套餐是否可用判断
|
|
447
|
+
var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
|
|
341
448
|
|
|
342
449
|
// 判断优惠券是否适用于该商品
|
|
343
|
-
if (isAvailableProduct && isLimitedProduct) {
|
|
450
|
+
if (isAvailableProduct && isLimitedProduct && isBundleAvailable && timeLimit) {
|
|
344
451
|
var _discountApplicabilit, _discount$metadata;
|
|
345
452
|
// 记录此优惠券适用的商品
|
|
346
453
|
(_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
|
|
@@ -348,8 +455,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
348
455
|
// 记录可抵扣的商品详情
|
|
349
456
|
var applicableProducts = discountApplicableProducts.get(discount.id) || [];
|
|
350
457
|
var discountType = discount.tag || discount.type;
|
|
458
|
+
var isGoodPass = discountType === 'good_pass';
|
|
459
|
+
|
|
460
|
+
// 使用数量
|
|
461
|
+
var num = isGoodPass || (flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'main' ? 1 : product.num;
|
|
351
462
|
var productData = {
|
|
352
|
-
amount: product.price,
|
|
463
|
+
amount: product.price * num,
|
|
353
464
|
type: discountType,
|
|
354
465
|
tag: discountType,
|
|
355
466
|
discount: {
|
|
@@ -357,49 +468,88 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
357
468
|
fixed_amount: product.price,
|
|
358
469
|
resource_id: discount.id,
|
|
359
470
|
title: discount.format_title,
|
|
360
|
-
original_amount: product.origin_total,
|
|
471
|
+
original_amount: product.price || product.origin_total,
|
|
361
472
|
pre_value: discount.par_value,
|
|
362
473
|
product_id: originProduct.id
|
|
474
|
+
},
|
|
475
|
+
metadata: {
|
|
476
|
+
num: num
|
|
363
477
|
}
|
|
364
478
|
};
|
|
365
|
-
|
|
366
|
-
// 如果 discount.tag 或者 discount.type 是 good_pass,则不需要添加 num 属性
|
|
367
|
-
if (discountType !== 'good_pass') {
|
|
368
|
-
productData.num = product.num || 1;
|
|
369
|
-
}
|
|
370
479
|
applicableProducts.push(productData);
|
|
371
480
|
discountApplicableProducts.set(discount.id, applicableProducts);
|
|
372
481
|
}
|
|
373
482
|
});
|
|
374
483
|
});
|
|
375
|
-
|
|
484
|
+
|
|
485
|
+
// 🔥 用于存储扁平化商品处理结果的Map
|
|
486
|
+
var processedFlatItemsMap = new Map();
|
|
376
487
|
|
|
377
488
|
// 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
var
|
|
381
|
-
|
|
489
|
+
// 🔥 使用扁平化后的列表进行处理
|
|
490
|
+
sortedFlattenedList.forEach(function (flatItem, index) {
|
|
491
|
+
var _product5, _product$discount_lis2, _product6, _product$discount_lis10;
|
|
492
|
+
// 获取商品数据
|
|
493
|
+
var product, originProduct;
|
|
494
|
+
if (flatItem.type === 'main') {
|
|
495
|
+
product = flatItem.product;
|
|
496
|
+
originProduct = flatItem.originProduct;
|
|
497
|
+
} else {
|
|
498
|
+
var _flatItem$bundleItem2, _flatItem$bundleItem3, _flatItem$bundleItem4;
|
|
499
|
+
// bundle子商品
|
|
500
|
+
product = {
|
|
501
|
+
_id: flatItem._id,
|
|
502
|
+
id: flatItem.id,
|
|
503
|
+
price: flatItem.price,
|
|
504
|
+
quantity: flatItem.quantity,
|
|
505
|
+
num: flatItem.num,
|
|
506
|
+
total: flatItem.total,
|
|
507
|
+
original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem2 = flatItem.bundleItem) === null || _flatItem$bundleItem2 === void 0 ? void 0 : _flatItem$bundleItem2.original_price,
|
|
508
|
+
origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem3 = flatItem.bundleItem) === null || _flatItem$bundleItem3 === void 0 ? void 0 : _flatItem$bundleItem3.original_price,
|
|
509
|
+
booking_id: flatItem.booking_id,
|
|
510
|
+
discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem4 = flatItem.bundleItem) === null || _flatItem$bundleItem4 === void 0 ? void 0 : _flatItem$bundleItem4.discount_list) || []
|
|
511
|
+
};
|
|
512
|
+
originProduct = flatItem.originProduct;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// 已有优惠的商品跳过
|
|
516
|
+
if ((_product5 = product) !== null && _product5 !== void 0 && _product5.booking_id && (_product$discount_lis2 = product.discount_list) !== null && _product$discount_lis2 !== void 0 && _product$discount_lis2.length && (_product6 = product) !== null && _product6 !== void 0 && (_product6 = _product6.discount_list) !== null && _product6 !== void 0 && _product6.every(function (discount) {
|
|
382
517
|
return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
|
|
383
518
|
})) {
|
|
384
|
-
|
|
519
|
+
if (flatItem.type === 'main') {
|
|
520
|
+
processedProductsMap.set(product._id, [originProduct]);
|
|
521
|
+
} else {
|
|
522
|
+
processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
|
|
523
|
+
processed: true
|
|
524
|
+
})]);
|
|
525
|
+
}
|
|
385
526
|
return;
|
|
386
527
|
}
|
|
387
528
|
|
|
388
529
|
// 找到适用于此商品的所有优惠券,仅考虑isSelected不为false的优惠券
|
|
389
530
|
var applicableDiscounts = sortedDiscountList.filter(function (discount) {
|
|
390
|
-
var _product$
|
|
531
|
+
var _product$discount_lis3, _product$discount_lis4, _flatItem$parentProdu2, _flatItem$product2;
|
|
391
532
|
// 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
|
|
392
|
-
//
|
|
393
|
-
if ((Number(product.price) <= 0 || !product.price) && (
|
|
394
|
-
// 折扣卡时总价小于等于0时不可用
|
|
395
|
-
if ((Number(product.total) <= 0 || !product.total) && !((_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.find(function (n) {
|
|
533
|
+
// 商品券时主商品价格为0不可用
|
|
534
|
+
if ((Number(product.price) <= 0 || !product.price) && !((_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.find(function (n) {
|
|
396
535
|
var _n$discount;
|
|
397
536
|
return ((_n$discount = n.discount) === null || _n$discount === void 0 ? void 0 : _n$discount.resource_id) === discount.id;
|
|
537
|
+
})) && (discount.tag || discount.type) === 'good_pass') return false;
|
|
538
|
+
|
|
539
|
+
// 折扣卡商品价格为0时不可用
|
|
540
|
+
if ((Number(product.price) === 0 || !product.price) && !((_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.find(function (n) {
|
|
541
|
+
var _n$discount2;
|
|
542
|
+
return ((_n$discount2 = n.discount) === null || _n$discount2 === void 0 ? void 0 : _n$discount2.resource_id) === discount.id;
|
|
398
543
|
})) && (discount.tag || discount.type) !== 'good_pass') return false;
|
|
399
544
|
// 如果优惠券已被使用,则跳过
|
|
400
545
|
var targetUsedDiscounts = usedDiscounts.get(discount.id);
|
|
401
546
|
if (targetUsedDiscounts && (discount.tag || discount.type) === 'good_pass') return false;
|
|
402
547
|
var limitedData = discount.limited_relation_product_data;
|
|
548
|
+
var timeLimit = true;
|
|
549
|
+
timeLimit = !!filterDiscountListByBookingTime([discount], (product.startDate || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
|
|
550
|
+
if (!timeLimit) {
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
403
553
|
|
|
404
554
|
// 拿到discount配置的holder信息 product信息 product.holder 不可用return false
|
|
405
555
|
var isHolderMatch = _this3.checkHolderMatch(discount, {
|
|
@@ -410,8 +560,16 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
410
560
|
|
|
411
561
|
// 判断优惠券是否适用于该商品
|
|
412
562
|
if (limitedData.type === 'product_all') {
|
|
563
|
+
// 检查 package_sub_item_usage_rules
|
|
564
|
+
if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
413
567
|
return true;
|
|
414
568
|
} else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
|
|
569
|
+
// 检查 package_sub_item_usage_rules
|
|
570
|
+
if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
415
573
|
return true;
|
|
416
574
|
}
|
|
417
575
|
return false;
|
|
@@ -426,60 +584,139 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
426
584
|
var selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
|
|
427
585
|
|
|
428
586
|
// 如果是手动折扣,则不适用优惠券
|
|
429
|
-
var isManualDiscount =
|
|
430
|
-
|
|
431
|
-
|
|
587
|
+
var isManualDiscount = false;
|
|
588
|
+
if (flatItem.type === 'main') {
|
|
589
|
+
var _product$discount_lis5, _product7, _product7$every;
|
|
590
|
+
// 主商品:判断自身是否手动折扣
|
|
591
|
+
isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every(function (item) {
|
|
592
|
+
var _ref3;
|
|
593
|
+
return !((_ref3 = item.discount_list || []) !== null && _ref3 !== void 0 && _ref3.length);
|
|
594
|
+
}) && (!((_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.length) || ((_product7 = product) === null || _product7 === void 0 || (_product7 = _product7.discount_list) === null || _product7 === void 0 || (_product7$every = _product7.every) === null || _product7$every === void 0 ? void 0 : _product7$every.call(_product7, function (item) {
|
|
595
|
+
return item.type === 'product';
|
|
596
|
+
})));
|
|
597
|
+
} else {
|
|
598
|
+
// bundle子商品:判断父主商品是否手动折扣
|
|
599
|
+
var parentProduct = flatItem.parentProduct;
|
|
600
|
+
if (parentProduct) {
|
|
601
|
+
var _parentProduct$discou, _parentProduct$discou2, _parentProduct$discou3;
|
|
602
|
+
isManualDiscount = typeof parentProduct.isManualDiscount === 'boolean' ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every(function (item) {
|
|
603
|
+
var _ref4;
|
|
604
|
+
return !((_ref4 = item.discount_list || []) !== null && _ref4 !== void 0 && _ref4.length);
|
|
605
|
+
}) && (!((_parentProduct$discou = parentProduct.discount_list) !== null && _parentProduct$discou !== void 0 && _parentProduct$discou.length) || (parentProduct === null || parentProduct === void 0 || (_parentProduct$discou2 = parentProduct.discount_list) === null || _parentProduct$discou2 === void 0 || (_parentProduct$discou3 = _parentProduct$discou2.every) === null || _parentProduct$discou3 === void 0 ? void 0 : _parentProduct$discou3.call(_parentProduct$discou2, function (item) {
|
|
606
|
+
return item.type === 'product';
|
|
607
|
+
})));
|
|
608
|
+
}
|
|
609
|
+
}
|
|
432
610
|
|
|
433
611
|
// 勾选时覆盖手动折扣
|
|
434
|
-
if (options !== null && options !== void 0 && options.discountId
|
|
435
|
-
var
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
612
|
+
if (options !== null && options !== void 0 && options.discountId) {
|
|
613
|
+
var _product$discount_lis6;
|
|
614
|
+
// 主商品:检查自己的 discount_list
|
|
615
|
+
if (flatItem.type === 'main' && (_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.some(function (item) {
|
|
616
|
+
var _item$discount;
|
|
617
|
+
return ((_item$discount = item.discount) === null || _item$discount === void 0 ? void 0 : _item$discount.resource_id) === options.discountId;
|
|
618
|
+
})) {
|
|
619
|
+
isManualDiscount = false;
|
|
620
|
+
}
|
|
621
|
+
// bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
|
|
622
|
+
if (flatItem.type === 'bundle') {
|
|
623
|
+
var _product$discount_lis7, _flatItem$parentProdu3;
|
|
624
|
+
if ((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.some(function (item) {
|
|
625
|
+
var _item$discount2;
|
|
626
|
+
return ((_item$discount2 = item.discount) === null || _item$discount2 === void 0 ? void 0 : _item$discount2.resource_id) === options.discountId;
|
|
627
|
+
}) || (_flatItem$parentProdu3 = flatItem.parentProduct) !== null && _flatItem$parentProdu3 !== void 0 && (_flatItem$parentProdu3 = _flatItem$parentProdu3.discount_list) !== null && _flatItem$parentProdu3 !== void 0 && _flatItem$parentProdu3.some(function (item) {
|
|
628
|
+
var _item$discount3;
|
|
629
|
+
return ((_item$discount3 = item.discount) === null || _item$discount3 === void 0 ? void 0 : _item$discount3.resource_id) === options.discountId;
|
|
630
|
+
})) {
|
|
631
|
+
isManualDiscount = false;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
439
634
|
}
|
|
440
|
-
if (options !== null && options !== void 0 && options.selectedList
|
|
441
|
-
var
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
635
|
+
if (options !== null && options !== void 0 && options.selectedList) {
|
|
636
|
+
var _product$discount_lis8;
|
|
637
|
+
// 主商品:检查自己的 discount_list
|
|
638
|
+
if (flatItem.type === 'main' && (_product$discount_lis8 = product.discount_list) !== null && _product$discount_lis8 !== void 0 && _product$discount_lis8.some(function (item) {
|
|
639
|
+
var _options$selectedList;
|
|
640
|
+
return options === null || options === void 0 || (_options$selectedList = options.selectedList) === null || _options$selectedList === void 0 ? void 0 : _options$selectedList.some(function (n) {
|
|
641
|
+
var _item$discount4;
|
|
642
|
+
return n.discountId === ((_item$discount4 = item.discount) === null || _item$discount4 === void 0 ? void 0 : _item$discount4.resource_id);
|
|
643
|
+
});
|
|
644
|
+
})) {
|
|
645
|
+
isManualDiscount = false;
|
|
646
|
+
}
|
|
647
|
+
// bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
|
|
648
|
+
if (flatItem.type === 'bundle') {
|
|
649
|
+
var _product$discount_lis9, _flatItem$parentProdu4;
|
|
650
|
+
if ((_product$discount_lis9 = product.discount_list) !== null && _product$discount_lis9 !== void 0 && _product$discount_lis9.some(function (item) {
|
|
651
|
+
var _options$selectedList2;
|
|
652
|
+
return options === null || options === void 0 || (_options$selectedList2 = options.selectedList) === null || _options$selectedList2 === void 0 ? void 0 : _options$selectedList2.some(function (n) {
|
|
653
|
+
var _item$discount5;
|
|
654
|
+
return n.discountId === ((_item$discount5 = item.discount) === null || _item$discount5 === void 0 ? void 0 : _item$discount5.resource_id);
|
|
655
|
+
});
|
|
656
|
+
}) || (_flatItem$parentProdu4 = flatItem.parentProduct) !== null && _flatItem$parentProdu4 !== void 0 && (_flatItem$parentProdu4 = _flatItem$parentProdu4.discount_list) !== null && _flatItem$parentProdu4 !== void 0 && _flatItem$parentProdu4.some(function (item) {
|
|
657
|
+
var _options$selectedList3;
|
|
658
|
+
return options === null || options === void 0 || (_options$selectedList3 = options.selectedList) === null || _options$selectedList3 === void 0 ? void 0 : _options$selectedList3.some(function (n) {
|
|
659
|
+
var _item$discount6;
|
|
660
|
+
return n.discountId === ((_item$discount6 = item.discount) === null || _item$discount6 === void 0 ? void 0 : _item$discount6.resource_id);
|
|
661
|
+
});
|
|
662
|
+
})) {
|
|
663
|
+
isManualDiscount = false;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (options !== null && options !== void 0 && options.selectedList && (_product$discount_lis10 = product.discount_list) !== null && _product$discount_lis10 !== void 0 && _product$discount_lis10.some(function (item) {
|
|
668
|
+
var _options$selectedList4;
|
|
669
|
+
return options === null || options === void 0 || (_options$selectedList4 = options.selectedList) === null || _options$selectedList4 === void 0 ? void 0 : _options$selectedList4.some(function (n) {
|
|
670
|
+
var _item$discount7;
|
|
671
|
+
return n.discountId === ((_item$discount7 = item.discount) === null || _item$discount7 === void 0 ? void 0 : _item$discount7.resource_id);
|
|
445
672
|
});
|
|
446
673
|
})) {
|
|
447
674
|
isManualDiscount = false;
|
|
448
675
|
}
|
|
449
676
|
|
|
450
677
|
// 如果没有适用的优惠券,或者手动折扣,则不适用优惠券
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
678
|
+
if (applicableDiscounts.length === 0 || isManualDiscount) {
|
|
679
|
+
if (flatItem.type === 'main') {
|
|
680
|
+
// 主商品:保持原有逻辑
|
|
681
|
+
if (product.isClient) {
|
|
682
|
+
processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
|
|
683
|
+
origin_total: getProductOriginTotalPrice({
|
|
684
|
+
product: {
|
|
685
|
+
original_price: product.original_price
|
|
686
|
+
},
|
|
687
|
+
bundle: product.bundle,
|
|
688
|
+
options: product.options
|
|
689
|
+
}),
|
|
690
|
+
variant: originProduct._productInit.variant,
|
|
691
|
+
original_price: originProduct._productInit.original_price,
|
|
692
|
+
total: getProductTotalPrice({
|
|
693
|
+
product: {
|
|
694
|
+
price: product.price
|
|
695
|
+
},
|
|
696
|
+
bundle: product.bundle,
|
|
697
|
+
options: product.options
|
|
698
|
+
}),
|
|
699
|
+
price: product.price
|
|
700
|
+
}), {}, {
|
|
701
|
+
discount_list: []
|
|
702
|
+
}))]);
|
|
703
|
+
} else {
|
|
704
|
+
processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
|
|
705
|
+
_id: product._id.split('___')[0] + '___' + index,
|
|
706
|
+
total: product.origin_total || product.total,
|
|
707
|
+
price: product.price,
|
|
708
|
+
main_product_selling_price: product.price
|
|
709
|
+
}), {}, {
|
|
710
|
+
discount_list: []
|
|
711
|
+
}))]);
|
|
712
|
+
}
|
|
475
713
|
} else {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
price:
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
}))]);
|
|
714
|
+
// bundle子商品:保存到扁平化Map
|
|
715
|
+
processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
|
|
716
|
+
discount_list: [],
|
|
717
|
+
price: flatItem.bundleItem.original_price,
|
|
718
|
+
processed: true
|
|
719
|
+
})]);
|
|
483
720
|
}
|
|
484
721
|
return;
|
|
485
722
|
}
|
|
@@ -487,137 +724,598 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
487
724
|
return;
|
|
488
725
|
}
|
|
489
726
|
|
|
490
|
-
//
|
|
727
|
+
// 是否需要拆分(商品券需要拆分)
|
|
491
728
|
var isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === 'good_pass';
|
|
492
729
|
|
|
493
730
|
// 需要拆分出来的数量
|
|
494
|
-
var
|
|
731
|
+
var totalQuantity = product.quantity || product.num || 1;
|
|
732
|
+
var availableGoodPassCount = applicableDiscounts.filter(function (item) {
|
|
495
733
|
return (item.tag || item.type) === 'good_pass';
|
|
496
|
-
}).length
|
|
734
|
+
}).length;
|
|
735
|
+
var splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
|
|
497
736
|
var arr = [];
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
737
|
+
|
|
738
|
+
// 🔥 主商品和bundle子商品分别处理
|
|
739
|
+
if (flatItem.type === 'main') {
|
|
740
|
+
// 主商品:保持原有逻辑
|
|
741
|
+
if (splitCount < totalQuantity && isNeedSplit) {
|
|
742
|
+
arr.push(_this3.hooks.setProduct(originProduct, {
|
|
743
|
+
discount_list: [],
|
|
744
|
+
quantity: totalQuantity - splitCount,
|
|
745
|
+
_id: product._id.split('___')[0]
|
|
746
|
+
}));
|
|
747
|
+
}
|
|
748
|
+
for (var i = 0; i < splitCount; i++) {
|
|
749
|
+
var _product$discount_lis11, _originProduct, _selectedDiscount$met;
|
|
750
|
+
// 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
|
|
751
|
+
var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
|
|
752
|
+
// 标记优惠券为已使用
|
|
753
|
+
usedDiscounts.set(_selectedDiscount.id, true);
|
|
754
|
+
|
|
755
|
+
// 记录实际应用了优惠券的商品信息
|
|
756
|
+
var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
|
|
757
|
+
|
|
758
|
+
// 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
|
|
759
|
+
var productOriginTotal = product.origin_total || product.total || 0;
|
|
760
|
+
// 如果当前 product 有 discount_list,则先从 origin_total 拿
|
|
761
|
+
if ((_product$discount_lis11 = product.discount_list) !== null && _product$discount_lis11 !== void 0 && _product$discount_lis11.length && product.origin_total) {
|
|
762
|
+
productOriginTotal = product.origin_total;
|
|
763
|
+
}
|
|
764
|
+
// 如果originProduct?._productInit?.original_price为 0,product.origin_total可能为空,此时取 product.total
|
|
765
|
+
if (Number(((_originProduct = originProduct) === null || _originProduct === void 0 || (_originProduct = _originProduct._productInit) === null || _originProduct === void 0 ? void 0 : _originProduct.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
|
|
766
|
+
productOriginTotal = product.total;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// 计算使用折扣卡/商品券以后,单个商品的总 total
|
|
770
|
+
var targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
|
|
771
|
+
var discountType = _selectedDiscount.tag || _selectedDiscount.type;
|
|
772
|
+
var isGoodPass = discountType === 'good_pass';
|
|
773
|
+
var amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
|
|
774
|
+
var discountDetail = {
|
|
775
|
+
amount: amount,
|
|
776
|
+
type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : discountType,
|
|
777
|
+
discount: {
|
|
778
|
+
discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
|
|
779
|
+
fixed_amount: amount,
|
|
780
|
+
resource_id: _selectedDiscount.id,
|
|
781
|
+
title: _selectedDiscount.format_title,
|
|
782
|
+
original_amount: product.price,
|
|
783
|
+
product_id: originProduct.id,
|
|
784
|
+
percent: _selectedDiscount.par_value
|
|
785
|
+
},
|
|
786
|
+
// 前端使用的num数量,为了计算优惠金额
|
|
787
|
+
_num: isGoodPass ? 1 : product.num,
|
|
788
|
+
metadata: {
|
|
789
|
+
num: 1
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
appliedProducts.push(discountDetail);
|
|
793
|
+
appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
|
|
794
|
+
var total = targetProductTotal;
|
|
795
|
+
if (product.options) {
|
|
796
|
+
total = product.options.reduce(function (accumulator, currentValue) {
|
|
797
|
+
var currentPrice = new Decimal(currentValue.price || 0);
|
|
798
|
+
var currentNum = new Decimal(currentValue.num || 0);
|
|
799
|
+
return accumulator.add(currentPrice.mul(currentNum));
|
|
800
|
+
}, new Decimal(total)).toNumber();
|
|
540
801
|
}
|
|
541
|
-
};
|
|
542
802
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
803
|
+
// 记录应用了优惠券的商品
|
|
804
|
+
// 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
|
|
805
|
+
if (product.isClient) {
|
|
806
|
+
debugger;
|
|
807
|
+
arr.push(_this3.hooks.setProduct(originProduct, {
|
|
808
|
+
discount_list: [discountDetail],
|
|
809
|
+
price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
|
|
810
|
+
quantity: isNeedSplit ? 1 : product.quantity,
|
|
811
|
+
origin_total: getProductOriginTotalPrice({
|
|
812
|
+
product: {
|
|
813
|
+
original_price: product.original_price
|
|
814
|
+
},
|
|
815
|
+
bundle: product.bundle,
|
|
816
|
+
options: product.options
|
|
817
|
+
}),
|
|
818
|
+
variant: originProduct._productInit.variant,
|
|
819
|
+
original_price: new Decimal(product.price || 0).toNumber(),
|
|
820
|
+
total: total
|
|
821
|
+
}));
|
|
822
|
+
} else {
|
|
823
|
+
arr.push(_this3.hooks.setProduct(originProduct, {
|
|
824
|
+
discount_list: [discountDetail],
|
|
825
|
+
_id: product._id.split('___')[0] + "___" + _selectedDiscount.id + index,
|
|
826
|
+
price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
|
|
827
|
+
quantity: isNeedSplit ? 1 : product.quantity,
|
|
828
|
+
total: total,
|
|
829
|
+
origin_total: productOriginTotal,
|
|
830
|
+
main_product_selling_price: targetProductTotal
|
|
831
|
+
}));
|
|
832
|
+
}
|
|
546
833
|
}
|
|
547
|
-
|
|
548
|
-
|
|
834
|
+
processedProductsMap.set(product._id, arr);
|
|
835
|
+
} else {
|
|
836
|
+
// 🔥 bundle子商品:支持拆分
|
|
837
|
+
var processedItems = [];
|
|
838
|
+
if (isNeedSplit) {
|
|
839
|
+
// 商品券:需要拆分数量
|
|
840
|
+
var discountNum = splitCount;
|
|
841
|
+
var normalNum = totalQuantity - discountNum;
|
|
549
842
|
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
843
|
+
// 生成有折扣的商品(每张商品券对应 num: 1)
|
|
844
|
+
for (var _i = 0; _i < discountNum; _i++) {
|
|
845
|
+
var _selectedDiscount2 = applicableDiscounts[_i];
|
|
846
|
+
usedDiscounts.set(_selectedDiscount2.id, true);
|
|
847
|
+
|
|
848
|
+
// 🔥 生成唯一的 _id
|
|
849
|
+
var uniqueId = "".concat(flatItem._id, "_split_").concat(_i);
|
|
850
|
+
var _discountDetail = {
|
|
851
|
+
amount: product.origin_total,
|
|
852
|
+
type: 'good_pass',
|
|
853
|
+
discount: {
|
|
854
|
+
fixed_amount: product.origin_total,
|
|
855
|
+
resource_id: _selectedDiscount2.id,
|
|
856
|
+
title: _selectedDiscount2.format_title,
|
|
857
|
+
original_amount: product.origin_total,
|
|
858
|
+
product_id: product.id
|
|
560
859
|
},
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
860
|
+
metadata: {
|
|
861
|
+
// 🔥 使用拆分后的唯一 _id
|
|
862
|
+
custom_product_bundle_map_id: uniqueId,
|
|
863
|
+
num: 1
|
|
864
|
+
},
|
|
865
|
+
_num: 1
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
// 记录实际应用的折扣
|
|
869
|
+
var _appliedProducts = appliedDiscountProducts.get(_selectedDiscount2.id) || [];
|
|
870
|
+
_appliedProducts.push(_discountDetail);
|
|
871
|
+
appliedDiscountProducts.set(_selectedDiscount2.id, _appliedProducts);
|
|
872
|
+
processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
|
|
873
|
+
// 🔥 使用唯一的 _id
|
|
874
|
+
_id: uniqueId,
|
|
875
|
+
num: 1,
|
|
876
|
+
quantity: 1,
|
|
877
|
+
price: 0,
|
|
878
|
+
// 商品券价格为0
|
|
879
|
+
total: 0,
|
|
880
|
+
discount_list: [_discountDetail],
|
|
881
|
+
processed: true,
|
|
882
|
+
_discountId: _selectedDiscount2.id
|
|
883
|
+
}));
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// 生成无折扣的商品(剩余数量)
|
|
887
|
+
if (normalNum > 0) {
|
|
888
|
+
processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
|
|
889
|
+
// 🔥 为剩余商品生成唯一的 _id
|
|
890
|
+
_id: "".concat(flatItem._id, "_split_rest"),
|
|
891
|
+
num: normalNum,
|
|
892
|
+
quantity: normalNum,
|
|
893
|
+
discount_list: [],
|
|
894
|
+
processed: true
|
|
895
|
+
}));
|
|
896
|
+
}
|
|
568
897
|
} else {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
898
|
+
var _selectedDiscount3$me, _flatItem$parentProdu5;
|
|
899
|
+
// 折扣卡:不拆分数量,直接应用
|
|
900
|
+
var _selectedDiscount3 = selectedDiscountCard || applicableDiscounts[0];
|
|
901
|
+
usedDiscounts.set(_selectedDiscount3.id, true);
|
|
902
|
+
var _productOriginTotal = product.original_price || product.price || 0;
|
|
903
|
+
var _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
|
|
904
|
+
|
|
905
|
+
// 🔥 使用当前的 _id 作为唯一标识
|
|
906
|
+
var _uniqueId = flatItem._id;
|
|
907
|
+
var _discountDetail2 = {
|
|
908
|
+
amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber() * (product.num || 1),
|
|
909
|
+
type: _selectedDiscount3.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount3.tag,
|
|
910
|
+
discount: {
|
|
911
|
+
discount_card_type: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me = _selectedDiscount3.metadata) === null || _selectedDiscount3$me === void 0 ? void 0 : _selectedDiscount3$me.discount_card_type,
|
|
912
|
+
fixed_amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber(),
|
|
913
|
+
resource_id: _selectedDiscount3.id,
|
|
914
|
+
title: _selectedDiscount3.format_title,
|
|
915
|
+
original_amount: product.original_price,
|
|
916
|
+
product_id: product.id,
|
|
917
|
+
percent: _selectedDiscount3.par_value
|
|
918
|
+
},
|
|
919
|
+
metadata: {
|
|
920
|
+
// 🔥 使用唯一的 _id
|
|
921
|
+
custom_product_bundle_map_id: _uniqueId,
|
|
922
|
+
num: product.num || 1
|
|
923
|
+
},
|
|
924
|
+
_num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu5 = flatItem.parentProduct) === null || _flatItem$parentProdu5 === void 0 ? void 0 : _flatItem$parentProdu5.num) || 1)
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
// 记录实际应用的折扣
|
|
928
|
+
var _appliedProducts2 = appliedDiscountProducts.get(_selectedDiscount3.id) || [];
|
|
929
|
+
_appliedProducts2.push(_discountDetail2);
|
|
930
|
+
appliedDiscountProducts.set(_selectedDiscount3.id, _appliedProducts2);
|
|
931
|
+
processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
|
|
932
|
+
total: _targetProductTotal,
|
|
933
|
+
price: new Decimal(_productOriginTotal || 0).minus(_discountDetail2.discount.fixed_amount).toNumber(),
|
|
934
|
+
discount_list: [_discountDetail2],
|
|
935
|
+
processed: true
|
|
576
936
|
}));
|
|
577
937
|
}
|
|
938
|
+
processedFlatItemsMap.set(flatItem._id, processedItems);
|
|
578
939
|
}
|
|
579
|
-
console.log(arr, 'arrarrarr');
|
|
580
|
-
processedProductsMap.set(product._id, arr);
|
|
581
940
|
});
|
|
582
941
|
|
|
583
|
-
//
|
|
584
|
-
var
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
942
|
+
// 🔥 重组:将处理后的bundle子商品重新组装回主商品
|
|
943
|
+
var reconstructProductsWithBundle = function reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, originalProductList) {
|
|
944
|
+
var result = [];
|
|
945
|
+
originalProductList.forEach(function (originProduct) {
|
|
946
|
+
var product = _this3.hooks.getProduct(originProduct);
|
|
947
|
+
|
|
948
|
+
// 获取主商品处理结果
|
|
949
|
+
var mainProductArr = processedProductsMap.get(product._id);
|
|
950
|
+
if (!mainProductArr || mainProductArr.length === 0) {
|
|
951
|
+
// 如果没有处理结果,返回默认商品
|
|
952
|
+
var getDefaultProduct = function getDefaultProduct() {
|
|
953
|
+
if (product.isClient) {
|
|
954
|
+
return _this3.hooks.setProduct(originProduct, {
|
|
955
|
+
discount_list: [],
|
|
956
|
+
price: product.price,
|
|
957
|
+
origin_total: getProductOriginTotalPrice({
|
|
958
|
+
product: {
|
|
959
|
+
original_price: product.original_price
|
|
960
|
+
},
|
|
961
|
+
bundle: product.bundle,
|
|
962
|
+
options: product.options
|
|
963
|
+
}),
|
|
964
|
+
variant: originProduct._productInit.variant,
|
|
965
|
+
original_price: originProduct._productInit.original_price,
|
|
966
|
+
total: getProductTotalPrice({
|
|
967
|
+
product: {
|
|
968
|
+
price: product.price
|
|
969
|
+
},
|
|
970
|
+
bundle: product.bundle,
|
|
971
|
+
options: product.options
|
|
972
|
+
})
|
|
973
|
+
});
|
|
974
|
+
} else {
|
|
975
|
+
return _this3.hooks.setProduct(originProduct, {
|
|
976
|
+
discount_list: [],
|
|
977
|
+
total: product.total,
|
|
978
|
+
origin_total: product.origin_total,
|
|
603
979
|
price: product.price
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
result.push(getDefaultProduct());
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
// 检查是否有bundle子商品需要重组
|
|
988
|
+
var hasBundle = product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0;
|
|
989
|
+
if (!hasBundle) {
|
|
990
|
+
// 没有bundle,直接使用主商品处理结果
|
|
991
|
+
result.push.apply(result, _toConsumableArray(mainProductArr));
|
|
609
992
|
} else {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
993
|
+
// 🔥 有bundle,需要检查子商品是否应用了商品券
|
|
994
|
+
// 1. 先收集所有bundle子商品的处理结果
|
|
995
|
+
var bundleProcessingInfo = new Map();
|
|
996
|
+
var hasGoodPassApplied = false; // 标记是否有子商品应用了商品券
|
|
997
|
+
|
|
998
|
+
if (product.bundle && Array.isArray(product.bundle)) {
|
|
999
|
+
product.bundle.forEach(function (bundleItem, bundleIndex) {
|
|
1000
|
+
var bundleItemId = "".concat(product._id, "_bundle_").concat(bundleIndex);
|
|
1001
|
+
var processedBundleItems = processedFlatItemsMap.get(bundleItemId);
|
|
1002
|
+
if (!processedBundleItems || processedBundleItems.length === 0) {
|
|
1003
|
+
// 未处理的bundle item,保持原样
|
|
1004
|
+
bundleProcessingInfo.set(bundleIndex, [bundleItem]);
|
|
1005
|
+
} else {
|
|
1006
|
+
// 处理过的bundle item
|
|
1007
|
+
bundleProcessingInfo.set(bundleIndex, processedBundleItems);
|
|
1008
|
+
// 🔥 检查是否应用了商品券(good_pass)
|
|
1009
|
+
var hasGoodPass = processedBundleItems.some(function (item) {
|
|
1010
|
+
var _item$discount_list;
|
|
1011
|
+
return (_item$discount_list = item.discount_list) === null || _item$discount_list === void 0 ? void 0 : _item$discount_list.some(function (discount) {
|
|
1012
|
+
return discount.type === 'good_pass' || discount.tag === 'good_pass';
|
|
1013
|
+
});
|
|
1014
|
+
});
|
|
1015
|
+
if (hasGoodPass) {
|
|
1016
|
+
hasGoodPassApplied = true;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
// 🔥 2. 如果有子商品应用了商品券,且主商品数量大于1,需要拆分主商品
|
|
1023
|
+
var mainProductQuantity = mainProductArr[0] ? _this3.hooks.getProduct(mainProductArr[0]).quantity : 1;
|
|
1024
|
+
if (hasGoodPassApplied && mainProductArr.length === 1 && mainProductQuantity > 1) {
|
|
1025
|
+
var mainProduct = mainProductArr[0];
|
|
1026
|
+
var mainProductData = _this3.hooks.getProduct(mainProduct);
|
|
1027
|
+
|
|
1028
|
+
// 🔥 方案:拆分成2个主商品
|
|
1029
|
+
// 1. 一个主商品(qty=1)包含拆分后的bundle
|
|
1030
|
+
// 2. 一个主商品(qty=原quantity-1)包含原始未拆分的bundle
|
|
1031
|
+
|
|
1032
|
+
// 第一个:包含拆分后的bundle (qty=1)
|
|
1033
|
+
var newBundleWithDiscount = [];
|
|
1034
|
+
(product.bundle || []).forEach(function (bundleItem, bundleIndex) {
|
|
1035
|
+
var processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
|
|
1036
|
+
if (processedItems.length > 1) {
|
|
1037
|
+
// 被拆分的子商品,添加所有拆分项
|
|
1038
|
+
processedItems.forEach(function (item) {
|
|
1039
|
+
// 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
|
|
1040
|
+
var updatedDiscountList = (item.discount_list || []).map(function (discount) {
|
|
1041
|
+
var _item$metadata;
|
|
1042
|
+
return _objectSpread(_objectSpread({}, discount), {}, {
|
|
1043
|
+
metadata: {
|
|
1044
|
+
num: item.num,
|
|
1045
|
+
custom_product_bundle_map_id: (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.custom_product_bundle_map_id
|
|
1046
|
+
}
|
|
1047
|
+
// num: item.num, // 使用拆分后的 num
|
|
1048
|
+
});
|
|
1049
|
+
});
|
|
1050
|
+
newBundleWithDiscount.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
|
|
1051
|
+
_id: item._id,
|
|
1052
|
+
product_id: bundleItem.product_id,
|
|
1053
|
+
price: item.price,
|
|
1054
|
+
num: item.num,
|
|
1055
|
+
discount_list: updatedDiscountList
|
|
1056
|
+
}));
|
|
1057
|
+
});
|
|
1058
|
+
} else {
|
|
1059
|
+
// 未拆分的子商品,保持原样
|
|
1060
|
+
var item = processedItems[0];
|
|
1061
|
+
if (item.processed) {
|
|
1062
|
+
// 🔥 同样需要更新 discount_list 中的 num
|
|
1063
|
+
var _updatedDiscountList = (item.discount_list || []).map(function (discount) {
|
|
1064
|
+
var _item$metadata2;
|
|
1065
|
+
return _objectSpread(_objectSpread({}, discount), {}, {
|
|
1066
|
+
metadata: {
|
|
1067
|
+
num: item.num,
|
|
1068
|
+
custom_product_bundle_map_id: (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.custom_product_bundle_map_id
|
|
1069
|
+
}
|
|
1070
|
+
// num: item.num, // 使用当前的 num
|
|
1071
|
+
});
|
|
1072
|
+
});
|
|
1073
|
+
newBundleWithDiscount.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
|
|
1074
|
+
_id: item._id,
|
|
1075
|
+
product_id: bundleItem.product_id,
|
|
1076
|
+
price: item.price,
|
|
1077
|
+
num: item.num,
|
|
1078
|
+
discount_list: _updatedDiscountList
|
|
1079
|
+
}));
|
|
1080
|
+
} else {
|
|
1081
|
+
newBundleWithDiscount.push(item);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
});
|
|
1085
|
+
|
|
1086
|
+
// 计算第一个主商品的总价(包含拆分后的bundle)
|
|
1087
|
+
var newTotalWithDiscount = Number(mainProductData.price || 0);
|
|
1088
|
+
var newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
|
|
1089
|
+
|
|
1090
|
+
// 🔥 更新主商品自己的 discount_list 中的 num(quantity=1)
|
|
1091
|
+
var updatedMainDiscountList = mainProductData.discount_list.map(function (discount) {
|
|
1092
|
+
var _discount$metadata2, _discount$metadata3;
|
|
1093
|
+
if (discount !== null && discount !== void 0 && (_discount$metadata2 = discount.metadata) !== null && _discount$metadata2 !== void 0 && _discount$metadata2.custom_product_bundle_map_id) {
|
|
1094
|
+
// bundle的discount_list保持不变
|
|
1095
|
+
return discount;
|
|
1096
|
+
}
|
|
1097
|
+
// 主商品自己的discount,更新num为1
|
|
1098
|
+
return _objectSpread(_objectSpread({}, discount), {}, {
|
|
1099
|
+
// num: 1,
|
|
1100
|
+
metadata: {
|
|
1101
|
+
custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata3 = discount.metadata) === null || _discount$metadata3 === void 0 ? void 0 : _discount$metadata3.custom_product_bundle_map_id,
|
|
1102
|
+
num: 1
|
|
1103
|
+
}
|
|
1104
|
+
});
|
|
1105
|
+
});
|
|
1106
|
+
|
|
1107
|
+
// 🔥 使用更新后的列表计算折扣金额
|
|
1108
|
+
var mainDiscountList = updatedMainDiscountList.filter(function (item) {
|
|
1109
|
+
var _item$metadata3;
|
|
1110
|
+
return !(item !== null && item !== void 0 && (_item$metadata3 = item.metadata) !== null && _item$metadata3 !== void 0 && _item$metadata3.custom_product_bundle_map_id);
|
|
1111
|
+
});
|
|
1112
|
+
if (mainDiscountList && mainDiscountList.length > 0) {
|
|
1113
|
+
var _Decimal$minus$toNumb, _mainProductData$orig;
|
|
1114
|
+
var allDiscountAmount = getDiscountListAmountTotal(mainDiscountList);
|
|
1115
|
+
newTotalWithDiscount = (_Decimal$minus$toNumb = new Decimal(mainProductData.price || 0).minus(allDiscountAmount).toNumber()) !== null && _Decimal$minus$toNumb !== void 0 ? _Decimal$minus$toNumb : newTotalWithDiscount;
|
|
1116
|
+
newOriginTotalWithDiscount = (_mainProductData$orig = mainProductData.origin_total) !== null && _mainProductData$orig !== void 0 ? _mainProductData$orig : newOriginTotalWithDiscount;
|
|
1117
|
+
if (newBundleWithDiscount.length > 0) {
|
|
1118
|
+
newBundleWithDiscount.forEach(function (item) {
|
|
1119
|
+
var _item$discount_list2;
|
|
1120
|
+
newTotalWithDiscount += Number(item.price) * Number(item.num);
|
|
1121
|
+
var originalPrice = ((_item$discount_list2 = item.discount_list) === null || _item$discount_list2 === void 0 || (_item$discount_list2 = _item$discount_list2[0]) === null || _item$discount_list2 === void 0 || (_item$discount_list2 = _item$discount_list2.discount) === null || _item$discount_list2 === void 0 ? void 0 : _item$discount_list2.original_amount) || item.price;
|
|
1122
|
+
newOriginTotalWithDiscount += Number(originalPrice) * Number(item.num);
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
// 累加options的价格(options不参与折扣,在最终设置total时处理)
|
|
1128
|
+
if (product !== null && product !== void 0 && product.options) {
|
|
1129
|
+
newTotalWithDiscount = product.options.reduce(function (accumulator, currentValue) {
|
|
1130
|
+
var currentPrice = new Decimal(currentValue.price || 0);
|
|
1131
|
+
var currentNum = new Decimal(currentValue.num || 0);
|
|
1132
|
+
return accumulator.add(currentPrice.mul(currentNum));
|
|
1133
|
+
}, new Decimal(newTotalWithDiscount)).toNumber();
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// 添加第一个主商品:qty=1,包含拆分后的bundle
|
|
1137
|
+
result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
|
|
1138
|
+
_id: "".concat(mainProductData._id.split('___')[0], "___split_discount"),
|
|
1139
|
+
quantity: 1,
|
|
1140
|
+
discount_list: updatedMainDiscountList,
|
|
1141
|
+
bundle: newBundleWithDiscount,
|
|
1142
|
+
total: newTotalWithDiscount,
|
|
1143
|
+
origin_total: newOriginTotalWithDiscount
|
|
1144
|
+
})));
|
|
1145
|
+
|
|
1146
|
+
// 第二个:包含原始bundle (qty=原quantity-1)
|
|
1147
|
+
if (mainProductQuantity > 1) {
|
|
1148
|
+
var newBundleOriginal = [];
|
|
1149
|
+
(product.bundle || []).forEach(function (bundleItem, bundleIndex) {
|
|
1150
|
+
// 使用原始的bundle配置,不包含拆分
|
|
1151
|
+
newBundleOriginal.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
|
|
1152
|
+
discount_list: []
|
|
1153
|
+
}));
|
|
1154
|
+
});
|
|
1155
|
+
|
|
1156
|
+
// 计算第二个主商品的总价
|
|
1157
|
+
var newTotalOriginal = Number(mainProductData.price || 0);
|
|
1158
|
+
var newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
|
|
1159
|
+
|
|
1160
|
+
// 🔥 更新主商品自己的 discount_list 中的 num(quantity=原quantity-1)
|
|
1161
|
+
var updatedMainDiscountListOriginal = mainProductData.discount_list.map(function (discount) {
|
|
1162
|
+
var _discount$metadata4, _discount$metadata5;
|
|
1163
|
+
if (discount !== null && discount !== void 0 && (_discount$metadata4 = discount.metadata) !== null && _discount$metadata4 !== void 0 && _discount$metadata4.custom_product_bundle_map_id) {
|
|
1164
|
+
// bundle的discount_list保持不变
|
|
1165
|
+
return discount;
|
|
1166
|
+
}
|
|
1167
|
+
// 主商品自己的discount,更新num为 mainProductQuantity - 1
|
|
1168
|
+
return _objectSpread(_objectSpread({}, discount), {}, {
|
|
1169
|
+
metadata: {
|
|
1170
|
+
num: mainProductQuantity - 1,
|
|
1171
|
+
custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata5 = discount.metadata) === null || _discount$metadata5 === void 0 ? void 0 : _discount$metadata5.custom_product_bundle_map_id
|
|
1172
|
+
}
|
|
1173
|
+
// num: mainProductQuantity - 1,
|
|
1174
|
+
});
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
// 🔥 使用更新后的列表计算折扣金额
|
|
1178
|
+
var mainDiscountListOriginal = updatedMainDiscountListOriginal.filter(function (item) {
|
|
1179
|
+
var _item$metadata4;
|
|
1180
|
+
return !(item !== null && item !== void 0 && (_item$metadata4 = item.metadata) !== null && _item$metadata4 !== void 0 && _item$metadata4.custom_product_bundle_map_id);
|
|
1181
|
+
});
|
|
1182
|
+
if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
|
|
1183
|
+
var _Decimal$minus$toNumb2, _mainProductData$orig2;
|
|
1184
|
+
var _allDiscountAmount = getDiscountListAmount(mainDiscountListOriginal);
|
|
1185
|
+
newTotalOriginal = (_Decimal$minus$toNumb2 = new Decimal(mainProductData.price || 0).minus(_allDiscountAmount).toNumber()) !== null && _Decimal$minus$toNumb2 !== void 0 ? _Decimal$minus$toNumb2 : newTotalOriginal;
|
|
1186
|
+
newOriginTotalOriginal = (_mainProductData$orig2 = mainProductData.origin_total) !== null && _mainProductData$orig2 !== void 0 ? _mainProductData$orig2 : newOriginTotalOriginal;
|
|
1187
|
+
if (newBundleOriginal.length > 0) {
|
|
1188
|
+
newBundleOriginal.forEach(function (item) {
|
|
1189
|
+
newTotalOriginal += Number(item.price) * Number(item.num);
|
|
1190
|
+
newOriginTotalOriginal += Number(item.price) * Number(item.num);
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
// 累加options的价格(options不参与折扣,在最终设置total时处理)
|
|
1196
|
+
if (product !== null && product !== void 0 && product.options) {
|
|
1197
|
+
newTotalOriginal = product.options.reduce(function (accumulator, currentValue) {
|
|
1198
|
+
var currentPrice = new Decimal(currentValue.price || 0);
|
|
1199
|
+
var currentNum = new Decimal(currentValue.num || 0);
|
|
1200
|
+
return accumulator.add(currentPrice.mul(currentNum));
|
|
1201
|
+
}, new Decimal(newTotalOriginal)).toNumber();
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// 添加第二个主商品:qty=原quantity-1,包含原始bundle
|
|
1205
|
+
result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
|
|
1206
|
+
_id: "".concat(mainProductData._id.split('___')[0], "___split_normal"),
|
|
1207
|
+
quantity: mainProductQuantity - 1,
|
|
1208
|
+
discount_list: updatedMainDiscountListOriginal,
|
|
1209
|
+
bundle: newBundleOriginal,
|
|
1210
|
+
total: newTotalOriginal,
|
|
1211
|
+
origin_total: newOriginTotalOriginal
|
|
1212
|
+
})));
|
|
1213
|
+
}
|
|
1214
|
+
} else {
|
|
1215
|
+
// 🔥 没有子商品被拆分,使用原有逻辑
|
|
1216
|
+
mainProductArr.forEach(function (mainProduct) {
|
|
1217
|
+
var _mainProductData$disc, _mainProductData$disc2, _mainProductData$disc3;
|
|
1218
|
+
var mainProductData = _this3.hooks.getProduct(mainProduct);
|
|
1219
|
+
|
|
1220
|
+
// 重组bundle
|
|
1221
|
+
var newBundle = [];
|
|
1222
|
+
if (product.bundle && Array.isArray(product.bundle)) {
|
|
1223
|
+
product.bundle.forEach(function (bundleItem, bundleIndex) {
|
|
1224
|
+
var bundleItemId = "".concat(product._id, "_bundle_").concat(bundleIndex);
|
|
1225
|
+
var processedBundleItems = processedFlatItemsMap.get(bundleItemId);
|
|
1226
|
+
if (!processedBundleItems || processedBundleItems.length === 0) {
|
|
1227
|
+
// 未处理的bundle item,保持原样
|
|
1228
|
+
newBundle.push(bundleItem);
|
|
1229
|
+
} else {
|
|
1230
|
+
// 🔥 关键:拆分后的bundle item
|
|
1231
|
+
processedBundleItems.forEach(function (item) {
|
|
1232
|
+
// 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
|
|
1233
|
+
var updatedDiscountList = (item.discount_list || []).map(function (discount) {
|
|
1234
|
+
var _discount$metadata6;
|
|
1235
|
+
return _objectSpread(_objectSpread({}, discount), {}, {
|
|
1236
|
+
metadata: {
|
|
1237
|
+
num: item.num,
|
|
1238
|
+
custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata6 = discount.metadata) === null || _discount$metadata6 === void 0 ? void 0 : _discount$metadata6.custom_product_bundle_map_id
|
|
1239
|
+
}
|
|
1240
|
+
// num: item.num, // 使用拆分后的 num
|
|
1241
|
+
});
|
|
1242
|
+
});
|
|
1243
|
+
newBundle.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
|
|
1244
|
+
_id: item._id,
|
|
1245
|
+
product_id: bundleItem.product_id,
|
|
1246
|
+
price: item.price,
|
|
1247
|
+
num: item.num,
|
|
1248
|
+
discount_list: updatedDiscountList
|
|
1249
|
+
}));
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// 🔥 重新计算主商品的总价(包含bundle)
|
|
1256
|
+
var newTotal = Number(mainProductData.price || 0);
|
|
1257
|
+
var newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
|
|
1258
|
+
|
|
1259
|
+
// 判断是否是手动折扣
|
|
1260
|
+
var isManualDiscount = typeof mainProductData.isManualDiscount === 'boolean' ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_mainProductData$disc = mainProductData.discount_list) !== null && _mainProductData$disc !== void 0 && _mainProductData$disc.length) || (mainProductData === null || mainProductData === void 0 || (_mainProductData$disc2 = mainProductData.discount_list) === null || _mainProductData$disc2 === void 0 || (_mainProductData$disc3 = _mainProductData$disc2.every) === null || _mainProductData$disc3 === void 0 ? void 0 : _mainProductData$disc3.call(_mainProductData$disc2, function (item) {
|
|
1261
|
+
return item.type === 'product';
|
|
1262
|
+
})));
|
|
1263
|
+
|
|
1264
|
+
// 如果是手动折扣,使用原有的total和origin_total,不重新计算
|
|
1265
|
+
if (isManualDiscount) {
|
|
1266
|
+
var _mainProductData$tota, _mainProductData$orig3;
|
|
1267
|
+
newTotal = (_mainProductData$tota = mainProductData.total) !== null && _mainProductData$tota !== void 0 ? _mainProductData$tota : newTotal;
|
|
1268
|
+
newOriginTotal = (_mainProductData$orig3 = mainProductData.origin_total) !== null && _mainProductData$orig3 !== void 0 ? _mainProductData$orig3 : newOriginTotal;
|
|
1269
|
+
} else {
|
|
1270
|
+
// 不是手动折扣时,才重新计算
|
|
1271
|
+
var _mainDiscountList = mainProductData.discount_list.filter(function (item) {
|
|
1272
|
+
var _item$metadata5;
|
|
1273
|
+
return !(item !== null && item !== void 0 && (_item$metadata5 = item.metadata) !== null && _item$metadata5 !== void 0 && _item$metadata5.custom_product_bundle_map_id);
|
|
1274
|
+
});
|
|
1275
|
+
|
|
1276
|
+
// 如果主商品本身有折扣,需要重新计算主商品价格
|
|
1277
|
+
if (_mainDiscountList && _mainDiscountList.length > 0) {
|
|
1278
|
+
var _Decimal$minus$toNumb3, _mainProductData$orig4;
|
|
1279
|
+
var _allDiscountAmount2 = getDiscountListAmount(_mainDiscountList);
|
|
1280
|
+
newTotal = (_Decimal$minus$toNumb3 = new Decimal(mainProductData.price || 0).minus(_allDiscountAmount2).toNumber()) !== null && _Decimal$minus$toNumb3 !== void 0 ? _Decimal$minus$toNumb3 : newTotal;
|
|
1281
|
+
newOriginTotal = (_mainProductData$orig4 = mainProductData.origin_total) !== null && _mainProductData$orig4 !== void 0 ? _mainProductData$orig4 : newOriginTotal;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
// 累加bundle的价格(只累加一次)
|
|
1285
|
+
if (newBundle.length > 0) {
|
|
1286
|
+
newBundle.forEach(function (item) {
|
|
1287
|
+
var _item$discount_list3;
|
|
1288
|
+
newTotal += Number(item.price) * Number(item.num);
|
|
1289
|
+
var originalPrice = ((_item$discount_list3 = item.discount_list) === null || _item$discount_list3 === void 0 || (_item$discount_list3 = _item$discount_list3[0]) === null || _item$discount_list3 === void 0 || (_item$discount_list3 = _item$discount_list3.discount) === null || _item$discount_list3 === void 0 ? void 0 : _item$discount_list3.original_amount) || item.price;
|
|
1290
|
+
newOriginTotal += Number(originalPrice) * Number(item.num);
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
// 累加options的价格(options不参与折扣,在最终设置total时处理)
|
|
1296
|
+
if (product !== null && product !== void 0 && product.options) {
|
|
1297
|
+
newTotal = product.options.reduce(function (accumulator, currentValue) {
|
|
1298
|
+
var currentPrice = new Decimal(currentValue.price || 0);
|
|
1299
|
+
var currentNum = new Decimal(currentValue.num || 0);
|
|
1300
|
+
return accumulator.add(currentPrice.mul(currentNum));
|
|
1301
|
+
}, new Decimal(newTotal)).toNumber();
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
// 生成最终的主商品
|
|
1305
|
+
result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
|
|
1306
|
+
bundle: newBundle,
|
|
1307
|
+
total: newTotal,
|
|
1308
|
+
origin_total: newOriginTotal
|
|
1309
|
+
})));
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
616
1312
|
}
|
|
617
|
-
};
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
1313
|
+
});
|
|
1314
|
+
return result;
|
|
1315
|
+
};
|
|
1316
|
+
|
|
1317
|
+
// 按原始顺序构建处理后的商品列表
|
|
1318
|
+
var processedProductList = reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, productList);
|
|
621
1319
|
|
|
622
1320
|
// 按原始顺序更新优惠券列表,标记已使用和可用的优惠券
|
|
623
1321
|
var updatedDiscountList = addModeDiscount.map(function (discount) {
|
|
@@ -688,6 +1386,145 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
688
1386
|
discountList: [].concat(editModeDiscount, _toConsumableArray(updatedDiscountList))
|
|
689
1387
|
};
|
|
690
1388
|
}
|
|
1389
|
+
|
|
1390
|
+
/**
|
|
1391
|
+
* 检查优惠是否符合 PackageSubItemUsageRules 配置
|
|
1392
|
+
* @param discount 优惠券
|
|
1393
|
+
* @param flatItem 扁平化后的商品项(可能是主商品或bundle子商品)
|
|
1394
|
+
* @returns 是否可用
|
|
1395
|
+
*/
|
|
1396
|
+
}, {
|
|
1397
|
+
key: "checkPackageSubItemUsageRules",
|
|
1398
|
+
value: function checkPackageSubItemUsageRules(discount, flatItem) {
|
|
1399
|
+
var limitedData = discount.limited_relation_product_data;
|
|
1400
|
+
var usageRules = limitedData === null || limitedData === void 0 ? void 0 : limitedData.package_sub_item_usage_rules;
|
|
1401
|
+
var rules = ['original_price'].concat(_toConsumableArray((usageRules === null || usageRules === void 0 ? void 0 : usageRules.rules) || []));
|
|
1402
|
+
|
|
1403
|
+
// 如果没有配置 package_sub_item_usage_rules,则默认可用
|
|
1404
|
+
if (!usageRules) {
|
|
1405
|
+
return true;
|
|
1406
|
+
}
|
|
1407
|
+
var ruleType = usageRules.type;
|
|
1408
|
+
var isMainProduct = flatItem.type === 'main'; // 单独购买
|
|
1409
|
+
var isBundleItem = flatItem.type === 'bundle'; // 套餐中购买
|
|
1410
|
+
|
|
1411
|
+
// 主商品直接可用
|
|
1412
|
+
if (isMainProduct) {
|
|
1413
|
+
return true;
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
// universal_discount: 单独购买和套餐中(子商品为原价)均可使用
|
|
1417
|
+
if (ruleType === 'universal_discount') {
|
|
1418
|
+
if (isMainProduct) {
|
|
1419
|
+
return true; // 单独购买时可用
|
|
1420
|
+
}
|
|
1421
|
+
if (isBundleItem) {
|
|
1422
|
+
var _flatItem$bundleItem5, _flatItem$bundleItem6;
|
|
1423
|
+
// 套餐中购买时,判断是否为原价
|
|
1424
|
+
var priceType = (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.price_type;
|
|
1425
|
+
var priceTypeExt = (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.price_type_ext;
|
|
1426
|
+
// original_price 对应:
|
|
1427
|
+
// 1. price_type: "markup" && price_type_ext: "product_price"
|
|
1428
|
+
// markup_price 对应:
|
|
1429
|
+
// price_type: "markup" && price_type_ext: ""
|
|
1430
|
+
/** 原价 */
|
|
1431
|
+
var isOriginalPrice = priceType === 'markup' && priceTypeExt === 'product_price';
|
|
1432
|
+
/** 加价 */
|
|
1433
|
+
var isMarkupPrice = priceType === 'markup' && (priceTypeExt === '' || !priceTypeExt);
|
|
1434
|
+
|
|
1435
|
+
// 检查 rules
|
|
1436
|
+
if (rules.length > 0) {
|
|
1437
|
+
// 检查原价
|
|
1438
|
+
if (isOriginalPrice && rules.includes('original_price')) {
|
|
1439
|
+
return true;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// 检查加价
|
|
1443
|
+
if (isMarkupPrice && rules.includes('markup_price')) {
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
// 如果都不匹配,则不可用
|
|
1448
|
+
return false;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
// 原价包括:price_type: "markup" && price_type_ext: "product_price"
|
|
1452
|
+
return isOriginalPrice;
|
|
1453
|
+
}
|
|
1454
|
+
return true;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
// package_exclusive: 仅在套餐中(子商品为原价)时可使用
|
|
1458
|
+
if (ruleType === 'package_exclusive') {
|
|
1459
|
+
if (isMainProduct) {
|
|
1460
|
+
return false; // 单独购买时不可用
|
|
1461
|
+
}
|
|
1462
|
+
if (isBundleItem) {
|
|
1463
|
+
var _flatItem$bundleItem7, _flatItem$bundleItem8;
|
|
1464
|
+
// 套餐中购买时,判断是否为原价
|
|
1465
|
+
var _priceType = (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.price_type;
|
|
1466
|
+
var _priceTypeExt = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type_ext;
|
|
1467
|
+
// 原价包括:price_type: "markup" && price_type_ext: "product_price"
|
|
1468
|
+
return _priceType === 'markup' && _priceTypeExt === 'product_price';
|
|
1469
|
+
}
|
|
1470
|
+
return false;
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
// single_item_promo: 仅在单独购买时可使用
|
|
1474
|
+
if (ruleType === 'single_item_promo') {
|
|
1475
|
+
return isMainProduct; // 只有单独购买时可用
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
/*
|
|
1479
|
+
// custom_usage_rules: 根据自定义规则判断
|
|
1480
|
+
if (ruleType === 'custom_usage_rules') {
|
|
1481
|
+
const customRules = usageRules.custom_usage_rules;
|
|
1482
|
+
if (!customRules) {
|
|
1483
|
+
return true; // 如果没有自定义规则,默认可用
|
|
1484
|
+
}
|
|
1485
|
+
const { types, package_sub_item_rules } = customRules;
|
|
1486
|
+
// 判断商品类型是否在允许的类型中
|
|
1487
|
+
if (isMainProduct) {
|
|
1488
|
+
// 主商品对应 standalone_product
|
|
1489
|
+
if (!types.includes('standalone_product')) {
|
|
1490
|
+
return false;
|
|
1491
|
+
}
|
|
1492
|
+
return true; // 主商品不需要判断 package_sub_item_rules
|
|
1493
|
+
}
|
|
1494
|
+
if (isBundleItem) {
|
|
1495
|
+
// bundle子商品对应 package_sub_item
|
|
1496
|
+
if (!types.includes('package_sub_item')) {
|
|
1497
|
+
return false;
|
|
1498
|
+
}
|
|
1499
|
+
// 如果包含 package_sub_item,还需要判断 price_type
|
|
1500
|
+
const priceType = flatItem.bundleItem?.price_type;
|
|
1501
|
+
const priceTypeExt = flatItem.bundleItem?.price_type_ext;
|
|
1502
|
+
// 检查 package_sub_item_rules
|
|
1503
|
+
if (package_sub_item_rules && package_sub_item_rules.length > 0) {
|
|
1504
|
+
// original_price 对应:
|
|
1505
|
+
// 1. price_type: "markup" && price_type_ext: "product_price"
|
|
1506
|
+
// markup_price 对应:
|
|
1507
|
+
// price_type: "markup" && price_type_ext: ""
|
|
1508
|
+
const isOriginalPrice = (priceType === 'markup' && priceTypeExt === 'product_price');
|
|
1509
|
+
const isMarkupPrice = priceType === 'markup' && (priceTypeExt === '' || !priceTypeExt);
|
|
1510
|
+
if (isOriginalPrice && package_sub_item_rules.includes('original_price')) {
|
|
1511
|
+
return true;
|
|
1512
|
+
}
|
|
1513
|
+
if (isMarkupPrice && package_sub_item_rules.includes('markup_price')) {
|
|
1514
|
+
return true;
|
|
1515
|
+
}
|
|
1516
|
+
// 如果都不匹配,则不可用
|
|
1517
|
+
return false;
|
|
1518
|
+
}
|
|
1519
|
+
return true; // 如果没有 package_sub_item_rules 配置,默认可用
|
|
1520
|
+
}
|
|
1521
|
+
return true;
|
|
1522
|
+
}
|
|
1523
|
+
* */
|
|
1524
|
+
|
|
1525
|
+
// 默认可用
|
|
1526
|
+
return true;
|
|
1527
|
+
}
|
|
691
1528
|
}, {
|
|
692
1529
|
key: "destroy",
|
|
693
1530
|
value: function () {
|