sahab-utils 1.2.7 → 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/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './constants/types'
2
- export * from './ts/calcs'
package/ts/calcs.ts DELETED
@@ -1,345 +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
- let amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
39
-
40
- // Check if promo code is for specific services/addons only
41
- if (
42
- (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
43
- (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
44
- ) {
45
- let applicableTotal = 0;
46
- order?.services?.forEach((carService) => {
47
- carService?.services?.forEach((service) => {
48
- if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
49
- applicableTotal += service?.price?.price || 0;
50
- }
51
- });
52
- });
53
-
54
- amount = (preventNaN(order?.promoCode?.amount) / 100) * applicableTotal;
55
- }
56
-
57
- if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
58
- discounts += Math.min(amount, order?.promoCode?.maxAmount);
59
- } else {
60
- discounts += amount;
61
- }
62
- } else {
63
- let amount = preventNaN(order?.promoCode?.amount);
64
-
65
- // Check if promo code is for specific services/addons only
66
- if (
67
- (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
68
- (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
69
- ) {
70
- let applicableTotal = 0;
71
- order?.services?.forEach((carService) => {
72
- carService?.services?.forEach((service) => {
73
- if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
74
- applicableTotal += service?.price?.price || 0;
75
- }
76
- });
77
- });
78
-
79
- amount = Math.min(amount, applicableTotal);
80
- }
81
-
82
- if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
83
- discounts += Math.min(amount, order?.promoCode?.maxAmount);
84
- } else {
85
- discounts += amount;
86
- }
87
- }
88
- }
89
-
90
- // Sahab Services Discounts
91
- (order?.services ?? [])?.forEach((carService) => {
92
- (carService?.services || []).forEach((service) => {
93
- let sahabDiscounts = [service?.discount].filter((discount) => !discount?.createdByProvider) as Discount[];
94
-
95
- if (sahabDiscounts.length) {
96
- sahabDiscounts.forEach((discount) => {
97
- // Check if discount is for specific services/addons only
98
- if (discount?.servicesIds && discount?.servicesIds.length > 0) {
99
- if (!discount?.servicesIds?.includes(service?.serviceId || '')) {
100
- return;
101
- }
102
- }
103
-
104
- if (discount?.addonsIds && discount?.addonsIds.length > 0) {
105
- if (!discount?.addonsIds?.includes(service?.addonId || '')) {
106
- return;
107
- }
108
- }
109
-
110
- if (discount?.isPercentage) {
111
- discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
112
- } else {
113
- discounts += preventNaN(discount?.amount ?? 0);
114
- }
115
- });
116
- }
117
- });
118
- });
119
-
120
- if (order?.usedBalance && order?.usedBalance > 0) {
121
- discounts += order?.usedBalance;
122
- }
123
-
124
- return discounts;
125
- }
126
-
127
- export function getProviderDiscountsAmount(order: Order) {
128
- const orderTotal = getServicesTotal(order);
129
- let discounts = 0;
130
-
131
- if (
132
- ['CHOCO', 'ROSE', 'ركن', 'الإيمان', 'التقوى', 'نور العترة', 'Zhraa_fit', 'Greens_fit', 'لوكيت', 'المنتزه'].includes(
133
- order?.promoCode?.promoCode || ''
134
- )
135
- ) {
136
- discounts += 0.05 * orderTotal;
137
- } else if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && order?.promoCode?.createdByProvider) {
138
- if (order?.promoCode?.isPercentage) {
139
- let amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
140
-
141
- // Check if promo code is for specific services/addons only
142
- if (
143
- (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
144
- (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
145
- ) {
146
- let applicableTotal = 0;
147
- order?.services?.forEach((carService) => {
148
- carService?.services?.forEach((service) => {
149
- if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
150
- applicableTotal += service?.price?.price || 0;
151
- }
152
- });
153
- });
154
-
155
- amount = (preventNaN(order?.promoCode?.amount) / 100) * applicableTotal;
156
- }
157
-
158
- if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
159
- discounts += Math.min(amount, order?.promoCode?.maxAmount);
160
- } else {
161
- discounts += amount;
162
- }
163
- } else {
164
- let amount = preventNaN(order?.promoCode?.amount);
165
-
166
- // Check if promo code is for specific services/addons only
167
- if (
168
- (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
169
- (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
170
- ) {
171
- let applicableTotal = 0;
172
- order?.services?.forEach((carService) => {
173
- carService?.services?.forEach((service) => {
174
- if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
175
- applicableTotal += service?.price?.price || 0;
176
- }
177
- });
178
- });
179
-
180
- amount = Math.min(amount, applicableTotal);
181
- }
182
-
183
- if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
184
- discounts += Math.min(amount, order?.promoCode?.maxAmount);
185
- } else {
186
- discounts += amount;
187
- }
188
- }
189
- }
190
-
191
- (order?.services ?? [])?.forEach((carService) => {
192
- (carService?.services || []).forEach((service) => {
193
- let sahabDiscounts = [service?.discount].filter((discount) => discount?.createdByProvider) as Discount[];
194
-
195
- if (sahabDiscounts.length) {
196
- sahabDiscounts.forEach((discount) => {
197
- // Check if discount is for specific services/addons only
198
- if (discount?.servicesIds && discount?.servicesIds.length > 0) {
199
- if (!discount?.servicesIds?.includes(service?.serviceId || '')) {
200
- return;
201
- }
202
- }
203
-
204
- if (discount?.addonsIds && discount?.addonsIds.length > 0) {
205
- if (!discount?.addonsIds?.includes(service?.addonId || '')) {
206
- return;
207
- }
208
- }
209
-
210
- if (discount?.isPercentage) {
211
- discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
212
- } else {
213
- discounts += preventNaN(discount?.amount ?? 0);
214
- }
215
- });
216
- }
217
- });
218
- });
219
-
220
- return discounts;
221
- }
222
-
223
- export function getOrderTotalDiscountsAmount(order: Order) {
224
- return getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
225
- }
226
-
227
- export function getCustomerTotalToPay(order: Order) {
228
- const sahabExtraPaymentAmount = order?.paymentType === OrderPaymentType.CASH ? order?.sahabExtraPaymentAmount || 0 : 0;
229
- const orderTotal = getServicesTotal(order) + sahabExtraPaymentAmount;
230
- const discounts = getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
231
-
232
- return orderTotal - discounts;
233
- }
234
-
235
- export function getProviderTransferAmount(order: Order) {
236
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
237
- const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
238
-
239
- const originalPrice = orderTotal / (1 + preventNaN(order?.sahabClientPercentage ?? 0));
240
- const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
241
-
242
- if (order?.paymentType === OrderPaymentType.CASH) {
243
- return minimumProviderAmount - paymentAmount;
244
- } else {
245
- const onlinePercentage = getOnlineProcessingFees(order);
246
- const sahabFees = paymentAmount < minimumProviderAmount ? orderTotal - minimumProviderAmount : paymentAmount - minimumProviderAmount;
247
-
248
- return orderTotal - sahabFees - onlinePercentage;
249
- }
250
- }
251
-
252
- export function getProviderProfits(order: Order) {
253
- const sahabExtraPaymentAmount = order?.paymentType === OrderPaymentType.CASH ? order?.sahabExtraPaymentAmount || 0 : 0;
254
- return order?.paymentType === OrderPaymentType.CASH
255
- ? getCustomerTotalToPay(order) - sahabExtraPaymentAmount + getProviderTransferAmount(order)
256
- : getProviderTransferAmount(order);
257
- }
258
-
259
- export function getOnlineProcessingFees(order: Order) {
260
- if (order?.paymentType !== OrderPaymentType.CASH) {
261
- const orderTotal = preventNaN(order?.paymentAmount ?? 0);
262
-
263
- return preventNaN(order?.sahabOnlinePercentage ?? 0) * orderTotal;
264
- }
265
-
266
- return 0;
267
- }
268
-
269
- export function getSahabFees(order: Order) {
270
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
271
- const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
272
-
273
- const originalPrice = orderTotal / (1 + preventNaN(order?.sahabClientPercentage ?? 0));
274
- const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
275
-
276
- const sahabExtraPaymentAmount = order?.paymentType === OrderPaymentType.CASH ? order?.sahabExtraPaymentAmount || 0 : 0;
277
-
278
- if (paymentAmount < minimumProviderAmount) {
279
- return 0 + sahabExtraPaymentAmount;
280
- } else {
281
- return paymentAmount - minimumProviderAmount + sahabExtraPaymentAmount;
282
- }
283
- }
284
-
285
- export function calcOrdersData(orders: Order[]) {
286
- //Filter Orders
287
- const cashOrders = orders.filter((o) => o.paymentType === OrderPaymentType.CASH);
288
- const onlineOrders = orders.filter((o) => o.paymentType !== OrderPaymentType.CASH);
289
-
290
- const sahabOnlineRevenue = sum(onlineOrders.map((o) => getSahabFees(o)));
291
- const totalOnlineTransfer = sum(onlineOrders.map((o) => getProviderTransferAmount(o)));
292
- const providerOnlineRevenue = sum(onlineOrders.map((o) => getProviderProfits(o)));
293
-
294
- const sahabCashRevenue = sum(cashOrders.map((o) => getSahabFees(o)));
295
- const totalCashTransfer = sum(cashOrders.map((o) => getProviderTransferAmount(o)));
296
- const providerCashRevenue = sum(cashOrders.map((o) => getProviderProfits(o)));
297
-
298
- //Calculate Percentages
299
- const totalTransfer = totalOnlineTransfer + totalCashTransfer;
300
- const totalSahabRevenue = sahabOnlineRevenue + sahabCashRevenue;
301
- const totalProviderRevenue = providerOnlineRevenue + providerCashRevenue;
302
-
303
- return {
304
- name: orders?.[0]?.providerName?.ar ?? '',
305
- numCashOrders: cashOrders.length,
306
- numOnlineOrders: onlineOrders.length,
307
- providerCashRevenue: toFixed(providerCashRevenue),
308
- providerOnlineRevenue: toFixed(providerOnlineRevenue),
309
- totalProviderRevenue: toFixed(totalProviderRevenue),
310
- totalCashTransfer: toFixed(totalCashTransfer),
311
- totalOnlineTransfer: toFixed(totalOnlineTransfer),
312
- sahabCashRevenue: toFixed(sahabCashRevenue),
313
- sahabOnlineRevenue: toFixed(sahabOnlineRevenue),
314
- totalTransfer: toFixed(totalTransfer),
315
- totalSahabRevenue: toFixed(totalSahabRevenue),
316
- };
317
- }
318
-
319
- export function calculatePromoCodeAmount(order: Order) {
320
- if (isEmpty(order?.promoCode) || (order?.promoCode?.amount && order?.promoCode?.amount <= 0)) {
321
- return 0;
322
- }
323
-
324
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order) - getSahabDiscountsAmount(order);
325
- if (order?.promoCode?.isPercentage) {
326
- return (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
327
- }
328
- return preventNaN(order?.promoCode?.amount);
329
- }
330
-
331
- export function calculateOrderPercentage({
332
- isPaid,
333
- paymentId,
334
- providerPercentage,
335
- }: {
336
- isPaid: boolean;
337
- paymentId: string;
338
- providerPercentage: number;
339
- }) {
340
- if (isPaid && paymentId && !isEmpty(providerPercentage)) {
341
- return ONLINE_SAHAB_PERCENTAGE + SAHAB_PERCENTAGE + CLIENT_SAHAB_PERCENTAGE + providerPercentage;
342
- } else {
343
- return SAHAB_PERCENTAGE + CLIENT_SAHAB_PERCENTAGE + providerPercentage;
344
- }
345
- }
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
- };