tripstorekrabi-shared 1.13.39 → 1.13.41

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 (3) hide show
  1. package/package.json +1 -1
  2. package/price.js +76 -59
  3. package/product.js +2 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.13.39",
3
+ "version": "1.13.41",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
package/price.js CHANGED
@@ -17,23 +17,23 @@ export const EXTRA_PAYMENT_CATEGORIES = [STARTING_POINT_EXTRA_PAYMENT,
17
17
  ADD_ON_EXTRA_PAYMENT,
18
18
  OTHER_EXTRA_PAYMENT];
19
19
 
20
- const SELLING_PAYMENT_MODE = 0;
21
- const PARTNER_PAYMENT_MODE = 1;
22
- const OPERATOR_PAYMENT_MODE = 2;
20
+ const SELLING_MODE = 0;
21
+ const PARTNER_MODE = 1;
22
+ const OPERATOR_MODE = 2;
23
23
 
24
24
  export function calculateProductSellingPrice(booking, pricing) {
25
- return calculateProductPrice(booking, pricing, { paymentMode: SELLING_PAYMENT_MODE });
25
+ return calculateProductPrice(booking, pricing, { mode: SELLING_MODE });
26
26
  }
27
27
 
28
28
  export function calculateProductPartnerNetPrice(booking, pricing) {
29
- return calculateProductPrice(booking, pricing, { paymentMode: PARTNER_PAYMENT_MODE });
29
+ return calculateProductPrice(booking, pricing, { mode: PARTNER_MODE });
30
30
  }
31
31
 
32
32
  export function calculateProductOperatorPrice(booking, pricing) {
33
- return calculateProductPrice(booking, pricing, { paymentMode: OPERATOR_PAYMENT_MODE });
33
+ return calculateProductPrice(booking, pricing, { mode: OPERATOR_MODE });
34
34
  }
35
35
 
