sahab-utils 1.3.0 → 1.6.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/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
- function isProvidedService(service) {
27
- return service.type === 'service';
28
- }
29
- function isProvidedAddon(service) {
30
- return service.type === 'addon';
31
- }
32
- // Helper function to check if service/addon is applicable for promo codes
33
- function isServiceApplicableForPromo(service, servicesIds, addonsIds) {
34
- if (isProvidedService(service)) {
35
- return (servicesIds === null || servicesIds === void 0 ? void 0 : servicesIds.includes(service.serviceId || '')) || false;
36
- }
37
- else if (isProvidedAddon(service)) {
38
- return (addonsIds === null || addonsIds === void 0 ? void 0 : addonsIds.includes(service.addonId || '')) || false;
39
- }
40
- return false;
41
- }
42
- // Helper function to check if service/addon is applicable for discounts
43
- function isServiceApplicableForDiscount(service, discount) {
44
- if (isProvidedService(service) && (discount === null || discount === void 0 ? void 0 : discount.servicesIds) && discount.servicesIds.length > 0) {
45
- return discount.servicesIds.includes(service.serviceId || '');
46
- }
47
- else if (isProvidedAddon(service) && (discount === null || discount === void 0 ? void 0 : discount.addonsIds) && discount.addonsIds.length > 0) {
48
- return discount.addonsIds.includes(service.addonId || '');
49
- }
50
- return true; // If no specific IDs are set, apply to all
51
- }
52
- function getServicesTotal(order) {
53
- let total = 0;
54
- if (order) {
55
- if (order.services) {
56
- order.services.forEach((carService) => {
57
- carService.services.forEach((service) => {
58
- if (service.price) {
59
- const price = service.price.price;
60
- total += price;
61
- }
62
- });
63
- });
64
- }
65
- }
66
- return total;
67
- }
68
- function getSahabDiscountsAmount(order) {
69
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
70
- const orderTotal = getServicesTotal(order);
71
- let discounts = 0;
72
- if (['CHOCO', 'ROSE', 'ركن', 'الإيمان', 'التقوى', 'نور العترة', 'Zhraa_fit', 'Greens_fit', 'لوكيت', 'المنتزه'].includes(((_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.promoCode) || '')) {
73
- discounts += 0.05 * orderTotal;
74
- }
75
- else if (((_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.amount) && ((_c = order === null || order === void 0 ? void 0 : order.promoCode) === null || _c === void 0 ? void 0 : _c.amount) > 0) {
76
- if ((_d = order === null || order === void 0 ? void 0 : order.promoCode) === null || _d === void 0 ? void 0 : _d.isPercentage) {
77
- let amount = ((0, utils_1.preventNaN)((_e = order === null || order === void 0 ? void 0 : order.promoCode) === null || _e === void 0 ? void 0 : _e.amount) / 100) * orderTotal;
78
- let promoDiscountAmount = 0;
79
- // Check if promo code is for specific services/addons only
80
- if ((((_f = order === null || order === void 0 ? void 0 : order.promoCode) === null || _f === void 0 ? void 0 : _f.servicesIds) && ((_g = order === null || order === void 0 ? void 0 : order.promoCode) === null || _g === void 0 ? void 0 : _g.servicesIds.length) > 0) ||
81
- (((_h = order === null || order === void 0 ? void 0 : order.promoCode) === null || _h === void 0 ? void 0 : _h.addonsIds) && ((_j = order === null || order === void 0 ? void 0 : order.promoCode) === null || _j === void 0 ? void 0 : _j.addonsIds.length) > 0)) {
82
- let applicableTotal = 0;
83
- (_k = order === null || order === void 0 ? void 0 : order.services) === null || _k === void 0 ? void 0 : _k.forEach((carService) => {
84
- var _a;
85
- (_a = carService === null || carService === void 0 ? void 0 : carService.services) === null || _a === void 0 ? void 0 : _a.forEach((service) => {
86
- var _a, _b, _c;
87
- if (isServiceApplicableForPromo(service, (_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.servicesIds, (_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.addonsIds)) {
88
- applicableTotal += ((_c = service === null || service === void 0 ? void 0 : service.price) === null || _c === void 0 ? void 0 : _c.price) || 0;
89
- }
90
- });
91
- });
92
- amount = ((0, utils_1.preventNaN)((_l = order === null || order === void 0 ? void 0 : order.promoCode) === null || _l === void 0 ? void 0 : _l.amount) / 100) * applicableTotal;
93
- }
94
- if (((_m = order === null || order === void 0 ? void 0 : order.promoCode) === null || _m === void 0 ? void 0 : _m.maxAmount) && ((_o = order === null || order === void 0 ? void 0 : order.promoCode) === null || _o === void 0 ? void 0 : _o.maxAmount) > 0) {
95
- promoDiscountAmount += Math.min(amount, (_p = order === null || order === void 0 ? void 0 : order.promoCode) === null || _p === void 0 ? void 0 : _p.maxAmount);
96
- }
97
- else {
98
- promoDiscountAmount += amount;
99
- }
100
- // Apply Sahab's responsibility based on sahabPercentage or fallback to createdByProvider logic
101
- if (((_q = order === null || order === void 0 ? void 0 : order.promoCode) === null || _q === void 0 ? void 0 : _q.sahabPercentage) && ((_r = order === null || order === void 0 ? void 0 : order.promoCode) === null || _r === void 0 ? void 0 : _r.sahabPercentage) > 0) {
102
- discounts += ((0, utils_1.preventNaN)(order.promoCode.sahabPercentage) / 100) * promoDiscountAmount;
103
- }
104
- else if (!((_s = order === null || order === void 0 ? void 0 : order.promoCode) === null || _s === void 0 ? void 0 : _s.createdByProvider)) {
105
- // Fallback to original logic: if not created by provider, it's Sahab's responsibility
106
- discounts += promoDiscountAmount;
107
- }
108
- }
109
- else {
110
- let amount = (0, utils_1.preventNaN)((_t = order === null || order === void 0 ? void 0 : order.promoCode) === null || _t === void 0 ? void 0 : _t.amount);
111
- let promoDiscountAmount = 0;
112
- // Check if promo code is for specific services/addons only
113
- if ((((_u = order === null || order === void 0 ? void 0 : order.promoCode) === null || _u === void 0 ? void 0 : _u.servicesIds) && ((_v = order === null || order === void 0 ? void 0 : order.promoCode) === null || _v === void 0 ? void 0 : _v.servicesIds.length) > 0) ||
114
- (((_w = order === null || order === void 0 ? void 0 : order.promoCode) === null || _w === void 0 ? void 0 : _w.addonsIds) && ((_x = order === null || order === void 0 ? void 0 : order.promoCode) === null || _x === void 0 ? void 0 : _x.addonsIds.length) > 0)) {
115
- let applicableTotal = 0;
116
- (_y = order === null || order === void 0 ? void 0 : order.services) === null || _y === void 0 ? void 0 : _y.forEach((carService) => {
117
- var _a;
118
- (_a = carService === null || carService === void 0 ? void 0 : carService.services) === null || _a === void 0 ? void 0 : _a.forEach((service) => {
119
- var _a, _b, _c;
120
- if (isServiceApplicableForPromo(service, (_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.servicesIds, (_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.addonsIds)) {
121
- applicableTotal += ((_c = service === null || service === void 0 ? void 0 : service.price) === null || _c === void 0 ? void 0 : _c.price) || 0;
122
- }
123
- });
124
- });
125
- amount = Math.min(amount, applicableTotal);
126
- }
127
- if (((_z = order === null || order === void 0 ? void 0 : order.promoCode) === null || _z === void 0 ? void 0 : _z.maxAmount) && ((_0 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _0 === void 0 ? void 0 : _0.maxAmount) > 0) {
128
- promoDiscountAmount += Math.min(amount, (_1 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _1 === void 0 ? void 0 : _1.maxAmount);
129
- }
130
- else {
131
- promoDiscountAmount += amount;
132
- }
133
- // Apply Sahab's responsibility based on sahabPercentage or fallback to createdByProvider logic
134
- if (((_2 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _2 === void 0 ? void 0 : _2.sahabPercentage) && ((_3 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _3 === void 0 ? void 0 : _3.sahabPercentage) > 0) {
135
- discounts += ((0, utils_1.preventNaN)(order.promoCode.sahabPercentage) / 100) * promoDiscountAmount;
136
- }
137
- else if (!((_4 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _4 === void 0 ? void 0 : _4.createdByProvider)) {
138
- // Fallback to original logic: if not created by provider, it's Sahab's responsibility
139
- discounts += promoDiscountAmount;
140
- }
141
- }
142
- }
143
- // Sahab Services Discounts
144
- (_6 = ((_5 = order === null || order === void 0 ? void 0 : order.services) !== null && _5 !== void 0 ? _5 : [])) === null || _6 === void 0 ? void 0 : _6.forEach((carService) => {
145
- ((carService === null || carService === void 0 ? void 0 : carService.services) || []).forEach((service) => {
146
- let sahabDiscounts = [service === null || service === void 0 ? void 0 : service.discount];
147
- let promoDiscountAmount = 0;
148
- if (sahabDiscounts.length) {
149
- sahabDiscounts.forEach((discount) => {
150
- var _a, _b, _c, _d;
151
- // Check if discount is for specific services/addons only
152
- if (!isServiceApplicableForDiscount(service, discount)) {
153
- return;
154
- }
155
- if (discount === null || discount === void 0 ? void 0 : discount.isPercentage) {
156
- promoDiscountAmount += ((0, utils_1.preventNaN)((_a = discount === null || discount === void 0 ? void 0 : 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);
157
- }
158
- else {
159
- promoDiscountAmount += (0, utils_1.preventNaN)((_d = discount === null || discount === void 0 ? void 0 : discount.amount) !== null && _d !== void 0 ? _d : 0);
160
- }
161
- // Apply Sahab's responsibility based on sahabPercentage or fallback to createdByProvider logic
162
- if ((discount === null || discount === void 0 ? void 0 : discount.sahabPercentage) && (discount === null || discount === void 0 ? void 0 : discount.sahabPercentage) > 0) {
163
- discounts += ((0, utils_1.preventNaN)(discount === null || discount === void 0 ? void 0 : discount.sahabPercentage) / 100) * promoDiscountAmount;
164
- }
165
- else if (!(discount === null || discount === void 0 ? void 0 : discount.createdByProvider)) {
166
- // Fallback to original logic: if not created by provider, it's Sahab's responsibility
167
- discounts += promoDiscountAmount;
168
- }
169
- });
170
- }
171
- });
172
- });
173
- if ((order === null || order === void 0 ? void 0 : order.usedBalance) && (order === null || order === void 0 ? void 0 : order.usedBalance) > 0) {
174
- discounts += order === null || order === void 0 ? void 0 : order.usedBalance;
175
- }
176
- return discounts;
177
- }
178
- function getProviderDiscountsAmount(order) {
179
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
180
- const orderTotal = getServicesTotal(order);
181
- let discounts = 0;
182
- if (['CHOCO', 'ROSE', 'ركن', 'الإيمان', 'التقوى', 'نور العترة', 'Zhraa_fit', 'Greens_fit', 'لوكيت', 'المنتزه'].includes(((_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.promoCode) || '')) {
183
- discounts += 0.05 * orderTotal;
184
- }
185
- else if (((_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.amount) && ((_c = order === null || order === void 0 ? void 0 : order.promoCode) === null || _c === void 0 ? void 0 : _c.amount) > 0) {
186
- if ((_d = order === null || order === void 0 ? void 0 : order.promoCode) === null || _d === void 0 ? void 0 : _d.isPercentage) {
187
- let amount = ((0, utils_1.preventNaN)((_e = order === null || order === void 0 ? void 0 : order.promoCode) === null || _e === void 0 ? void 0 : _e.amount) / 100) * orderTotal;
188
- let promoDiscountAmount = 0;
189
- // Check if promo code is for specific services/addons only
190
- if ((((_f = order === null || order === void 0 ? void 0 : order.promoCode) === null || _f === void 0 ? void 0 : _f.servicesIds) && ((_g = order === null || order === void 0 ? void 0 : order.promoCode) === null || _g === void 0 ? void 0 : _g.servicesIds.length) > 0) ||
191
- (((_h = order === null || order === void 0 ? void 0 : order.promoCode) === null || _h === void 0 ? void 0 : _h.addonsIds) && ((_j = order === null || order === void 0 ? void 0 : order.promoCode) === null || _j === void 0 ? void 0 : _j.addonsIds.length) > 0)) {
192
- let applicableTotal = 0;
193
- (_k = order === null || order === void 0 ? void 0 : order.services) === null || _k === void 0 ? void 0 : _k.forEach((carService) => {
194
- var _a;
195
- (_a = carService === null || carService === void 0 ? void 0 : carService.services) === null || _a === void 0 ? void 0 : _a.forEach((service) => {
196
- var _a, _b, _c;
197
- if (isServiceApplicableForPromo(service, (_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.servicesIds, (_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.addonsIds)) {
198
- applicableTotal += ((_c = service === null || service === void 0 ? void 0 : service.price) === null || _c === void 0 ? void 0 : _c.price) || 0;
199
- }
200
- });
201
- });
202
- amount = ((0, utils_1.preventNaN)((_l = order === null || order === void 0 ? void 0 : order.promoCode) === null || _l === void 0 ? void 0 : _l.amount) / 100) * applicableTotal;
203
- }
204
- if (((_m = order === null || order === void 0 ? void 0 : order.promoCode) === null || _m === void 0 ? void 0 : _m.maxAmount) && ((_o = order === null || order === void 0 ? void 0 : order.promoCode) === null || _o === void 0 ? void 0 : _o.maxAmount) > 0) {
205
- promoDiscountAmount += Math.min(amount, (_p = order === null || order === void 0 ? void 0 : order.promoCode) === null || _p === void 0 ? void 0 : _p.maxAmount);
206
- }
207
- else {
208
- promoDiscountAmount += amount;
209
- }
210
- // Apply Provider's responsibility based on providerPercentage or fallback to createdByProvider logic
211
- if (((_q = order === null || order === void 0 ? void 0 : order.promoCode) === null || _q === void 0 ? void 0 : _q.providerPercentage) && ((_r = order === null || order === void 0 ? void 0 : order.promoCode) === null || _r === void 0 ? void 0 : _r.providerPercentage) > 0) {
212
- discounts += ((0, utils_1.preventNaN)(order.promoCode.providerPercentage) / 100) * promoDiscountAmount;
213
- }
214
- else if ((_s = order === null || order === void 0 ? void 0 : order.promoCode) === null || _s === void 0 ? void 0 : _s.createdByProvider) {
215
- // Fallback: if the promo was created by the provider, the provider eats the discount.
216
- discounts += promoDiscountAmount;
217
- }
218
- }
219
- else {
220
- let amount = (0, utils_1.preventNaN)((_t = order === null || order === void 0 ? void 0 : order.promoCode) === null || _t === void 0 ? void 0 : _t.amount);
221
- let promoDiscountAmount = 0;
222
- // Check if promo code is for specific services/addons only
223
- if ((((_u = order === null || order === void 0 ? void 0 : order.promoCode) === null || _u === void 0 ? void 0 : _u.servicesIds) && ((_v = order === null || order === void 0 ? void 0 : order.promoCode) === null || _v === void 0 ? void 0 : _v.servicesIds.length) > 0) ||
224
- (((_w = order === null || order === void 0 ? void 0 : order.promoCode) === null || _w === void 0 ? void 0 : _w.addonsIds) && ((_x = order === null || order === void 0 ? void 0 : order.promoCode) === null || _x === void 0 ? void 0 : _x.addonsIds.length) > 0)) {
225
- let applicableTotal = 0;
226
- (_y = order === null || order === void 0 ? void 0 : order.services) === null || _y === void 0 ? void 0 : _y.forEach((carService) => {
227
- var _a;
228
- (_a = carService === null || carService === void 0 ? void 0 : carService.services) === null || _a === void 0 ? void 0 : _a.forEach((service) => {
229
- var _a, _b, _c;
230
- if (isServiceApplicableForPromo(service, (_a = order === null || order === void 0 ? void 0 : order.promoCode) === null || _a === void 0 ? void 0 : _a.servicesIds, (_b = order === null || order === void 0 ? void 0 : order.promoCode) === null || _b === void 0 ? void 0 : _b.addonsIds)) {
231
- applicableTotal += ((_c = service === null || service === void 0 ? void 0 : service.price) === null || _c === void 0 ? void 0 : _c.price) || 0;
232
- }
233
- });
234
- });
235
- amount = Math.min(amount, applicableTotal);
236
- }
237
- if (((_z = order === null || order === void 0 ? void 0 : order.promoCode) === null || _z === void 0 ? void 0 : _z.maxAmount) && ((_0 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _0 === void 0 ? void 0 : _0.maxAmount) > 0) {
238
- promoDiscountAmount += Math.min(amount, (_1 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _1 === void 0 ? void 0 : _1.maxAmount);
239
- }
240
- else {
241
- promoDiscountAmount += amount;
242
- }
243
- // Apply Provider's responsibility based on providerPercentage or fallback to createdByProvider logic
244
- if (((_2 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _2 === void 0 ? void 0 : _2.providerPercentage) && ((_3 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _3 === void 0 ? void 0 : _3.providerPercentage) > 0) {
245
- discounts += ((0, utils_1.preventNaN)(order.promoCode.providerPercentage) / 100) * promoDiscountAmount;
246
- }
247
- else if ((_4 = order === null || order === void 0 ? void 0 : order.promoCode) === null || _4 === void 0 ? void 0 : _4.createdByProvider) {
248
- // Fallback: if the promo was created by the provider, the provider eats the discount.
249
- discounts += promoDiscountAmount;
250
- }
251
- }
252
- }
253
- (_6 = ((_5 = order === null || order === void 0 ? void 0 : order.services) !== null && _5 !== void 0 ? _5 : [])) === null || _6 === void 0 ? void 0 : _6.forEach((carService) => {
254
- ((carService === null || carService === void 0 ? void 0 : carService.services) || []).forEach((service) => {
255
- let sahabDiscounts = [service === null || service === void 0 ? void 0 : service.discount];
256
- if (sahabDiscounts.length) {
257
- sahabDiscounts.forEach((discount) => {
258
- var _a, _b, _c, _d;
259
- let promoDiscountAmount = 0;
260
- // Check if discount is for specific services/addons only
261
- if (!isServiceApplicableForDiscount(service, discount)) {
262
- return;
263
- }
264
- if (discount === null || discount === void 0 ? void 0 : discount.isPercentage) {
265
- promoDiscountAmount += ((0, utils_1.preventNaN)((_a = discount === null || discount === void 0 ? void 0 : 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);
266
- }
267
- else {
268
- promoDiscountAmount += (0, utils_1.preventNaN)((_d = discount === null || discount === void 0 ? void 0 : discount.amount) !== null && _d !== void 0 ? _d : 0);
269
- }
270
- // Apply Provider's responsibility based on providerPercentage or fallback to createdByProvider logic
271
- if ((discount === null || discount === void 0 ? void 0 : discount.providerPercentage) && (discount === null || discount === void 0 ? void 0 : discount.providerPercentage) > 0) {
272
- discounts += ((0, utils_1.preventNaN)(discount.providerPercentage) / 100) * promoDiscountAmount;
273
- }
274
- else if (discount === null || discount === void 0 ? void 0 : discount.createdByProvider) {
275
- // Fallback: if the discount was created by the provider, the provider eats it.
276
- discounts += promoDiscountAmount;
277
- }
278
- });
279
- }
280
- });
281
- });
282
- return discounts;
283
- }
284
- function getOrderTotalDiscountsAmount(order) {
285
- return getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
286
- }
287
- function getCustomerTotalToPay(order) {
288
- 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;
289
- const orderTotal = getServicesTotal(order) + sahabExtraPaymentAmount;
290
- const discounts = getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
291
- return orderTotal - discounts;
292
- }
293
- function getProviderTransferAmount(order) {
294
- var _a, _b, _c;
295
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
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
+ }