sahab-utils 1.4.0 → 1.7.0
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/README.md +86 -0
- package/dist/constants/types.d.ts +963 -749
- package/dist/constants/types.js +102 -82
- package/dist/index.d.ts +4 -2
- package/dist/index.js +20 -18
- package/dist/ts/calcs.d.ts +34 -34
- package/dist/ts/calcs.js +295 -385
- package/dist/ts/gifts.d.ts +97 -0
- package/dist/ts/gifts.js +201 -0
- package/dist/ts/loyalty.d.ts +79 -0
- package/dist/ts/loyalty.js +222 -0
- package/dist/ts/utils.d.ts +3 -3
- package/dist/ts/utils.js +24 -24
- package/package.json +37 -36
package/dist/ts/calcs.js
CHANGED
|
@@ -1,385 +1,295 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.CLIENT_SAHAB_PERCENTAGE = exports.ONLINE_SAHAB_PERCENTAGE = exports.SAHAB_PERCENTAGE = void 0;
|
|
7
|
-
exports.getServicesTotal = getServicesTotal;
|
|
8
|
-
exports.getSahabDiscountsAmount = getSahabDiscountsAmount;
|
|
9
|
-
exports.getProviderDiscountsAmount = getProviderDiscountsAmount;
|
|
10
|
-
exports.getOrderTotalDiscountsAmount = getOrderTotalDiscountsAmount;
|
|
11
|
-
exports.getCustomerTotalToPay = getCustomerTotalToPay;
|
|
12
|
-
exports.getProviderTransferAmount = getProviderTransferAmount;
|
|
13
|
-
exports.getProviderProfits = getProviderProfits;
|
|
14
|
-
exports.getOnlineProcessingFees = getOnlineProcessingFees;
|
|
15
|
-
exports.getSahabFees = getSahabFees;
|
|
16
|
-
exports.calcOrdersData = calcOrdersData;
|
|
17
|
-
exports.calculatePromoCodeAmount = calculatePromoCodeAmount;
|
|
18
|
-
exports.calculateOrderPercentage = calculateOrderPercentage;
|
|
19
|
-
const sum_1 = __importDefault(require("lodash/sum"));
|
|
20
|
-
const types_1 = require("../constants/types");
|
|
21
|
-
const utils_1 = require("./utils");
|
|
22
|
-
exports.SAHAB_PERCENTAGE = 0;
|
|
23
|
-
exports.ONLINE_SAHAB_PERCENTAGE = 0.03;
|
|
24
|
-
exports.CLIENT_SAHAB_PERCENTAGE = 0.03;
|
|
25
|
-
// Type guard functions
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const paymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.paymentAmount) !== null && _a !== void 0 ? _a : 0) : getCustomerTotalToPay(order);
|
|
297
|
-
const originalPrice = orderTotal / (1 + (0, utils_1.preventNaN)((_b = order === null || order === void 0 ? void 0 : order.sahabClientPercentage) !== null && _b !== void 0 ? _b : 0));
|
|
298
|
-
const minimumProviderAmount = originalPrice - originalPrice * ((_c = order === null || order === void 0 ? void 0 : order.sahabProviderPercentage) !== null && _c !== void 0 ? _c : 0);
|
|
299
|
-
if ((order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH) {
|
|
300
|
-
return minimumProviderAmount - paymentAmount;
|
|
301
|
-
}
|
|
302
|
-
else {
|
|
303
|
-
const onlinePercentage = getOnlineProcessingFees(order);
|
|
304
|
-
const sahabFees = paymentAmount < minimumProviderAmount ? orderTotal - minimumProviderAmount : paymentAmount - minimumProviderAmount;
|
|
305
|
-
return orderTotal - sahabFees - onlinePercentage;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
function getProviderProfits(order) {
|
|
309
|
-
const sahabExtraPaymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (order === null || order === void 0 ? void 0 : order.sahabExtraPaymentAmount) || 0 : 0;
|
|
310
|
-
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH
|
|
311
|
-
? getCustomerTotalToPay(order) - sahabExtraPaymentAmount + getProviderTransferAmount(order)
|
|
312
|
-
: getProviderTransferAmount(order);
|
|
313
|
-
}
|
|
314
|
-
function getOnlineProcessingFees(order) {
|
|
315
|
-
var _a;
|
|
316
|
-
if ((order === null || order === void 0 ? void 0 : order.paymentType) !== types_1.OrderPaymentType.CASH) {
|
|
317
|
-
const orderTotal = getCustomerTotalToPay(order);
|
|
318
|
-
return (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabOnlinePercentage) !== null && _a !== void 0 ? _a : 0) * orderTotal;
|
|
319
|
-
}
|
|
320
|
-
return 0;
|
|
321
|
-
}
|
|
322
|
-
function getSahabFees(order) {
|
|
323
|
-
var _a, _b, _c;
|
|
324
|
-
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
325
|
-
const paymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.paymentAmount) !== null && _a !== void 0 ? _a : 0) : getCustomerTotalToPay(order);
|
|
326
|
-
const originalPrice = orderTotal / (1 + (0, utils_1.preventNaN)((_b = order === null || order === void 0 ? void 0 : order.sahabClientPercentage) !== null && _b !== void 0 ? _b : 0));
|
|
327
|
-
const minimumProviderAmount = originalPrice - originalPrice * ((_c = order === null || order === void 0 ? void 0 : order.sahabProviderPercentage) !== null && _c !== void 0 ? _c : 0);
|
|
328
|
-
const sahabExtraPaymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (order === null || order === void 0 ? void 0 : order.sahabExtraPaymentAmount) || 0 : 0;
|
|
329
|
-
if (paymentAmount < minimumProviderAmount) {
|
|
330
|
-
return 0 + sahabExtraPaymentAmount;
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
return paymentAmount - minimumProviderAmount + sahabExtraPaymentAmount;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
function calcOrdersData(orders) {
|
|
337
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
338
|
-
//Filter Orders
|
|
339
|
-
const cashOrders = orders.filter((o) => o.paymentType === types_1.OrderPaymentType.CASH);
|
|
340
|
-
const onlineOrders = orders.filter((o) => o.paymentType !== types_1.OrderPaymentType.CASH);
|
|
341
|
-
const sahabOnlineRevenue = (0, sum_1.default)(onlineOrders.map((o) => getSahabFees(o)));
|
|
342
|
-
const totalOnlineTransfer = (0, sum_1.default)(onlineOrders.map((o) => getProviderTransferAmount(o)));
|
|
343
|
-
const providerOnlineRevenue = (0, sum_1.default)(onlineOrders.map((o) => getProviderProfits(o)));
|
|
344
|
-
const sahabCashRevenue = (0, sum_1.default)(cashOrders.map((o) => getSahabFees(o)));
|
|
345
|
-
const totalCashTransfer = (0, sum_1.default)(cashOrders.map((o) => getProviderTransferAmount(o)));
|
|
346
|
-
const providerCashRevenue = (0, sum_1.default)(cashOrders.map((o) => getProviderProfits(o)));
|
|
347
|
-
//Calculate Percentages
|
|
348
|
-
const totalTransfer = totalOnlineTransfer + totalCashTransfer;
|
|
349
|
-
const totalSahabRevenue = sahabOnlineRevenue + sahabCashRevenue;
|
|
350
|
-
const totalProviderRevenue = providerOnlineRevenue + providerCashRevenue;
|
|
351
|
-
return {
|
|
352
|
-
name: (_f = (_c = (_b = (_a = orders === null || orders === void 0 ? void 0 : orders[0]) === null || _a === void 0 ? void 0 : _a.providerName) === null || _b === void 0 ? void 0 : _b.ar) !== null && _c !== void 0 ? _c : (_e = (_d = orders === null || orders === void 0 ? void 0 : orders[0]) === null || _d === void 0 ? void 0 : _d.providerName) === null || _e === void 0 ? void 0 : _e.en) !== null && _f !== void 0 ? _f : '',
|
|
353
|
-
providerId: (_h = (_g = orders === null || orders === void 0 ? void 0 : orders[0]) === null || _g === void 0 ? void 0 : _g.providerId) !== null && _h !== void 0 ? _h : '',
|
|
354
|
-
numCashOrders: cashOrders.length,
|
|
355
|
-
numOnlineOrders: onlineOrders.length,
|
|
356
|
-
providerCashRevenue: (0, utils_1.toFixed)(providerCashRevenue),
|
|
357
|
-
providerOnlineRevenue: (0, utils_1.toFixed)(providerOnlineRevenue),
|
|
358
|
-
totalProviderRevenue: (0, utils_1.toFixed)(totalProviderRevenue),
|
|
359
|
-
totalCashTransfer: (0, utils_1.toFixed)(totalCashTransfer),
|
|
360
|
-
totalOnlineTransfer: (0, utils_1.toFixed)(totalOnlineTransfer),
|
|
361
|
-
sahabCashRevenue: (0, utils_1.toFixed)(sahabCashRevenue),
|
|
362
|
-
sahabOnlineRevenue: (0, utils_1.toFixed)(sahabOnlineRevenue),
|
|
363
|
-
totalTransfer: (0, utils_1.toFixed)(totalTransfer),
|
|
364
|
-
totalSahabRevenue: (0, utils_1.toFixed)(totalSahabRevenue),
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
function calculatePromoCodeAmount(order) {
|
|
368
|
-
var _a, _b, _c, _d, _e;
|
|
369
|
-
if ((0, utils_1.isEmpty)(order === null || order === void 0 ? void 0 : order.promoCode) || (((_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.amount) && ((_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.amount) <= 0)) {
|
|
370
|
-
return 0;
|
|
371
|
-
}
|
|
372
|
-
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order) - getSahabDiscountsAmount(order);
|
|
373
|
-
if ((_c = order === null || order === void 0 ? void 0 : order.promoCode) === null || _c === void 0 ? void 0 : _c.isPercentage) {
|
|
374
|
-
return ((0, utils_1.preventNaN)((_d = order === null || order === void 0 ? void 0 : order.promoCode) === null || _d === void 0 ? void 0 : _d.amount) / 100) * orderTotal;
|
|
375
|
-
}
|
|
376
|
-
return (0, utils_1.preventNaN)((_e = order === null || order === void 0 ? void 0 : order.promoCode) === null || _e === void 0 ? void 0 : _e.amount);
|
|
377
|
-
}
|
|
378
|
-
function calculateOrderPercentage({ isPaid, paymentId, providerPercentage, }) {
|
|
379
|
-
if (isPaid && paymentId && !(0, utils_1.isEmpty)(providerPercentage)) {
|
|
380
|
-
return exports.ONLINE_SAHAB_PERCENTAGE + exports.SAHAB_PERCENTAGE + exports.CLIENT_SAHAB_PERCENTAGE + providerPercentage;
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
383
|
-
return exports.SAHAB_PERCENTAGE + exports.CLIENT_SAHAB_PERCENTAGE + providerPercentage;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CLIENT_SAHAB_PERCENTAGE = exports.ONLINE_SAHAB_PERCENTAGE = exports.SAHAB_PERCENTAGE = void 0;
|
|
7
|
+
exports.getServicesTotal = getServicesTotal;
|
|
8
|
+
exports.getSahabDiscountsAmount = getSahabDiscountsAmount;
|
|
9
|
+
exports.getProviderDiscountsAmount = getProviderDiscountsAmount;
|
|
10
|
+
exports.getOrderTotalDiscountsAmount = getOrderTotalDiscountsAmount;
|
|
11
|
+
exports.getCustomerTotalToPay = getCustomerTotalToPay;
|
|
12
|
+
exports.getProviderTransferAmount = getProviderTransferAmount;
|
|
13
|
+
exports.getProviderProfits = getProviderProfits;
|
|
14
|
+
exports.getOnlineProcessingFees = getOnlineProcessingFees;
|
|
15
|
+
exports.getSahabFees = getSahabFees;
|
|
16
|
+
exports.calcOrdersData = calcOrdersData;
|
|
17
|
+
exports.calculatePromoCodeAmount = calculatePromoCodeAmount;
|
|
18
|
+
exports.calculateOrderPercentage = calculateOrderPercentage;
|
|
19
|
+
const sum_1 = __importDefault(require("lodash/sum"));
|
|
20
|
+
const types_1 = require("../constants/types");
|
|
21
|
+
const utils_1 = require("./utils");
|
|
22
|
+
exports.SAHAB_PERCENTAGE = 0;
|
|
23
|
+
exports.ONLINE_SAHAB_PERCENTAGE = 0.03;
|
|
24
|
+
exports.CLIENT_SAHAB_PERCENTAGE = 0.03;
|
|
25
|
+
// Type guard functions
|
|
26
|
+
// `type` is not present on every historical order, so fall back to the id field that only
|
|
27
|
+
// one of the two shapes carries. Without this, legacy line items match neither guard and
|
|
28
|
+
// scoped promos silently resolved to a zero discount.
|
|
29
|
+
function isProvidedService(service) {
|
|
30
|
+
if (service === null || service === void 0 ? void 0 : service.type)
|
|
31
|
+
return service.type === 'service';
|
|
32
|
+
return (service === null || service === void 0 ? void 0 : service.serviceId) !== undefined;
|
|
33
|
+
}
|
|
34
|
+
function isProvidedAddon(service) {
|
|
35
|
+
if (service === null || service === void 0 ? void 0 : service.type)
|
|
36
|
+
return service.type === 'addon';
|
|
37
|
+
return (service === null || service === void 0 ? void 0 : service.addonId) !== undefined;
|
|
38
|
+
}
|
|
39
|
+
// Helper function to check if service/addon is applicable for promo codes
|
|
40
|
+
function isServiceApplicableForPromo(service, servicesIds, addonsIds) {
|
|
41
|
+
if (isProvidedService(service)) {
|
|
42
|
+
return (servicesIds === null || servicesIds === void 0 ? void 0 : servicesIds.includes(service.serviceId || '')) || false;
|
|
43
|
+
}
|
|
44
|
+
else if (isProvidedAddon(service)) {
|
|
45
|
+
return (addonsIds === null || addonsIds === void 0 ? void 0 : addonsIds.includes(service.addonId || '')) || false;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
// Helper function to check if service/addon is applicable for discounts
|
|
50
|
+
function isServiceApplicableForDiscount(service, discount) {
|
|
51
|
+
var _a, _b, _c, _d;
|
|
52
|
+
const scopedToServices = !!((_a = discount === null || discount === void 0 ? void 0 : discount.servicesIds) === null || _a === void 0 ? void 0 : _a.length);
|
|
53
|
+
const scopedToAddons = !!((_b = discount === null || discount === void 0 ? void 0 : discount.addonsIds) === null || _b === void 0 ? void 0 : _b.length);
|
|
54
|
+
// Unscoped discounts apply to everything.
|
|
55
|
+
if (!scopedToServices && !scopedToAddons)
|
|
56
|
+
return true;
|
|
57
|
+
if (isProvidedService(service)) {
|
|
58
|
+
// A discount scoped only to addons must not leak onto services (and vice versa below).
|
|
59
|
+
return scopedToServices ? !!((_c = discount.servicesIds) === null || _c === void 0 ? void 0 : _c.includes(service.serviceId || '')) : false;
|
|
60
|
+
}
|
|
61
|
+
if (isProvidedAddon(service)) {
|
|
62
|
+
return scopedToAddons ? !!((_d = discount.addonsIds) === null || _d === void 0 ? void 0 : _d.includes(service.addonId || '')) : false;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* How a discount is funded, as fractions of the gross discount that always sum to exactly 1.
|
|
68
|
+
*
|
|
69
|
+
* Previously the Sahab and Provider sides each decided independently, so a partially
|
|
70
|
+
* configured discount could be funded at 150% (both a percentage and `createdByProvider`)
|
|
71
|
+
* or at 60% (one percentage set, the remainder funded by nobody). Deriving both shares in
|
|
72
|
+
* one place makes over/under-funding structurally impossible.
|
|
73
|
+
*/
|
|
74
|
+
function responsibilitySplit(discount) {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
const s = (0, utils_1.preventNaN)((_a = discount === null || discount === void 0 ? void 0 : discount.sahabPercentage) !== null && _a !== void 0 ? _a : 0);
|
|
77
|
+
const p = (0, utils_1.preventNaN)((_b = discount === null || discount === void 0 ? void 0 : discount.providerPercentage) !== null && _b !== void 0 ? _b : 0);
|
|
78
|
+
if (s > 0 && p > 0) {
|
|
79
|
+
// Both set — normalise so they sum to 1 even if the pair does not add up to 100.
|
|
80
|
+
const total = s + p;
|
|
81
|
+
return { sahab: s / total, provider: p / total };
|
|
82
|
+
}
|
|
83
|
+
if (s > 0) {
|
|
84
|
+
const sahab = Math.min(s / 100, 1);
|
|
85
|
+
return { sahab, provider: 1 - sahab };
|
|
86
|
+
}
|
|
87
|
+
if (p > 0) {
|
|
88
|
+
const provider = Math.min(p / 100, 1);
|
|
89
|
+
return { sahab: 1 - provider, provider };
|
|
90
|
+
}
|
|
91
|
+
// Neither set: whoever created it funds all of it.
|
|
92
|
+
return (discount === null || discount === void 0 ? void 0 : discount.createdByProvider) ? { sahab: 0, provider: 1 } : { sahab: 1, provider: 0 };
|
|
93
|
+
}
|
|
94
|
+
/** Gross promo-code discount for an order, before the responsibility split. */
|
|
95
|
+
function grossPromoDiscount(order) {
|
|
96
|
+
var _a, _b, _c;
|
|
97
|
+
const promo = order === null || order === void 0 ? void 0 : order.promoCode;
|
|
98
|
+
if (!(promo === null || promo === void 0 ? void 0 : promo.amount) || promo.amount <= 0)
|
|
99
|
+
return 0;
|
|
100
|
+
const scoped = !!((_a = promo.servicesIds) === null || _a === void 0 ? void 0 : _a.length) || !!((_b = promo.addonsIds) === null || _b === void 0 ? void 0 : _b.length);
|
|
101
|
+
let applicableTotal = getServicesTotal(order);
|
|
102
|
+
if (scoped) {
|
|
103
|
+
applicableTotal = 0;
|
|
104
|
+
(_c = order === null || order === void 0 ? void 0 : order.services) === null || _c === void 0 ? void 0 : _c.forEach((carService) => {
|
|
105
|
+
var _a;
|
|
106
|
+
(_a = carService === null || carService === void 0 ? void 0 : carService.services) === null || _a === void 0 ? void 0 : _a.forEach((service) => {
|
|
107
|
+
var _a;
|
|
108
|
+
if (isServiceApplicableForPromo(service, promo.servicesIds, promo.addonsIds)) {
|
|
109
|
+
applicableTotal += ((_a = service === null || service === void 0 ? void 0 : service.price) === null || _a === void 0 ? void 0 : _a.price) || 0;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
let amount = promo.isPercentage ? ((0, utils_1.preventNaN)(promo.amount) / 100) * applicableTotal : (0, utils_1.preventNaN)(promo.amount);
|
|
115
|
+
// A fixed-amount promo can never exceed the basket it applies to.
|
|
116
|
+
if (!promo.isPercentage && scoped)
|
|
117
|
+
amount = Math.min(amount, applicableTotal);
|
|
118
|
+
if (promo.maxAmount && promo.maxAmount > 0)
|
|
119
|
+
amount = Math.min(amount, promo.maxAmount);
|
|
120
|
+
return amount;
|
|
121
|
+
}
|
|
122
|
+
/** Gross line-item discount, before the responsibility split. */
|
|
123
|
+
function grossServiceDiscount(service, discount) {
|
|
124
|
+
var _a, _b, _c, _d;
|
|
125
|
+
if (!discount)
|
|
126
|
+
return 0;
|
|
127
|
+
if (!isServiceApplicableForDiscount(service, discount))
|
|
128
|
+
return 0;
|
|
129
|
+
return discount.isPercentage
|
|
130
|
+
? ((0, utils_1.preventNaN)((_a = discount.amount) !== null && _a !== void 0 ? _a : 0) / 100) * ((_c = (_b = service === null || service === void 0 ? void 0 : service.price) === null || _b === void 0 ? void 0 : _b.price) !== null && _c !== void 0 ? _c : 0)
|
|
131
|
+
: (0, utils_1.preventNaN)((_d = discount.amount) !== null && _d !== void 0 ? _d : 0);
|
|
132
|
+
}
|
|
133
|
+
/** Sums one side of every discount on the order. */
|
|
134
|
+
function discountsForSide(order, side) {
|
|
135
|
+
var _a;
|
|
136
|
+
let discounts = 0;
|
|
137
|
+
const promoGross = grossPromoDiscount(order);
|
|
138
|
+
if (promoGross > 0) {
|
|
139
|
+
discounts += responsibilitySplit(order === null || order === void 0 ? void 0 : order.promoCode)[side] * promoGross;
|
|
140
|
+
}
|
|
141
|
+
((_a = order === null || order === void 0 ? void 0 : order.services) !== null && _a !== void 0 ? _a : []).forEach((carService) => {
|
|
142
|
+
var _a;
|
|
143
|
+
((_a = carService === null || carService === void 0 ? void 0 : carService.services) !== null && _a !== void 0 ? _a : []).forEach((service) => {
|
|
144
|
+
const gross = grossServiceDiscount(service, service === null || service === void 0 ? void 0 : service.discount);
|
|
145
|
+
if (gross > 0) {
|
|
146
|
+
discounts += responsibilitySplit(service === null || service === void 0 ? void 0 : service.discount)[side] * gross;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
return discounts;
|
|
151
|
+
}
|
|
152
|
+
function getServicesTotal(order) {
|
|
153
|
+
let total = 0;
|
|
154
|
+
if (order) {
|
|
155
|
+
if (order.services) {
|
|
156
|
+
order.services.forEach((carService) => {
|
|
157
|
+
carService.services.forEach((service) => {
|
|
158
|
+
if (service.price) {
|
|
159
|
+
const price = service.price.price;
|
|
160
|
+
total += price;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return total;
|
|
167
|
+
}
|
|
168
|
+
function getSahabDiscountsAmount(order) {
|
|
169
|
+
let discounts = discountsForSide(order, 'sahab');
|
|
170
|
+
// Wallet balance the client spent is money Sahab has already taken in.
|
|
171
|
+
if ((order === null || order === void 0 ? void 0 : order.usedBalance) && (order === null || order === void 0 ? void 0 : order.usedBalance) > 0) {
|
|
172
|
+
discounts += order.usedBalance;
|
|
173
|
+
}
|
|
174
|
+
return discounts;
|
|
175
|
+
}
|
|
176
|
+
function getProviderDiscountsAmount(order) {
|
|
177
|
+
return discountsForSide(order, 'provider');
|
|
178
|
+
}
|
|
179
|
+
function getOrderTotalDiscountsAmount(order) {
|
|
180
|
+
return getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
|
|
181
|
+
}
|
|
182
|
+
function getCustomerTotalToPay(order) {
|
|
183
|
+
const sahabExtraPaymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (order === null || order === void 0 ? void 0 : order.sahabExtraPaymentAmount) || 0 : 0;
|
|
184
|
+
const orderTotal = getServicesTotal(order) + sahabExtraPaymentAmount;
|
|
185
|
+
const discounts = getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
|
|
186
|
+
return orderTotal - discounts;
|
|
187
|
+
}
|
|
188
|
+
/** Sahab's cash surcharge on this order (zero for online orders). */
|
|
189
|
+
function cashSurcharge(order) {
|
|
190
|
+
var _a;
|
|
191
|
+
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabExtraPaymentAmount) !== null && _a !== void 0 ? _a : 0) : 0;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The part of a cash collection that settles against the provider's entitlement.
|
|
195
|
+
*
|
|
196
|
+
* `paymentAmount` for a cash order is the full amount the customer hands over, which
|
|
197
|
+
* includes Sahab's cash surcharge. That surcharge is Sahab's revenue, not the provider's,
|
|
198
|
+
* so it must come off before comparing against `minimumProviderAmount` — otherwise the
|
|
199
|
+
* surcharge is counted twice in `getSahabFees` and deducted from the provider twice in
|
|
200
|
+
* `getProviderProfits`.
|
|
201
|
+
*/
|
|
202
|
+
function cashSettlementBase(order) {
|
|
203
|
+
var _a;
|
|
204
|
+
return Math.max(0, (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.paymentAmount) !== null && _a !== void 0 ? _a : 0) - cashSurcharge(order));
|
|
205
|
+
}
|
|
206
|
+
function getProviderTransferAmount(order) {
|
|
207
|
+
var _a, _b;
|
|
208
|
+
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
209
|
+
const paymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? cashSettlementBase(order) : getCustomerTotalToPay(order);
|
|
210
|
+
const originalPrice = orderTotal / (1 + (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabClientPercentage) !== null && _a !== void 0 ? _a : 0));
|
|
211
|
+
const minimumProviderAmount = originalPrice - originalPrice * ((_b = order === null || order === void 0 ? void 0 : order.sahabProviderPercentage) !== null && _b !== void 0 ? _b : 0);
|
|
212
|
+
if ((order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH) {
|
|
213
|
+
return minimumProviderAmount - paymentAmount;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const onlinePercentage = getOnlineProcessingFees(order);
|
|
217
|
+
const sahabFees = paymentAmount < minimumProviderAmount ? orderTotal - minimumProviderAmount : paymentAmount - minimumProviderAmount;
|
|
218
|
+
return orderTotal - sahabFees - onlinePercentage;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function getProviderProfits(order) {
|
|
222
|
+
const sahabExtraPaymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (order === null || order === void 0 ? void 0 : order.sahabExtraPaymentAmount) || 0 : 0;
|
|
223
|
+
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH
|
|
224
|
+
? getCustomerTotalToPay(order) - sahabExtraPaymentAmount + getProviderTransferAmount(order)
|
|
225
|
+
: getProviderTransferAmount(order);
|
|
226
|
+
}
|
|
227
|
+
function getOnlineProcessingFees(order) {
|
|
228
|
+
var _a;
|
|
229
|
+
if ((order === null || order === void 0 ? void 0 : order.paymentType) !== types_1.OrderPaymentType.CASH) {
|
|
230
|
+
const orderTotal = getCustomerTotalToPay(order);
|
|
231
|
+
return (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabOnlinePercentage) !== null && _a !== void 0 ? _a : 0) * orderTotal;
|
|
232
|
+
}
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
function getSahabFees(order) {
|
|
236
|
+
var _a, _b;
|
|
237
|
+
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
238
|
+
const paymentAmount = (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? cashSettlementBase(order) : getCustomerTotalToPay(order);
|
|
239
|
+
const originalPrice = orderTotal / (1 + (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabClientPercentage) !== null && _a !== void 0 ? _a : 0));
|
|
240
|
+
const minimumProviderAmount = originalPrice - originalPrice * ((_b = order === null || order === void 0 ? void 0 : order.sahabProviderPercentage) !== null && _b !== void 0 ? _b : 0);
|
|
241
|
+
// When a Sahab-funded discount pushes the customer below the provider's entitlement,
|
|
242
|
+
// Sahab is genuinely subsidising the order. Reporting a floor of 0 there overstated
|
|
243
|
+
// margin on every discounted order, so the negative figure is returned as-is.
|
|
244
|
+
return paymentAmount - minimumProviderAmount + cashSurcharge(order);
|
|
245
|
+
}
|
|
246
|
+
function calcOrdersData(orders) {
|
|
247
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
248
|
+
//Filter Orders
|
|
249
|
+
const cashOrders = orders.filter((o) => o.paymentType === types_1.OrderPaymentType.CASH);
|
|
250
|
+
const onlineOrders = orders.filter((o) => o.paymentType !== types_1.OrderPaymentType.CASH);
|
|
251
|
+
const sahabOnlineRevenue = (0, sum_1.default)(onlineOrders.map((o) => getSahabFees(o)));
|
|
252
|
+
const totalOnlineTransfer = (0, sum_1.default)(onlineOrders.map((o) => getProviderTransferAmount(o)));
|
|
253
|
+
const providerOnlineRevenue = (0, sum_1.default)(onlineOrders.map((o) => getProviderProfits(o)));
|
|
254
|
+
const sahabCashRevenue = (0, sum_1.default)(cashOrders.map((o) => getSahabFees(o)));
|
|
255
|
+
const totalCashTransfer = (0, sum_1.default)(cashOrders.map((o) => getProviderTransferAmount(o)));
|
|
256
|
+
const providerCashRevenue = (0, sum_1.default)(cashOrders.map((o) => getProviderProfits(o)));
|
|
257
|
+
//Calculate Percentages
|
|
258
|
+
const totalTransfer = totalOnlineTransfer + totalCashTransfer;
|
|
259
|
+
const totalSahabRevenue = sahabOnlineRevenue + sahabCashRevenue;
|
|
260
|
+
const totalProviderRevenue = providerOnlineRevenue + providerCashRevenue;
|
|
261
|
+
return {
|
|
262
|
+
name: (_f = (_c = (_b = (_a = orders === null || orders === void 0 ? void 0 : orders[0]) === null || _a === void 0 ? void 0 : _a.providerName) === null || _b === void 0 ? void 0 : _b.ar) !== null && _c !== void 0 ? _c : (_e = (_d = orders === null || orders === void 0 ? void 0 : orders[0]) === null || _d === void 0 ? void 0 : _d.providerName) === null || _e === void 0 ? void 0 : _e.en) !== null && _f !== void 0 ? _f : '',
|
|
263
|
+
providerId: (_h = (_g = orders === null || orders === void 0 ? void 0 : orders[0]) === null || _g === void 0 ? void 0 : _g.providerId) !== null && _h !== void 0 ? _h : '',
|
|
264
|
+
numCashOrders: cashOrders.length,
|
|
265
|
+
numOnlineOrders: onlineOrders.length,
|
|
266
|
+
providerCashRevenue: (0, utils_1.toFixed)(providerCashRevenue),
|
|
267
|
+
providerOnlineRevenue: (0, utils_1.toFixed)(providerOnlineRevenue),
|
|
268
|
+
totalProviderRevenue: (0, utils_1.toFixed)(totalProviderRevenue),
|
|
269
|
+
totalCashTransfer: (0, utils_1.toFixed)(totalCashTransfer),
|
|
270
|
+
totalOnlineTransfer: (0, utils_1.toFixed)(totalOnlineTransfer),
|
|
271
|
+
sahabCashRevenue: (0, utils_1.toFixed)(sahabCashRevenue),
|
|
272
|
+
sahabOnlineRevenue: (0, utils_1.toFixed)(sahabOnlineRevenue),
|
|
273
|
+
totalTransfer: (0, utils_1.toFixed)(totalTransfer),
|
|
274
|
+
totalSahabRevenue: (0, utils_1.toFixed)(totalSahabRevenue),
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
function calculatePromoCodeAmount(order) {
|
|
278
|
+
var _a, _b, _c, _d, _e;
|
|
279
|
+
if ((0, utils_1.isEmpty)(order === null || order === void 0 ? void 0 : order.promoCode) || (((_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.amount) && ((_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.amount) <= 0)) {
|
|
280
|
+
return 0;
|
|
281
|
+
}
|
|
282
|
+
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order) - getSahabDiscountsAmount(order);
|
|
283
|
+
if ((_c = order === null || order === void 0 ? void 0 : order.promoCode) === null || _c === void 0 ? void 0 : _c.isPercentage) {
|
|
284
|
+
return ((0, utils_1.preventNaN)((_d = order === null || order === void 0 ? void 0 : order.promoCode) === null || _d === void 0 ? void 0 : _d.amount) / 100) * orderTotal;
|
|
285
|
+
}
|
|
286
|
+
return (0, utils_1.preventNaN)((_e = order === null || order === void 0 ? void 0 : order.promoCode) === null || _e === void 0 ? void 0 : _e.amount);
|
|
287
|
+
}
|
|
288
|
+
function calculateOrderPercentage({ isPaid, paymentId, providerPercentage, }) {
|
|
289
|
+
if (isPaid && paymentId && !(0, utils_1.isEmpty)(providerPercentage)) {
|
|
290
|
+
return exports.ONLINE_SAHAB_PERCENTAGE + exports.SAHAB_PERCENTAGE + exports.CLIENT_SAHAB_PERCENTAGE + providerPercentage;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
return exports.SAHAB_PERCENTAGE + exports.CLIENT_SAHAB_PERCENTAGE + providerPercentage;
|
|
294
|
+
}
|
|
295
|
+
}
|