@pisell/pisellos 2.1.77 → 2.1.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,25 +29,44 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // src/modules/Summary/utils.ts
30
30
  var utils_exports = {};
31
31
  __export(utils_exports, {
32
+ calcDiscountListDifference: () => calcDiscountListDifference,
32
33
  calculateDeposit: () => calculateDeposit,
33
34
  calculatePriceDetails: () => calculatePriceDetails,
34
35
  calculateSubtotal: () => calculateSubtotal,
35
36
  calculateTaxFee: () => calculateTaxFee,
36
- getBundleDiscountList: () => getBundleDiscountList
37
+ getBundleDiscountList: () => getBundleDiscountList,
38
+ getProductDiscountProductDiscountDifference: () => getProductDiscountProductDiscountDifference,
39
+ getSurcharge: () => getSurcharge,
40
+ getSurchargeAmount: () => getSurchargeAmount,
41
+ getTax: () => getTax
37
42
  });
38
43
  module.exports = __toCommonJS(utils_exports);
39
44
  var import_decimal = __toESM(require("decimal.js"));
40
- var calculatePriceDetails = (shopInfo, items) => {
45
+ var calculatePriceDetails = (shopInfo, items, isInScheduleByDate, surchargeList) => {
41
46
  const subtotal = new import_decimal.default(calculateSubtotal(items));
42
47
  const totalTaxFee = new import_decimal.default(calculateTaxFee(shopInfo, items));
43
- const total = (shopInfo == null ? void 0 : shopInfo.is_price_include_tax) ? subtotal : subtotal.plus(totalTaxFee);
48
+ const surcharge = getSurcharge({ service: items, addons: [], bookingDetail: null, bookingId: void 0 }, { isEdit: false, isInScheduleByDate, surcharge_list: surchargeList, scheduleById: {} });
49
+ const surchargeAmount = new import_decimal.default(getSurchargeAmount({ bookingDetail: null, bookingId: void 0 }, surcharge, { isEdit: false }));
50
+ const { tax, originTax } = getTax({ service: items, bookingDetail: null, bookingId: void 0 }, {
51
+ computed: {
52
+ productExpectAmount: subtotal.toNumber(),
53
+ shopDiscount: 0
54
+ },
55
+ isEdit: false,
56
+ tax_rate: shopInfo == null ? void 0 : shopInfo.tax_rate,
57
+ is_price_include_tax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax
58
+ });
59
+ const total = (shopInfo == null ? void 0 : shopInfo.is_price_include_tax) ? subtotal.plus(surchargeAmount) : subtotal.plus(tax).plus(surchargeAmount);
44
60
  const deposit = calculateDeposit(items);
45
61
  return {
46
62
  subtotal: subtotal.toFixed(2),
47
63
  total: total.toFixed(2),
48
64
  taxTitle: shopInfo == null ? void 0 : shopInfo.tax_title,
49
- totalTaxFee: totalTaxFee.toFixed(2),
65
+ taxRate: shopInfo == null ? void 0 : shopInfo.tax_rate,
66
+ totalTaxFee: tax,
50
67
  isPriceIncludeTax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax,
68
+ surcharge,
69
+ surchargeAmount: surchargeAmount.toFixed(2),
51
70
  deposit
52
71
  };
53
72
  };
@@ -63,14 +82,270 @@ var getBundleDiscountList = (bundle) => {
63
82
  });
64
83
  return discountList;
65
84
  };
