tripstorekrabi-shared 1.13.31 → 1.13.33

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 +68 -61
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.13.31",
3
+ "version": "1.13.33",
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,20 @@ 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 calculateTotalPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE }) {
156
+ let total = calculateProductPrice(booking, product.pricing, options);
157
+ total += calculateStartEndPointPrice(booking, findStartingPoint(product, booking.startingPointId, false), options);
158
+ total += calculateStartEndPointPrice(booking, findEndPoint(product, booking.endPointId, false), options);
155
159
  total += calculateNationalParkFee(booking, product);
156
- total += calculateExtraPayments(booking, mode === "retail" || mode === "partner", true);
157
- total += calculateAddOnsPrice(booking, product, mode === "retail" || mode === "partner");
160
+ total += calculateExtraPayments(booking, { ...options, calculationMode: true });
161
+ total += calculateAddOnsPrice(booking, product, options);
158
162
  return total;
159
163
  }
160
164
 
@@ -162,32 +166,35 @@ export function calculateNationalParkFee(booking, product) {
162
166
  return booking.nationalParkFee === ALREADY_PAID && product.nationalPark ? product.nationalPark.pricing.adult * booking.adults + product.nationalPark.pricing.child * booking.children : 0;
163
167
  }
164
168
 
165
- export function calculateExtraPayments(booking, selling = true, calculationMode = false) {
169
+ export function calculateExtraPayments(booking, options = { paymentMode: SELLING_PAYMENT_MODE, calculationMode: false }) {
170
+ function getPrice(item) {
171
+ switch (options.paymentMode) {
172
+ case SELLING_PAYMENT_MODE:
173
+ case PARTNER_PAYMENT_MODE:
174
+ return item.receivedAmount ? item.receivedAmount : item.price;
175
+ case OPERATOR_PAYMENT_MODE:
176
+ return item.operatoPrice;
177
+ }
178
+ }
179
+
166
180
  let total = 0;
167
181
  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;
182
+ if (item.paymentMethod === PAY_ON_TOUR) {
183
+ if (options.calculationMode && item.category !== OTHER_EXTRA_PAYMENT) {
184
+ total -= getPrice(item, options);
175
185
  }
176
- } else {
177
- if (item.paymentMethod === PAY_ON_TOUR) {
178
- if (calculationMode && item.category !== OTHER_EXTRA_PAYMENT) {
179
- total -= item.operatorPrice;
180
- }
181
186
 
187
+ if (options.paymentMode === OPERATOR_PAYMENT_MODE) {
182
188
  let ourCommission = item.price - item.operatorPrice;
183
189
  if (ourCommission > 0) {
184
190
  total -= ourCommission;
185
191
  }
186
- } else if (!calculationMode || item.category === OTHER_EXTRA_PAYMENT) {
187
- total += item.operatorPrice;
188
192
  }
193
+ } else if (!options.calculationMode || item.category === OTHER_EXTRA_PAYMENT) {
194
+ total += getPrice(item, options);
189
195
  }
190
196
  }
197
+
191
198
  return total;
192
199
  }
193
200
 
@@ -198,7 +205,7 @@ export function getExtraPaymentsPaidWithPartner(booking, product) {
198
205
  (product.addOns ?? []).forEach((addOn) => {
199
206
  addOnDefs[addOn.id] = addOn;
200
207
  });
201
- let addOns = Object.entries(booking.addOns).filter(([id]) => addOnDefs[id] != null).map(([id, addOn]) => {
208
+ let addOns = Object.entries(booking.addOns ?? {}).filter(([id]) => addOnDefs[id] != null).map(([id, addOn]) => {
202
209
  let addOnDef = addOnDefs[id];
203
210
 
204
211
  return {
@@ -241,30 +248,30 @@ export function getExtraPaymentsPaidWithPartner(booking, product) {
241
248
  );
242
249
  }
243
250
 
244
- export function calculateAddOnsPrice(booking, product, selling = true) {
251
+ export function calculateAddOnsPrice(booking, product, options = { paymentMode: SELLING_PAYMENT_MODE }) {
245
252
  let total = 0;
246
- if (booking.addOns) {
247
- for (const [id, addOn] of Object.entries(booking.addOns)) {
248
- let addOnDef = findAddOn(id, product.addOns);
249
- if (addOnDef) {
250
- total += addOn.adults * (selling ? addOnDef.pricing.adult
251
- .sellingPrice : addOnDef.pricing.adult
252
- .operatorNetPrice) + (addOn.children > 0 ? addOn.children * (selling ? addOnDef.pricing.child
253
- .sellingPrice : addOnDef.pricing.child
254
- .operatorNetPrice) : 0);
255
- }
253
+ let isSelling = options.paymentMode in [SELLING_PAYMENT_MODE, PARTNER_PAYMENT_MODE];
254
+ for (const [id, addOn] of Object.entries(booking.addOns ?? {})) {
255
+ let addOnDef = findAddOn(id, product.addOns ?? []);
256
+ if (addOnDef) {
257
+ total += addOn.adults * (isSelling ? addOnDef.pricing.adult
258
+ .sellingPrice : addOnDef.pricing.adult
259
+ .operatorNetPrice) + (addOn.children > 0 ? addOn.children * (isSelling ? addOnDef.pricing.child
260
+ .sellingPrice : addOnDef.pricing.child
261
+ .operatorNetPrice) : 0);
256
262
  }
257
263
  }
258
264
  return total;
259
265
  }
260
266
 
261
- export function calculateStartEndPointPrice(people, startEndPoint, selling = true) {
267
+ export function calculateStartEndPointPrice(people, startEndPoint, options = { paymentMode: SELLING_PAYMENT_MODE }) {
262
268
  let pricing = startEndPoint?.pricing;
269
+ let isSelling = options.paymentMode in [SELLING_PAYMENT_MODE, PARTNER_PAYMENT_MODE];
263
270
  if (pricing) {
264
271
  if (pricing.type === "group") {
265
- return (selling ? pricing.sellingPrice : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);
272
+ return (isSelling ? pricing.sellingPrice : pricing.operatorNetPrice) * Math.ceil((people.adults + people.children) / pricing.max);
266
273
  } else if (pricing.type === "person") {
267
- return (selling ? pricing.sellingPrice : pricing.operatorNetPrice) * (people.adults + people.children);
274
+ return (isSelling ? pricing.sellingPrice : pricing.operatorNetPrice) * (people.adults + people.children);
268
275
  }
269
276
  } else {
270
277
  return 0;