sahab-utils 1.2.9 → 1.3.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,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
- 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 to original logic: if created by provider, it's Provider's responsibility
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 to original logic: if created by provider, it's Provider's responsibility
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 to original logic: if created by provider, it's Provider's responsibility
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
+ 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
+ }