85
+ var calcDiscountListDifference = (discountList) => {
86
+ return discountList.reduce((pre, cur) => {
87
+ var _a;
88
+ return pre + (((_a = cur == null ? void 0 : cur.metadata) == null ? void 0 : _a.product_discount_difference) || 0);
89
+ }, 0);
90
+ };
66
91
  var getProductDiscountProductDiscountDifference = (item) => {
67
92
  var _a, _b, _c, _d;
68
93
  const mainDiscountList = ((_b = (_a = item._origin) == null ? void 0 : _a.product) == null ? void 0 : _b.discount_list) || [];
69
94
  const bundleDiscountList = getBundleDiscountList(((_d = (_c = item._origin) == null ? void 0 : _c.product) == null ? void 0 : _d.product_bundle) || []);
70
95
  const discountList = [...mainDiscountList, ...bundleDiscountList];
71
- return discountList.reduce((pre, cur) => {
72
- return pre + (cur.metadata.product_discount_difference || 0);
73
- }, 0);
96
+ return calcDiscountListDifference(discountList);
97
+ };
98
+ var getTax = ({ service, addons, bookingDetail, bookingId }, options) => {
99
+ const { isEdit, computed, tax_rate, is_price_include_tax } = options;
100
+ let totalOriginTax = new import_decimal.default(0);
101
+ let totalTax = new import_decimal.default(0);
102
+ if (service || addons) {
103
+ if (service == null ? void 0 : service.length) {
104
+ const serviceResult = processItemsTax(service.map((item) => {
105
+ return {
106
+ ...item,
107
+ option: (item == null ? void 0 : item.option) || (item == null ? void 0 : item.options) || [],
108
+ bundle: (item == null ? void 0 : item.bundle) || (item == null ? void 0 : item.bundles) || []
109
+ };
110
+ }), { bookingDetail, bookingId }, {
111
+ tax_rate,
112
+ is_price_include_tax,
113
+ computed
114
+ });
115
+ totalOriginTax = totalOriginTax.plus(serviceResult.originTax);
116
+ totalTax = totalTax.plus(serviceResult.tax);
117
+ }
118
+ if (addons == null ? void 0 : addons.length) {
119
+ const addonsResult = processItemsTax(addons, { bookingDetail, bookingId }, {
120
+ tax_rate,
121
+ is_price_include_tax,
122
+ computed
123
+ });
124
+ totalOriginTax = totalOriginTax.plus(addonsResult.originTax);
125
+ totalTax = totalTax.plus(addonsResult.tax);
126
+ }
127
+ }
128
+ return {
129
+ originTax: totalOriginTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber(),
130
+ tax: totalTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber()
131
+ };
132
+ };
133
+ function getDiscountedTaxableBase(input) {
134
+ const { basePrice, discount, surchargeFee } = input;
135
+ if (surchargeFee.lte(0) && basePrice.lte(0))
136
+ return new import_decimal.default(0);
137
+ const discountedBase = basePrice.minus(discount);
138
+ if (discountedBase.lte(0))
139
+ return surchargeFee;
140
+ return discountedBase.plus(surchargeFee);
141
+ }
142
+ var processItemsTax = (items, { bookingDetail, bookingId }, options) => {
143
+ const { tax_rate, is_price_include_tax, computed } = options;
144
+ const { shopDiscount, productExpectAmount } = computed;
145
+ let preciseOriginTax = new import_decimal.default(0);
146
+ let preciseTax = new import_decimal.default(0);
147
+ let roundedOriginTax = new import_decimal.default(0);
148
+ let roundedTax = new import_decimal.default(0);
149
+ let lastTaxableItem = null;
150
+ for (let item of items) {
151
+ const quantity = new import_decimal.default(item.num || 1);
152
+ const unitPrice = new import_decimal.default(item.total || 0);
153
+ const mainProductPrice = new import_decimal.default(getMainProductTotal({
154
+ ...item,
155
+ option: (item == null ? void 0 : item.option) || (item == null ? void 0 : item.options) || [],
156
+ bundle: (item == null ? void 0 : item.bundle) || (item == null ? void 0 : item.bundles) || []
157
+ }) || 0);
158
+ const originalTaxableBase = getDiscountedTaxableBase({
159
+ basePrice: mainProductPrice,
160
+ discount: new import_decimal.default(0),
161
+ surchargeFee: new import_decimal.default((item == null ? void 0 : item.surcharge_fee) || 0)
162
+ });
163
+ let originalTaxPerItemPrecise = getProductItemTax(
164
+ {
165
+ ...item,
166
+ total: originalTaxableBase.toNumber(),
167
+ quantity: 1
168
+ },
169
+ { bookingDetail, bookingId },
170
+ { tax_rate, is_price_include_tax }
171
+ );
172
+ let itemDiscount = new import_decimal.default(0);
173
+ if (productExpectAmount > 0) {
174
+ itemDiscount = unitPrice.dividedBy(productExpectAmount).times(shopDiscount);
175
+ }
176
+ let mainItemDiscount = new import_decimal.default(0);
177
+ if (unitPrice.greaterThan(0)) {
178
+ mainItemDiscount = mainProductPrice.dividedBy(productExpectAmount).times(shopDiscount);
179
+ }
180
+ const mainItemDiscountedUnitPrice = getDiscountedTaxableBase({
181
+ basePrice: mainProductPrice,
182
+ discount: mainItemDiscount,
183
+ surchargeFee: new import_decimal.default((item == null ? void 0 : item.surcharge_fee) || 0)
184
+ });
185
+ const maxDiscountedTaxPerItemPrecise = getProductItemTax(
186
+ {
187
+ ...item,
188
+ total: mainItemDiscountedUnitPrice.toNumber(),
189
+ price: item.total,
190
+ quantity: 1
191
+ },
192
+ { bookingDetail, bookingId },
193
+ { tax_rate, is_price_include_tax }
194
+ );
195
+ const maxDiscountedTaxPerItemRounded = maxDiscountedTaxPerItemPrecise.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
196
+ item.main_product_attached_bundle_tax_fee = maxDiscountedTaxPerItemRounded.toNumber();
197
+ if (maxDiscountedTaxPerItemPrecise.gt(0)) {
198
+ lastTaxableItem = { type: "main", item };
199
+ }
200
+ let addTimePreciseTax = new import_decimal.default(0);
201
+ let addTimeRoundedTax = new import_decimal.default(0);
202
+ if (Array.isArray(item.relation_details)) {
203
+ for (let atItem of item.relation_details) {
204
+ const _originTotal = new import_decimal.default(
205
+ atItem.selling_price || atItem.price || 0
206
+ );
207
+ const addTimeDiscount = productExpectAmount > 0 ? _originTotal.dividedBy(productExpectAmount).times(shopDiscount) : new import_decimal.default(0);
208
+ const _discountedTotal = getDiscountedTaxableBase({
209
+ basePrice: _originTotal,
210
+ discount: addTimeDiscount,
211
+ surchargeFee: new import_decimal.default((atItem == null ? void 0 : atItem.surcharge_fee) || 0)
212
+ });
213
+ const addTimeTaxPerItemPrecise = getProductItemTax(
214
+ {
215
+ ...atItem,
216
+ total: _discountedTotal.toNumber(),
217
+ quantity: atItem.product_quantity || atItem.num || 1
218
+ },
219
+ { bookingDetail, bookingId },
220
+ { tax_rate, is_price_include_tax }
221
+ );
222
+ const addTimeTaxPerItemRounded = addTimeTaxPerItemPrecise.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
223
+ atItem.tax_fee = addTimeTaxPerItemRounded.toNumber();
224
+ addTimePreciseTax = addTimePreciseTax.plus(addTimeTaxPerItemPrecise);
225
+ addTimeRoundedTax = addTimeRoundedTax.plus(addTimeTaxPerItemRounded);
226
+ if (addTimeTaxPerItemPrecise.gt(0)) {
227
+ lastTaxableItem = { type: "addTime", item: atItem };
228
+ }
229
+ }
230
+ }
231
+ let bundlePreciseOriginTax = new import_decimal.default(0);
232
+ let bundlePreciseTax = new import_decimal.default(0);
233
+ let bundleRoundedOriginTax = new import_decimal.default(0);
234
+ let bundleRoundedTax = new import_decimal.default(0);
235
+ if (Array.isArray(item == null ? void 0 : item.bundle)) {
236
+ for (let bundleItem of item == null ? void 0 : item.bundle) {
237
+ if (getBundleItemIsOriginalPrice(bundleItem)) {
238
+ const bundleQuantity = new import_decimal.default(bundleItem.num || bundleItem.quantity || 1);
239
+ const bundleUnitPrice = new import_decimal.default(
240
+ bundleItem.bundle_selling_price ?? bundleItem.price ?? 0
241
+ );
242
+ const bundleOriginalTaxableBase = getDiscountedTaxableBase({
243
+ basePrice: bundleUnitPrice,
244
+ discount: new import_decimal.default(0),
245
+ surchargeFee: new import_decimal.default((bundleItem == null ? void 0 : bundleItem.surcharge_fee) || 0)
246
+ });
247
+ const bundleOriginalTaxPerItemPrecise = getProductItemTax(
248
+ {
249
+ ...bundleItem,
250
+ total: bundleOriginalTaxableBase.toNumber(),
251
+ quantity: 1
252
+ },
253
+ { bookingDetail, bookingId },
254
+ { tax_rate, is_price_include_tax }
255
+ );
256
+ let bundleItemDiscount = new import_decimal.default(0);
257
+ if (productExpectAmount > 0) {
258
+ bundleItemDiscount = bundleUnitPrice.dividedBy(productExpectAmount).times(shopDiscount);
259
+ }
260
+ const bundleDiscountedUnitPrice = getDiscountedTaxableBase({
261
+ basePrice: bundleUnitPrice,
262
+ discount: bundleItemDiscount,
263
+ surchargeFee: new import_decimal.default((bundleItem == null ? void 0 : bundleItem.surcharge_fee) || 0)
264
+ });
265
+ const bundleDiscountedTaxPerItemPrecise = getProductItemTax(
266
+ {
267
+ ...bundleItem,
268
+ total: bundleDiscountedUnitPrice.toNumber(),
269
+ quantity: 1
270
+ },
271
+ { bookingDetail, bookingId },
272
+ { tax_rate, is_price_include_tax }
273
+ );
274
+ const bundleOriginalTaxPerItemRounded = bundleOriginalTaxPerItemPrecise.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
275
+ const bundleDiscountedTaxPerItemRounded = bundleDiscountedTaxPerItemPrecise.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
276
+ bundleItem.original_tax_fee = bundleOriginalTaxPerItemRounded.toNumber();
277
+ bundleItem.tax_fee = bundleDiscountedTaxPerItemRounded.toNumber();
278
+ bundlePreciseOriginTax = bundlePreciseOriginTax.plus(bundleOriginalTaxPerItemPrecise.times(bundleQuantity));
279
+ bundlePreciseTax = bundlePreciseTax.plus(bundleDiscountedTaxPerItemPrecise.times(bundleQuantity));
280
+ bundleRoundedOriginTax = bundleRoundedOriginTax.plus(bundleOriginalTaxPerItemRounded.times(bundleQuantity));
281
+ bundleRoundedTax = bundleRoundedTax.plus(bundleDiscountedTaxPerItemRounded.times(bundleQuantity));
282
+ if (bundleDiscountedTaxPerItemPrecise.gt(0)) {
283
+ lastTaxableItem = { type: "bundle", item: bundleItem };
284
+ }
285
+ }
286
+ }
287
+ }
288
+ const originalTaxFeePrecise = originalTaxPerItemPrecise.plus(bundlePreciseOriginTax);
289
+ const taxFeePrecise = maxDiscountedTaxPerItemPrecise.plus(bundlePreciseTax);
290
+ const originalTaxFeeRounded = originalTaxPerItemPrecise.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).plus(bundleRoundedOriginTax);
291
+ const taxFeeRounded = maxDiscountedTaxPerItemRounded.plus(bundleRoundedTax);
292
+ item.original_tax_fee = originalTaxFeeRounded.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
293
+ item.tax_fee = taxFeeRounded.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
294
+ preciseOriginTax = preciseOriginTax.plus(originalTaxFeePrecise.times(quantity)).plus(addTimePreciseTax);
295
+ preciseTax = preciseTax.plus(taxFeePrecise.times(quantity)).plus(addTimePreciseTax);
296
+ roundedOriginTax = roundedOriginTax.plus(originalTaxFeeRounded.times(quantity)).plus(addTimeRoundedTax);
297
+ roundedTax = roundedTax.plus(taxFeeRounded.times(quantity)).plus(addTimeRoundedTax);
298
+ }
299
+ const expectedOriginTax = preciseOriginTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
300
+ const expectedTax = preciseTax.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
301
+ const originTaxRemainder = expectedOriginTax.minus(roundedOriginTax).toNumber();
302
+ const taxRemainder = expectedTax.minus(roundedTax).toNumber();
303
+ if (lastTaxableItem) {
304
+ if (originTaxRemainder !== 0) {
305
+ if (lastTaxableItem.type === "main") {
306
+ lastTaxableItem.item.original_tax_fee_rounding_remainder = originTaxRemainder;
307
+ } else {
308
+ lastTaxableItem.item.original_tax_fee_rounding_remainder = originTaxRemainder;
309
+ }
310
+ }
311
+ if (taxRemainder !== 0) {
312
+ if (lastTaxableItem.type === "main") {
313
+ lastTaxableItem.item.tax_fee_rounding_remainder = taxRemainder;
314
+ } else {
315
+ lastTaxableItem.item.tax_fee_rounding_remainder = taxRemainder;
316
+ }
317
+ }
318
+ }
319
+ return { originTax: expectedOriginTax, tax: expectedTax };
320
+ };
321
+ var getProductItemTax = (item, state, options) => {
322
+ const { bookingDetail } = state;
323
+ const { tax_rate, is_price_include_tax } = options;
324
+ const productDiscountPrice = new import_decimal.default(item.total || 0);
325
+ const quantity = new import_decimal.default(item.quantity ?? item.num ?? 1);
326
+ if (productDiscountPrice.lte(0)) {
327
+ return new import_decimal.default(0);
328
+ }
329
+ const currentTaxRate = new import_decimal.default(
330
+ (bookingDetail == null ? void 0 : bookingDetail.tax_rate) ?? (tax_rate ? tax_rate / 100 : 0)
331
+ );
332
+ const currentIsPriceIncludeTax = (bookingDetail == null ? void 0 : bookingDetail.is_price_include_tax) ?? is_price_include_tax ?? 0;
333
+ let singleItemTax = new import_decimal.default(0);
334
+ if (item.is_charge_tax === 0 || !item.is_charge_tax) {
335
+ singleItemTax = new import_decimal.default(0);
336
+ } else {
337
+ if (currentIsPriceIncludeTax === 0) {
338
+ singleItemTax = productDiscountPrice.times(currentTaxRate);
339
+ } else {
340
+ const divisor = new import_decimal.default(1).plus(currentTaxRate);
341
+ if (divisor.lte(0)) {
342
+ singleItemTax = new import_decimal.default(0);
343
+ } else {
344
+ singleItemTax = productDiscountPrice.dividedBy(divisor).times(currentTaxRate);
345
+ }
346
+ }
347
+ }
348
+ return singleItemTax.times(quantity);
74
349
  };
