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