tripstorekrabi-shared 1.9.7 → 1.9.8

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/booking.js CHANGED
@@ -95,7 +95,7 @@ export function addBookingFromInvoice(firestore, admin, invoice, paymentData, co
95
95
  }
96
96
 
97
97
  if (productSnapshot) {
98
- data.operatorNetPrice = calculateTotalPrice(data, productSnapshot.data(), false);
98
+ data.operatorNetPrice = calculateTotalPrice(data, productSnapshot.data(), "operator");
99
99
  } else {
100
100
  data.operatorNetPrice = 0;
101
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.9.7",
3
+ "version": "1.9.8",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
package/price.js CHANGED
@@ -3,7 +3,7 @@ import { getBookingType } from "./utils.js";
3
3
 
4
4
  export const MAX_PAX = 30;
5
5
 
6
- export function calculateProductPrice(booking, pricing, selling = true) {
6
+ export function calculateProductPrice(booking, pricing, mode = "retail") {
7
7
  let type = pricing.type;
8
8
  let total = 0;
9
9
 
@@ -17,9 +17,26 @@ export function calculateProductPrice(booking, pricing, selling = true) {
17
17
 
18
18
  if (volume[bookingType]) {
19
19
  let customPricing = volume[bookingType];
20
- total = booking.adults * (selling ? customPricing.adult.partnerNetPrice : customPricing.adult.operatorNetPrice ?? volume.adult.operatorNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (selling ? customPricing.child.partnerNetPrice : customPricing.child.operatorNetPrice ?? volume.child.operatorNetPrice) : 0);
20
+
21
+ if (mode === "retail") {
22
+ let commissionRate = pricing.commissionRates?.[bookingType];
23
+
24
+ if (commissionRate) {
25
+ total = booking.adults * calculateRetailPrice(commissionRate, customPricing.adult.partnerNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? (booking.children * calculateRetailPrice(commissionRate, customPricing.child.partnerNetPrice)) : 0);
26
+ } else {
27
+ total = booking.adults * customPricing.adult.retailPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (customPricing.child.retailPrice) : 0);
28
+ }
29
+ } else if (mode === "partner") {
30
+ total = booking.adults * (customPricing.adult.partnerNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (customPricing.child.partnerNetPrice) : 0);
31
+ } else if (mode === "operator") {
32
+ 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);
33
+ }
21
34
  } else {
22
- total = booking.adults * (selling ? volume.adult.sellingPrice : volume.adult.operatorNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (selling ? volume.child.sellingPrice : volume.child.operatorNetPrice) : 0);
35
+ if (mode === "retail") {
36
+ total = booking.adults * (volume.adult.sellingPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (volume.child.sellingPrice) : 0);
37
+ } else if (mode === "operator") {
38
+ total = booking.adults * (volume.adult.operatorNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (volume.child.operatorNetPrice) : 0);
39
+ }
23
40
  }
24
41
  break;
25
42
  }
@@ -34,9 +51,26 @@ export function calculateProductPrice(booking, pricing, selling = true) {
34
51
 
35
52
  if (volume[bookingType]) {
36
53
  let customPricing = volume[bookingType];
37
- total = selling ? customPricing.partnerNetPrice : customPricing.operatorNetPrice ?? volume.operatorNetPrice;
54
+
55
+ if (mode === "retail") {
56
+ let commissionRate = pricing.commissionRates?.[bookingType];
57
+
58
+ if (commissionRate) {
59
+ total = calculateRetailPrice(commissionRate, customPricing.partnerNetPrice);
60
+ } else {
61
+ total = customPricing.retailPrice;
62
+ }
63
+ } else if (mode === "partner") {
64
+ total = customPricing.partnerNetPrice
65
+ } else if (mode === "operator") {
66
+ total = customPricing.operatorNetPrice ?? volume.operatorNetPrice;
67
+ }
38
68
  } else {
39
- total = (selling ? volume.sellingPrice : volume.operatorNetPrice);
69
+ if (mode === "retail" || mode === "partner") {
70
+ total = volume.sellingPrice;
71
+ } else if (mode === "operator") {
72
+ total = volume.operatorNetPrice;
73
+ }
40
74
  }
41
75
 
42
76
  break;
@@ -49,6 +83,14 @@ export function calculateProductPrice(booking, pricing, selling = true) {
49
83
  return total;
50
84
  }
51
85
 
86
+ function calculateRetailPrice(commissionRate, price) {
87
+ return Math.floor(
88
+ (price * 100) /
89
+ (100 -
90
+ commissionRate)
91
+ );
92
+ }
93
+
52
94
  export function findVolume(people, pricing) {
53
95
  let total = people.adults + people.children ?? 0;
54
96
  return pricing.volumes.sort((v1, v2) => v1.max - v2.max).find((item) => total <= item.max);
@@ -64,12 +106,12 @@ export function countMaxPeople(pricing) {
64
106
  return volume ? volume.reduce((p, v) => p > v ? p : v) : MAX_PAX;
65
107
  }
66
108
 
67
- export function calculateTotalPrice(data, product, selling = true) {
68
- let total = calculateProductPrice(data, product.pricing, selling);
69
- total += calculateStartEndPointPrice(data, findStartingPoint(product, data.startingPointId, false), selling);
70
- total += calculateStartEndPointPrice(data, findEndPoint(product, data.endPointId, false), selling);
109
+ export function calculateTotalPrice(data, product, mode = "retail") {
110
+ let total = calculateProductPrice(data, product.pricing, mode);
111
+ total += calculateStartEndPointPrice(data, findStartingPoint(product, data.startingPointId, false), mode === "retail" || mode === "partner");
112
+ total += calculateStartEndPointPrice(data, findEndPoint(product, data.endPointId, false), mode === "retail" || mode === "partner");
71
113
  total += calculateNationalParkFee(data, product);
72
- total += calculateAddOnsPrice(data, product, selling);
114
+ total += calculateAddOnsPrice(data, product, mode === "retail" || mode === "partner");
73
115
  return total;
74
116
  }
75
117
 
package/utils.js CHANGED
@@ -91,7 +91,7 @@ export function formatDuration(minutes) {
91
91
  }
92
92
 
93
93
  export function getPickuptimeSummary(pickupZones) {
94
- if (pickupZones && pickupZones.length>0) {
94
+ if (pickupZones && pickupZones.length > 0) {
95
95
  let pickupTimesArray = [];
96
96
  pickupZones.forEach((pickupZone) => {
97
97
  let pickupLength = pickupZone.indexOf(": ");