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