@pisell/pisellos 2.2.178 → 2.2.179
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/model/strategy/adapter/promotion/index.js +9 -0
- package/dist/modules/Customer/index.d.ts +0 -6
- package/dist/modules/Customer/index.js +83 -129
- package/dist/modules/Customer/types.d.ts +0 -2
- package/dist/modules/Discount/index.d.ts +4 -2
- package/dist/modules/Discount/index.js +93 -8
- package/dist/modules/Discount/types.d.ts +7 -1
- package/dist/modules/Order/index.d.ts +18 -88
- package/dist/modules/Order/index.js +501 -1357
- package/dist/modules/Order/types.d.ts +19 -198
- package/dist/modules/Order/types.js +0 -31
- package/dist/modules/Order/utils.d.ts +1 -50
- package/dist/modules/Order/utils.js +32 -261
- package/dist/modules/Quotation/index.d.ts +1 -0
- package/dist/modules/Quotation/index.js +21 -23
- package/dist/modules/Rules/index.d.ts +0 -4
- package/dist/modules/Rules/index.js +10 -26
- package/dist/modules/SalesSummary/index.js +2 -4
- package/dist/modules/SalesSummary/utils.js +7 -21
- package/dist/modules/Schedule/index.js +2 -6
- package/dist/modules/index.d.ts +0 -1
- package/dist/modules/index.js +1 -2
- package/dist/server/index.js +29 -87
- package/dist/server/modules/order/index.d.ts +0 -23
- package/dist/server/modules/order/index.js +46 -123
- package/dist/server/modules/order/types.d.ts +2 -0
- package/dist/server/modules/order/utils/filterOrders.js +6 -20
- package/dist/server/modules/products/index.d.ts +1 -1
- package/dist/server/modules/products/index.js +113 -130
- package/dist/server/modules/schedule/index.js +6 -13
- package/dist/solution/BaseSales/index.d.ts +7 -89
- package/dist/solution/BaseSales/index.js +375 -1494
- package/dist/solution/BaseSales/types.d.ts +1 -67
- package/dist/solution/BaseSales/types.js +1 -3
- package/dist/solution/BaseSales/utils/parseSalesResponse.d.ts +1 -0
- package/dist/solution/BaseSales/utils/parseSalesResponse.js +1 -1
- package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +16 -68
- package/dist/solution/BaseSales/utils.js +8 -46
- package/dist/solution/BookingByStep/index.d.ts +1 -1
- package/dist/solution/BookingTicket/index.d.ts +1 -133
- package/dist/solution/BookingTicket/index.js +175 -780
- package/dist/solution/BookingTicket/types.d.ts +0 -2
- package/dist/solution/BookingTicket/types.js +0 -3
- package/dist/solution/BookingTicket/utils/cartView.js +2 -4
- package/dist/solution/ScanOrder/index.d.ts +3 -9
- package/dist/solution/ScanOrder/index.js +128 -231
- package/dist/solution/ScanOrder/utils.js +8 -46
- package/dist/solution/ShopDiscount/index.d.ts +1 -0
- package/dist/solution/ShopDiscount/index.js +23 -18
- package/dist/solution/VenueBooking/index.d.ts +9 -5
- package/dist/solution/VenueBooking/index.js +55 -103
- package/dist/solution/VenueBooking/utils/slotMerge.d.ts +0 -4
- package/dist/solution/VenueBooking/utils/slotMerge.js +0 -10
- package/lib/modules/Customer/index.d.ts +0 -6
- package/lib/modules/Customer/index.js +11 -26
- package/lib/modules/Customer/types.d.ts +0 -2
- package/lib/modules/Discount/index.d.ts +4 -2
- package/lib/modules/Discount/index.js +63 -8
- package/lib/modules/Discount/types.d.ts +7 -1
- package/lib/modules/Order/index.d.ts +18 -88
- package/lib/modules/Order/index.js +149 -677
- package/lib/modules/Order/types.d.ts +19 -198
- package/lib/modules/Order/types.js +0 -1
- package/lib/modules/Order/utils.d.ts +1 -50
- package/lib/modules/Order/utils.js +22 -229
- package/lib/modules/Quotation/index.d.ts +1 -0
- package/lib/modules/Quotation/index.js +15 -18
- package/lib/modules/Rules/index.d.ts +0 -4
- package/lib/modules/Rules/index.js +14 -22
- package/lib/modules/SalesSummary/index.js +2 -4
- package/lib/modules/SalesSummary/utils.js +7 -21
- package/lib/modules/Schedule/index.js +1 -3
- package/lib/modules/index.d.ts +0 -1
- package/lib/modules/index.js +1 -3
- package/lib/server/index.js +4 -41
- package/lib/server/modules/order/index.d.ts +0 -23
- package/lib/server/modules/order/index.js +14 -46
- package/lib/server/modules/order/types.d.ts +2 -0
- package/lib/server/modules/order/utils/filterOrders.js +4 -12
- package/lib/server/modules/products/index.d.ts +1 -1
- package/lib/server/modules/products/index.js +20 -30
- package/lib/server/modules/schedule/index.js +1 -2
- package/lib/solution/BaseSales/index.d.ts +7 -89
- package/lib/solution/BaseSales/index.js +22 -736
- package/lib/solution/BaseSales/types.d.ts +1 -67
- package/lib/solution/BaseSales/types.js +1 -3
- package/lib/solution/BaseSales/utils/parseSalesResponse.d.ts +1 -0
- package/lib/solution/BaseSales/utils/parseSalesResponse.js +1 -0
- package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +12 -71
- package/lib/solution/BaseSales/utils.js +6 -46
- package/lib/solution/BookingByStep/index.d.ts +1 -1
- package/lib/solution/BookingTicket/index.d.ts +1 -133
- package/lib/solution/BookingTicket/index.js +8 -352
- package/lib/solution/BookingTicket/types.d.ts +0 -2
- package/lib/solution/BookingTicket/types.js +0 -6
- package/lib/solution/BookingTicket/utils/cartView.js +2 -4
- package/lib/solution/ScanOrder/index.d.ts +3 -9
- package/lib/solution/ScanOrder/index.js +66 -147
- package/lib/solution/ScanOrder/utils.js +6 -46
- package/lib/solution/ShopDiscount/index.d.ts +1 -0
- package/lib/solution/ShopDiscount/index.js +4 -2
- package/lib/solution/VenueBooking/index.d.ts +9 -5
- package/lib/solution/VenueBooking/index.js +14 -50
- package/lib/solution/VenueBooking/utils/slotMerge.d.ts +0 -4
- package/lib/solution/VenueBooking/utils/slotMerge.js +0 -10
- package/package.json +2 -2
- package/dist/modules/BookingContext/index.d.ts +0 -37
- package/dist/modules/BookingContext/index.js +0 -436
- package/dist/modules/BookingContext/types.d.ts +0 -102
- package/dist/modules/BookingContext/types.js +0 -51
- package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +0 -16
- package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +0 -193
- package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +0 -8
- package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +0 -137
- package/dist/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +0 -75
- package/dist/modules/BookingContext/utils/cacheItemToBookingInput.js +0 -215
- package/dist/modules/BookingContext/utils/flexible.d.ts +0 -28
- package/dist/modules/BookingContext/utils/flexible.js +0 -56
- package/dist/modules/BookingContext/utils/formatResourceList.d.ts +0 -8
- package/dist/modules/BookingContext/utils/formatResourceList.js +0 -38
- package/dist/modules/BookingContext/utils/index.d.ts +0 -11
- package/dist/modules/BookingContext/utils/index.js +0 -11
- package/dist/modules/BookingContext/utils/noResource.d.ts +0 -13
- package/dist/modules/BookingContext/utils/noResource.js +0 -77
- package/dist/modules/BookingContext/utils/productExtend.d.ts +0 -72
- package/dist/modules/BookingContext/utils/productExtend.js +0 -339
- package/dist/modules/BookingContext/utils/resourceErrors.d.ts +0 -41
- package/dist/modules/BookingContext/utils/resourceErrors.js +0 -74
- package/dist/modules/BookingContext/utils/resourceSelection.d.ts +0 -7
- package/dist/modules/BookingContext/utils/resourceSelection.js +0 -24
- package/dist/modules/BookingContext/utils/resources.d.ts +0 -80
- package/dist/modules/BookingContext/utils/resources.js +0 -486
- package/dist/modules/BookingContext/utils/serviceTimes.d.ts +0 -55
- package/dist/modules/BookingContext/utils/serviceTimes.js +0 -151
- package/dist/modules/BookingContext/utils/timeSlices.d.ts +0 -42
- package/dist/modules/BookingContext/utils/timeSlices.js +0 -100
- package/dist/modules/Order/utils/manualProductDiscount.d.ts +0 -106
- package/dist/modules/Order/utils/manualProductDiscount.js +0 -212
- package/dist/solution/BaseSales/utils/cartPromotion.d.ts +0 -275
- package/dist/solution/BaseSales/utils/cartPromotion.js +0 -1258
- package/dist/solution/BaseSales/utils/quotationPrice.d.ts +0 -7
- package/dist/solution/BaseSales/utils/quotationPrice.js +0 -237
- package/dist/solution/BookingTicket/utils/addProductDecision.d.ts +0 -113
- package/dist/solution/BookingTicket/utils/addProductDecision.js +0 -346
- package/lib/modules/BookingContext/index.d.ts +0 -37
- package/lib/modules/BookingContext/index.js +0 -297
- package/lib/modules/BookingContext/types.d.ts +0 -102
- package/lib/modules/BookingContext/types.js +0 -34
- package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +0 -16
- package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +0 -207
- package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +0 -8
- package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +0 -141
- package/lib/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +0 -75
- package/lib/modules/BookingContext/utils/cacheItemToBookingInput.js +0 -198
- package/lib/modules/BookingContext/utils/flexible.d.ts +0 -28
- package/lib/modules/BookingContext/utils/flexible.js +0 -80
- package/lib/modules/BookingContext/utils/formatResourceList.d.ts +0 -8
- package/lib/modules/BookingContext/utils/formatResourceList.js +0 -50
- package/lib/modules/BookingContext/utils/index.d.ts +0 -11
- package/lib/modules/BookingContext/utils/index.js +0 -43
- package/lib/modules/BookingContext/utils/noResource.d.ts +0 -13
- package/lib/modules/BookingContext/utils/noResource.js +0 -90
- package/lib/modules/BookingContext/utils/productExtend.d.ts +0 -72
- package/lib/modules/BookingContext/utils/productExtend.js +0 -283
- package/lib/modules/BookingContext/utils/resourceErrors.d.ts +0 -41
- package/lib/modules/BookingContext/utils/resourceErrors.js +0 -80
- package/lib/modules/BookingContext/utils/resourceSelection.d.ts +0 -7
- package/lib/modules/BookingContext/utils/resourceSelection.js +0 -46
- package/lib/modules/BookingContext/utils/resources.d.ts +0 -80
- package/lib/modules/BookingContext/utils/resources.js +0 -377
- package/lib/modules/BookingContext/utils/serviceTimes.d.ts +0 -55
- package/lib/modules/BookingContext/utils/serviceTimes.js +0 -162
- package/lib/modules/BookingContext/utils/timeSlices.d.ts +0 -42
- package/lib/modules/BookingContext/utils/timeSlices.js +0 -124
- package/lib/modules/Order/utils/manualProductDiscount.d.ts +0 -106
- package/lib/modules/Order/utils/manualProductDiscount.js +0 -207
- package/lib/solution/BaseSales/utils/cartPromotion.d.ts +0 -275
- package/lib/solution/BaseSales/utils/cartPromotion.js +0 -836
- package/lib/solution/BaseSales/utils/quotationPrice.d.ts +0 -7
- package/lib/solution/BaseSales/utils/quotationPrice.js +0 -277
- package/lib/solution/BookingTicket/utils/addProductDecision.d.ts +0 -113
- package/lib/solution/BookingTicket/utils/addProductDecision.js +0 -318
|
@@ -1,836 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
|
|
29
|
-
// src/solution/BaseSales/utils/cartPromotion.ts
|
|
30
|
-
var cartPromotion_exports = {};
|
|
31
|
-
__export(cartPromotion_exports, {
|
|
32
|
-
appendPromotionTags: () => appendPromotionTags,
|
|
33
|
-
calculateUnfulfilledPromotions: () => calculateUnfulfilledPromotions,
|
|
34
|
-
extractProductMatchIdentifiersFromStrategyConfig: () => extractProductMatchIdentifiersFromStrategyConfig,
|
|
35
|
-
getOrderProductBasePrice: () => getOrderProductBasePrice,
|
|
36
|
-
getOrderProductOriginalPriceForPromotion: () => getOrderProductOriginalPriceForPromotion,
|
|
37
|
-
getOrderProductUid: () => getOrderProductUid,
|
|
38
|
-
mergeIdenticalPromotionCartLines: () => mergeIdenticalPromotionCartLines,
|
|
39
|
-
processCartPromotion: () => processCartPromotion,
|
|
40
|
-
resolveStrategyEligibleProducts: () => resolveStrategyEligibleProducts,
|
|
41
|
-
resolveStrategyRequiredQuantity: () => resolveStrategyRequiredQuantity
|
|
42
|
-
});
|
|
43
|
-
module.exports = __toCommonJS(cartPromotion_exports);
|
|
44
|
-
var import_decimal = __toESM(require("decimal.js"));
|
|
45
|
-
var import_lodash_es = require("lodash-es");
|
|
46
|
-
var import_utils = require("../../../modules/Order/utils");
|
|
47
|
-
var import_utils2 = require("../../ScanOrder/utils");
|
|
48
|
-
function getOrderProductBasePrice(product) {
|
|
49
|
-
const metadata = (product == null ? void 0 : product.metadata) || {};
|
|
50
|
-
const optionSum = (0, import_utils.sumOptionUnitPrice)(product == null ? void 0 : product.product_option_item).toNumber();
|
|
51
|
-
if (metadata.source_product_price !== void 0) {
|
|
52
|
-
const v2 = Number(metadata.source_product_price);
|
|
53
|
-
return Number.isFinite(v2) ? v2 : 0;
|
|
54
|
-
}
|
|
55
|
-
if (metadata.main_product_original_price !== void 0) {
|
|
56
|
-
const v2 = Number(metadata.main_product_original_price) - optionSum;
|
|
57
|
-
return Number.isFinite(v2) ? v2 : 0;
|
|
58
|
-
}
|
|
59
|
-
const v = Number((product == null ? void 0 : product.original_price) ?? (product == null ? void 0 : product.selling_price) ?? 0);
|
|
60
|
-
return Number.isFinite(v) ? v : 0;
|
|
61
|
-
}
|
|
62
|
-
function getOrderProductOriginalPriceForPromotion(product) {
|
|
63
|
-
var _a;
|
|
64
|
-
const metadata = (product == null ? void 0 : product.metadata) || {};
|
|
65
|
-
const optionSum = (0, import_utils.sumOptionUnitPrice)(product == null ? void 0 : product.product_option_item).toNumber();
|
|
66
|
-
if (metadata.main_product_original_price !== void 0 && metadata.main_product_original_price !== null && metadata.main_product_original_price !== "") {
|
|
67
|
-
const v = Number(metadata.main_product_original_price) - optionSum;
|
|
68
|
-
if (Number.isFinite(v) && v >= 0)
|
|
69
|
-
return v;
|
|
70
|
-
}
|
|
71
|
-
const promoOriginal = (_a = metadata._promotion) == null ? void 0 : _a.originalPrice;
|
|
72
|
-
if (promoOriginal !== void 0 && promoOriginal !== null && promoOriginal !== "") {
|
|
73
|
-
const v = Number(promoOriginal);
|
|
74
|
-
if (Number.isFinite(v) && v >= 0)
|
|
75
|
-
return v;
|
|
76
|
-
}
|
|
77
|
-
return getOrderProductBasePrice(product);
|
|
78
|
-
}
|
|
79
|
-
function updatePromotionDiscountList(item, promotionDiscount) {
|
|
80
|
-
let discountList = Array.isArray(item.discount_list) ? [...item.discount_list] : [];
|
|
81
|
-
discountList = discountList.filter((d) => (d == null ? void 0 : d.type) !== "promotion");
|
|
82
|
-
if (promotionDiscount) {
|
|
83
|
-
discountList.push({
|
|
84
|
-
type: "promotion",
|
|
85
|
-
amount: promotionDiscount.amount,
|
|
86
|
-
discount: {
|
|
87
|
-
original_amount: promotionDiscount.original_amount,
|
|
88
|
-
fixed_amount: promotionDiscount.fixed_amount,
|
|
89
|
-
title: promotionDiscount.title,
|
|
90
|
-
resource_id: ""
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
if (discountList.length > 0) {
|
|
95
|
-
item.discount_list = discountList;
|
|
96
|
-
} else {
|
|
97
|
-
delete item.discount_list;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
var X_ITEMS_FOR_Y_PRICE = "X_ITEMS_FOR_Y_PRICE";
|
|
101
|
-
function applyPromoUnitPriceToLine(item, input) {
|
|
102
|
-
const metadata = item.metadata || (item.metadata = {});
|
|
103
|
-
const optionSum = (0, import_utils.sumOptionUnitPrice)(item.product_option_item).toNumber();
|
|
104
|
-
const promoUnitPrice = new import_decimal.default(Number(input.promoUnitPrice) || 0).toDecimalPlaces(2).toFixed(2);
|
|
105
|
-
const mainSelling = new import_decimal.default(Number(promoUnitPrice) || 0).plus(optionSum).toDecimalPlaces(2).toFixed(2);
|
|
106
|
-
const catalogSource = new import_decimal.default(input.originalBasePrice).toDecimalPlaces(2).toFixed(2);
|
|
107
|
-
metadata.source_product_price = catalogSource;
|
|
108
|
-
metadata.main_product_original_price = new import_decimal.default(Number(catalogSource) || 0).plus(optionSum).toDecimalPlaces(2).toFixed(2);
|
|
109
|
-
metadata.main_product_selling_price = mainSelling;
|
|
110
|
-
metadata.price_schema_version = 2;
|
|
111
|
-
item.selling_price = (0, import_utils.composeLinePrice)({
|
|
112
|
-
mainPrice: mainSelling,
|
|
113
|
-
bundle: item.product_bundle
|
|
114
|
-
});
|
|
115
|
-
item.original_price = (0, import_utils.composeLinePrice)({
|
|
116
|
-
mainPrice: metadata.main_product_original_price,
|
|
117
|
-
bundle: item.product_bundle,
|
|
118
|
-
useOriginalBundle: true
|
|
119
|
-
});
|
|
120
|
-
const discountAmount = new import_decimal.default(input.originalBasePrice).minus(Number(promoUnitPrice) || 0).toDecimalPlaces(2).toFixed(2);
|
|
121
|
-
if (Number(catalogSource) !== Number(promoUnitPrice)) {
|
|
122
|
-
updatePromotionDiscountList(item, {
|
|
123
|
-
title: input.strategyTitle || "",
|
|
124
|
-
amount: discountAmount,
|
|
125
|
-
original_amount: catalogSource,
|
|
126
|
-
fixed_amount: discountAmount
|
|
127
|
-
});
|
|
128
|
-
} else {
|
|
129
|
-
updatePromotionDiscountList(item, null);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
function getPromotionActionType(promotion) {
|
|
133
|
-
var _a, _b;
|
|
134
|
-
if (promotion == null ? void 0 : promotion.actionType)
|
|
135
|
-
return String(promotion.actionType);
|
|
136
|
-
const actions = (_a = promotion == null ? void 0 : promotion.strategyMetadata) == null ? void 0 : _a.actions;
|
|
137
|
-
if (Array.isArray(actions) && ((_b = actions[0]) == null ? void 0 : _b.type))
|
|
138
|
-
return String(actions[0].type);
|
|
139
|
-
return void 0;
|
|
140
|
-
}
|
|
141
|
-
function resolveXItemsExpectedUnitPrice(promotion, sourceCartNum) {
|
|
142
|
-
if (getPromotionActionType(promotion) !== X_ITEMS_FOR_Y_PRICE)
|
|
143
|
-
return void 0;
|
|
144
|
-
const detail = promotion == null ? void 0 : promotion.actionDetail;
|
|
145
|
-
const x = Number(detail == null ? void 0 : detail.x);
|
|
146
|
-
const y = Number(detail == null ? void 0 : detail.price);
|
|
147
|
-
if (!Number.isFinite(x) || x <= 0 || !Number.isFinite(y))
|
|
148
|
-
return void 0;
|
|
149
|
-
if (sourceCartNum < x)
|
|
150
|
-
return void 0;
|
|
151
|
-
return Number(new import_decimal.default(y).div(x).toDecimalPlaces(2).toFixed(2));
|
|
152
|
-
}
|
|
153
|
-
function mergeIdenticalPromotionCartLines(products) {
|
|
154
|
-
var _a, _b, _c;
|
|
155
|
-
if (!Array.isArray(products) || products.length <= 1) {
|
|
156
|
-
return [...products || []];
|
|
157
|
-
}
|
|
158
|
-
const merged = [];
|
|
159
|
-
const indexByKey = /* @__PURE__ */ new Map();
|
|
160
|
-
for (const line of products) {
|
|
161
|
-
if (getGiftInfo(line)) {
|
|
162
|
-
merged.push(line);
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
const promotion = (_a = line == null ? void 0 : line.metadata) == null ? void 0 : _a._promotion;
|
|
166
|
-
if (!(promotion == null ? void 0 : promotion.inPromotion) || !promotion.strategyId) {
|
|
167
|
-
merged.push(line);
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
const fingerprint = (0, import_utils2.buildProductLineFingerprint)(
|
|
171
|
-
line.product_option_item,
|
|
172
|
-
line.product_bundle
|
|
173
|
-
);
|
|
174
|
-
const unitPrice = String(
|
|
175
|
-
((_b = line.metadata) == null ? void 0 : _b.source_product_price) ?? line.selling_price ?? ""
|
|
176
|
-
);
|
|
177
|
-
const promoUnitPrice = String(
|
|
178
|
-
promotion.finalPrice ?? ((_c = line.metadata) == null ? void 0 : _c.main_product_selling_price) ?? ""
|
|
179
|
-
);
|
|
180
|
-
const mergeKey = [
|
|
181
|
-
line.product_id,
|
|
182
|
-
line.product_variant_id ?? 0,
|
|
183
|
-
fingerprint,
|
|
184
|
-
promotion.strategyId,
|
|
185
|
-
unitPrice,
|
|
186
|
-
promoUnitPrice
|
|
187
|
-
].join("#");
|
|
188
|
-
const hitIndex = indexByKey.get(mergeKey);
|
|
189
|
-
if (hitIndex === void 0) {
|
|
190
|
-
indexByKey.set(mergeKey, merged.length);
|
|
191
|
-
merged.push(line);
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
const target = merged[hitIndex];
|
|
195
|
-
target.num = (0, import_utils2.getSafeProductNum)(Number(target.num || 0) + Number(line.num || 0));
|
|
196
|
-
}
|
|
197
|
-
return merged;
|
|
198
|
-
}
|
|
199
|
-
function dedupePromotionProcessedLines(lines) {
|
|
200
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
201
|
-
if (!Array.isArray(lines) || lines.length <= 1)
|
|
202
|
-
return [...lines || []];
|
|
203
|
-
const passthrough = [];
|
|
204
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
205
|
-
for (const line of lines) {
|
|
206
|
-
const promo = (_a = line == null ? void 0 : line.metadata) == null ? void 0 : _a._promotion;
|
|
207
|
-
const originalUid = String(line._originalProductUid || getOrderProductUid(line) || "");
|
|
208
|
-
if (!(promo == null ? void 0 : promo.inPromotion) || !originalUid) {
|
|
209
|
-
passthrough.push(line);
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
const group = buckets.get(originalUid) || [];
|
|
213
|
-
group.push(line);
|
|
214
|
-
buckets.set(originalUid, group);
|
|
215
|
-
}
|
|
216
|
-
const promotionDelta = (line) => {
|
|
217
|
-
var _a2;
|
|
218
|
-
const promo = (_a2 = line.metadata) == null ? void 0 : _a2._promotion;
|
|
219
|
-
const original = Number(promo == null ? void 0 : promo.originalPrice);
|
|
220
|
-
const final = Number(promo == null ? void 0 : promo.finalPrice);
|
|
221
|
-
if (!Number.isFinite(original) || !Number.isFinite(final))
|
|
222
|
-
return 0;
|
|
223
|
-
return Math.abs(original - final);
|
|
224
|
-
};
|
|
225
|
-
const deduped = [...passthrough];
|
|
226
|
-
for (const [originalUid, group] of buckets) {
|
|
227
|
-
if (group.length === 1) {
|
|
228
|
-
deduped.push(group[0]);
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
const sourceCartNum = Math.max(
|
|
232
|
-
...group.map((line) => Number(line._sourceCartNum || line.num || 0))
|
|
233
|
-
);
|
|
234
|
-
const pricingLines = group.filter((line) => promotionDelta(line) > 0);
|
|
235
|
-
const xItemsLines = (pricingLines.length ? pricingLines : group).filter(
|
|
236
|
-
(line) => {
|
|
237
|
-
var _a2;
|
|
238
|
-
return getPromotionActionType((_a2 = line.metadata) == null ? void 0 : _a2._promotion) === X_ITEMS_FOR_Y_PRICE;
|
|
239
|
-
}
|
|
240
|
-
);
|
|
241
|
-
const pool = xItemsLines.length ? xItemsLines : pricingLines.length ? pricingLines : group;
|
|
242
|
-
let winner = pool[0];
|
|
243
|
-
const expectedUnit = resolveXItemsExpectedUnitPrice(
|
|
244
|
-
(_b = winner.metadata) == null ? void 0 : _b._promotion,
|
|
245
|
-
sourceCartNum
|
|
246
|
-
);
|
|
247
|
-
if (expectedUnit != null && pool.length > 1) {
|
|
248
|
-
winner = pool.reduce((best, line) => {
|
|
249
|
-
var _a2, _b2, _c2, _d2;
|
|
250
|
-
const bestDiff = Math.abs(Number((_b2 = (_a2 = best.metadata) == null ? void 0 : _a2._promotion) == null ? void 0 : _b2.finalPrice) - expectedUnit);
|
|
251
|
-
const lineDiff = Math.abs(Number((_d2 = (_c2 = line.metadata) == null ? void 0 : _c2._promotion) == null ? void 0 : _d2.finalPrice) - expectedUnit);
|
|
252
|
-
return lineDiff < bestDiff ? line : best;
|
|
253
|
-
}, pool[0]);
|
|
254
|
-
} else if (pool.length > 1 && !expectedUnit) {
|
|
255
|
-
winner = pool.reduce((best, line) => {
|
|
256
|
-
var _a2, _b2, _c2, _d2, _e2, _f2;
|
|
257
|
-
const bestFinal = Number((_b2 = (_a2 = best.metadata) == null ? void 0 : _a2._promotion) == null ? void 0 : _b2.finalPrice);
|
|
258
|
-
const lineFinal = Number((_d2 = (_c2 = line.metadata) == null ? void 0 : _c2._promotion) == null ? void 0 : _d2.finalPrice);
|
|
259
|
-
const original = Number((_f2 = (_e2 = line.metadata) == null ? void 0 : _e2._promotion) == null ? void 0 : _f2.originalPrice);
|
|
260
|
-
if (Number.isFinite(original) && lineFinal >= original && bestFinal >= original) {
|
|
261
|
-
return lineFinal < bestFinal ? line : best;
|
|
262
|
-
}
|
|
263
|
-
return promotionDelta(line) > promotionDelta(best) ? line : best;
|
|
264
|
-
}, pool[0]);
|
|
265
|
-
}
|
|
266
|
-
winner.num = (0, import_utils2.getSafeProductNum)(sourceCartNum || winner.num || 1);
|
|
267
|
-
const recomputeUnit = resolveXItemsExpectedUnitPrice(
|
|
268
|
-
(_c = winner.metadata) == null ? void 0 : _c._promotion,
|
|
269
|
-
sourceCartNum
|
|
270
|
-
);
|
|
271
|
-
if (recomputeUnit != null) {
|
|
272
|
-
const promo = winner.metadata._promotion;
|
|
273
|
-
applyPromoUnitPriceToLine(winner, {
|
|
274
|
-
promoUnitPrice: recomputeUnit,
|
|
275
|
-
originalBasePrice: Number(promo.originalPrice),
|
|
276
|
-
strategyTitle: (_d = promo.strategyMetadata) == null ? void 0 : _d.name
|
|
277
|
-
});
|
|
278
|
-
promo.finalPrice = recomputeUnit;
|
|
279
|
-
}
|
|
280
|
-
for (const line of group) {
|
|
281
|
-
const giftInfo = (_f = (_e = line.metadata) == null ? void 0 : _e._promotion) == null ? void 0 : _f.giftInfo;
|
|
282
|
-
if (giftInfo && ((_g = winner.metadata) == null ? void 0 : _g._promotion) && !winner.metadata._promotion.giftInfo) {
|
|
283
|
-
winner.metadata._promotion.giftInfo = giftInfo;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
deduped.push(winner);
|
|
287
|
-
}
|
|
288
|
-
return deduped;
|
|
289
|
-
}
|
|
290
|
-
function getOrderProductUid(product) {
|
|
291
|
-
var _a;
|
|
292
|
-
return String(
|
|
293
|
-
((_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a.unique_identification_number) ?? (product == null ? void 0 : product.unique_identification_number) ?? ""
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
function getGiftInfo(product) {
|
|
297
|
-
var _a;
|
|
298
|
-
return ((_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a._giftInfo) || (product == null ? void 0 : product._giftInfo) || null;
|
|
299
|
-
}
|
|
300
|
-
function getProductBundleForEvaluator(product) {
|
|
301
|
-
const bundles = Array.isArray(product == null ? void 0 : product.product_bundle) ? product.product_bundle : [];
|
|
302
|
-
return bundles.map((b) => ({
|
|
303
|
-
product_id: (b == null ? void 0 : b.bundle_product_id) ?? (b == null ? void 0 : b._bundle_product_id) ?? (b == null ? void 0 : b.product_id),
|
|
304
|
-
product_variant_id: (b == null ? void 0 : b.bundle_variant_id) ?? (b == null ? void 0 : b.product_variant_id) ?? 0,
|
|
305
|
-
quantity: Number((b == null ? void 0 : b.num) ?? (b == null ? void 0 : b.quantity) ?? 1) || 1,
|
|
306
|
-
price: Number((b == null ? void 0 : b.bundle_selling_price) ?? (b == null ? void 0 : b.price) ?? 0) || 0,
|
|
307
|
-
original_price: Number((b == null ? void 0 : b.original_price) ?? (b == null ? void 0 : b.product_price) ?? (b == null ? void 0 : b.price) ?? 0) || 0,
|
|
308
|
-
id: (b == null ? void 0 : b.bundle_id) ?? (b == null ? void 0 : b.id) ?? 0,
|
|
309
|
-
_bundle_product_id: (b == null ? void 0 : b._bundle_product_id) ?? (b == null ? void 0 : b.bundle_product_id) ?? (b == null ? void 0 : b.product_id)
|
|
310
|
-
}));
|
|
311
|
-
}
|
|
312
|
-
function generateNumericId() {
|
|
313
|
-
return Math.floor(Math.random() * 1e9) + Date.now();
|
|
314
|
-
}
|
|
315
|
-
function convertToPromotionProduct(product, index) {
|
|
316
|
-
const metadata = (product == null ? void 0 : product.metadata) || {};
|
|
317
|
-
const numericId = generateNumericId();
|
|
318
|
-
return {
|
|
319
|
-
id: numericId,
|
|
320
|
-
product_id: Number((product == null ? void 0 : product.product_id) ?? 0) || 0,
|
|
321
|
-
product_variant_id: Number((product == null ? void 0 : product.product_variant_id) ?? 0) || 0,
|
|
322
|
-
name: metadata.product_name || (product == null ? void 0 : product.title) || "",
|
|
323
|
-
price: getOrderProductOriginalPriceForPromotion(product),
|
|
324
|
-
quantity: Number((product == null ? void 0 : product.num) ?? 1) || 1,
|
|
325
|
-
bundle: getProductBundleForEvaluator(product),
|
|
326
|
-
_originalItem: product,
|
|
327
|
-
_originalId: getOrderProductUid(product),
|
|
328
|
-
_originalIndex: index
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
function calculateGiftActions(gifts, existingGifts) {
|
|
332
|
-
var _a, _b, _c;
|
|
333
|
-
const toAdd = [];
|
|
334
|
-
const toReduce = [];
|
|
335
|
-
const toRemove = [];
|
|
336
|
-
const existingByStrategy = /* @__PURE__ */ new Map();
|
|
337
|
-
for (const gift of existingGifts) {
|
|
338
|
-
const strategyId = (_a = getGiftInfo(gift)) == null ? void 0 : _a.strategyId;
|
|
339
|
-
if (!strategyId)
|
|
340
|
-
continue;
|
|
341
|
-
const existing = existingByStrategy.get(strategyId);
|
|
342
|
-
const qty = Number((gift == null ? void 0 : gift.num) ?? 1) || 1;
|
|
343
|
-
if (existing) {
|
|
344
|
-
existing.items.push(gift);
|
|
345
|
-
existing.totalQuantity += qty;
|
|
346
|
-
} else {
|
|
347
|
-
existingByStrategy.set(strategyId, { items: [gift], totalQuantity: qty });
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
const processedStrategyIds = /* @__PURE__ */ new Set();
|
|
351
|
-
for (const giftInfo of gifts) {
|
|
352
|
-
const { strategyId, strategyName, giftCount, giftOptions, sourceProductIds } = giftInfo;
|
|
353
|
-
processedStrategyIds.add(strategyId);
|
|
354
|
-
const existing = existingByStrategy.get(strategyId);
|
|
355
|
-
const existingCount = (existing == null ? void 0 : existing.totalQuantity) ?? 0;
|
|
356
|
-
const diff = giftCount - existingCount;
|
|
357
|
-
if (diff > 0) {
|
|
358
|
-
const lastEvaluated = (_c = getGiftInfo((_b = existing == null ? void 0 : existing.items) == null ? void 0 : _b[0])) == null ? void 0 : _c.lastEvaluatedGiftCount;
|
|
359
|
-
const strategyChanged = lastEvaluated === void 0 || giftCount !== lastEvaluated;
|
|
360
|
-
if (strategyChanged) {
|
|
361
|
-
toAdd.push({
|
|
362
|
-
strategyId,
|
|
363
|
-
strategyName,
|
|
364
|
-
giftCount: diff,
|
|
365
|
-
giftOptions,
|
|
366
|
-
sourceProductIds
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
} else if (diff < 0) {
|
|
370
|
-
let remaining = Math.abs(diff);
|
|
371
|
-
const reduceItems = [];
|
|
372
|
-
const removeIds = [];
|
|
373
|
-
const reversed = [...(existing == null ? void 0 : existing.items) || []].reverse();
|
|
374
|
-
for (const giftItem of reversed) {
|
|
375
|
-
if (remaining <= 0)
|
|
376
|
-
break;
|
|
377
|
-
const uid = getOrderProductUid(giftItem);
|
|
378
|
-
const qty = Number((giftItem == null ? void 0 : giftItem.num) ?? 1) || 1;
|
|
379
|
-
if (qty <= remaining) {
|
|
380
|
-
removeIds.push(uid);
|
|
381
|
-
remaining -= qty;
|
|
382
|
-
} else {
|
|
383
|
-
reduceItems.push({ uid, currentQuantity: qty, newQuantity: qty - remaining });
|
|
384
|
-
remaining = 0;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
if (reduceItems.length > 0)
|
|
388
|
-
toReduce.push({ strategyId, items: reduceItems });
|
|
389
|
-
toRemove.push(...removeIds);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
for (const [strategyId, existing] of existingByStrategy) {
|
|
393
|
-
if (processedStrategyIds.has(strategyId))
|
|
394
|
-
continue;
|
|
395
|
-
for (const giftItem of existing.items) {
|
|
396
|
-
toRemove.push(getOrderProductUid(giftItem));
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return { toAdd, toReduce, toRemove };
|
|
400
|
-
}
|
|
401
|
-
function extractProductMatchIdentifiersFromStrategyConfig(strategyConfig) {
|
|
402
|
-
var _a;
|
|
403
|
-
const rules = (_a = strategyConfig == null ? void 0 : strategyConfig.conditions) == null ? void 0 : _a.rules;
|
|
404
|
-
if (!Array.isArray(rules))
|
|
405
|
-
return [];
|
|
406
|
-
for (const rule of rules) {
|
|
407
|
-
if ((rule == null ? void 0 : rule.field) === "productIdAndVariantId" && ((rule == null ? void 0 : rule.operator) === "product_match" || (rule == null ? void 0 : rule.operator) === "object_in") && Array.isArray(rule == null ? void 0 : rule.value)) {
|
|
408
|
-
return rule.value.map((item) => ({
|
|
409
|
-
product_id: Number(item == null ? void 0 : item.product_id) || 0,
|
|
410
|
-
product_variant_id: Number(item == null ? void 0 : item.product_variant_id) || 0
|
|
411
|
-
})).filter((item) => item.product_id > 0);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return [];
|
|
415
|
-
}
|
|
416
|
-
function resolveStrategyRequiredQuantity(matched) {
|
|
417
|
-
const preset = Number(matched.requiredQuantity);
|
|
418
|
-
if (Number.isFinite(preset) && preset > 0)
|
|
419
|
-
return preset;
|
|
420
|
-
const detail = matched.actionDetail || {};
|
|
421
|
-
if (matched.actionType === "X_ITEMS_FOR_Y_PRICE") {
|
|
422
|
-
return Number(detail.x) || 2;
|
|
423
|
-
}
|
|
424
|
-
if (matched.actionType === "BUY_X_GET_Y_FREE") {
|
|
425
|
-
return Number(detail.buyQuantity) || 1;
|
|
426
|
-
}
|
|
427
|
-
return 1;
|
|
428
|
-
}
|
|
429
|
-
function resolveStrategyEligibleProducts(matched) {
|
|
430
|
-
if (Array.isArray(matched.eligibleProducts) && matched.eligibleProducts.length > 0) {
|
|
431
|
-
return matched.eligibleProducts;
|
|
432
|
-
}
|
|
433
|
-
return extractProductMatchIdentifiersFromStrategyConfig(matched.strategyConfig);
|
|
434
|
-
}
|
|
435
|
-
function calculateUnfulfilledPromotions(processedProducts, applicableStrategiesResult, mainProducts) {
|
|
436
|
-
var _a, _b;
|
|
437
|
-
const unfulfilledMap = /* @__PURE__ */ new Map();
|
|
438
|
-
for (let i = 0; i < processedProducts.length; i++) {
|
|
439
|
-
const product = processedProducts[i];
|
|
440
|
-
const promotion = (_a = product == null ? void 0 : product.metadata) == null ? void 0 : _a._promotion;
|
|
441
|
-
if (!promotion || promotion.inPromotion !== false)
|
|
442
|
-
continue;
|
|
443
|
-
const strategyId = promotion.strategyId;
|
|
444
|
-
if (!strategyId)
|
|
445
|
-
continue;
|
|
446
|
-
const lookupUid = String(
|
|
447
|
-
(product == null ? void 0 : product._originalProductUid) ?? ((_b = product == null ? void 0 : product.metadata) == null ? void 0 : _b.unique_identification_number) ?? ""
|
|
448
|
-
);
|
|
449
|
-
const originalIndex = mainProducts.findIndex(
|
|
450
|
-
(item) => getOrderProductUid(item) === lookupUid
|
|
451
|
-
);
|
|
452
|
-
if (originalIndex === -1)
|
|
453
|
-
continue;
|
|
454
|
-
const strategyData = applicableStrategiesResult[originalIndex];
|
|
455
|
-
if (!strategyData || !strategyData.hasApplicableStrategy)
|
|
456
|
-
continue;
|
|
457
|
-
const matched = strategyData.applicableStrategies.find(
|
|
458
|
-
(s) => s.strategyId === strategyId
|
|
459
|
-
);
|
|
460
|
-
if (!matched)
|
|
461
|
-
continue;
|
|
462
|
-
const quantity = Number((product == null ? void 0 : product.num) ?? 1) || 1;
|
|
463
|
-
if (unfulfilledMap.has(strategyId)) {
|
|
464
|
-
unfulfilledMap.get(strategyId).currentQuantity += quantity;
|
|
465
|
-
} else {
|
|
466
|
-
unfulfilledMap.set(strategyId, {
|
|
467
|
-
strategyId,
|
|
468
|
-
strategyName: matched.strategyName,
|
|
469
|
-
actionType: matched.actionType,
|
|
470
|
-
currentQuantity: quantity,
|
|
471
|
-
requiredQuantity: resolveStrategyRequiredQuantity(matched),
|
|
472
|
-
needQuantity: 0,
|
|
473
|
-
eligibleProducts: resolveStrategyEligibleProducts(matched),
|
|
474
|
-
strategyMetadata: matched.strategyMetadata,
|
|
475
|
-
display: matched.display
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
const result = [];
|
|
480
|
-
for (const data of unfulfilledMap.values()) {
|
|
481
|
-
const needQuantity = data.requiredQuantity - data.currentQuantity;
|
|
482
|
-
if (needQuantity > 0) {
|
|
483
|
-
result.push({ ...data, needQuantity });
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
return result;
|
|
487
|
-
}
|
|
488
|
-
function appendPromotionTags(products, evaluator) {
|
|
489
|
-
if (!Array.isArray(products) || products.length === 0 || !evaluator) {
|
|
490
|
-
return products || [];
|
|
491
|
-
}
|
|
492
|
-
const evaluatorInput = products.map((item, index) => ({
|
|
493
|
-
id: index,
|
|
494
|
-
product_id: Number((item == null ? void 0 : item.id) ?? (item == null ? void 0 : item.product_id) ?? 0) || 0,
|
|
495
|
-
product_variant_id: Number((item == null ? void 0 : item.product_variant_id) ?? 0) || 0,
|
|
496
|
-
name: (item == null ? void 0 : item.title) || (item == null ? void 0 : item.name) || "",
|
|
497
|
-
price: Number((item == null ? void 0 : item.price) ?? 0) || 0,
|
|
498
|
-
quantity: Number((item == null ? void 0 : item.quantity) ?? 1) || 1,
|
|
499
|
-
bundle: []
|
|
500
|
-
}));
|
|
501
|
-
const strategies = evaluator.getProductsApplicableStrategies(
|
|
502
|
-
{ products: evaluatorInput },
|
|
503
|
-
false
|
|
504
|
-
);
|
|
505
|
-
return products.map((item, index) => {
|
|
506
|
-
const data = strategies[index];
|
|
507
|
-
const tags = [];
|
|
508
|
-
if (data == null ? void 0 : data.hasApplicableStrategy) {
|
|
509
|
-
for (const strategy of data.applicableStrategies) {
|
|
510
|
-
if (strategy.display) {
|
|
511
|
-
tags.push({
|
|
512
|
-
text: strategy.display.text,
|
|
513
|
-
type: strategy.display.type,
|
|
514
|
-
strategyId: strategy.strategyId
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
if (tags.length === 0) {
|
|
520
|
-
const { _promotionTags, ...rest } = item;
|
|
521
|
-
return rest;
|
|
522
|
-
}
|
|
523
|
-
return { ...item, _promotionTags: tags };
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
function processCartPromotion(list, evaluator, options) {
|
|
527
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
528
|
-
const empty = {
|
|
529
|
-
products: Array.isArray(list) ? [...list] : [],
|
|
530
|
-
totalDiscount: 0,
|
|
531
|
-
gifts: [],
|
|
532
|
-
giftActions: { toAdd: [], toReduce: [], toRemove: [] },
|
|
533
|
-
unfulfilledPromotions: []
|
|
534
|
-
};
|
|
535
|
-
if (!list || list.length === 0 || !evaluator)
|
|
536
|
-
return empty;
|
|
537
|
-
const mainProductsWithIndex = [];
|
|
538
|
-
const existingGiftsWithIndex = [];
|
|
539
|
-
const editProductsWithIndex = [];
|
|
540
|
-
for (let i = 0; i < list.length; i++) {
|
|
541
|
-
const item = list[i];
|
|
542
|
-
const giftInfo = getGiftInfo(item);
|
|
543
|
-
if (giftInfo) {
|
|
544
|
-
existingGiftsWithIndex.push({ item, originalListIndex: i });
|
|
545
|
-
} else if ((item == null ? void 0 : item.booking_id) !== void 0 && (item == null ? void 0 : item.booking_id) !== null && item.booking_id !== 0 && item.booking_id !== "0") {
|
|
546
|
-
editProductsWithIndex.push({ item, originalListIndex: i });
|
|
547
|
-
} else {
|
|
548
|
-
mainProductsWithIndex.push({ item, originalListIndex: i });
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
const mainProducts = mainProductsWithIndex.map((x) => x.item);
|
|
552
|
-
const existingGifts = existingGiftsWithIndex.map((x) => x.item);
|
|
553
|
-
if (mainProducts.length === 0) {
|
|
554
|
-
const editProducts = editProductsWithIndex.map((x) => x.item);
|
|
555
|
-
if (existingGifts.length > 0) {
|
|
556
|
-
const toRemove = [];
|
|
557
|
-
for (const gift of existingGifts) {
|
|
558
|
-
const uid = getOrderProductUid(gift);
|
|
559
|
-
if (uid)
|
|
560
|
-
toRemove.push(uid);
|
|
561
|
-
}
|
|
562
|
-
return {
|
|
563
|
-
products: editProducts,
|
|
564
|
-
totalDiscount: 0,
|
|
565
|
-
gifts: [],
|
|
566
|
-
giftActions: { toAdd: [], toReduce: [], toRemove },
|
|
567
|
-
unfulfilledPromotions: []
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
return {
|
|
571
|
-
...empty,
|
|
572
|
-
products: editProducts.length > 0 ? editProducts : [...list]
|
|
573
|
-
};
|
|
574
|
-
}
|
|
575
|
-
const promotionProducts = [];
|
|
576
|
-
const originalItemMap = /* @__PURE__ */ new Map();
|
|
577
|
-
const productsForStrategies = mainProductsWithIndex.map(({ item }, i) => {
|
|
578
|
-
var _a2;
|
|
579
|
-
return {
|
|
580
|
-
id: i,
|
|
581
|
-
product_id: Number((item == null ? void 0 : item.product_id) ?? 0) || 0,
|
|
582
|
-
product_variant_id: Number((item == null ? void 0 : item.product_variant_id) ?? 0) || 0,
|
|
583
|
-
name: ((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.product_name) || (item == null ? void 0 : item.title) || "",
|
|
584
|
-
price: getOrderProductBasePrice(item),
|
|
585
|
-
quantity: Number((item == null ? void 0 : item.num) ?? 1) || 1,
|
|
586
|
-
bundle: getProductBundleForEvaluator(item)
|
|
587
|
-
};
|
|
588
|
-
});
|
|
589
|
-
for (let i = 0; i < mainProductsWithIndex.length; i++) {
|
|
590
|
-
const { item, originalListIndex } = mainProductsWithIndex[i];
|
|
591
|
-
const promotionProduct = convertToPromotionProduct(item, originalListIndex);
|
|
592
|
-
promotionProducts.push(promotionProduct);
|
|
593
|
-
originalItemMap.set(promotionProduct.id, { item, index: originalListIndex });
|
|
594
|
-
}
|
|
595
|
-
const applicableStrategiesResult = evaluator.getProductsApplicableStrategies({
|
|
596
|
-
products: productsForStrategies,
|
|
597
|
-
channel: options == null ? void 0 : options.channel
|
|
598
|
-
});
|
|
599
|
-
const strategyActionMap = /* @__PURE__ */ new Map();
|
|
600
|
-
for (const row of applicableStrategiesResult || []) {
|
|
601
|
-
for (const strategy of (row == null ? void 0 : row.applicableStrategies) || []) {
|
|
602
|
-
if (!(strategy == null ? void 0 : strategy.strategyId))
|
|
603
|
-
continue;
|
|
604
|
-
strategyActionMap.set(strategy.strategyId, {
|
|
605
|
-
actionType: strategy.actionType,
|
|
606
|
-
actionDetail: strategy.actionDetail
|
|
607
|
-
});
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
const result = evaluator.evaluateCartWithPricing({
|
|
611
|
-
products: promotionProducts,
|
|
612
|
-
channel: options == null ? void 0 : options.channel
|
|
613
|
-
});
|
|
614
|
-
const strategySourceProductIds = /* @__PURE__ */ new Map();
|
|
615
|
-
for (const priced of result.products) {
|
|
616
|
-
if (priced.inPromotion && priced.strategyId) {
|
|
617
|
-
const sourceId = priced.originalId ?? priced.id;
|
|
618
|
-
const originalData = originalItemMap.get(sourceId);
|
|
619
|
-
if (originalData) {
|
|
620
|
-
const uid = getOrderProductUid(originalData.item);
|
|
621
|
-
if (!uid)
|
|
622
|
-
continue;
|
|
623
|
-
const arr = strategySourceProductIds.get(priced.strategyId) || [];
|
|
624
|
-
if (!arr.includes(uid))
|
|
625
|
-
arr.push(uid);
|
|
626
|
-
strategySourceProductIds.set(priced.strategyId, arr);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
const extendedGifts = result.gifts.map((gift) => ({
|
|
631
|
-
...gift,
|
|
632
|
-
sourceProductIds: strategySourceProductIds.get(gift.strategyId) || []
|
|
633
|
-
}));
|
|
634
|
-
const strategyGiftInfoMap = /* @__PURE__ */ new Map();
|
|
635
|
-
for (const gift of extendedGifts)
|
|
636
|
-
strategyGiftInfoMap.set(gift.strategyId, gift);
|
|
637
|
-
const strategyGiftInfoOwner = /* @__PURE__ */ new Map();
|
|
638
|
-
const processedProducts = [];
|
|
639
|
-
for (const priced of result.products) {
|
|
640
|
-
const rawSourceId = priced.originalId ?? priced.id;
|
|
641
|
-
const sourceId = Number(rawSourceId);
|
|
642
|
-
const originalData = Number.isFinite(sourceId) ? originalItemMap.get(sourceId) : void 0;
|
|
643
|
-
if (!originalData)
|
|
644
|
-
continue;
|
|
645
|
-
const { item: originalItem, index: originalIndex } = originalData;
|
|
646
|
-
const pricedProductId = Number(priced.product_id ?? 0);
|
|
647
|
-
const originalProductId = Number(originalItem.product_id ?? 0);
|
|
648
|
-
if (pricedProductId > 0 && originalProductId > 0 && pricedProductId !== originalProductId) {
|
|
649
|
-
continue;
|
|
650
|
-
}
|
|
651
|
-
const newItem = (0, import_lodash_es.cloneDeep)(originalItem);
|
|
652
|
-
const metadata = newItem.metadata || {};
|
|
653
|
-
const sourceCartNum = (0, import_utils2.getSafeProductNum)(originalItem.num);
|
|
654
|
-
newItem._sourceCartNum = sourceCartNum;
|
|
655
|
-
const wasInPromotion = ((_a = metadata._promotion) == null ? void 0 : _a.inPromotion) === true;
|
|
656
|
-
delete metadata._promotion;
|
|
657
|
-
if (Array.isArray(newItem.product_bundle)) {
|
|
658
|
-
for (const b of newItem.product_bundle) {
|
|
659
|
-
if (b && typeof b === "object")
|
|
660
|
-
delete b._promotion;
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
const originalBasePrice = getOrderProductOriginalPriceForPromotion(originalItem);
|
|
664
|
-
const shouldModifyPrice = priced.inPromotion || wasInPromotion;
|
|
665
|
-
const priceUnchangedPromo = priced.inPromotion && Number(priced.finalPrice) === Number(originalBasePrice);
|
|
666
|
-
if (shouldModifyPrice) {
|
|
667
|
-
if (priced.inPromotion && !priceUnchangedPromo) {
|
|
668
|
-
const promoUnitPrice = new import_decimal.default(priced.finalPrice).toDecimalPlaces(2).toFixed(2);
|
|
669
|
-
applyPromoUnitPriceToLine(newItem, {
|
|
670
|
-
promoUnitPrice,
|
|
671
|
-
originalBasePrice,
|
|
672
|
-
strategyTitle: (_b = priced.strategyMetadata) == null ? void 0 : _b.name
|
|
673
|
-
});
|
|
674
|
-
} else {
|
|
675
|
-
const optionSum = (0, import_utils.sumOptionUnitPrice)(newItem.product_option_item).toNumber();
|
|
676
|
-
const sourceRestore = new import_decimal.default(originalBasePrice).toDecimalPlaces(2).toFixed(2);
|
|
677
|
-
const mainRestore = new import_decimal.default(Number(sourceRestore) || 0).plus(optionSum).toDecimalPlaces(2).toFixed(2);
|
|
678
|
-
metadata.source_product_price = sourceRestore;
|
|
679
|
-
metadata.main_product_selling_price = mainRestore;
|
|
680
|
-
metadata.price_schema_version = 2;
|
|
681
|
-
newItem.selling_price = (0, import_utils.composeLinePrice)({
|
|
682
|
-
mainPrice: mainRestore,
|
|
683
|
-
bundle: newItem.product_bundle
|
|
684
|
-
});
|
|
685
|
-
updatePromotionDiscountList(newItem, null);
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
newItem.num = priced.isSplit ? (0, import_utils2.getSafeProductNum)(priced.quantity) : sourceCartNum;
|
|
689
|
-
if (priced.isSplit) {
|
|
690
|
-
metadata.unique_identification_number = (0, import_utils.createUuidV4)();
|
|
691
|
-
}
|
|
692
|
-
if (priced.strategyId) {
|
|
693
|
-
const actionMeta = strategyActionMap.get(priced.strategyId);
|
|
694
|
-
const promotionInfo = {
|
|
695
|
-
strategyId: priced.strategyId,
|
|
696
|
-
strategyMetadata: priced.strategyMetadata,
|
|
697
|
-
inPromotion: priced.inPromotion,
|
|
698
|
-
originalPrice: originalBasePrice,
|
|
699
|
-
finalPrice: priced.finalPrice,
|
|
700
|
-
actionType: actionMeta == null ? void 0 : actionMeta.actionType,
|
|
701
|
-
actionDetail: actionMeta == null ? void 0 : actionMeta.actionDetail
|
|
702
|
-
};
|
|
703
|
-
const bundleIdx = priced.matchedBundleIndex;
|
|
704
|
-
if (bundleIdx !== void 0 && Array.isArray(newItem.product_bundle) && newItem.product_bundle[bundleIdx]) {
|
|
705
|
-
newItem.product_bundle[bundleIdx]._promotion = promotionInfo;
|
|
706
|
-
} else {
|
|
707
|
-
metadata._promotion = promotionInfo;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
newItem.metadata = metadata;
|
|
711
|
-
if (priced.inPromotion && priced.strategyId) {
|
|
712
|
-
const gift = strategyGiftInfoMap.get(priced.strategyId);
|
|
713
|
-
if (gift && Array.isArray(gift.giftOptions) && gift.giftOptions.length > 0 && !strategyGiftInfoOwner.has(priced.strategyId)) {
|
|
714
|
-
const giftInfoData = {
|
|
715
|
-
strategyName: gift.strategyName,
|
|
716
|
-
giftCount: gift.giftCount,
|
|
717
|
-
giftOptions: gift.giftOptions
|
|
718
|
-
};
|
|
719
|
-
const bundleIdx = priced.matchedBundleIndex;
|
|
720
|
-
if (bundleIdx !== void 0 && Array.isArray(newItem.product_bundle) && ((_c = newItem.product_bundle[bundleIdx]) == null ? void 0 : _c._promotion)) {
|
|
721
|
-
newItem.product_bundle[bundleIdx]._promotion.giftInfo = giftInfoData;
|
|
722
|
-
} else if (metadata._promotion) {
|
|
723
|
-
metadata._promotion.giftInfo = giftInfoData;
|
|
724
|
-
}
|
|
725
|
-
strategyGiftInfoOwner.set(
|
|
726
|
-
priced.strategyId,
|
|
727
|
-
getOrderProductUid(newItem)
|
|
728
|
-
);
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
newItem._sortIndex = originalIndex;
|
|
732
|
-
newItem._sortSubIndex = priced.inPromotion ? 0 : 1;
|
|
733
|
-
newItem._originalProductUid = getOrderProductUid(originalItem);
|
|
734
|
-
processedProducts.push(newItem);
|
|
735
|
-
}
|
|
736
|
-
for (const { item, originalListIndex } of existingGiftsWithIndex) {
|
|
737
|
-
const next = item;
|
|
738
|
-
next._sortIndex = originalListIndex;
|
|
739
|
-
next._sortSubIndex = 0;
|
|
740
|
-
const strategyId = (_d = getGiftInfo(next)) == null ? void 0 : _d.strategyId;
|
|
741
|
-
if (strategyId) {
|
|
742
|
-
const latest = strategyGiftInfoMap.get(strategyId);
|
|
743
|
-
const owner = strategyGiftInfoOwner.get(strategyId);
|
|
744
|
-
if (latest && ((_e = next.metadata) == null ? void 0 : _e._giftInfo)) {
|
|
745
|
-
next.metadata._giftInfo.sourceProductIds = latest.sourceProductIds;
|
|
746
|
-
}
|
|
747
|
-
if (owner && ((_f = next.metadata) == null ? void 0 : _f._giftInfo)) {
|
|
748
|
-
next.metadata._giftInfo.sourceProductId = owner;
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
processedProducts.push(next);
|
|
752
|
-
}
|
|
753
|
-
for (const { item, originalListIndex } of editProductsWithIndex) {
|
|
754
|
-
const next = item;
|
|
755
|
-
next._sortIndex = originalListIndex;
|
|
756
|
-
next._sortSubIndex = 0;
|
|
757
|
-
processedProducts.push(next);
|
|
758
|
-
}
|
|
759
|
-
processedProducts.sort((a, b) => {
|
|
760
|
-
if (a._sortIndex !== b._sortIndex)
|
|
761
|
-
return a._sortIndex - b._sortIndex;
|
|
762
|
-
return a._sortSubIndex - b._sortSubIndex;
|
|
763
|
-
});
|
|
764
|
-
const dedupedPromotionLines = dedupePromotionProcessedLines(processedProducts);
|
|
765
|
-
processedProducts.splice(0, processedProducts.length, ...dedupedPromotionLines);
|
|
766
|
-
const mergedPromotionLines = mergeIdenticalPromotionCartLines(processedProducts);
|
|
767
|
-
processedProducts.splice(0, processedProducts.length, ...mergedPromotionLines);
|
|
768
|
-
for (const product of processedProducts) {
|
|
769
|
-
const metadata = product.metadata || {};
|
|
770
|
-
if (metadata._promotion)
|
|
771
|
-
continue;
|
|
772
|
-
if (getGiftInfo(product))
|
|
773
|
-
continue;
|
|
774
|
-
const lookupUid = String(product._originalProductUid ?? getOrderProductUid(product));
|
|
775
|
-
const originalIndex = mainProducts.findIndex(
|
|
776
|
-
(item) => getOrderProductUid(item) === lookupUid
|
|
777
|
-
);
|
|
778
|
-
if (originalIndex === -1)
|
|
779
|
-
continue;
|
|
780
|
-
const strategyData = applicableStrategiesResult[originalIndex];
|
|
781
|
-
if (!strategyData || !strategyData.hasApplicableStrategy)
|
|
782
|
-
continue;
|
|
783
|
-
const first = strategyData.applicableStrategies[0];
|
|
784
|
-
if (!first)
|
|
785
|
-
continue;
|
|
786
|
-
const originalPrice = getOrderProductOriginalPriceForPromotion(mainProducts[originalIndex]);
|
|
787
|
-
metadata._promotion = {
|
|
788
|
-
strategyId: first.strategyId,
|
|
789
|
-
strategyMetadata: first.strategyMetadata,
|
|
790
|
-
inPromotion: false,
|
|
791
|
-
originalPrice,
|
|
792
|
-
finalPrice: originalPrice
|
|
793
|
-
};
|
|
794
|
-
product.metadata = metadata;
|
|
795
|
-
}
|
|
796
|
-
const giftActions = calculateGiftActions(extendedGifts, existingGifts);
|
|
797
|
-
for (const gift of existingGifts) {
|
|
798
|
-
const strategyId = (_g = getGiftInfo(gift)) == null ? void 0 : _g.strategyId;
|
|
799
|
-
if (!strategyId)
|
|
800
|
-
continue;
|
|
801
|
-
const latest = strategyGiftInfoMap.get(strategyId);
|
|
802
|
-
if (latest && ((_h = gift.metadata) == null ? void 0 : _h._giftInfo)) {
|
|
803
|
-
gift.metadata._giftInfo.lastEvaluatedGiftCount = latest.giftCount;
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
const unfulfilledPromotions = calculateUnfulfilledPromotions(
|
|
807
|
-
processedProducts,
|
|
808
|
-
applicableStrategiesResult,
|
|
809
|
-
mainProducts
|
|
810
|
-
);
|
|
811
|
-
for (const product of processedProducts) {
|
|
812
|
-
delete product._sortIndex;
|
|
813
|
-
delete product._sortSubIndex;
|
|
814
|
-
delete product._originalProductUid;
|
|
815
|
-
}
|
|
816
|
-
return {
|
|
817
|
-
products: processedProducts,
|
|
818
|
-
totalDiscount: result.totalDiscount,
|
|
819
|
-
gifts: extendedGifts,
|
|
820
|
-
giftActions,
|
|
821
|
-
unfulfilledPromotions
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
825
|
-
0 && (module.exports = {
|
|
826
|
-
appendPromotionTags,
|
|
827
|
-
calculateUnfulfilledPromotions,
|
|
828
|
-
extractProductMatchIdentifiersFromStrategyConfig,
|
|
829
|
-
getOrderProductBasePrice,
|
|
830
|
-
getOrderProductOriginalPriceForPromotion,
|
|
831
|
-
getOrderProductUid,
|
|
832
|
-
mergeIdenticalPromotionCartLines,
|
|
833
|
-
processCartPromotion,
|
|
834
|
-
resolveStrategyEligibleProducts,
|
|
835
|
-
resolveStrategyRequiredQuantity
|
|
836
|
-
});
|