75
350
  var calculateSubtotal = (items) => {
76
351
  if (!(items == null ? void 0 : items.length)) {
@@ -121,11 +396,392 @@ var calculateDeposit = (items) => {
121
396
  }
122
397
  return void 0;
123
398
  };
399
+ var getSurchargeAmount = ({ bookingDetail, bookingId }, surcharge, options) => {
400
+ const { isEdit } = options;
401
+ if (!isEdit) {
402
+ if (bookingDetail && bookingId) {
403
+ return Number(bookingDetail.surcharge_fee);
404
+ }
405
+ }
406
+ if (!Array.isArray(surcharge))
407
+ return 0;
408
+ return surcharge.reduce((total, item) => {
409
+ return total + (item.value || 0);
410
+ }, 0);
411
+ };
412
+ var getBundleItemIsOriginalPrice = (item) => {
413
+ return (item == null ? void 0 : item.price_type) === "markup" && (item == null ? void 0 : item.price_type_ext) === "product_price";
414
+ };
415
+ var getBundleItemIsMarkupPrice = (item) => {
416
+ return (item == null ? void 0 : item.price_type) === "markup" && ((item == null ? void 0 : item.price_type_ext) === "" || !(item == null ? void 0 : item.price_type_ext));
417
+ };
418
+ var getBundleItemIsDiscountPrice = (item) => {
419
+ return (item == null ? void 0 : item.price_type) === "markdown" && ((item == null ? void 0 : item.price_type_ext) === "" || !(item == null ? void 0 : item.price_type_ext));
420
+ };
421
+ var getBundleItemIsMarkupOrDiscountPrice = (item) => {
422
+ return getBundleItemIsMarkupPrice(item) || getBundleItemIsDiscountPrice(item);
423
+ };
424
+ var getDiscountAmount = (discounts) => {
425
+ return (discounts || []).reduce((total, discount) => {
426
+ return total.add(new import_decimal.default(discount.amount || 0));
427
+ }, new import_decimal.default(0)).toNumber();
428
+ };
429
+ var getMainProductTotal = (item) => {
430
+ var _a, _b, _c, _d, _e, _f;
431
+ let total = new import_decimal.default((item == null ? void 0 : item.main_product_selling_price) ?? ((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.main_product_selling_price) ?? item.price ?? 0);
432
+ const discount = ((_c = (_b = item == null ? void 0 : item._origin) == null ? void 0 : _b.product) == null ? void 0 : _c.discount_list) || ((_f = (_e = (_d = item == null ? void 0 : item._originData) == null ? void 0 : _d.product) == null ? void 0 : _e.discount_list) == null ? void 0 : _f.filter((item2) => {
433
+ var _a2;
434
+ return !((_a2 = item2 == null ? void 0 : item2.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
435
+ })) || [];
436
+ const mainProductDiscountAmount = getDiscountAmount(discount);
437
+ total = total.minus(mainProductDiscountAmount);
438
+ if ((item == null ? void 0 : item.option) && Array.isArray(item == null ? void 0 : item.option)) {
439
+ total = total.add(item == null ? void 0 : item.option.reduce((t, option) => {
440
+ return t.add(new import_decimal.default(option.price || 0).mul(option.num || 1));
441
+ }, new import_decimal.default(0)));
442
+ }
443
+ for (let bundleItem of (item == null ? void 0 : item.bundle) || []) {
444
+ if (getBundleItemIsMarkupOrDiscountPrice(bundleItem)) {
445
+ const discountAmount = getDiscountAmount(bundleItem.discount_list);
446
+ const bundleItemPrice = new import_decimal.default(bundleItem.bundle_selling_price ?? bundleItem.price ?? 0);
447
+ const bundleItemTotal = bundleItemPrice.minus(discountAmount);
448
+ total = total.add(bundleItemTotal);
449
+ }
450
+ }
451
+ return total.toNumber();
452
+ };
453
+ var isProductMatchSurchargeCondition = (item, options) => {
454
+ const isInScheduleByDate = options == null ? void 0 : options.isInScheduleByDate;
455
+ const { surchargeConfig, scheduleById = {} } = options;
456
+ const { startDate, product_id, isCustomItem } = item;
457
+ const {
458
+ open_product = 0,
459
+ // 1 | 0
460
+ is_all = 0,
461
+ // 0 | 1(所有商品)
462
+ available_product_ids = [],
463
+ // 指定商品
464
+ open_schedule = 0,
465
+ // 1 | 0
466
+ schedule_type,
467
+ // custom | all(所有日程)
468
+ available_schedule_ids = []
469
+ } = surchargeConfig;
470
+ let isProductMatch = false;
471
+ if (open_product === 0) {
472
+ isProductMatch = true;
473
+ } else {
474
+ if (is_all === 1) {
475
+ isProductMatch = true;
476
+ } else {
477
+ if (isCustomItem) {
478
+ isProductMatch = false;
479
+ } else {
480
+ isProductMatch = available_product_ids.includes(product_id);
481
+ }
482
+ }
483
+ }
484
+ let isScheduleMatch = false;
485
+ if (open_schedule === 0) {
486
+ isScheduleMatch = true;
487
+ } else {
488
+ if (schedule_type === "all") {
489
+ isScheduleMatch = true;
490
+ } else {
491
+ for (let j = 0; j < available_schedule_ids.length; j++) {
492
+ const scheduleId = available_schedule_ids[j];
493
+ const schedule = scheduleById[scheduleId];
494
+ if (schedule) {
495
+ const isInSchedule = isInScheduleByDate({
496
+ // date: startDate?.format('YYYY-MM-DD HH:mm:ss') || '',
497
+ date: startDate || "",
498
+ schedule
499
+ });
500
+ if (isInSchedule) {
501
+ isScheduleMatch = true;
502
+ break;
503
+ }
504
+ }
505
+ }
506
+ }
507
+ }
508
+ return isProductMatch && isScheduleMatch;
509
+ };
510
+ var getSurcharge = ({ service, addons, bookingDetail, bookingId }, options) => {
511
+ var _a, _b, _c;
512
+ const { isEdit, isInScheduleByDate, surcharge_list, scheduleById } = options;
513
+ if (!isEdit && bookingId) {
514
+ if (Array.isArray(bookingDetail == null ? void 0 : bookingDetail.surcharge)) {
515
+ return ((bookingDetail == null ? void 0 : bookingDetail.surcharge) || []).filter((d) => Number(d.amount) > 0).map(
516
+ (d, index) => {
517
+ return {
518
+ key: `custom_surcharge_${index}`,
519
+ // label:
520
+ // d.name[state.locale || 'en'] ||
521
+ // d.name['en'] ||
522
+ // d.name['original'],
523
+ label: d.name,
524
+ value: d.amount,
525
+ name: d.name,
526
+ surcharge_id: d.id,
527
+ description: d.description,
528
+ // 后端说这个固定传 default
529
+ type: "default",
530
+ fixed: d.fixed,
531
+ amount: d.amount,
532
+ percentage: d.percentage
533
+ };
534
+ }
535
+ );
536
+ }
537
+ return [];
538
+ }
539
+ resetItemsSurchargeSideEffects({ service, addons });
540
+ if (!Array.isArray(surcharge_list) || !(surcharge_list == null ? void 0 : surcharge_list.length))
541
+ return [];
542
+ const surchargeWithAmount = [];
543
+ for (let i = 0; i < surcharge_list.length; i++) {
544
+ const surchargeConfig = surcharge_list[i];
545
+ const {
546
+ percentage = "0",
547
+ fixed = "0",
548
+ id,
549
+ type,
550
+ description,
551
+ open_product
552
+ } = surchargeConfig;
553
+ const matchedItems = [];
554
+ if (Array.isArray(service)) {
555
+ for (let item of service) {
556
+ if (isProductMatchSurchargeCondition(
557
+ {
558
+ isCustomItem: item.isCustomItem,
559
+ startDate: (item == null ? void 0 : item.startDate) || "",
560
+ product_id: item.id
561
+ },
562
+ {
563
+ surchargeConfig,
564
+ scheduleById: scheduleById || {},
565
+ isInScheduleByDate
566
+ }
567
+ )) {
568
+ const total = getMainProductTotal({
569
+ ...item,
570
+ option: (item == null ? void 0 : item.option) || (item == null ? void 0 : item.options) || [],
571
+ bundle: (item == null ? void 0 : item.bundle) || (item == null ? void 0 : item.bundles) || []
572
+ });
573
+ const mainQuantity = item.num || 1;
574
+ matchedItems.push({
575
+ total,
576
+ // addTimeTotal: getRelationDetailsTotal(
577
+ // item._extend.relation_details || []
578
+ // ),
579
+ isMain: true,
580
+ quantity: mainQuantity,
581
+ item,
582
+ mainQuantity
583
+ // 主商品的mainQuantity等于自己的quantity
584
+ });
585
+ }
586
+ const arr = (item == null ? void 0 : item.bundle) || (item == null ? void 0 : item.bundles) || [];
587
+ for (let bundleItem of arr) {
588
+ if (getBundleItemIsOriginalPrice(bundleItem) && isProductMatchSurchargeCondition(
589
+ {
590
+ isCustomItem: false,
591
+ startDate: item.startDate,
592
+ product_id: bundleItem._bundle_product_id
593
+ },
594
+ {
595
+ surchargeConfig,
596
+ scheduleById: scheduleById || {},
597
+ isInScheduleByDate
598
+ }
599
+ )) {
600
+ const mainQuantity = item.num || 1;
601
+ matchedItems.push({
602
+ isMain: false,
603
+ total: Number(
604
+ bundleItem.bundle_selling_price ?? bundleItem.price ?? 0
605
+ ),
606
+ quantity: bundleItem.num || bundleItem.quantity || 1,
607
+ item: bundleItem,
608
+ mainQuantity
609
+ // 子商品的mainQuantity是所属主商品的quantity
610
+ });
611
+ }
612
+ }
613
+ for (let relationDetail of item.relation_details || []) {
614
+ if (isProductMatchSurchargeCondition(
615
+ {
616
+ isCustomItem: false,
617
+ startDate: item.startDate,
618
+ product_id: relationDetail.product_id
619
+ },
620
+ {
621
+ surchargeConfig,
622
+ scheduleById: scheduleById || {},
623
+ isInScheduleByDate
624
+ }
625
+ )) {
626
+ matchedItems.push({
627
+ isMain: false,
628
+ total: Number(
629
+ (relationDetail == null ? void 0 : relationDetail.price) || ((_a = relationDetail == null ? void 0 : relationDetail.metadata) == null ? void 0 : _a.main_product_attached_bundle_selling_price)
630
+ ),
631
+ quantity: relationDetail.num || relationDetail.quantity || 1,
632
+ item: relationDetail,
633
+ mainQuantity: 1
634
+ // 加时商品不受主商品数量影响
635
+ });
636
+ }
637
+ }
638
+ }
639
+ }
640
+ if (Array.isArray(addons == null ? void 0 : addons.value)) {
641
+ for (let item of addons.value) {
642
+ if (isProductMatchSurchargeCondition(item, {
643
+ surchargeConfig,
644
+ scheduleById: scheduleById || {},
645
+ isInScheduleByDate
646
+ })) {
647
+ matchedItems.push({
648
+ isMain: true,
649
+ total: Number(item.total),
650
+ quantity: item.num || 1,
651
+ item,
652
+ mainQuantity: 1
653
+ // addons独立商品,mainQuantity为1
654
+ });
655
+ }
656
+ }
657
+ }
658
+ let finalAmount = 0;
659
+ const productCount = matchedItems.reduce((total, item) => {
660
+ if (item.isMain) {
661
+ return total + (item.num || item.quantity || 1);
662
+ } else {
663
+ return total + (item.num || item.quantity || 1) * (item.mainQuantity || 1);
664
+ }
665
+ }, 0);
666
+ let fixedTotal = new import_decimal.default(0);
667
+ let productTotalSurcharge = new import_decimal.default(0);
668
+ if (matchedItems.length > 0) {
669
+ let configTotal = new import_decimal.default(0);
670
+ if (fixed && new import_decimal.default(fixed).gt(0)) {
671
+ configTotal = configTotal.plus(fixed);
672
+ }
673
+ if (percentage && new import_decimal.default(percentage).gt(0) || fixed) {
674
+ const percentageRate = new import_decimal.default(percentage);
675
+ for (let [index, item] of matchedItems.entries()) {
676
+ const isLast = index === matchedItems.length - 1;
677
+ const itemPrice = new import_decimal.default(Math.max(0, item.total || 0));
678
+ const itemQuantity = new import_decimal.default(item.isMain ? item.quantity || 1 : item.quantity * item.mainQuantity || 1);
679
+ const addTimeTotal = new import_decimal.default(item.addTimeTotal || 0);
680
+ const itemTotalSurcharge = itemPrice.times(itemQuantity).plus(addTimeTotal).times(percentageRate);
681
+ configTotal = configTotal.plus(itemTotalSurcharge);
682
+ const itemSurcharge = itemPrice.times(percentageRate);
683
+ const fixedSurcharge = new import_decimal.default(1).div(productCount).times(fixed).times(100).floor().div(100).toFixed(2);
684
+ const quantity = item.isMain ? item.quantity : item.quantity * item.mainQuantity;
685
+ fixedTotal = fixedTotal.plus(new import_decimal.default(fixedSurcharge).times(quantity).toNumber());
686
+ if (isLast && fixedTotal.gt(0)) {
687
+ const remaining = new import_decimal.default(fixed).minus(fixedTotal).toNumber();
688
+ if (remaining > 0) {
689
+ productTotalSurcharge = productTotalSurcharge.plus(remaining);
690
+ if (item.isMain) {
691
+ item.item.surcharge_rounding_remainder = remaining;
692
+ } else {
693
+ item.item.surcharge_rounding_remainder = remaining;
694
+ }
695
+ }
696
+ }
697
+ if (open_product !== 0) {
698
+ if (item.isMain) {
699
+ const originSurchargeFee = new import_decimal.default(item.item.surcharge_fee || 0);
700
+ const surchargeFee = originSurchargeFee.plus(itemSurcharge).plus(fixedSurcharge).toDecimalPlaces(2, import_decimal.default.ROUND_DOWN).toNumber();
701
+ productTotalSurcharge = productTotalSurcharge.plus(new import_decimal.default(surchargeFee).times(itemQuantity).toNumber());
702
+ item.item.surcharge_fee = surchargeFee;
703
+ item.item.relation_surcharge_ids = [...((_b = item.item) == null ? void 0 : _b.relation_surcharge_ids) || [], id];
704
+ } else {
705
+ const originSurchargeFee = new import_decimal.default(item.item.surcharge_fee || 0);
706
+ const surchargeFee = originSurchargeFee.plus(itemSurcharge).plus(fixedSurcharge).toDecimalPlaces(2, import_decimal.default.ROUND_DOWN).toNumber();
707
+ productTotalSurcharge = productTotalSurcharge.plus(new import_decimal.default(surchargeFee).times(itemQuantity).toNumber());
708
+ item.item.surcharge_fee = surchargeFee;
709
+ item.item.relation_surcharge_ids = [...((_c = item.item) == null ? void 0 : _c.relation_surcharge_ids) || [], id];
710
+ }
711
+ }
712
+ if (isLast && configTotal.minus(productTotalSurcharge).gt(0)) {
713
+ const configTotalRounded = configTotal.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
714
+ if (item.isMain) {
715
+ let newRoundingRemainder = new import_decimal.default(item.item.surcharge_rounding_remainder || 0);
716
+ newRoundingRemainder = newRoundingRemainder.plus(configTotalRounded.minus(productTotalSurcharge));
717
+ item.item.surcharge_rounding_remainder = newRoundingRemainder.toNumber();
718
+ } else {
719
+ let newRoundingRemainder = new import_decimal.default(item.item.surcharge_rounding_remainder || 0);
720
+ newRoundingRemainder = newRoundingRemainder.plus(configTotalRounded.minus(productTotalSurcharge));
721
+ item.item.surcharge_rounding_remainder = newRoundingRemainder.toNumber();
722
+ }
723
+ }
724
+ }
725
+ }
726
+ finalAmount = configTotal.toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP).toNumber();
727
+ }
728
+ const name = surchargeConfig.name || {};
729
+ if (Number(finalAmount) <= 0) {
730
+ continue;
731
+ }
732
+ surchargeWithAmount.push({
733
+ key: `custom_surcharge_${i}`,
734
+ // label: name[state.locale || 'en'] || name['en'] || name['original'],
735
+ label: name,
736
+ name,
737
+ value: finalAmount,
738
+ surcharge_id: id,
739
+ description,
740
+ // 后端说固定传 default
741
+ type: "default",
742
+ fixed,
743
+ amount: finalAmount,
744
+ percentage,
745
+ metadata: {
746
+ open_product
747
+ }
748
+ });
749
+ }
750
+ return surchargeWithAmount;
751
+ };
752
+ function resetItemsSurchargeSideEffects({ service, addons }) {
753
+ const resetItem = (item) => {
754
+ if (!item)
755
+ return;
756
+ item.surcharge_fee = 0;
757
+ item.surcharge_rounding_remainder = 0;
758
+ item.relation_surcharge_ids = [];
759
+ };
760
+ if (Array.isArray(service)) {
761
+ for (let item of service) {
762
+ resetItem(item);
763
+ const arr = (item == null ? void 0 : item.bundle) || (item == null ? void 0 : item.bundles) || [];
764
+ for (let bundleItem of arr)
765
+ resetItem(bundleItem);
766
+ for (let relationDetail of (item == null ? void 0 : item.relation_details) || [])
767
+ resetItem(relationDetail);
768
+ }
769
+ }
770
+ if (Array.isArray(addons == null ? void 0 : addons.value)) {
771
+ for (let item of addons.value)
772
+ resetItem(item);
773
+ }
774
+ }
124
775
  // Annotate the CommonJS export names for ESM import in node:
125
776
  0 && (module.exports = {
777
+ calcDiscountListDifference,
126
778
  calculateDeposit,
127
779
  calculatePriceDetails,
128
780
  calculateSubtotal,
129
781
  calculateTaxFee,
130
- getBundleDiscountList
782
+ getBundleDiscountList,
783
+ getProductDiscountProductDiscountDifference,
784
+ getSurcharge,
785
+ getSurchargeAmount,
786
+ getTax
131
787
  });