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.
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RegistrationSection = exports.ReviewStatus = exports.ComplaintStatus = exports.ComplaintType = exports.MovementType = exports.OrderPaymentType = exports.OrderStatus = exports.CarSize = void 0;
4
+ var CarSize;
5
+ (function (CarSize) {
6
+ CarSize[CarSize["X_SMALL"] = 0] = "X_SMALL";
7
+ CarSize[CarSize["SMALL"] = 1] = "SMALL";
8
+ CarSize[CarSize["MEDIUM"] = 2] = "MEDIUM";
9
+ CarSize[CarSize["LARGE"] = 3] = "LARGE";
10
+ CarSize[CarSize["X_LARGE"] = 4] = "X_LARGE";
11
+ })(CarSize || (exports.CarSize = CarSize = {}));
12
+ var OrderStatus;
13
+ (function (OrderStatus) {
14
+ OrderStatus[OrderStatus["PENDING"] = 0] = "PENDING";
15
+ OrderStatus[OrderStatus["CONFIRMED"] = 1] = "CONFIRMED";
16
+ OrderStatus[OrderStatus["REJECTED"] = 2] = "REJECTED";
17
+ OrderStatus[OrderStatus["EXPIRED"] = 3] = "EXPIRED";
18
+ OrderStatus[OrderStatus["IN_PROGRESS"] = 4] = "IN_PROGRESS";
19
+ OrderStatus[OrderStatus["COMPLETED"] = 5] = "COMPLETED";
20
+ OrderStatus[OrderStatus["CANCELLED"] = 6] = "CANCELLED";
21
+ OrderStatus[OrderStatus["RATED"] = 7] = "RATED";
22
+ OrderStatus[OrderStatus["STARTED"] = 8] = "STARTED";
23
+ })(OrderStatus || (exports.OrderStatus = OrderStatus = {}));
24
+ var OrderPaymentType;
25
+ (function (OrderPaymentType) {
26
+ OrderPaymentType[OrderPaymentType["APPLE_PAY"] = 0] = "APPLE_PAY";
27
+ OrderPaymentType[OrderPaymentType["CREDIT"] = 1] = "CREDIT";
28
+ OrderPaymentType[OrderPaymentType["CASH"] = 2] = "CASH";
29
+ OrderPaymentType[OrderPaymentType["GOOGLE_PAY"] = 3] = "GOOGLE_PAY";
30
+ OrderPaymentType[OrderPaymentType["SAMSUNG_PAY"] = 4] = "SAMSUNG_PAY";
31
+ OrderPaymentType[OrderPaymentType["STC_PAY"] = 5] = "STC_PAY";
32
+ OrderPaymentType[OrderPaymentType["TABBY"] = 6] = "TABBY";
33
+ OrderPaymentType[OrderPaymentType["TAMARA"] = 7] = "TAMARA";
34
+ })(OrderPaymentType || (exports.OrderPaymentType = OrderPaymentType = {}));
35
+ var MovementType;
36
+ (function (MovementType) {
37
+ MovementType[MovementType["CLIENT_ORDER_PAYMENT"] = 0] = "CLIENT_ORDER_PAYMENT";
38
+ MovementType[MovementType["CLIENT_BALANCE_TOP_UP"] = 1] = "CLIENT_BALANCE_TOP_UP";
39
+ MovementType[MovementType["PROVIDER_NEGATIVE_BALANCE_PAYMENT"] = 2] = "PROVIDER_NEGATIVE_BALANCE_PAYMENT";
40
+ MovementType[MovementType["PROVIDER_POSITIVE_BALANCE_PAYMENT"] = 3] = "PROVIDER_POSITIVE_BALANCE_PAYMENT";
41
+ MovementType[MovementType["ORDER_REFUND"] = 4] = "ORDER_REFUND";
42
+ MovementType[MovementType["SAHAB_PAYMENT_TO_PROVIDER"] = 5] = "SAHAB_PAYMENT_TO_PROVIDER";
43
+ MovementType[MovementType["SAHAB_RECEIVE_FROM_PROVIDER"] = 6] = "SAHAB_RECEIVE_FROM_PROVIDER";
44
+ MovementType[MovementType["USING_BALANCE"] = 7] = "USING_BALANCE";
45
+ })(MovementType || (exports.MovementType = MovementType = {}));
46
+ var ComplaintType;
47
+ (function (ComplaintType) {
48
+ ComplaintType[ComplaintType["BUG"] = 0] = "BUG";
49
+ ComplaintType[ComplaintType["COMPLAINT"] = 1] = "COMPLAINT";
50
+ })(ComplaintType || (exports.ComplaintType = ComplaintType = {}));
51
+ var ComplaintStatus;
52
+ (function (ComplaintStatus) {
53
+ ComplaintStatus[ComplaintStatus["PENDING"] = 0] = "PENDING";
54
+ ComplaintStatus[ComplaintStatus["IN_REVIEW"] = 1] = "IN_REVIEW";
55
+ ComplaintStatus[ComplaintStatus["RESOLVED"] = 2] = "RESOLVED";
56
+ })(ComplaintStatus || (exports.ComplaintStatus = ComplaintStatus = {}));
57
+ var ReviewStatus;
58
+ (function (ReviewStatus) {
59
+ ReviewStatus[ReviewStatus["PENDING"] = 0] = "PENDING";
60
+ ReviewStatus[ReviewStatus["IN_REVIEW"] = 1] = "IN_REVIEW";
61
+ ReviewStatus[ReviewStatus["ACCEPTED"] = 2] = "ACCEPTED";
62
+ ReviewStatus[ReviewStatus["DECLINED"] = 3] = "DECLINED";
63
+ })(ReviewStatus || (exports.ReviewStatus = ReviewStatus = {}));
64
+ var RegistrationSection;
65
+ (function (RegistrationSection) {
66
+ RegistrationSection[RegistrationSection["OWNER_NAME"] = 0] = "OWNER_NAME";
67
+ RegistrationSection[RegistrationSection["CITY"] = 1] = "CITY";
68
+ RegistrationSection[RegistrationSection["RULES"] = 2] = "RULES";
69
+ RegistrationSection[RegistrationSection["BUSINESS_INFO"] = 3] = "BUSINESS_INFO";
70
+ RegistrationSection[RegistrationSection["DRIVERS_INFO"] = 4] = "DRIVERS_INFO";
71
+ RegistrationSection[RegistrationSection["VEHICLE_PERMIT"] = 5] = "VEHICLE_PERMIT";
72
+ RegistrationSection[RegistrationSection["VEHICLE_REGISTRATION"] = 6] = "VEHICLE_REGISTRATION";
73
+ RegistrationSection[RegistrationSection["SERVICES"] = 7] = "SERVICES";
74
+ RegistrationSection[RegistrationSection["ADDONS"] = 8] = "ADDONS";
75
+ RegistrationSection[RegistrationSection["WORKING_HOURS"] = 9] = "WORKING_HOURS";
76
+ RegistrationSection[RegistrationSection["OVERVIEW"] = 10] = "OVERVIEW";
77
+ RegistrationSection[RegistrationSection["STATUS"] = 11] = "STATUS";
78
+ RegistrationSection[RegistrationSection["DONE"] = 12] = "DONE";
79
+ })(RegistrationSection || (exports.RegistrationSection = RegistrationSection = {}));
@@ -0,0 +1,2 @@
1
+ export * from './constants/types';
2
+ export * from './ts/calcs';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./constants/types"), exports);
18
+ __exportStar(require("./ts/calcs"), exports);
@@ -0,0 +1,34 @@
1
+ import { Order } from '../constants/types';
2
+ export declare const SAHAB_PERCENTAGE = 0;
3
+ export declare const ONLINE_SAHAB_PERCENTAGE = 0.03;
4
+ export declare const CLIENT_SAHAB_PERCENTAGE = 0.03;
5
+ export declare function getServicesTotal(order?: Order): number;
6
+ export declare function getSahabDiscountsAmount(order: Order): number;
7
+ export declare function getProviderDiscountsAmount(order: Order): number;
8
+ export declare function getOrderTotalDiscountsAmount(order: Order): number;
9
+ export declare function getCustomerTotalToPay(order: Order): number;
10
+ export declare function getProviderTransferAmount(order: Order): number;
11
+ export declare function getProviderProfits(order: Order): number;
12
+ export declare function getOnlineProcessingFees(order: Order): number;
13
+ export declare function getSahabFees(order: Order): number;
14
+ export declare function calcOrdersData(orders: Order[]): {
15
+ name: string;
16
+ providerId: string;
17
+ numCashOrders: number;
18
+ numOnlineOrders: number;
19
+ providerCashRevenue: string;
20
+ providerOnlineRevenue: string;
21
+ totalProviderRevenue: string;
22
+ totalCashTransfer: string;
23
+ totalOnlineTransfer: string;
24
+ sahabCashRevenue: string;
25
+ sahabOnlineRevenue: string;
26
+ totalTransfer: string;
27
+ totalSahabRevenue: string;
28
+ };
29
+ export declare function calculatePromoCodeAmount(order: Order): number;
30
+ export declare function calculateOrderPercentage({ isPaid, paymentId, providerPercentage, }: {
31
+ isPaid: boolean;
32
+ paymentId: string;
33
+ providerPercentage: number;
34
+ }): number;
@@ -0,0 +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
+ }
@@ -0,0 +1,3 @@
1
+ export declare function preventNaN(x?: number | string): number;
2
+ export declare const toFixed: (num: any) => string;
3
+ export declare const isEmpty: (text: any) => boolean;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isEmpty = exports.toFixed = void 0;
4
+ exports.preventNaN = preventNaN;
5
+ function preventNaN(x) {
6
+ if (x === undefined || x === null || isNaN(x)) {
7
+ return 0;
8
+ }
9
+ return Number(x);
10
+ }
11
+ const toFixed = (num) => {
12
+ const decimal_places = 2;
13
+ num = Number(num || 0);
14
+ return (Math.round(num * 10 ** decimal_places) / 10 ** decimal_places).toFixed(decimal_places);
15
+ };
16
+ exports.toFixed = toFixed;
17
+ const isEmpty = (text) => {
18
+ const txt = typeof text === 'string' ? text.trim() : text;
19
+ if ((txt !== 0 && !txt) || txt === '' || txt === null || txt === 'null' || txt === ' ' || txt === undefined || txt === 'undefined') {
20
+ return true;
21
+ }
22
+ return false;
23
+ };
24
+ exports.isEmpty = isEmpty;
package/package.json CHANGED
@@ -1,27 +1,36 @@
1
- {
2
- "name": "sahab-utils",
3
- "version": "1.2.7",
4
- "description": "This package containts types and utils fuctions",
5
- "main": "index.ts",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/SahabApp/sahab-utils.git"
12
- },
13
- "keywords": [
14
- "sahab"
15
- ],
16
- "author": "Sahab",
17
- "license": "ISC",
18
- "bugs": {
19
- "url": "https://github.com/SahabApp/sahab-utils/issues"
20
- },
21
- "homepage": "https://github.com/SahabApp/sahab-utils#readme",
22
- "dependencies": {
23
- "firebase": "^10.7.1",
24
- "lodash": "^4.17.21"
25
- },
26
- "packageManager": "yarn@4.9.1+sha512.f95ce356460e05be48d66401c1ae64ef84d163dd689964962c6888a9810865e39097a5e9de748876c2e0bf89b232d583c33982773e9903ae7a76257270986538"
27
- }
1
+ {
2
+ "name": "sahab-utils",
3
+ "version": "1.2.9",
4
+ "description": "This package contains types and utils functions",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/SahabApp/sahab-utils.git"
14
+ },
15
+ "keywords": [
16
+ "sahab"
17
+ ],
18
+ "author": "Sahab",
19
+ "license": "ISC",
20
+ "bugs": {
21
+ "url": "https://github.com/SahabApp/sahab-utils/issues"
22
+ },
23
+ "homepage": "https://github.com/SahabApp/sahab-utils#readme",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "devDependencies": {
28
+ "@types/lodash": "^4.17.24",
29
+ "typescript": "^6.0.2"
30
+ },
31
+ "dependencies": {
32
+ "firebase": "^10.7.1",
33
+ "lodash": "^4.17.21"
34
+ },
35
+ "packageManager": "yarn@4.9.1+sha512.f95ce356460e05be48d66401c1ae64ef84d163dd689964962c6888a9810865e39097a5e9de748876c2e0bf89b232d583c33982773e9903ae7a76257270986538"
36
+ }
Binary file
package/.yarnrc.yml DELETED
@@ -1 +0,0 @@
1
- nodeLinker: node-modules