tripstorekrabi-shared 1.13.32 → 1.13.34

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 +69 -52
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.13.32",
3
+ "version": "1.13.34",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
package/price.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import { getBookingType } from "./utils.js";
2
2
 
3
+ export const SELLING_PAYMENT_MODE = 0;
4
+ export const PARTNER_PAYMENT_MODE = 1;
5
+ export const OPERATOR_PAYMENT_MODE = 2;
3
6
  export const MAX_PAX = 30;
4
7
  export const PAY_ON_TOUR = 'Pay on tour';
5
8
  export const ALREADY_PAID = "Already paid";
@@ -17,7 +20,14 @@ export const EXTRA_PAYMENT_CATEGORIES = [STARTING_POINT_EXTRA_PAYMENT,
17
20
  ADD_ON_EXTRA_PAYMENT,
18
21
  OTHER_EXTRA_PAYMENT];
19
22
 
20
- export function calculateProductPrice(booking, pricing, mode = "retail") {
23
+ export function calculateProductPrice(booking, pricing, options = { paymentMode: SELLING_PAYMENT_MODE }) {
24
+ function calculateRetailPrice(commissionRate, price) {
25
+ return Math.floor(
26
+ (price * 100) /
27
+ (100 -
28
+ commissionRate)
29
+ );
30
+ }
21
31
  let type = pricing.type;
22
32
  let total = 0;
23
33
 
@@ -33,24 +43,24 @@ export function calculateProductPrice(booking, pricing, mode = "retail") {
33
43
  if (volume[bookingType]) {
34
44
  let customPricing = volume[bookingType];
35
45
 
36
- if (mode === "retail") {
46
+ if (options.paymentMode === SELLING_PAYMENT_MODE) {
37
47
  let commissionRate = pricing.commissionRates?.[bookingType];
38
48
 
39
49
  if (commissionRate && commissionRate > 0) {
40
50
  total = booking.adults * calculateRetailPrice(commissionRate, customPricing.adult.partnerNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? (booking.children * calculateRetailPrice(commissionRate, customPricing.child.partnerNetPrice)) : 0);
41
51
  } else {
42
- total = booking.adults * customPricing.adult.retailPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (customPricing.child.retailPrice) : 0);
52
+ total = booking.adults * customPricing.adult.retailPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * customPricing.child.retailPrice : 0);
43
53
  }
44
- } else if (mode === "partner") {
45
- total = booking.adults * (customPricing.adult.partnerNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (customPricing.child.partnerNetPrice) : 0);
46
- } else if (mode === "operator") {
54
+ } else if (options.paymentMode === PARTNER_PAYMENT_MODE) {
55
+ total = booking.adults * customPricing.adult.partnerNetPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * customPricing.child.partnerNetPrice : 0);
56
+ } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
47
57
  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);
48
58
  }
49
59
  } else {
50
- if (mode === "retail" || mode === "partner") {
51
- total = booking.adults * (volume.adult.sellingPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (volume.child.sellingPrice) : 0);
52
- } else if (mode === "operator") {
53
- total = booking.adults * (volume.adult.operatorNetPrice) + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * (volume.child.operatorNetPrice) : 0);
60
+ if (options.paymentMode === SELLING_PAYMENT_MODE || options.paymentMode === PARTNER_PAYMENT_MODE) {
61
+ total = booking.adults * volume.adult.sellingPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * volume.child.sellingPrice : 0);
62
+ } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
63
+ total = booking.adults * volume.adult.operatorNetPrice + (pricing.ageBands.child.active && volume.child && booking.children ? booking.children * volume.child.operatorNetPrice : 0);
54
64
  }
55
65
  }
56
66
  break;
