sahab-utils 1.2.6 → 1.2.9
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/constants/types.d.ts +659 -0
- package/dist/constants/types.js +79 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/ts/calcs.d.ts +34 -0
- package/dist/ts/calcs.js +385 -0
- package/dist/ts/utils.d.ts +3 -0
- package/dist/ts/utils.js +24 -0
- package/package.json +36 -27
- package/.yarn/install-state.gz +0 -0
- package/.yarnrc.yml +0 -1
- package/constants/types.ts +0 -776
- package/index.ts +0 -2
- package/ts/calcs.ts +0 -308
- package/ts/utils.ts +0 -21
package/index.ts
DELETED
package/ts/calcs.ts
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
import sum from 'lodash/sum';
|
|
2
|
-
import { Discount, Order, OrderPaymentType, ProvidedAddon, ProvidedService } from '../constants/types';
|
|
3
|
-
import { isEmpty, preventNaN, toFixed } from './utils';
|
|
4
|
-
|
|
5
|
-
export const SAHAB_PERCENTAGE = 0;
|
|
6
|
-
export const ONLINE_SAHAB_PERCENTAGE = 0.03;
|
|
7
|
-
export const CLIENT_SAHAB_PERCENTAGE = 0.03;
|
|
8
|
-
|
|
9
|
-
export function getServicesTotal(order?: Order) {
|
|
10
|
-
let total = 0;
|
|
11
|
-
if (order) {
|
|
12
|
-
if (order.services) {
|
|
13
|
-
order.services.forEach((carService) => {
|
|
14
|
-
carService.services.forEach((service) => {
|
|
15
|
-
if (service.price) {
|
|
16
|
-
const price = service.price.price;
|
|
17
|
-
total += price;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return total;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function getSahabDiscountsAmount(order: Order) {
|
|
27
|
-
const orderTotal = getServicesTotal(order);
|
|
28
|
-
let discounts = 0;
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
['CHOCO', 'ROSE', 'ركن', 'الإيمان', 'التقوى', 'نور العترة', 'Zhraa_fit', 'Greens_fit', 'لوكيت', 'المنتزه'].includes(
|
|
32
|
-
order?.promoCode?.promoCode || ''
|
|
33
|
-
)
|
|
34
|
-
) {
|
|
35
|
-
discounts += 0.05 * orderTotal;
|
|
36
|
-
} else if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && !order?.promoCode?.createdByProvider) {
|
|
37
|
-
if (order?.promoCode?.isPercentage) {
|
|
38
|
-
const amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
|
|
39
|
-
if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
|
|
40
|
-
discounts += Math.min(amount, order?.promoCode?.maxAmount);
|
|
41
|
-
} else {
|
|
42
|
-
discounts += amount;
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
const amount = preventNaN(order?.promoCode?.amount);
|
|
46
|
-
if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
|
|
47
|
-
discounts += Math.min(amount, order?.promoCode?.maxAmount);
|
|
48
|
-
} else {
|
|
49
|
-
discounts += amount;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Sahab Services Discounts
|
|
55
|
-
(order?.services ?? [])?.forEach((carService) => {
|
|
56
|
-
(carService?.services || []).forEach((service) => {
|
|
57
|
-
let sahabDiscounts = [service?.discount].filter((discount) => !discount?.createdByProvider) as Discount[];
|
|
58
|
-
|
|
59
|
-
if (sahabDiscounts.length) {
|
|
60
|
-
sahabDiscounts.forEach((discount) => {
|
|
61
|
-
if (discount?.isPercentage) {
|
|
62
|
-
discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
|
|
63
|
-
} else {
|
|
64
|
-
discounts += preventNaN(discount?.amount ?? 0);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
if (order?.usedBalance && order?.usedBalance > 0) {
|
|
72
|
-
discounts += order?.usedBalance;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return discounts;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function getProviderDiscountsAmount(order: Order) {
|
|
79
|
-
const orderTotal = getServicesTotal(order);
|
|
80
|
-
let discounts = 0;
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
['CHOCO', 'ROSE', 'ركن', 'الإيمان', 'التقوى', 'نور العترة', 'Zhraa_fit', 'Greens_fit', 'لوكيت', 'المنتزه'].includes(
|
|
84
|
-
order?.promoCode?.promoCode || ''
|
|
85
|
-
)
|
|
86
|
-
) {
|
|
87
|
-
discounts += 0.05 * orderTotal;
|
|
88
|
-
} else if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && order?.promoCode?.createdByProvider) {
|
|
89
|
-
if (order?.promoCode?.isPercentage) {
|
|
90
|
-
const amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
|
|
91
|
-
if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
|
|
92
|
-
discounts += Math.min(amount, order?.promoCode?.maxAmount);
|
|
93
|
-
} else {
|
|
94
|
-
discounts += amount;
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
const amount = preventNaN(order?.promoCode?.amount);
|
|
98
|
-
|
|
99
|
-
if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
|
|
100
|
-
discounts += Math.min(amount, order?.promoCode?.maxAmount);
|
|
101
|
-
} else {
|
|
102
|
-
discounts += amount;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
(order?.services ?? [])?.forEach((carService) => {
|
|
108
|
-
(carService?.services || []).forEach((service) => {
|
|
109
|
-
let sahabDiscounts = [service?.discount].filter((discount) => discount?.createdByProvider) as Discount[];
|
|
110
|
-
|
|
111
|
-
if (sahabDiscounts.length) {
|
|
112
|
-
sahabDiscounts.forEach((discount) => {
|
|
113
|
-
if (discount?.isPercentage) {
|
|
114
|
-
discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
|
|
115
|
-
} else {
|
|
116
|
-
discounts += preventNaN(discount?.amount ?? 0);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
return discounts;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export function getOrderTotalDiscountsAmount(order: Order) {
|
|
127
|
-
return getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function getCustomerTotalToPay(order: Order) {
|
|
131
|
-
const sahabExtraPaymentAmount = order?.paymentType === OrderPaymentType.CASH ? order?.sahabExtraPaymentAmount || 0 : 0;
|
|
132
|
-
const orderTotal = getServicesTotal(order) + sahabExtraPaymentAmount;
|
|
133
|
-
const discounts = getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
|
|
134
|
-
|
|
135
|
-
return orderTotal - discounts;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function getProviderTransferAmount(order: Order) {
|
|
139
|
-
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
140
|
-
const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
|
|
141
|
-
|
|
142
|
-
const originalPrice = orderTotal / (1 + preventNaN(order?.sahabClientPercentage ?? 0));
|
|
143
|
-
const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
|
|
144
|
-
|
|
145
|
-
if (order?.paymentType === OrderPaymentType.CASH) {
|
|
146
|
-
return minimumProviderAmount - paymentAmount;
|
|
147
|
-
} else {
|
|
148
|
-
const onlinePercentage = getOnlineProcessingFees(order);
|
|
149
|
-
const sahabFees = paymentAmount < minimumProviderAmount ? orderTotal - minimumProviderAmount : paymentAmount - minimumProviderAmount;
|
|
150
|
-
|
|
151
|
-
return orderTotal - sahabFees - onlinePercentage;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export function getProviderProfits(order: Order) {
|
|
156
|
-
const sahabExtraPaymentAmount = order?.paymentType === OrderPaymentType.CASH ? order?.sahabExtraPaymentAmount || 0 : 0;
|
|
157
|
-
return order?.paymentType === OrderPaymentType.CASH
|
|
158
|
-
? getCustomerTotalToPay(order) - sahabExtraPaymentAmount + getProviderTransferAmount(order)
|
|
159
|
-
: getProviderTransferAmount(order);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export function getOnlineProcessingFees(order: Order) {
|
|
163
|
-
if (order?.paymentType !== OrderPaymentType.CASH) {
|
|
164
|
-
const orderTotal = preventNaN(order?.paymentAmount ?? 0);
|
|
165
|
-
|
|
166
|
-
return preventNaN(order?.sahabOnlinePercentage ?? 0) * orderTotal;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return 0;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function getSahabFees(order: Order) {
|
|
173
|
-
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
174
|
-
const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
|
|
175
|
-
|
|
176
|
-
const originalPrice = orderTotal / (1 + preventNaN(order?.sahabClientPercentage ?? 0));
|
|
177
|
-
const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
|
|
178
|
-
|
|
179
|
-
const sahabExtraPaymentAmount = order?.paymentType === OrderPaymentType.CASH ? order?.sahabExtraPaymentAmount || 0 : 0;
|
|
180
|
-
|
|
181
|
-
if (paymentAmount < minimumProviderAmount) {
|
|
182
|
-
return 0 + sahabExtraPaymentAmount;
|
|
183
|
-
} else {
|
|
184
|
-
return paymentAmount - minimumProviderAmount + sahabExtraPaymentAmount;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export function calcOrdersData(orders: Order[]) {
|
|
189
|
-
//Filter Orders
|
|
190
|
-
const cashOrders = orders.filter((o) => o.paymentType === OrderPaymentType.CASH);
|
|
191
|
-
const onlineOrders = orders.filter((o) => o.paymentType !== OrderPaymentType.CASH);
|
|
192
|
-
|
|
193
|
-
const sahabOnlineRevenue = sum(onlineOrders.map((o) => getSahabFees(o)));
|
|
194
|
-
const totalOnlineTransfer = sum(onlineOrders.map((o) => getProviderTransferAmount(o)));
|
|
195
|
-
const providerOnlineRevenue = sum(onlineOrders.map((o) => getProviderProfits(o)));
|
|
196
|
-
|
|
197
|
-
const sahabCashRevenue = sum(cashOrders.map((o) => getSahabFees(o)));
|
|
198
|
-
const totalCashTransfer = sum(cashOrders.map((o) => getProviderTransferAmount(o)));
|
|
199
|
-
const providerCashRevenue = sum(cashOrders.map((o) => getProviderProfits(o)));
|
|
200
|
-
|
|
201
|
-
//Calculate Percentages
|
|
202
|
-
const totalTransfer = totalOnlineTransfer + totalCashTransfer;
|
|
203
|
-
const totalSahabRevenue = sahabOnlineRevenue + sahabCashRevenue;
|
|
204
|
-
const totalProviderRevenue = providerOnlineRevenue + providerCashRevenue;
|
|
205
|
-
|
|
206
|
-
return {
|
|
207
|
-
name: orders?.[0]?.providerName?.ar ?? '',
|
|
208
|
-
numCashOrders: cashOrders.length,
|
|
209
|
-
numOnlineOrders: onlineOrders.length,
|
|
210
|
-
providerCashRevenue: toFixed(providerCashRevenue),
|
|
211
|
-
providerOnlineRevenue: toFixed(providerOnlineRevenue),
|
|
212
|
-
totalProviderRevenue: toFixed(totalProviderRevenue),
|
|
213
|
-
totalCashTransfer: toFixed(totalCashTransfer),
|
|
214
|
-
totalOnlineTransfer: toFixed(totalOnlineTransfer),
|
|
215
|
-
sahabCashRevenue: toFixed(sahabCashRevenue),
|
|
216
|
-
sahabOnlineRevenue: toFixed(sahabOnlineRevenue),
|
|
217
|
-
totalTransfer: toFixed(totalTransfer),
|
|
218
|
-
totalSahabRevenue: toFixed(totalSahabRevenue),
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export function calculatePromoCodeAmount(order: Order) {
|
|
223
|
-
if (isEmpty(order?.promoCode) || (order?.promoCode?.amount && order?.promoCode?.amount <= 0)) {
|
|
224
|
-
return 0;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order) - getSahabDiscountsAmount(order);
|
|
228
|
-
if (order?.promoCode?.isPercentage) {
|
|
229
|
-
return (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
|
|
230
|
-
}
|
|
231
|
-
return preventNaN(order?.promoCode?.amount);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export function calculateDiscountAmount(order: Order) {
|
|
235
|
-
let discounts = 0;
|
|
236
|
-
|
|
237
|
-
if (!order?.promoCode?.amount && !order?.services?.some((service) => service?.services?.some((s) => s?.discount))) {
|
|
238
|
-
return 0;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const orderTotal = getServicesTotal(order);
|
|
242
|
-
if (order?.promoCode?.isPercentage) {
|
|
243
|
-
discounts += (preventNaN(order?.promoCode?.amount ?? 0) / 100) * orderTotal;
|
|
244
|
-
} else if (order?.promoCode?.amount) {
|
|
245
|
-
discounts += preventNaN(order?.promoCode?.amount ?? 0);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if (order?.services?.some((service) => service?.services?.some((s) => s?.discount))) {
|
|
249
|
-
(order?.services ?? [])?.forEach((carService) => {
|
|
250
|
-
(carService?.services || []).forEach((service) => {
|
|
251
|
-
let sahabDiscounts = [service?.discount] as Discount[];
|
|
252
|
-
|
|
253
|
-
if (sahabDiscounts.length) {
|
|
254
|
-
sahabDiscounts.forEach((discount) => {
|
|
255
|
-
if (discount?.isPercentage) {
|
|
256
|
-
discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
|
|
257
|
-
} else {
|
|
258
|
-
discounts += preventNaN(discount?.amount ?? 0);
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return discounts;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function getServiceTotal(providedService: ProvidedService | ProvidedAddon) {
|
|
270
|
-
let total = 0;
|
|
271
|
-
|
|
272
|
-
total += providedService?.price?.price ?? 0;
|
|
273
|
-
let sahabDiscounts = [providedService?.discount] as Discount[];
|
|
274
|
-
|
|
275
|
-
if (sahabDiscounts.length) {
|
|
276
|
-
sahabDiscounts.forEach((discount) => {
|
|
277
|
-
if (discount?.isPercentage) {
|
|
278
|
-
total -= (preventNaN(discount?.amount ?? 0) / 100) * (providedService?.price?.price ?? 0);
|
|
279
|
-
} else {
|
|
280
|
-
total -= preventNaN(discount?.amount ?? 0);
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (total < 0) {
|
|
286
|
-
total = 0;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const clientPercentage = 0.03 * total;
|
|
290
|
-
|
|
291
|
-
return total + clientPercentage;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export function calculateOrderPercentage({
|
|
295
|
-
isPaid,
|
|
296
|
-
paymentId,
|
|
297
|
-
providerPercentage,
|
|
298
|
-
}: {
|
|
299
|
-
isPaid: boolean;
|
|
300
|
-
paymentId: string;
|
|
301
|
-
providerPercentage: number;
|
|
302
|
-
}) {
|
|
303
|
-
if (isPaid && paymentId && !isEmpty(providerPercentage)) {
|
|
304
|
-
return ONLINE_SAHAB_PERCENTAGE + SAHAB_PERCENTAGE + CLIENT_SAHAB_PERCENTAGE + providerPercentage;
|
|
305
|
-
} else {
|
|
306
|
-
return SAHAB_PERCENTAGE + CLIENT_SAHAB_PERCENTAGE + providerPercentage;
|
|
307
|
-
}
|
|
308
|
-
}
|
package/ts/utils.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export function preventNaN(x?: number | string) {
|
|
2
|
-
if (x === undefined || x === null || isNaN(x as any)) {
|
|
3
|
-
return 0;
|
|
4
|
-
}
|
|
5
|
-
return Number(x);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const toFixed = (num: any) => {
|
|
9
|
-
const decimal_places = 2;
|
|
10
|
-
num = Number(num || 0);
|
|
11
|
-
return (Math.round(num * 10 ** decimal_places) / 10 ** decimal_places).toFixed(decimal_places);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const isEmpty = (text: any) => {
|
|
15
|
-
const txt = typeof text === 'string' ? text.trim() : text;
|
|
16
|
-
|
|
17
|
-
if ((txt !== 0 && !txt) || txt === '' || txt === null || txt === 'null' || txt === ' ' || txt === undefined || txt === 'undefined') {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
return false;
|
|
21
|
-
};
|