sahab-utils 1.0.0 → 1.2.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/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './constants/types'
1
+ export * from './constants/types'
2
2
  export * from './ts/calcs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sahab-utils",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "This package containts types and utils fuctions",
5
5
  "main": "index.ts",
6
6
  "scripts": {
package/ts/calcs.ts CHANGED
@@ -1,195 +1,195 @@
1
- import { CarService, Order, OrderPaymentType, ProvidedAddon, ProvidedService, ReduxOrder } from '../constants/types';
2
- import { preventNaN } from './utils';
3
-
4
- export function getServicesTotal(order: Order | ReduxOrder | undefined) {
5
- let total = 0;
6
- if (order) {
7
- if (order.services) {
8
- order.services.forEach((carService) => {
9
- carService.services.forEach((service) => {
10
- if (service.price) {
11
- const price = service.price.price;
12
- total += price;
13
- }
14
- });
15
- });
16
- }
17
- }
18
- return total;
19
- }
20
-
21
- export const getSahabDiscountsAmount = (order: Order | ReduxOrder | undefined) => {
22
- const orderTotal = getServicesTotal(order);
23
- let discounts = 0;
24
-
25
- if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && !order?.promoCode?.createdByProvider) {
26
- if (order?.promoCode?.isPercentage) {
27
- discounts += (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
28
- } else {
29
- discounts += preventNaN(order?.promoCode?.amount);
30
- }
31
- }
32
-
33
- // Sahab Services Discounts
34
- (order?.services ?? [])?.forEach((carService) => {
35
- (carService?.services || []).forEach((service) => {
36
- let sahabDiscounts = (service?.discounts ?? []).filter((discount) => !discount?.createdByProvider);
37
-
38
- if (sahabDiscounts.length) {
39
- sahabDiscounts.forEach((discount) => {
40
- if (discount.isPercentage) {
41
- discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
42
- } else {
43
- discounts += preventNaN(discount?.amount ?? 0);
44
- }
45
- });
46
- }
47
- });
48
- });
49
-
50
- if (order?.usedBalance && order?.usedBalance > 0) {
51
- discounts += order?.usedBalance;
52
- }
53
-
54
- return discounts;
55
- };
56
-
57
- export const getProviderDiscountsAmount = (order: Order | ReduxOrder | undefined) => {
58
- const orderTotal = getServicesTotal(order);
59
- let discounts = 0;
60
-
61
- if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && order?.promoCode?.createdByProvider) {
62
- if (order?.promoCode?.isPercentage) {
63
- discounts += (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
64
- } else {
65
- discounts += preventNaN(order?.promoCode?.amount);
66
- }
67
- }
68
-
69
- (order?.services ?? [])?.forEach((carService) => {
70
- (carService?.services || []).forEach((service) => {
71
- let sahabDiscounts = (service?.discounts ?? []).filter((discount) => discount?.createdByProvider);
72
-
73
- if (sahabDiscounts.length) {
74
- sahabDiscounts.forEach((discount) => {
75
- if (discount.isPercentage) {
76
- discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
77
- } else {
78
- discounts += preventNaN(discount?.amount ?? 0);
79
- }
80
- });
81
- }
82
- });
83
- });
84
-
85
- return discounts;
86
- };
87
-
88
- export const getCustomerTotalToPay = (order: Order | ReduxOrder | undefined) => {
89
- const orderTotal = getServicesTotal(order);
90
- const discounts = getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
91
-
92
- return orderTotal - discounts;
93
- };
94
-
95
- export const getProviderTransferAmount = (order: Order | ReduxOrder | undefined) => {
96
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
97
- const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
98
-
99
- const originalPrice = orderTotal / (1 + (order?.sahabClientPercentage ?? 0));
100
- const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
101
-
102
- if (order?.paymentType === OrderPaymentType.CASH) {
103
- return minimumProviderAmount - paymentAmount;
104
- } else {
105
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
106
- const onlinePercentage = getOnlineProcessingFees(order);
107
- const sahabFees = paymentAmount < minimumProviderAmount ? orderTotal - minimumProviderAmount : paymentAmount - minimumProviderAmount;
108
-
109
- return orderTotal - sahabFees - onlinePercentage;
110
- }
111
- };
112
-
113
- export const getProviderProfits = (order: Order | ReduxOrder | undefined) => {
114
- return order?.paymentType === OrderPaymentType.CASH
115
- ? getCustomerTotalToPay(order) + getProviderTransferAmount(order)
116
- : getProviderTransferAmount(order);
117
- };
118
-
119
- export const getOnlineProcessingFees = (order: Order | ReduxOrder | undefined) => {
120
- if (order?.paymentType !== OrderPaymentType.CASH) {
121
- const orderTotal = preventNaN(order?.paymentAmount ?? 0);
122
-
123
- return preventNaN(order?.sahabOnlinePercentage ?? 0) * orderTotal;
124
- }
125
-
126
- return 0;
127
- };
128
-
129
- export const getSahabFees = (order: Order | ReduxOrder | undefined) => {
130
- const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
131
- const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
132
-
133
- const originalPrice = orderTotal / (1 + (order?.sahabClientPercentage ?? 0));
134
- const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
135
-
136
- if (paymentAmount < minimumProviderAmount) {
137
- return 0;
138
- } else {
139
- return paymentAmount - minimumProviderAmount;
140
- }
141
- };
142
-
143
- export function calculateDiscountAmount(order: Order) {
144
- let discounts = 0;
145
-
146
- if (!order?.promoCode?.amount && !order?.services?.some((service) => service?.services?.some((s) => s?.discounts?.length))) {
147
- return 0;
148
- }
149
-
150
- const orderTotal = getServicesTotal(order);
151
- if (order?.promoCode?.isPercentage) {
152
- discounts += (preventNaN(order?.promoCode?.amount ?? 0) / 100) * orderTotal;
153
- } else if (order?.promoCode?.amount) {
154
- discounts += preventNaN(order?.promoCode?.amount ?? 0);
155
- }
156
-
157
- if (order?.services?.some((service) => service?.services?.some((s) => s?.discounts?.length))) {
158
- (order?.services ?? [])?.forEach((carService) => {
159
- (carService?.services || []).forEach((service) => {
160
- let sahabDiscounts = service?.discounts ?? [];
161
-
162
- if (sahabDiscounts.length) {
163
- sahabDiscounts.forEach((discount) => {
164
- if (discount.isPercentage) {
165
- discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
166
- } else {
167
- discounts += preventNaN(discount?.amount ?? 0);
168
- }
169
- });
170
- }
171
- });
172
- });
173
- }
174
-
175
- return discounts;
176
- }
177
-
178
- export function getServiceTotal(providedService: ProvidedService | ProvidedAddon) {
179
- let total = 0;
180
-
181
- total += providedService?.price?.price ?? 0;
182
- let sahabDiscounts = providedService?.discounts ?? [];
183
-
184
- if (sahabDiscounts.length) {
185
- sahabDiscounts.forEach((discount) => {
186
- if (discount.isPercentage) {
187
- total -= (preventNaN(discount?.amount ?? 0) / 100) * (providedService?.price?.price ?? 0);
188
- } else {
189
- total -= preventNaN(discount?.amount ?? 0);
190
- }
191
- });
192
- }
193
-
194
- return total;
195
- }
1
+ import { CarService, Order, OrderPaymentType, ProvidedAddon, ProvidedService, ReduxOrder } from '../constants/types';
2
+ import { preventNaN } from './utils';
3
+
4
+ export function getServicesTotal(order: Order | ReduxOrder | undefined) {
5
+ let total = 0;
6
+ if (order) {
7
+ if (order.services) {
8
+ order.services.forEach((carService) => {
9
+ carService.services.forEach((service) => {
10
+ if (service.price) {
11
+ const price = service.price.price;
12
+ total += price;
13
+ }
14
+ });
15
+ });
16
+ }
17
+ }
18
+ return total;
19
+ }
20
+
21
+ export const getSahabDiscountsAmount = (order: Order | ReduxOrder | undefined) => {
22
+ const orderTotal = getServicesTotal(order);
23
+ let discounts = 0;
24
+
25
+ if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && !order?.promoCode?.createdByProvider) {
26
+ if (order?.promoCode?.isPercentage) {
27
+ discounts += (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
28
+ } else {
29
+ discounts += preventNaN(order?.promoCode?.amount);
30
+ }
31
+ }
32
+
33
+ // Sahab Services Discounts
34
+ (order?.services ?? [])?.forEach((carService) => {
35
+ (carService?.services || []).forEach((service) => {
36
+ let sahabDiscounts = (service?.discounts ?? []).filter((discount) => !discount?.createdByProvider);
37
+
38
+ if (sahabDiscounts.length) {
39
+ sahabDiscounts.forEach((discount) => {
40
+ if (discount.isPercentage) {
41
+ discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
42
+ } else {
43
+ discounts += preventNaN(discount?.amount ?? 0);
44
+ }
45
+ });
46
+ }
47
+ });
48
+ });
49
+
50
+ if (order?.usedBalance && order?.usedBalance > 0) {
51
+ discounts += order?.usedBalance;
52
+ }
53
+
54
+ return discounts;
55
+ };
56
+
57
+ export const getProviderDiscountsAmount = (order: Order | ReduxOrder | undefined) => {
58
+ const orderTotal = getServicesTotal(order);
59
+ let discounts = 0;
60
+
61
+ if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && order?.promoCode?.createdByProvider) {
62
+ if (order?.promoCode?.isPercentage) {
63
+ discounts += (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
64
+ } else {
65
+ discounts += preventNaN(order?.promoCode?.amount);
66
+ }
67
+ }
68
+
69
+ (order?.services ?? [])?.forEach((carService) => {
70
+ (carService?.services || []).forEach((service) => {
71
+ let sahabDiscounts = (service?.discounts ?? []).filter((discount) => discount?.createdByProvider);
72
+
73
+ if (sahabDiscounts.length) {
74
+ sahabDiscounts.forEach((discount) => {
75
+ if (discount.isPercentage) {
76
+ discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
77
+ } else {
78
+ discounts += preventNaN(discount?.amount ?? 0);
79
+ }
80
+ });
81
+ }
82
+ });
83
+ });
84
+
85
+ return discounts;
86
+ };
87
+
88
+ export const getCustomerTotalToPay = (order: Order | ReduxOrder | undefined) => {
89
+ const orderTotal = getServicesTotal(order);
90
+ const discounts = getSahabDiscountsAmount(order) + getProviderDiscountsAmount(order);
91
+
92
+ return orderTotal - discounts;
93
+ };
94
+
95
+ export const getProviderTransferAmount = (order: Order | ReduxOrder | undefined) => {
96
+ const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
97
+ const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
98
+
99
+ const originalPrice = orderTotal / (1 + (order?.sahabClientPercentage ?? 0));
100
+ const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
101
+
102
+ if (order?.paymentType === OrderPaymentType.CASH) {
103
+ return minimumProviderAmount - paymentAmount;
104
+ } else {
105
+ const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
106
+ const onlinePercentage = getOnlineProcessingFees(order);
107
+ const sahabFees = paymentAmount < minimumProviderAmount ? orderTotal - minimumProviderAmount : paymentAmount - minimumProviderAmount;
108
+
109
+ return orderTotal - sahabFees - onlinePercentage;
110
+ }
111
+ };
112
+
113
+ export const getProviderProfits = (order: Order | ReduxOrder | undefined) => {
114
+ return order?.paymentType === OrderPaymentType.CASH
115
+ ? getCustomerTotalToPay(order) + getProviderTransferAmount(order)
116
+ : getProviderTransferAmount(order);
117
+ };
118
+
119
+ export const getOnlineProcessingFees = (order: Order | ReduxOrder | undefined) => {
120
+ if (order?.paymentType !== OrderPaymentType.CASH) {
121
+ const orderTotal = preventNaN(order?.paymentAmount ?? 0);
122
+
123
+ return preventNaN(order?.sahabOnlinePercentage ?? 0) * orderTotal;
124
+ }
125
+
126
+ return 0;
127
+ };
128
+
129
+ export const getSahabFees = (order: Order | ReduxOrder | undefined) => {
130
+ const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
131
+ const paymentAmount = preventNaN(order?.paymentAmount ?? 0);
132
+
133
+ const originalPrice = orderTotal / (1 + (order?.sahabClientPercentage ?? 0));
134
+ const minimumProviderAmount = originalPrice - originalPrice * (order?.sahabProviderPercentage ?? 0);
135
+
136
+ if (paymentAmount < minimumProviderAmount) {
137
+ return 0;
138
+ } else {
139
+ return paymentAmount - minimumProviderAmount;
140
+ }
141
+ };
142
+
143
+ export function calculateDiscountAmount(order: Order) {
144
+ let discounts = 0;
145
+
146
+ if (!order?.promoCode?.amount && !order?.services?.some((service) => service?.services?.some((s) => s?.discounts?.length))) {
147
+ return 0;
148
+ }
149
+
150
+ const orderTotal = getServicesTotal(order);
151
+ if (order?.promoCode?.isPercentage) {
152
+ discounts += (preventNaN(order?.promoCode?.amount ?? 0) / 100) * orderTotal;
153
+ } else if (order?.promoCode?.amount) {
154
+ discounts += preventNaN(order?.promoCode?.amount ?? 0);
155
+ }
156
+
157
+ if (order?.services?.some((service) => service?.services?.some((s) => s?.discounts?.length))) {
158
+ (order?.services ?? [])?.forEach((carService) => {
159
+ (carService?.services || []).forEach((service) => {
160
+ let sahabDiscounts = service?.discounts ?? [];
161
+
162
+ if (sahabDiscounts.length) {
163
+ sahabDiscounts.forEach((discount) => {
164
+ if (discount.isPercentage) {
165
+ discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
166
+ } else {
167
+ discounts += preventNaN(discount?.amount ?? 0);
168
+ }
169
+ });
170
+ }
171
+ });
172
+ });
173
+ }
174
+
175
+ return discounts;
176
+ }
177
+
178
+ export function getServiceTotal(providedService: ProvidedService | ProvidedAddon) {
179
+ let total = 0;
180
+
181
+ total += providedService?.price?.price ?? 0;
182
+ let sahabDiscounts = providedService?.discounts ?? [];
183
+
184
+ if (sahabDiscounts.length) {
185
+ sahabDiscounts.forEach((discount) => {
186
+ if (discount.isPercentage) {
187
+ total -= (preventNaN(discount?.amount ?? 0) / 100) * (providedService?.price?.price ?? 0);
188
+ } else {
189
+ total -= preventNaN(discount?.amount ?? 0);
190
+ }
191
+ });
192
+ }
193
+
194
+ return total;
195
+ }
package/ts/utils.ts CHANGED
@@ -1,6 +1,6 @@
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);
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
6
  }