@@ -68,7 +78,7 @@ export function calculateProductPrice(booking, pricing, mode = "retail") {
68
78
  if (volume[bookingType]) {
69
79
  let customPricing = volume[bookingType];
70
80
 
71
- if (mode === "retail") {
81
+ if (options.paymentMode === SELLING_PAYMENT_MODE) {
72
82
  let commissionRate = pricing.commissionRates?.[bookingType];
73
83
 
74
84
  if (commissionRate && commissionRate > 0) {
@@ -76,15 +86,15 @@ export function calculateProductPrice(booking, pricing, mode = "retail") {
76
86
  } else {
77
87
  total = customPricing.retailPrice;
78
88
  }
79
- } else if (mode === "partner") {
89
+ } else if (options.paymentMode === PARTNER_PAYMENT_MODE) {
80
90
  total = customPricing.partnerNetPrice
81
- } else if (mode === "operator") {
91
+ } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
82
92
  total = customPricing.operatorNetPrice ?? volume.operatorNetPrice;
83
93
  }
84
94
  } else {
85
- if (mode === "retail" || mode === "partner") {
95
+ if (options.paymentMode === SELLING_PAYMENT_MODE || options.paymentMode === PARTNER_PAYMENT_MODE) {
86
96
  total = volume.sellingPrice;
87
- } else if (mode === "operator") {
97
+ } else if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
88
98
  total = volume.operatorNetPrice;
89
99
  }
90
100
  }
@@ -99,14 +109,6 @@ export function calculateProductPrice(booking, pricing, mode = "retail") {
99
109
  return total;
100
110
  }
101
111
 
102
- function calculateRetailPrice(commissionRate, price) {
103
- return Math.floor(
104
- (price * 100) /
105
- (100 -
106
- commissionRate)
107
- );
108
- }
109
-
110
112
  export function findVolume(people, pricing) {
111
113
  let total = people.adults + people.children ?? 0;
112
114
  return pricing.volumes.sort((v1, v2) => v1.max - v2.max).find((item) => total <= item.max);
@@ -128,7 +130,7 @@ export function getTotalAmountToBeReceived(booking) {
128
130
  if (booking.customerPaymentStatus === "Refunded") {
129
131
  total = 0;
130
132
  } else {
131
- total = booking.partnerNetPrice + calculateExtraPayments(booking, true);
133
+ total = booking.partnerNetPrice + calculateExtraPayments(booking);
132
134
  }
133
135
 
134
136
  return total;
@@ -143,18 +145,28 @@ export function calculatePartnerNetPrice(booking, product) {
143
145
  calculateProductPrice(
144
146
  booking,
145
147
  product.pricing,
146
- 'partner'
148
+ {
149
+ paymentMode: PARTNER_PAYMENT_MODE,
150
+ }
147
151
  )
148
152
  );
149
153
  }
150
154
 
151
- export function calculateTotalPrice(booking, product, mode = "retail") {
152
- let total = calculateProductPrice(booking, product.pricing, mode);
153
- total += calculateStartEndPointPrice(booking, findStartingPoint(product, booking.startingPointId, false), mode === "retail" || mode === "partner");
154
- total += calculateStartEndPointPrice(booking, findEndPoint(product, booking.endPointId, false), mode === "retail" || mode === "partner");
155
+ export function calculateTotalOperatorPrice(booking, product) {
156
+ return calculateTotalPrice(booking, product, options = { paymentMode: OPERATOR_PAYMENT_MODE });
157
+ }
158
+
159
+ export function calculateTotalPartnerNetPrice(booking, product) {
160
+ return calculateTotalPrice(booking, product, options = { paymentMode: PARTNER_PAYMENT_MODE });
161
+ }
162
+
163
+ export function calculateTotalPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE }) {
164
+ let total = calculateProductPrice(booking, product.pricing, options);
165
+ total += calculateStartEndPointPrice(booking, findStartingPoint(product, booking.startingPointId, false), options);
166
+ total += calculateStartEndPointPrice(booking, findEndPoint(product, booking.endPointId, false), options);
155
167
  total += calculateNationalParkFee(booking, product);
156
- total += calculateExtraPayments(booking, mode === "retail" || mode === "partner", true);
157
- total += calculateAddOnsPrice(booking, product, mode === "retail" || mode === "partner");
168
+ total += calculateExtraPayments(booking, { ...options, calculationMode: true });
169
+ total += calculateAddOnsPrice(booking, product, options);
158
170
  return total;
159
171
  }
160
172
 
@@ -162,32 +174,35 @@ export function calculateNationalParkFee(booking, product) {
162
174
  return booking.nationalParkFee === ALREADY_PAID && product.nationalPark ? product.nationalPark.pricing.adult * booking.adults + product.nationalPark.pricing.child * booking.children : 0;
163
175
  }
164
176
 
165
- export function calculateExtraPayments(booking, selling = true, calculationMode = false) {
177
+ export function calculateExtraPayments(booking, options = { paymentMode: SELLING_PAYMENT_MODE, calculationMode: false }) {
178
+ function getPrice(item) {
179
+ switch (options.paymentMode) {
180
+ case SELLING_PAYMENT_MODE:
181
+ case PARTNER_PAYMENT_MODE:
182
+ return item.receivedAmount ? item.receivedAmount : item.price;
183
+ case OPERATOR_PAYMENT_MODE:
184
+ return item.operatoPrice;
185
+ }
186
+ }
187
+
166
188
  let total = 0;
167
189
  for (const item of Object.values(booking.extraPayments).filter((item) => !item.disabled)) {
168
- if (selling) {
169
- if (item.paymentMethod === PAY_ON_TOUR) {
170
- if (calculationMode && item.category !== OTHER_EXTRA_PAYMENT) {
171
- total -= item.price;
172
- }
173
- } else if (!calculationMode || item.category === OTHER_EXTRA_PAYMENT) {
174
- total += item.receivedAmount ? item.receivedAmount : item.price;
190
+ if (item.paymentMethod === PAY_ON_TOUR) {
191
+ if (options.calculationMode && item.category !== OTHER_EXTRA_PAYMENT) {
192
+ total -= getPrice(item, options);
175
193
  }
176
- } else {
177
- if (item.paymentMethod === PAY_ON_TOUR) {
178
- if (calculationMode && item.category !== OTHER_EXTRA_PAYMENT) {
179
- total -= item.operatorPrice;
180
- }
181
194
 
195
+ if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
182
196
  let ourCommission = item.price - item.operatorPrice;
183
197
  if (ourCommission > 0) {
184
198
  total -= ourCommission;
185
199
  }
186
- } else if (!calculationMode || item.category === OTHER_EXTRA_PAYMENT) {
187
- total += item.operatorPrice;
188
200
  }
201
+ } else if (!options.calculationMode || item.category === OTHER_EXTRA_PAYMENT) {
202
+ total += getPrice(item, options);
189
203
  }
190
204
  }
205
+
191
206
  return total;
192
207
  }
193
208
 
@@ -241,14 +256,15 @@ export function getExtraPaymentsPaidWithPartner(booking, product) {
241
256
  );
242
257
  }
243
258
 
244
- export function calculateAddOnsPrice(booking, product, selling = true) {
259
+ export function calculateAddOnsPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE }) {
245
260
  let total = 0;
261
+ let isSelling = options.paymentMode in [SELLING_PAYMENT_MODE, PARTNER_PAYMENT_MODE];
246
262
  for (const [id, addOn] of Object.entries(booking.addOns ?? {})) {
247
263
  let addOnDef = findAddOn(id, product.addOns ?? []);
248
264
  if (addOnDef) {
249
- total += addOn.adults * (selling ? addOnDef.pricing.adult
265
+ total += addOn.adults * (isSelling ? addOnDef.pricing.adult
250
266
  .sellingPrice : addOnDef.pricing.adult
251
- .operatorNetPrice) + (addOn.children > 0 ? addOn.children * (selling ? addOnDef.pricing.child
267
+ .operatorNetPrice) + (addOn.children > 0 ? addOn.children * (isSelling ? addOnDef.pricing.child
252
268
  .sellingPrice : addOnDef.pricing.child
253
269
  .operatorNetPrice) : 0);
254
270
  }
@@ -256,13 +272,14 @@ export function calculateAddOnsPrice(booking, product, selling = true) {
256
272
  return total;
257
273
  }
258
274
 
259
- export function calculateStartEndPointPrice(people, startEndPoint, selling = true) {
275
+ export function calculateStartEndPointPrice(people, startEndPoint, options = { paymentMode: SELLING_PAYMENT_MODE }) {
260
276
  let pricing = startEndPoint?.pricing;
277
+ let isSelling = options.paymentMode in [SELLING_PAYMENT_MODE, PARTNER_PAYMENT_MODE];
261
278
  if (pricing) {
262
279
  if (pricing.type === "group") {
263
- return (selling ? pricing.sellingPrice : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);
280
+ return (isSelling ? pricing.sellingPrice : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);
264
281
  } else if (pricing.type === "person") {
265
- return (selling ? pricing.sellingPrice : pricing.operatorNetPrice) * (people.adults + people.children);
282
+ return (isSelling ? pricing.sellingPrice : pricing.operatorNetPrice) * (people.adults + people.children);
266
283
  }
267
284
  } else {
268
285
  return 0;