tripstorekrabi-shared 1.13.64 → 1.13.66

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 (2) hide show
  1. package/package.json +1 -1
  2. package/price.js +71 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.13.64",
3
+ "version": "1.13.66",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
package/price.js CHANGED
@@ -311,6 +311,34 @@ export function getExtraPaymentsPaidWithPartner(booking, product) {
311
311
  });
312
312
  }
313
313
 
314
+ export function getActiveExtraPayments(extraPayments) {
315
+ return extraPayments
316
+ .filter(([id, item]) => !item.disabled)
317
+ .sort(([id1, item1], [id2, item2]) =>
318
+ item1.description.localeCompare(item2.description)
319
+ );
320
+ }
321
+
322
+ export function getExtraPaymentsOperatorPrice(extraPayments) {
323
+ return getActiveExtraPayments(extraPayments).reduce(
324
+ (prev, curr) => {
325
+ let price;
326
+ let item = curr[1];
327
+ if (item.paymentMethod === PAY_ON_TOUR) {
328
+ let ourCommission = item.price - item.operatorPrice;
329
+ if (ourCommission > 0) {
330
+ price = -ourCommission;
331
+ }
332
+ } else {
333
+ price = item.operatorPrice;
334
+ }
335
+
336
+ return prev + price;
337
+ },
338
+ 0
339
+ );
340
+ }
341
+
314
342
  export function calculateAddOnsPrice(booking, product, options = { mode: SELLING_MODE }) {
315
343
  let total = 0;
316
344
  let isSelling = options.mode in [SELLING_MODE, PARTNER_MODE];
@@ -369,4 +397,47 @@ export function findAddOn(id, addOns) {
369
397
 
370
398
  export function inRange(expected, actual, boundary = 10) {
371
399
  return ((expected - boundary) <= actual) && ((expected + boundary) >= actual);
400
+ }
401
+
402
+ export function calculatePhayunPrices(booking, product) {
403
+ let productPrice;
404
+ let cashOnTourAmount = 0;
405
+ let activeExtraPayments = getActiveExtraPayments(Object.entries(booking.extraPayments));
406
+
407
+ if (
408
+ product.operator.default.invoicing?.operator?.create &&
409
+ product.operator.default.invoicing?.operator?.partners?.[
410
+ bookingType
411
+ ]?.include
412
+ ) {
413
+ productPrice = booking.partnerNetPrice;
414
+ } else {
415
+ productPrice = booking.operatorNetPrice;
416
+ }
417
+
418
+ (product.addOns ?? []).forEach((addOnDef) => {
419
+ let addOn = booking.addOns?.[addOnDef.id];
420
+ let pax =
421
+ (addOn?.adults ?? 0) +
422
+ (addOn?.children ?? 0);
423
+
424
+ if (pax > 0) {
425
+ let addOnPrice =
426
+ addOn.adults * addOnDef.pricing.adult.operatorNetPrice +
427
+ (addOn.children ?? 0) *
428
+ addOnDef.pricing.child.operatorNetPrice;
429
+
430
+ // check for extra payment
431
+ let extraPayment = activeExtraPayments.find(([id]) => id === addOnDef.id)?.[1];
432
+ if (!extraPayment) { // add-on paid via partner
433
+ productPrice -= addOnPrice;
434
+ } else if (extraPayment.paymentMethod === PAY_BEFORE) { // add-on paid via extra payment
435
+ productPrice -= addOnPrice;
436
+ } else if (extraPayment.paymentMethod === PAY_ON_TOUR) { // add-on is paid on tour
437
+ cashOnTourAmount += addOnPrice;
438
+ }
439
+ }
440
+ });
441
+
442
+ return { productPrice, cashOnTourAmount };
372
443
  }