@pisell/pisellos 2.2.10 → 2.2.11
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 +26 -41
- package/dist/modules/Customer/index.js +1 -1
- package/dist/modules/Discount/index.d.ts +0 -2
- package/dist/modules/Discount/index.js +36 -69
- package/dist/modules/Discount/types.d.ts +0 -16
- package/dist/modules/Order/index.js +1 -4
- package/dist/modules/Order/utils.d.ts +0 -1
- package/dist/modules/Order/utils.js +0 -9
- package/dist/modules/Payment/index.js +2 -2
- package/dist/modules/Payment/types.d.ts +0 -1
- package/dist/modules/Payment/walletpass.js +1 -3
- package/dist/modules/Product/index.d.ts +1 -1
- package/dist/modules/ProductList/index.js +9 -8
- package/dist/modules/Rules/index.d.ts +0 -7
- package/dist/modules/Rules/index.js +196 -1065
- package/dist/modules/Rules/types.d.ts +1 -4
- package/dist/solution/BookingByStep/index.d.ts +1 -1
- package/dist/solution/BookingByStep/index.js +8 -30
- package/dist/solution/BookingByStep/utils/products.d.ts +0 -6
- package/dist/solution/BookingByStep/utils/products.js +0 -10
- package/dist/solution/BookingTicket/index.d.ts +1 -1
- package/dist/solution/Checkout/index.js +49 -93
- package/dist/solution/ShopDiscount/index.d.ts +0 -2
- package/dist/solution/ShopDiscount/index.js +19 -80
- package/dist/solution/ShopDiscount/types.d.ts +1 -4
- package/dist/solution/ShopDiscount/utils.d.ts +0 -55
- package/dist/solution/ShopDiscount/utils.js +3 -432
- package/lib/modules/Cart/utils/cartProduct.js +22 -35
- package/lib/modules/Customer/index.js +1 -1
- package/lib/modules/Discount/index.d.ts +0 -2
- package/lib/modules/Discount/index.js +4 -19
- package/lib/modules/Discount/types.d.ts +0 -16
- package/lib/modules/Order/index.js +0 -2
- package/lib/modules/Order/utils.d.ts +0 -1
- package/lib/modules/Order/utils.js +0 -11
- package/lib/modules/Payment/index.js +1 -1
- package/lib/modules/Payment/types.d.ts +0 -1
- package/lib/modules/Payment/walletpass.js +1 -10
- package/lib/modules/Product/index.d.ts +1 -1
- package/lib/modules/ProductList/index.js +7 -0
- package/lib/modules/Rules/index.d.ts +0 -7
- package/lib/modules/Rules/index.js +182 -824
- package/lib/modules/Rules/types.d.ts +1 -4
- package/lib/solution/BookingByStep/index.d.ts +1 -1
- package/lib/solution/BookingByStep/index.js +2 -19
- package/lib/solution/BookingByStep/utils/products.d.ts +0 -6
- package/lib/solution/BookingByStep/utils/products.js +2 -8
- package/lib/solution/BookingTicket/index.d.ts +1 -1
- package/lib/solution/Checkout/index.js +19 -68
- package/lib/solution/ShopDiscount/index.d.ts +0 -2
- package/lib/solution/ShopDiscount/index.js +9 -55
- package/lib/solution/ShopDiscount/types.d.ts +1 -4
- package/lib/solution/ShopDiscount/utils.d.ts +0 -55
- package/lib/solution/ShopDiscount/utils.js +3 -266
- package/package.json +2 -2
|
@@ -24,12 +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
|
|
27
|
+
import { uniqueById, getDiscountAmount } 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
|
-
|
|
33
31
|
// 临时变量
|
|
34
32
|
var flatItem;
|
|
35
33
|
export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
@@ -164,15 +162,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
164
162
|
// 遍历处理后的产品列表,检查是否有产品使用了新折扣
|
|
165
163
|
result.productList.forEach(function (product) {
|
|
166
164
|
var _this2$hooks$getProdu = _this2.hooks.getProduct(product),
|
|
167
|
-
discount_list = _this2$hooks$getProdu.discount_list
|
|
168
|
-
|
|
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) {
|
|
165
|
+
discount_list = _this2$hooks$getProdu.discount_list;
|
|
166
|
+
if (discount_list && discount_list.length > 0) {
|
|
174
167
|
// 检查是否有使用新折扣的情况
|
|
175
|
-
var usedNewDiscount =
|
|
168
|
+
var usedNewDiscount = discount_list.some(function (discount) {
|
|
176
169
|
var _discount$discount;
|
|
177
170
|
return newDiscountIds.includes(discount === null || discount === void 0 || (_discount$discount = discount.discount) === null || _discount$discount === void 0 ? void 0 : _discount$discount.resource_id);
|
|
178
171
|
});
|
|
@@ -216,55 +209,6 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
216
209
|
return !discount.isManualSelect;
|
|
217
210
|
});
|
|
218
211
|
|
|
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
|
-
|
|
268
212
|
// 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
|
|
269
213
|
// 1. 商品券(n.tag=good_pass)视为最优惠(免费)
|
|
270
214
|
// 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
|
|
@@ -300,12 +244,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
300
244
|
}
|
|
301
245
|
}
|
|
302
246
|
|
|
303
|
-
// 3. 都是百分比时,折扣越大越优先(par_value
|
|
247
|
+
// 3. 都是百分比时,折扣越大越优先(par_value越小越优先)
|
|
304
248
|
if (typeA === 'percent' && typeB === 'percent') {
|
|
305
249
|
if (a.par_value !== b.par_value) {
|
|
306
250
|
var _valueA = new Decimal(100).minus(a.par_value || 0);
|
|
307
251
|
var _valueB = new Decimal(100).minus(b.par_value || 0);
|
|
308
|
-
return
|
|
252
|
+
return _valueB.minus(_valueA).toNumber(); // 折扣大的在前
|
|
309
253
|
}
|
|
310
254
|
}
|
|
311
255
|
|
|
@@ -339,50 +283,21 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
339
283
|
// }
|
|
340
284
|
// })
|
|
341
285
|
|
|
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
|
-
/**
|
|
371
286
|
// 对productList按价格降序排序(用于应用优惠券时优先选择高价商品) 价格相同时使用quantity 排序
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
287
|
+
var sortedProductList = _toConsumableArray(productList).sort(function (a, b) {
|
|
288
|
+
var aProduct = _this3.hooks.getProduct(a);
|
|
289
|
+
var bProduct = _this3.hooks.getProduct(b);
|
|
290
|
+
var priceA = new Decimal(aProduct.price || '0');
|
|
291
|
+
var priceB = new Decimal(bProduct.price || '0');
|
|
377
292
|
if (priceA.toNumber() === priceB.toNumber()) {
|
|
378
293
|
if (aProduct.quantity === bProduct.quantity) {
|
|
379
|
-
|
|
294
|
+
var _bProduct$discount_li, _aProduct$discount_li;
|
|
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);
|
|
380
296
|
}
|
|
381
297
|
return aProduct.quantity - bProduct.quantity;
|
|
382
298
|
}
|
|
383
299
|
return priceB.toNumber() - priceA.toNumber();
|
|
384
300
|
});
|
|
385
|
-
*/
|
|
386
301
|
|
|
387
302
|
// 标记已使用的优惠券
|
|
388
303
|
var usedDiscounts = new Map();
|
|
@@ -406,50 +321,26 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
406
321
|
var appliedDiscountProducts = new Map();
|
|
407
322
|
|
|
408
323
|
// 首先遍历所有商品和所有优惠券,确定每个优惠券的适用范围,包括isSelected为false的优惠券
|
|
409
|
-
|
|
410
|
-
|
|
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
|
-
}
|
|
324
|
+
sortedProductList.forEach(function (originProduct) {
|
|
325
|
+
var product = _this3.hooks.getProduct(originProduct);
|
|
431
326
|
addModeDiscount.forEach(function (discount) {
|
|
432
|
-
var _flatItem$parentProdu, _flatItem$product, _product,
|
|
327
|
+
var _flatItem$parentProdu, _flatItem$product, _product$discount_lis2, _product$discount_lis3;
|
|
433
328
|
var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
|
|
434
329
|
// 拿到discount配置的holder信息 product信息 product.holder 加在 isLimitedProduct
|
|
435
330
|
var isHolderMatch = _this3.checkHolderMatch(discount, {
|
|
436
331
|
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
|
|
437
332
|
}, holders);
|
|
438
|
-
|
|
439
|
-
timeLimit = !!filterDiscountListByBookingTime([discount], (((_product = product) === null || _product === void 0 ? void 0 : _product.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
|
|
333
|
+
|
|
440
334
|
// 是符合折扣的商品
|
|
441
335
|
var isLimitedProduct = (limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
|
|
442
336
|
|
|
443
337
|
// 编辑的商品 使用了优惠券不可用
|
|
444
|
-
var isAvailableProduct =
|
|
338
|
+
var isAvailableProduct = !(product !== null && product !== void 0 && product.booking_id && product !== null && product !== void 0 && (_product$discount_lis2 = product.discount_list) !== null && _product$discount_lis2 !== void 0 && _product$discount_lis2.length && product !== null && product !== void 0 && (_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.every(function (discount) {
|
|
445
339
|
return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
|
|
446
|
-
}))
|
|
447
|
-
|
|
448
|
-
// 套餐是否可用判断
|
|
449
|
-
var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
|
|
340
|
+
}));
|
|
450
341
|
|
|
451
342
|
// 判断优惠券是否适用于该商品
|
|
452
|
-
if (isAvailableProduct && isLimitedProduct
|
|
343
|
+
if (isAvailableProduct && isLimitedProduct) {
|
|
453
344
|
var _discountApplicabilit, _discount$metadata;
|
|
454
345
|
// 记录此优惠券适用的商品
|
|
455
346
|
(_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
|
|
@@ -457,12 +348,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
457
348
|
// 记录可抵扣的商品详情
|
|
458
349
|
var applicableProducts = discountApplicableProducts.get(discount.id) || [];
|
|
459
350
|
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;
|
|
464
351
|
var productData = {
|
|
465
|
-
amount: product.price
|
|
352
|
+
amount: product.price,
|
|
466
353
|
type: discountType,
|
|
467
354
|
tag: discountType,
|
|
468
355
|
discount: {
|
|
@@ -470,88 +357,49 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
470
357
|
fixed_amount: product.price,
|
|
471
358
|
resource_id: discount.id,
|
|
472
359
|
title: discount.format_title,
|
|
473
|
-
original_amount: product.
|
|
360
|
+
original_amount: product.origin_total,
|
|
474
361
|
pre_value: discount.par_value,
|
|
475
362
|
product_id: originProduct.id
|
|
476
|
-
},
|
|
477
|
-
metadata: {
|
|
478
|
-
num: num
|
|
479
363
|
}
|
|
480
364
|
};
|
|
365
|
+
|
|
366
|
+
// 如果 discount.tag 或者 discount.type 是 good_pass,则不需要添加 num 属性
|
|
367
|
+
if (discountType !== 'good_pass') {
|
|
368
|
+
productData.num = product.num || 1;
|
|
369
|
+
}
|
|
481
370
|
applicableProducts.push(productData);
|
|
482
371
|
discountApplicableProducts.set(discount.id, applicableProducts);
|
|
483
372
|
}
|
|
484
373
|
});
|
|
485
374
|
});
|
|
486
|
-
|
|
487
|
-
// 🔥 用于存储扁平化商品处理结果的Map
|
|
488
|
-
var processedFlatItemsMap = new Map();
|
|
375
|
+
console.log(sortedProductList, 'sortedProductListsortedProductList');
|
|
489
376
|
|
|
490
377
|
// 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
var
|
|
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) {
|
|
378
|
+
sortedProductList.forEach(function (originProduct, index) {
|
|
379
|
+
var _product$discount_lis4, _product$discount_lis5, _product$discount_lis7, _product$discount_lis8, _product$discount_lis9, _product$discount_lis10, _product$discount_lis11;
|
|
380
|
+
var product = _this3.hooks.getProduct(originProduct);
|
|
381
|
+
if (product !== null && product !== void 0 && product.booking_id && (_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.length && product !== null && product !== void 0 && (_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.every(function (discount) {
|
|
519
382
|
return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
|
|
520
383
|
})) {
|
|
521
|
-
|
|
522
|
-
processedProductsMap.set(product._id, [originProduct]);
|
|
523
|
-
} else {
|
|
524
|
-
processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
|
|
525
|
-
processed: true
|
|
526
|
-
})]);
|
|
527
|
-
}
|
|
384
|
+
processedProductsMap.set(product._id, [originProduct]);
|
|
528
385
|
return;
|
|
529
386
|
}
|
|
530
387
|
|
|
531
388
|
// 找到适用于此商品的所有优惠券,仅考虑isSelected不为false的优惠券
|
|
532
389
|
var applicableDiscounts = sortedDiscountList.filter(function (discount) {
|
|
533
|
-
var _product$
|
|
390
|
+
var _product$discount_lis6, _flatItem$parentProdu2, _flatItem$product2;
|
|
534
391
|
// 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
|
|
535
|
-
//
|
|
536
|
-
if ((Number(product.price) <= 0 || !product.price) &&
|
|
392
|
+
// 商品券时主商品价格小于等于0不可用
|
|
393
|
+
if ((Number(product.price) <= 0 || !product.price) && (discount.tag || discount.type) === 'good_pass') return false;
|
|
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) {
|
|
537
396
|
var _n$discount;
|
|
538
397
|
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;
|
|
545
398
|
})) && (discount.tag || discount.type) !== 'good_pass') return false;
|
|
546
399
|
// 如果优惠券已被使用,则跳过
|
|
547
400
|
var targetUsedDiscounts = usedDiscounts.get(discount.id);
|
|
548
401
|
if (targetUsedDiscounts && (discount.tag || discount.type) === 'good_pass') return false;
|
|
549
402
|
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
|
-
}
|
|
555
403
|
|
|
556
404
|
// 拿到discount配置的holder信息 product信息 product.holder 不可用return false
|
|
557
405
|
var isHolderMatch = _this3.checkHolderMatch(discount, {
|
|
@@ -562,16 +410,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
562
410
|
|
|
563
411
|
// 判断优惠券是否适用于该商品
|
|
564
412
|
if (limitedData.type === 'product_all') {
|
|
565
|
-
// 检查 package_sub_item_usage_rules
|
|
566
|
-
if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
|
|
567
|
-
return false;
|
|
568
|
-
}
|
|
569
413
|
return true;
|
|
570
414
|
} 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
|
-
}
|
|
575
415
|
return true;
|
|
576
416
|
}
|
|
577
417
|
return false;
|
|
@@ -586,133 +426,60 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
586
426
|
var selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
|
|
587
427
|
|
|
588
428
|
// 如果是手动折扣,则不适用优惠券
|
|
589
|
-
var isManualDiscount =
|
|
590
|
-
|
|
591
|
-
|
|
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
|
-
}
|
|
429
|
+
var isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (!((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.length) || (product === null || product === void 0 || (_product$discount_lis8 = product.discount_list) === null || _product$discount_lis8 === void 0 || (_product$discount_lis9 = _product$discount_lis8.every) === null || _product$discount_lis9 === void 0 ? void 0 : _product$discount_lis9.call(_product$discount_lis8, function (item) {
|
|
430
|
+
return item.type === 'product';
|
|
431
|
+
})));
|
|
612
432
|
|
|
613
433
|
// 勾选时覆盖手动折扣
|
|
614
|
-
if (options !== null && options !== void 0 && options.discountId) {
|
|
615
|
-
var
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
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
|
-
}
|
|
434
|
+
if (options !== null && options !== void 0 && options.discountId && (_product$discount_lis10 = product.discount_list) !== null && _product$discount_lis10 !== void 0 && _product$discount_lis10.some(function (item) {
|
|
435
|
+
var _item$discount;
|
|
436
|
+
return ((_item$discount = item.discount) === null || _item$discount === void 0 ? void 0 : _item$discount.resource_id) === options.discountId;
|
|
437
|
+
})) {
|
|
438
|
+
isManualDiscount = false;
|
|
636
439
|
}
|
|
637
|
-
if (options !== null && options !== void 0 && options.selectedList) {
|
|
638
|
-
var
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
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
|
-
}
|
|
440
|
+
if (options !== null && options !== void 0 && options.selectedList && (_product$discount_lis11 = product.discount_list) !== null && _product$discount_lis11 !== void 0 && _product$discount_lis11.some(function (item) {
|
|
441
|
+
var _options$selectedList;
|
|
442
|
+
return options === null || options === void 0 || (_options$selectedList = options.selectedList) === null || _options$selectedList === void 0 ? void 0 : _options$selectedList.some(function (n) {
|
|
443
|
+
var _item$discount2;
|
|
444
|
+
return n.discountId === ((_item$discount2 = item.discount) === null || _item$discount2 === void 0 ? void 0 : _item$discount2.resource_id);
|
|
445
|
+
});
|
|
446
|
+
})) {
|
|
447
|
+
isManualDiscount = false;
|
|
668
448
|
}
|
|
669
449
|
|
|
670
450
|
// 如果没有适用的优惠券,或者手动折扣,则不适用优惠券
|
|
451
|
+
// 自定义商品:如果未开启适用折扣卡(product.vouchersApplicable),则不适用优惠券
|
|
671
452
|
if (applicableDiscounts.length === 0 || isManualDiscount || isBoolean(product.vouchersApplicable) && !product.vouchersApplicable) {
|
|
672
|
-
if (
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
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
|
-
}
|
|
453
|
+
if (product.isClient) {
|
|
454
|
+
processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
|
|
455
|
+
origin_total: getProductOriginTotalPrice({
|
|
456
|
+
product: {
|
|
457
|
+
original_price: product.original_price
|
|
458
|
+
},
|
|
459
|
+
bundle: product.bundle,
|
|
460
|
+
options: product.options
|
|
461
|
+
}),
|
|
462
|
+
variant: originProduct._productInit.variant,
|
|
463
|
+
original_price: originProduct._productInit.original_price,
|
|
464
|
+
total: getProductTotalPrice({
|
|
465
|
+
product: {
|
|
466
|
+
price: product.price
|
|
467
|
+
},
|
|
468
|
+
bundle: product.bundle,
|
|
469
|
+
options: product.options
|
|
470
|
+
}),
|
|
471
|
+
price: product.price
|
|
472
|
+
}), {}, {
|
|
473
|
+
discount_list: []
|
|
474
|
+
}))]);
|
|
709
475
|
} else {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
price:
|
|
714
|
-
|
|
715
|
-
|
|
476
|
+
processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
|
|
477
|
+
_id: product._id.split('___')[0] + '___' + index,
|
|
478
|
+
total: product.origin_total || product.total,
|
|
479
|
+
price: product.price
|
|
480
|
+
}), {}, {
|
|
481
|
+
discount_list: []
|
|
482
|
+
}))]);
|
|
716
483
|
}
|
|
717
484
|
return;
|
|
718
485
|
}
|
|
@@ -720,608 +487,137 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
720
487
|
return;
|
|
721
488
|
}
|
|
722
489
|
|
|
723
|
-
//
|
|
490
|
+
// 是否需要拆分
|
|
724
491
|
var isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === 'good_pass';
|
|
725
492
|
|
|
726
493
|
// 需要拆分出来的数量
|
|
727
|
-
var
|
|
728
|
-
var availableGoodPassCount = applicableDiscounts.filter(function (item) {
|
|
494
|
+
var splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, applicableDiscounts.filter(function (item) {
|
|
729
495
|
return (item.tag || item.type) === 'good_pass';
|
|
730
|
-
}).length;
|
|
731
|
-
var splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
|
|
496
|
+
}).length) : 1;
|
|
732
497
|
var arr = [];
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
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();
|
|
498
|
+
if (splitCount < product.quantity && isNeedSplit) {
|
|
499
|
+
arr.push(_this3.hooks.setProduct(originProduct, {
|
|
500
|
+
discount_list: [],
|
|
501
|
+
quantity: product.quantity - splitCount,
|
|
502
|
+
_id: product._id.split('___')[0]
|
|
503
|
+
}));
|
|
504
|
+
}
|
|
505
|
+
for (var i = 0; i < splitCount; i++) {
|
|
506
|
+
var _product$discount_lis12, _originProduct$_produ, _selectedDiscount$met;
|
|
507
|
+
// 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
|
|
508
|
+
var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
|
|
509
|
+
// 标记优惠券为已使用
|
|
510
|
+
usedDiscounts.set(_selectedDiscount.id, true);
|
|
511
|
+
|
|
512
|
+
// 记录实际应用了优惠券的商品信息
|
|
513
|
+
var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
|
|
514
|
+
|
|
515
|
+
// 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
|
|
516
|
+
var productOriginTotal = product.origin_total || product.total || 0;
|
|
517
|
+
// 如果当前 product 有 discount_list,则先从 origin_total 拿
|
|
518
|
+
if ((_product$discount_lis12 = product.discount_list) !== null && _product$discount_lis12 !== void 0 && _product$discount_lis12.length && product.origin_total) {
|
|
519
|
+
productOriginTotal = product.origin_total;
|
|
520
|
+
}
|
|
521
|
+
// 如果originProduct?._productInit?.original_price为 0,product.origin_total可能为空,此时取 product.total
|
|
522
|
+
if (Number((originProduct === null || originProduct === void 0 || (_originProduct$_produ = originProduct._productInit) === null || _originProduct$_produ === void 0 ? void 0 : _originProduct$_produ.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
|
|
523
|
+
productOriginTotal = product.total;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// 计算使用折扣卡/商品券以后,单个商品的总 total
|
|
527
|
+
var targetProductTotal = getDiscountAmount(_selectedDiscount, productOriginTotal, product.price);
|
|
528
|
+
var discountType = _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount.tag;
|
|
529
|
+
var discountDetail = {
|
|
530
|
+
amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
|
|
531
|
+
type: discountType,
|
|
532
|
+
discount: {
|
|
533
|
+
discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
|
|
534
|
+
fixed_amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
|
|
535
|
+
resource_id: _selectedDiscount.id,
|
|
536
|
+
title: _selectedDiscount.format_title,
|
|
537
|
+
original_amount: productOriginTotal,
|
|
538
|
+
product_id: originProduct.id,
|
|
539
|
+
percent: _selectedDiscount.par_value
|
|
797
540
|
}
|
|
541
|
+
};
|
|
798
542
|
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
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
|
-
}
|
|
543
|
+
// 如果 discount.tag 或者 discount.type 是 good_pass,则不需要添加 num 属性
|
|
544
|
+
if ((_selectedDiscount.tag || _selectedDiscount.type) !== 'good_pass') {
|
|
545
|
+
discountDetail.num = product.num || 1;
|
|
829
546
|
}
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
// 🔥 bundle子商品:支持拆分
|
|
833
|
-
var processedItems = [];
|
|
834
|
-
if (isNeedSplit) {
|
|
835
|
-
// 商品券:需要拆分数量
|
|
836
|
-
var discountNum = splitCount;
|
|
837
|
-
var normalNum = totalQuantity - discountNum;
|
|
838
|
-
|
|
839
|
-
// 生成有折扣的商品(每张商品券对应 num: 1)
|
|
840
|
-
for (var _i = 0; _i < discountNum; _i++) {
|
|
841
|
-
var _selectedDiscount2 = applicableDiscounts[_i];
|
|
842
|
-
usedDiscounts.set(_selectedDiscount2.id, true);
|
|
547
|
+
appliedProducts.push(discountDetail);
|
|
548
|
+
appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
|
|
843
549
|
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
product_id: product.id
|
|
855
|
-
},
|
|
856
|
-
metadata: {
|
|
857
|
-
// 🔥 使用拆分后的唯一 _id
|
|
858
|
-
custom_product_bundle_map_id: uniqueId,
|
|
859
|
-
num: 1
|
|
550
|
+
// 记录应用了优惠券的商品
|
|
551
|
+
// 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
|
|
552
|
+
if (product.isClient) {
|
|
553
|
+
arr.push(_this3.hooks.setProduct(originProduct, {
|
|
554
|
+
discount_list: [discountDetail],
|
|
555
|
+
price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
|
|
556
|
+
quantity: isNeedSplit ? 1 : product.quantity,
|
|
557
|
+
origin_total: getProductOriginTotalPrice({
|
|
558
|
+
product: {
|
|
559
|
+
original_price: product.original_price
|
|
860
560
|
},
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
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
|
-
}
|
|
561
|
+
bundle: product.bundle,
|
|
562
|
+
options: product.options
|
|
563
|
+
}),
|
|
564
|
+
variant: originProduct._productInit.variant,
|
|
565
|
+
original_price: new Decimal(product.price || 0).toNumber(),
|
|
566
|
+
total: targetProductTotal
|
|
567
|
+
}));
|
|
893
568
|
} else {
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
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
|
|
569
|
+
arr.push(_this3.hooks.setProduct(originProduct, {
|
|
570
|
+
discount_list: [discountDetail],
|
|
571
|
+
_id: product._id.split('___')[0] + "___" + _selectedDiscount.id + "___" + index,
|
|
572
|
+
price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
|
|
573
|
+
quantity: isNeedSplit ? 1 : product.quantity,
|
|
574
|
+
total: targetProductTotal,
|
|
575
|
+
origin_total: productOriginTotal
|
|
932
576
|
}));
|
|
933
577
|
}
|
|
934
|
-
processedFlatItemsMap.set(flatItem._id, processedItems);
|
|
935
578
|
}
|
|
579
|
+
console.log(arr, 'arrarrarr');
|
|
580
|
+
processedProductsMap.set(product._id, arr);
|
|
936
581
|
});
|
|
937
582
|
|
|
938
|
-
//
|
|
939
|
-
var
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
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,
|
|
583
|
+
// 按原始顺序构建处理后的商品列表
|
|
584
|
+
var processedProductList = [];
|
|
585
|
+
productList.forEach(function (originProduct) {
|
|
586
|
+
var product = _this3.hooks.getProduct(originProduct);
|
|
587
|
+
var getDefaultProduct = function getDefaultProduct() {
|
|
588
|
+
if (product.isClient) {
|
|
589
|
+
return _this3.hooks.setProduct(originProduct, {
|
|
590
|
+
discount_list: [],
|
|
591
|
+
price: product.price,
|
|
592
|
+
origin_total: getProductOriginTotalPrice({
|
|
593
|
+
product: {
|
|
594
|
+
original_price: product.original_price
|
|
595
|
+
},
|
|
596
|
+
bundle: product.bundle,
|
|
597
|
+
options: product.options
|
|
598
|
+
}),
|
|
599
|
+
variant: originProduct._productInit.variant,
|
|
600
|
+
original_price: originProduct._productInit.original_price,
|
|
601
|
+
total: getProductTotalPrice({
|
|
602
|
+
product: {
|
|
975
603
|
price: product.price
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
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));
|
|
604
|
+
},
|
|
605
|
+
bundle: product.bundle,
|
|
606
|
+
options: product.options
|
|
607
|
+
})
|
|
608
|
+
});
|
|
988
609
|
} else {
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
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
|
-
}
|
|
610
|
+
return _this3.hooks.setProduct(originProduct, {
|
|
611
|
+
discount_list: [],
|
|
612
|
+
total: product.total,
|
|
613
|
+
origin_total: product.origin_total,
|
|
614
|
+
price: product.price
|
|
615
|
+
});
|
|
1318
616
|
}
|
|
1319
|
-
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
// 按原始顺序构建处理后的商品列表
|
|
1324
|
-
var processedProductList = reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, productList);
|
|
617
|
+
};
|
|
618
|
+
var arr = processedProductsMap.get(product._id);
|
|
619
|
+
arr !== null && arr !== void 0 && arr.length ? processedProductList.push.apply(processedProductList, _toConsumableArray(arr)) : processedProductList.push(getDefaultProduct());
|
|
620
|
+
});
|
|
1325
621
|
|
|
1326
622
|
// 按原始顺序更新优惠券列表,标记已使用和可用的优惠券
|
|
1327
623
|
var updatedDiscountList = addModeDiscount.map(function (discount) {
|
|
@@ -1392,171 +688,6 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
|
|
|
1392
688
|
discountList: [].concat(editModeDiscount, _toConsumableArray(updatedDiscountList))
|
|
1393
689
|
};
|
|
1394
690
|
}
|
|
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
|
-
}
|
|
1560
691
|
}, {
|
|
1561
692
|
key: "destroy",
|
|
1562
693
|
value: function () {
|