36
- export function calculateProductPrice(booking, pricing, options = { paymentMode: SELLING_PAYMENT_MODE }) {
36
+ function calculateProductPrice(booking, pricing, options = { mode: SELLING_MODE }) {
37
37
  function calculateRetailPrice(commissionRate, price) {
38
38
  return Math.floor(
39
39
  (price * 100) /
@@ -56,7 +56,7 @@ export function calculateProductPrice(booking, pricing, options = { paymentMode:
56
56
  if (volume[bookingType]) {
57
57
  let customPricing = volume[bookingType];
58
58
 
59
- if (options.paymentMode === SELLING_PAYMENT_MODE) {
59
+ if (options.mode === SELLING_MODE) {
60
60
  let commissionRate = pricing.commissionRates?.[bookingType];
61
61
 
62
62
  if (commissionRate && commissionRate > 0) {
@@ -64,15 +64,15 @@ export function calculateProductPrice(booking, pricing, options = { paymentMode:
64
64
  } else {
65
65
  total = booking.adults * customPricing.adult.retailPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * customPricing.child.retailPrice : 0);
66
66
  }
67
- } else if (options.paymentMode === PARTNER_PAYMENT_MODE) {
67
+ } else if (options.mode === PARTNER_MODE) {
68
68
  total = booking.adults * customPricing.adult.partnerNetPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * customPricing.child.partnerNetPrice : 0);
69
- } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
69
+ } else if (options.mode === OPERATOR_MODE) {
70
70
  total = booking.adults * (customPricing.adult.operatorNetPrice ?? volume.adult.operatorNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? (booking.children * (customPricing.child.operatorNetPrice ?? volume.child.operatorNetPrice)) : 0);
71
71
  }
72
72
  } else {
73
- if (options.paymentMode === SELLING_PAYMENT_MODE || options.paymentMode === PARTNER_PAYMENT_MODE) {
73
+ if (options.mode === SELLING_MODE || options.mode === PARTNER_MODE) {
74
74
  total = booking.adults * volume.adult.sellingPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * volume.child.sellingPrice : 0);
75
- } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
75
+ } else if (options.mode === OPERATOR_MODE) {
76
76
  total = booking.adults * volume.adult.operatorNetPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * volume.child.operatorNetPrice : 0);
77
77
  }
78
78
  }
@@ -91,7 +91,7 @@ export function calculateProductPrice(booking, pricing, options = { paymentMode:
91
91
  if (volume[bookingType]) {
92
92
  let customPricing = volume[bookingType];
93
93
 
94
- if (options.paymentMode === SELLING_PAYMENT_MODE) {
94
+ if (options.mode === SELLING_MODE) {
95
95
  let commissionRate = pricing.commissionRates?.[bookingType];
96
96
 
97
97
  if (commissionRate && commissionRate > 0) {
@@ -99,15 +99,15 @@ export function calculateProductPrice(booking, pricing, options = { paymentMode:
99
99
  } else {
100
100
  total = customPricing.retailPrice;
101
101
  }
102
- } else if (options.paymentMode === PARTNER_PAYMENT_MODE) {
102
+ } else if (options.mode === PARTNER_MODE) {
103
103
  total = customPricing.partnerNetPrice
104
- } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
104
+ } else if (options.mode === OPERATOR_MODE) {
105
105
  total = customPricing.operatorNetPrice ?? volume.operatorNetPrice;
106
106
  }
107
107
  } else {
108
- if (options.paymentMode === SELLING_PAYMENT_MODE || options.paymentMode === PARTNER_PAYMENT_MODE) {
108
+ if (options.mode === SELLING_MODE || options.mode === PARTNER_MODE) {
109
109
  total = volume.sellingPrice;
110
- } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
110
+ } else if (options.mode === OPERATOR_MODE) {
111
111
  total = volume.operatorNetPrice;
112
112
  }
113
113
  }
@@ -155,34 +155,54 @@ export function calculatePartnerNetPrice(booking, product) {
155
155
  product
156
156
  ).reduce(
157
157
  (prev, curr) => prev + curr.price,
158
- calculateProductPrice(
158
+ calculateProductPartnerNetPrice(
159
159
  booking,
160
- product.pricing,
161
- {
162
- paymentMode: PARTNER_PAYMENT_MODE,
163
- }
160
+ product.pricing
164
161
  )
165
162
  );
166
163
  }
167
164
 
168
165
  export function calculateTotalSellingPrice(booking, product) {
169
- return calculateTotalPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE });
166
+ return calculateTotalPrice(booking, product, options = { mode: SELLING_MODE });
170
167
  }
171
168
 
172
169
  export function calculateTotalOperatorPrice(booking, product) {
173
- return calculateTotalPrice(booking, product, options = { paymentMode: OPERATOR_PAYMENT_MODE });
170
+ return calculateTotalPrice(booking, product, options = { mode: OPERATOR_MODE });
174
171
  }
175
172
 
176
173
  export function calculateTotalPartnerNetPrice(booking, product) {
177
- return calculateTotalPrice(booking, product, options = { paymentMode: PARTNER_PAYMENT_MODE });
174
+ return calculateTotalPrice(booking, product, options = { mode: PARTNER_MODE });
178
175
  }
179
176
 
180
- function calculateTotalPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE }) {
177
+ function calculateTotalPrice(booking, product, options = { mode: SELLING_MODE }) {
181
178
  let total = calculateProductPrice(booking, product.pricing, options);
182
179
  total += calculateStartEndPointPrice(booking, findStartingPoint(product, booking.startingPointId, false), options);
183
180
  total += calculateStartEndPointPrice(booking, findEndPoint(product, booking.endPointId, false), options);
184
181
  total += calculateNationalParkFee(booking, product);
185
- total += calculateExtraPayments(booking, { ...options, calculationMode: true });
182
+ total += calculateExtraPayments(booking, {
183
+ ...options,
184
+ calculationMode: true,
185
+ filter: (item) => {
186
+ if (item.disabled === true) {
187
+ return false;
188
+ } else if (item.paymentMethod === PAY_ON_TOUR) {
189
+ return true;
190
+ } else if (item.paymentMethod === PAY_BEFORE && item.category === OTHER_EXTRA_PAYMENT) {
191
+ return true;
192
+ }
193
+ },
194
+ getPrice: (item, options) => {
195
+ if (item.paymentMethod === PAY_ON_TOUR) {
196
+ if (item.category !== OTHER_EXTRA_PAYMENT) {
197
+ return -getExtraPaymentPrice(item, options);
198
+ } else {
199
+ return 0;
200
+ }
201
+ } else {
202
+ return getExtraPaymentPrice(item, options);
203
+ }
204
+ }
205
+ });
186
206
  total += calculateAddOnsPrice(booking, product, options);
187
207
  return total;
188
208
  }
@@ -191,32 +211,29 @@ export function calculateNationalParkFee(booking, product) {
191
211
  return booking.nationalParkFee === ALREADY_PAID && product.nationalPark ? product.nationalPark.pricing.adult * booking.adults + product.nationalPark.pricing.child * booking.children : 0;
192
212
  }
193
213
 
194
- export function calculateExtraPayments(booking, options = { paymentMode: SELLING_PAYMENT_MODE, calculationMode: false }) {
195
- function getPrice(item) {
196
- switch (options.paymentMode) {
197
- case SELLING_PAYMENT_MODE:
198
- case PARTNER_PAYMENT_MODE:
199
- return item.receivedAmount ? item.receivedAmount : item.price;
200
- case OPERATOR_PAYMENT_MODE:
201
- return item.operatoPrice;
202
- }
214
+ function getExtraPaymentPrice(item, options) {
215
+ switch (options.mode) {
216
+ case SELLING_MODE:
217
+ case PARTNER_MODE:
218
+ return item.receivedAmount ? item.receivedAmount : item.price;
219
+ case OPERATOR_MODE:
220
+ return item.operatoPrice;
203
221
  }
222
+ }
223
+
224
+ export function calculateExtraPayments(booking, options = { mode: SELLING_MODE }) {
225
+ let filter = options.filter ?? ((item) => !item.disabled);
226
+ let getPrice = options.getPrice ?? getExtraPaymentPrice;
204
227
 
205
228
  let total = 0;
206
- for (const item of Object.values(booking.extraPayments).filter((item) => !item.disabled)) {
207
- if (item.paymentMethod === PAY_ON_TOUR) {
208
- if (options.calculationMode && item.category !== OTHER_EXTRA_PAYMENT) {
209
- total -= getPrice(item, options);
210
- }
229
+ for (const item of Object.values(booking.extraPayments).filter(filter)) {
230
+ total += getPrice(item, options);
211
231
 
212
- if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
213
- let ourCommission = item.price - item.operatorPrice;
214
- if (ourCommission > 0) {
215
- total -= ourCommission;
216
- }
232
+ if (item.paymentMethod === PAY_ON_TOUR && options.mode === OPERATOR_MODE) {
233
+ let ourCommission = item.price - item.operatorPrice;
234
+ if (ourCommission > 0) {
235
+ total -= ourCommission;
217
236
  }
218
- } else if (!options.calculationMode || item.category === OTHER_EXTRA_PAYMENT) {
219
- total += getPrice(item, options);
220
237
  }
221
238
  }
222
239
 
@@ -247,14 +264,14 @@ export function getExtraPaymentsPaidWithPartner(booking, product) {
247
264
  {
248
265
  category: STARTING_POINT_EXTRA_PAYMENT,
249
266
  description: startingPoint.name,
250
- price: calculateStartEndPointPrice(booking, startingPoint, { paymentMode: SELLING_PAYMENT_MODE }),
251
- operatorPrice: calculateStartEndPointPrice(booking, startingPoint, { paymentMode: OPERATOR_PAYMENT_MODE }),
267
+ price: calculateStartEndPointSellingPrice(booking, startingPoint),
268
+ operatorPrice: calculateStartEndPointOperatorPrice(booking, startingPoint),
252
269
  },
253
270
  {
254
271
  category: END_POINT_EXTRA_PAYMENT,
255
272
  description: endPoint.name,
256
- price: calculateStartEndPointPrice(booking, endPoint, { paymentMode: SELLING_PAYMENT_MODE }),
257
- operatorPrice: calculateStartEndPointPrice(booking, endPoint, { paymentMode: OPERATOR_PAYMENT_MODE }),
273
+ price: calculateStartEndPointSellingPrice(booking, endPoint),
274
+ operatorPrice: calculateStartEndPointOperatorPrice(booking, endPoint),
258
275
  },
259
276
  {
260
277
  category: NATIONAL_PARK_FEE_EXTRA_PAYMENT,
@@ -273,9 +290,9 @@ export function getExtraPaymentsPaidWithPartner(booking, product) {
273
290
  );
274
291
  }
275
292
 
276
- export function calculateAddOnsPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE }) {
293
+ export function calculateAddOnsPrice(booking, product, options = { mode: SELLING_MODE }) {
277
294
  let total = 0;
278
- let isSelling = options.paymentMode in [SELLING_PAYMENT_MODE, PARTNER_PAYMENT_MODE];
295
+ let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
279
296
  for (const [id, addOn] of Object.entries(booking.addOns ?? {})) {
280
297
  let addOnDef = findAddOn(id, product.addOns ?? []);
281
298
  if (addOnDef) {
@@ -290,20 +307,20 @@ export function calculateAddOnsPrice(booking, product, options = { paymentMode:
290
307
  }
291
308
 
292
309
  export function calculateStartEndPointSellingPrice(people, startEndPoint) {
293
- return calculateStartEndPointPrice(people, startEndPoint, { paymentMode: SELLING_PAYMENT_MODE });
310
+ return calculateStartEndPointPrice(people, startEndPoint, { mode: SELLING_MODE });
294
311
  }
295
312
 
296
313
  export function calculateStartEndPointOperatorPrice(people, startEndPoint) {
297
- return calculateStartEndPointPrice(people, startEndPoint, { paymentMode: OPERATOR_PAYMENT_MODE });
314
+ return calculateStartEndPointPrice(people, startEndPoint, { mode: OPERATOR_MODE });
298
315
  }
299
316
 
300
317
  export function calculateStartEndPointPartnerNetPrice(people, startEndPoint) {
301
- return calculateStartEndPointPrice(people, startEndPoint, { paymentMode: PARTNER_PAYMENT_MODE });
318
+ return calculateStartEndPointPrice(people, startEndPoint, { mode: PARTNER_MODE });
302
319
  }
303
320
 
304
- function calculateStartEndPointPrice(people, startEndPoint, options = { paymentMode: SELLING_PAYMENT_MODE }) {
321
+ function calculateStartEndPointPrice(people, startEndPoint, options = { mode: SELLING_MODE }) {
305
322
  let pricing = startEndPoint?.pricing;
306
- let isSelling = options.paymentMode in [SELLING_PAYMENT_MODE, PARTNER_PAYMENT_MODE];
323
+ let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
307
324
  if (pricing) {
308
325
  if (pricing.type === "group") {
309
326
  return (isSelling ? pricing.sellingPrice : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);
package/product.js CHANGED
@@ -1,4 +1,4 @@
1
- import { calculateProductPrice, countMinPeople } from "./price.js";
1
+ import { calculateProductSellingPrice, countMinPeople } from "./price.js";
2
2
 
3
3
  const MATCH_HTML_REGEXP = /(<([^>]+)>)/ig;
4
4
 
@@ -12,7 +12,7 @@ export function buildIndex(id, product) {
12
12
  heroPhoto: product.heroPhoto,
13
13
  operator: product.operator.default.name,
14
14
  url: product.url,
15
- price: calculateProductPrice({ adults: countMinPeople(product.pricing), children: 0, infants: 0 }, product.pricing) / countMinPeople(product.pricing),
15
+ price: calculateProductSellingPrice({ adults: countMinPeople(product.pricing), children: 0, infants: 0 }, product.pricing) / countMinPeople(product.pricing),
16
16
  priceType: product.pricing.type,
17
17
  availability: product.availability
18
18
  };