washday-sdk 0.0.8 → 0.0.10

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.
Files changed (48) hide show
  1. package/babel.config.js +8 -0
  2. package/dist/api/axiosInstance.js +42 -42
  3. package/dist/api/customers/get.js +48 -48
  4. package/dist/api/customers/index.js +1 -1
  5. package/dist/api/index.js +12 -12
  6. package/dist/enum/index.js +15 -15
  7. package/dist/index.js +33 -33
  8. package/dist/interfaces/Api.js +2 -2
  9. package/dist/interfaces/Company.js +1 -1
  10. package/dist/interfaces/Customer.js +2 -2
  11. package/dist/interfaces/Order.js +2 -2
  12. package/dist/interfaces/Permission.js +2 -2
  13. package/dist/interfaces/Product.js +2 -2
  14. package/dist/interfaces/Section.js +2 -2
  15. package/dist/interfaces/Store.js +2 -2
  16. package/dist/interfaces/StoreImage.js +2 -2
  17. package/dist/interfaces/User.js +2 -2
  18. package/dist/utils/index.js +17 -17
  19. package/dist/utils/orders/calculateOrderTotal.js +33 -33
  20. package/dist/utils/orders/calculateTotalTaxesIncluded.js +65 -65
  21. package/dist/utils/orders/calculateTotalTaxesOverPrice.js +74 -74
  22. package/dist/utils/orders/helpers.js +64 -64
  23. package/dist/utils/orders/index.js +5 -5
  24. package/jest.config.js +0 -0
  25. package/package.json +28 -23
  26. package/src/api/axiosInstance.ts +45 -45
  27. package/src/api/customers/get.ts +32 -32
  28. package/src/api/index.ts +26 -21
  29. package/src/api/stores/get.ts +33 -0
  30. package/src/enum/index.ts +10 -10
  31. package/src/index.ts +4 -4
  32. package/src/interfaces/Api.ts +2 -2
  33. package/src/interfaces/Customer.ts +51 -51
  34. package/src/interfaces/Order.ts +95 -95
  35. package/src/interfaces/Permission.ts +24 -24
  36. package/src/interfaces/Product.ts +58 -58
  37. package/src/interfaces/Section.ts +14 -14
  38. package/src/interfaces/Store.ts +350 -350
  39. package/src/interfaces/StoreImage.ts +9 -9
  40. package/src/interfaces/User.ts +42 -42
  41. package/src/utils/index.ts +1 -1
  42. package/src/utils/orders/calculateOrderTotal.test.js +399 -0
  43. package/src/utils/orders/calculateOrderTotal.ts +60 -60
  44. package/src/utils/orders/calculateTotalTaxesIncluded.ts +75 -75
  45. package/src/utils/orders/calculateTotalTaxesOverPrice.ts +82 -82
  46. package/src/utils/orders/helpers.ts +73 -73
  47. package/src/utils/orders/index.ts +4 -4
  48. package/tsconfig.json +9 -9
@@ -1,9 +1,9 @@
1
- import { IUser } from "./User"
2
-
3
- export interface IStoreImage {
4
- resourceUrl?: string,
5
- name: string,
6
- description?: string,
7
- createdBy: IUser | string,
8
- createdDate: Date,
9
- }
1
+ import { IUser } from "./User"
2
+
3
+ export interface IStoreImage {
4
+ resourceUrl?: string,
5
+ name: string,
6
+ description?: string,
7
+ createdBy: IUser | string,
8
+ createdDate: Date,
9
+ }
@@ -1,43 +1,43 @@
1
- import { IPermission } from "./Permission";
2
- import { ICashierBox, IStore } from "./Store";
3
-
4
- interface IPrinterConfig {
5
- name: String;
6
- paperSize: String;
7
- }
8
- export interface IUser {
9
- email: string
10
- name: string
11
- password: string
12
- roleNum: number,
13
- languagePreference?: string
14
- phoneNumber: string,
15
- roleChanged: boolean,
16
- lastLogonDate: Date,
17
- freshChatRestoreID: String,
18
- permission: IPermission,
19
- isActive: boolean,
20
- verified: boolean,
21
- cashierBox?: ICashierBox | string | null,
22
- printer?: IPrinterConfig,
23
- }
24
-
25
- export interface ISecretCode {
26
- email: string
27
- code: string
28
- dateCreated: Date,
29
- }
30
-
31
- export interface IStoreAdmin extends IUser {
32
- }
33
-
34
- export interface ISuperAdmin extends IUser {
35
- // company: ICompany | string,
36
- hasMultiStore: boolean,
37
- stripeCustomerId: string,
38
- }
39
-
40
- export interface IStaff extends IUser {
41
- store: IStore | string,
42
- cashierBox: ICashierBox | string,
1
+ import { IPermission } from "./Permission";
2
+ import { ICashierBox, IStore } from "./Store";
3
+
4
+ interface IPrinterConfig {
5
+ name: String;
6
+ paperSize: String;
7
+ }
8
+ export interface IUser {
9
+ email: string
10
+ name: string
11
+ password: string
12
+ roleNum: number,
13
+ languagePreference?: string
14
+ phoneNumber: string,
15
+ roleChanged: boolean,
16
+ lastLogonDate: Date,
17
+ freshChatRestoreID: String,
18
+ permission: IPermission,
19
+ isActive: boolean,
20
+ verified: boolean,
21
+ cashierBox?: ICashierBox | string | null,
22
+ printer?: IPrinterConfig,
23
+ }
24
+
25
+ export interface ISecretCode {
26
+ email: string
27
+ code: string
28
+ dateCreated: Date,
29
+ }
30
+
31
+ export interface IStoreAdmin extends IUser {
32
+ }
33
+
34
+ export interface ISuperAdmin extends IUser {
35
+ // company: ICompany | string,
36
+ hasMultiStore: boolean,
37
+ stripeCustomerId: string,
38
+ }
39
+
40
+ export interface IStaff extends IUser {
41
+ store: IStore | string,
42
+ cashierBox: ICashierBox | string,
43
43
  }
@@ -1 +1 @@
1
- export * from './orders';
1
+ export * from './orders';
@@ -0,0 +1,399 @@
1
+ // myModule.test.js
2
+ import { calculateOrderTotal } from "./index";
3
+
4
+ describe("sum function", () => {
5
+ const ORDER_DEMO = {
6
+ orderDto: {
7
+ deliveryDateFormatted: "lunes 08 abr.",
8
+ pickupInfo: null,
9
+ deliveryInfo: {
10
+ date: "2024-04-09T05:00:00.000Z",
11
+ fromTime: "2024-04-08T21:00:00.000Z",
12
+ },
13
+ express: false,
14
+ products: [
15
+ {
16
+ extraAmount: 0,
17
+ type: "weight",
18
+ showInApp: true,
19
+ order: 0,
20
+ invoice_description: "",
21
+ invoice_product_key: "",
22
+ invoice_product_unit_key: "",
23
+ invoice_product_unit_name: "",
24
+ productType: "storeProduct",
25
+ _id: "62d186387ec9330016319594",
26
+ name: "Lavado por kilo",
27
+ price: 17,
28
+ expressPrice: 20,
29
+ pieces: "1",
30
+ sku: "lavxkilo",
31
+ overlayText: "Lavado por kilo",
32
+ image: "6184782c3927a40018e36dea",
33
+ taxExemptOne: false,
34
+ taxExemptTwo: false,
35
+ taxExemptThree: false,
36
+ isActive: true,
37
+ store: "629c267812db5b001692adae",
38
+ owner: "629c267812db5b001692ada4",
39
+ productSupplies: [
40
+ {
41
+ usageAmount: 25,
42
+ _id: "62d6151fac2fc200166d0304",
43
+ supplyId: "62d35942c642440016cfcda8",
44
+ },
45
+ ],
46
+ __v: 1,
47
+ discountAmount: 0,
48
+ quantity: 3.7,
49
+ productId: "62d186387ec9330016319594",
50
+ qty: 3.7,
51
+ },
52
+ ],
53
+ taxesType: "included",
54
+ total: 62.9,
55
+ customer: "66147eecae83bb001540dd96",
56
+ store: "629c267812db5b001692adae",
57
+ paymentMethod: "delivery",
58
+ notifyBy: "none",
59
+ pickup: false,
60
+ delivery: false,
61
+ phone: "",
62
+ email: "",
63
+ address: "",
64
+ notes: "",
65
+ privateNotes: "",
66
+ creditApplied: 0,
67
+ amountPaid: 0,
68
+ customerInfo: {
69
+ password: "",
70
+ google: false,
71
+ facebook: false,
72
+ apple: false,
73
+ useStoreApp: false,
74
+ discount: 0,
75
+ credit: 0,
76
+ isActive: true,
77
+ createdDate: "2024-04-08T23:34:04.586Z",
78
+ stripeCustomerId: "",
79
+ invoiceInfo: {
80
+ _id: "66147eecae83bb001540dd95",
81
+ legal_name: "",
82
+ tax_system: "",
83
+ zipCode: "",
84
+ },
85
+ _id: "66147eecae83bb001540dd96",
86
+ name: "Carlos Piña 🍍",
87
+ phone: "",
88
+ address: "",
89
+ notes: "",
90
+ privateNotes: "",
91
+ rfc: "",
92
+ createdBy: "62d194d77ec933001631e831",
93
+ createdIn: "629c267812db5b001692adae",
94
+ company: "629c267712db5b001692ad9e",
95
+ addresses: [],
96
+ paymentMethods: [],
97
+ __v: 0,
98
+ title: "Carlos Piña 🍍",
99
+ id: "66147eecae83bb001540dd96",
100
+ },
101
+ customerName: "Carlos Piña 🍍",
102
+ pickingUpDateTime: null,
103
+ cleanedDateTime: null,
104
+ readyDateTime: null,
105
+ deliveringDateTime: null,
106
+ collectedDateTime: null,
107
+ cancelledDateTime: null,
108
+ paidDateTime: null,
109
+ status: "cleaning",
110
+ prepaidAmount: 0,
111
+ prepaidPaymentMethod: "",
112
+ customerDiscount: 0,
113
+ productTotal: 54.22,
114
+ taxesTotal: 8.68,
115
+ productTotalWithoutDiscount: 54.22,
116
+ totalDiscountAmount: 0,
117
+ shippingServiceTotal: 0,
118
+ discountsToApply: [],
119
+ usePrepaid: false,
120
+ cashierBox: "629c267812db5b001692adb3",
121
+ discountCode: "",
122
+ buyAndGetProducts: [],
123
+ deliveryRoute: null,
124
+ pickupRoute: null,
125
+ createdDate: "2024-04-08T23:34:48.950Z",
126
+ },
127
+ customer: {
128
+ customer: {
129
+ password: "",
130
+ google: false,
131
+ facebook: false,
132
+ apple: false,
133
+ useStoreApp: false,
134
+ discount: 0,
135
+ credit: 0,
136
+ isActive: true,
137
+ createdDate: "2024-04-08T23:34:04.586Z",
138
+ stripeCustomerId: "",
139
+ invoiceInfo: {
140
+ _id: "66147eecae83bb001540dd95",
141
+ legal_name: "",
142
+ tax_system: "",
143
+ zipCode: "",
144
+ },
145
+ _id: "66147eecae83bb001540dd96",
146
+ name: "Carlos Piña 🍍",
147
+ phone: "",
148
+ address: "",
149
+ notes: "",
150
+ privateNotes: "",
151
+ rfc: "",
152
+ createdBy: "62d194d77ec933001631e831",
153
+ createdIn: "629c267812db5b001692adae",
154
+ company: "629c267712db5b001692ad9e",
155
+ addresses: [],
156
+ paymentMethods: [],
157
+ __v: 0,
158
+ },
159
+ },
160
+ store: {
161
+ taxesType: "included",
162
+ notificationConfig: {
163
+ notifyOrderCreated: false,
164
+ notifyPickedUp: false,
165
+ notifyCleaned: true,
166
+ notifyCollected: false,
167
+ notifyDelivered: false,
168
+ notifyOwnerOnOrderCreated: false,
169
+ notifyEmail: "",
170
+ _id: "62a2d05fd0a7c800160869bc",
171
+ },
172
+ paymentConfig: {
173
+ defaultPaymentMethod: "delivery",
174
+ _id: "63650e8dcfa0e30016729776",
175
+ },
176
+ orderPageConfig: {
177
+ defaultDeliveryDate: "0",
178
+ defaultDeliveryTime: "18:00",
179
+ defaultNotifyMethod: "none",
180
+ printTicketAfterOrder: true,
181
+ shippingServiceCost: 0,
182
+ showProductSearchBarWeb: true,
183
+ _id: "6407b53301a1aa00141b217e",
184
+ },
185
+ _id: "629c267812db5b001692adae",
186
+ name: "Clean & Go",
187
+ phone: "9983524573",
188
+ inventory: [],
189
+ discountCodes: [
190
+ {
191
+ type: "percentage",
192
+ value: 14,
193
+ products: [
194
+ "62d1893f7ec933001631ab24",
195
+ "62d189697ec933001631acab",
196
+ "62d19a287ec933001632156f",
197
+ "62d199117ec9330016320ec0",
198
+ "62d198e07ec9330016320cc7",
199
+ "62d191d67ec933001631cd76",
200
+ "62d191747ec933001631c9cc",
201
+ "62d191ad7ec933001631cbef",
202
+ ],
203
+ applyToAllProducts: false,
204
+ applyOnceOnOrder: false,
205
+ hasUseLimit: false,
206
+ useLimitQty: 0,
207
+ usedCounter: 0,
208
+ onePerCustomer: true,
209
+ isActive: true,
210
+ buyAndGetConditions: [],
211
+ _id: "62d1ae1b7ec9330016328166",
212
+ name: "Anfitriones Airbnb",
213
+ code: "AIRBNBANF",
214
+ fromDate: "2022-07-01T05:00:00.000Z",
215
+ toDate: "2022-08-01T04:59:59.999Z",
216
+ fromTime: "00:00",
217
+ toTime: "23:59",
218
+ createdBy: "629c267812db5b001692ada4",
219
+ createdAt: "2022-07-15T18:12:43.506Z",
220
+ updatedAt: "2022-07-15T18:12:43.506Z",
221
+ },
222
+ {
223
+ type: "percentage",
224
+ value: 20,
225
+ products: [],
226
+ applyToAllProducts: true,
227
+ applyOnceOnOrder: false,
228
+ hasUseLimit: false,
229
+ useLimitQty: 0,
230
+ usedCounter: 0,
231
+ onePerCustomer: true,
232
+ isActive: true,
233
+ buyAndGetConditions: [],
234
+ _id: "635821305b689d0016c97f8c",
235
+ name: "Descuento por comentarios",
236
+ code: "20DESC01",
237
+ fromDate: "2022-01-01T05:00:00.000Z",
238
+ toDate: "2023-01-01T04:59:59.999Z",
239
+ fromTime: "00:00",
240
+ toTime: "23:59",
241
+ createdBy: "629c267812db5b001692ada4",
242
+ createdAt: "2022-10-25T17:47:28.314Z",
243
+ updatedAt: "2022-10-25T17:47:28.314Z",
244
+ },
245
+ {
246
+ type: "percentage",
247
+ value: 20,
248
+ products: [],
249
+ applyToAllProducts: true,
250
+ applyOnceOnOrder: false,
251
+ hasUseLimit: false,
252
+ useLimitQty: 0,
253
+ usedCounter: 0,
254
+ onePerCustomer: true,
255
+ isActive: true,
256
+ buyAndGetConditions: [],
257
+ _id: "6359c5d21332cf00168c5008",
258
+ name: "Descuento por comentario",
259
+ code: "20DESC02",
260
+ fromDate: "2022-01-01T05:00:00.000Z",
261
+ toDate: "2023-01-01T04:59:59.999Z",
262
+ fromTime: "00:00",
263
+ toTime: "23:59",
264
+ createdBy: "62d194d77ec933001631e831",
265
+ createdAt: "2022-10-26T23:42:10.732Z",
266
+ updatedAt: "2022-10-26T23:42:10.732Z",
267
+ },
268
+ {
269
+ type: "percentage",
270
+ value: 19,
271
+ products: [],
272
+ applyToAllProducts: true,
273
+ applyOnceOnOrder: false,
274
+ hasUseLimit: false,
275
+ useLimitQty: 0,
276
+ usedCounter: 0,
277
+ onePerCustomer: true,
278
+ isActive: true,
279
+ buyAndGetConditions: [],
280
+ _id: "6359c5ef1332cf00168c516d",
281
+ name: "Descuento por comentario",
282
+ code: "20DESC03",
283
+ fromDate: "2022-01-01T05:00:00.000Z",
284
+ toDate: "2023-01-01T04:59:59.999Z",
285
+ fromTime: "00:00",
286
+ toTime: "23:59",
287
+ createdBy: "62d194d77ec933001631e831",
288
+ createdAt: "2022-10-26T23:42:39.555Z",
289
+ updatedAt: "2022-10-26T23:42:39.555Z",
290
+ },
291
+ {
292
+ type: "percentage",
293
+ value: 20,
294
+ products: [],
295
+ applyToAllProducts: true,
296
+ applyOnceOnOrder: false,
297
+ hasUseLimit: false,
298
+ useLimitQty: 0,
299
+ usedCounter: 0,
300
+ onePerCustomer: true,
301
+ isActive: true,
302
+ buyAndGetConditions: [],
303
+ _id: "6359c60c1332cf00168c52c6",
304
+ name: "Descuento por comentario",
305
+ code: "20DESC04",
306
+ fromDate: "2022-01-01T05:00:00.000Z",
307
+ toDate: "2023-01-01T04:59:59.999Z",
308
+ fromTime: "00:00",
309
+ toTime: "23:59",
310
+ createdBy: "62d194d77ec933001631e831",
311
+ createdAt: "2022-10-26T23:43:08.577Z",
312
+ updatedAt: "2022-10-26T23:43:08.577Z",
313
+ },
314
+ {
315
+ type: "percentage",
316
+ value: 20,
317
+ products: [],
318
+ applyToAllProducts: true,
319
+ applyOnceOnOrder: false,
320
+ hasUseLimit: false,
321
+ useLimitQty: 0,
322
+ usedCounter: 0,
323
+ onePerCustomer: true,
324
+ isActive: true,
325
+ buyAndGetConditions: [],
326
+ _id: "6359c6261332cf00168c5468",
327
+ name: "Descuento por comentario",
328
+ code: "20DESC05",
329
+ fromDate: "2022-01-01T05:00:00.000Z",
330
+ toDate: "2023-01-01T04:59:59.999Z",
331
+ fromTime: "00:00",
332
+ toTime: "23:59",
333
+ createdBy: "62d194d77ec933001631e831",
334
+ createdAt: "2022-10-26T23:43:34.663Z",
335
+ updatedAt: "2022-10-26T23:43:34.663Z",
336
+ },
337
+ ],
338
+ taxOne: {
339
+ _id: "658bb2f376aa7b001482888c",
340
+ value: 16,
341
+ name: "IVA",
342
+ },
343
+ taxThree: null,
344
+ taxTwo: null,
345
+ },
346
+ hasShippingCost: false,
347
+ storeDiscounts: [],
348
+ discountCodeObj: null,
349
+ };
350
+ beforeEach(() => {
351
+ // Run this setup function before each test case
352
+ });
353
+ it("test calculateOrderTotal taxes included", () => {
354
+ // Test case
355
+ const {
356
+ orderDto,
357
+ customer,
358
+ store,
359
+ hasShippingCost,
360
+ storeDiscounts,
361
+ discountCodeObj,
362
+ } = ORDER_DEMO;
363
+
364
+ const { ...informationNewPackage } = calculateOrderTotal(
365
+ orderDto,
366
+ { customer },
367
+ store,
368
+ orderDto.delivery || orderDto.pickup,
369
+ // storeDiscounts.data?.discounts || [],
370
+ storeDiscounts,
371
+ discountCodeObj
372
+ );
373
+
374
+ const res = {
375
+ totalDiscountAmount: informationNewPackage.totalDiscountAmount,
376
+ totalQuantity: informationNewPackage.totalQuantity,
377
+ totalImportWithDiscount: informationNewPackage.productTotal,
378
+ totalImportWithoutDiscount:
379
+ informationNewPackage.productTotalWithoutDiscount,
380
+ totalImporTaxes: informationNewPackage.taxesTotal,
381
+ shippingCost: informationNewPackage.shippingServiceTotal,
382
+ };
383
+ expect(res).toEqual({
384
+ totalDiscountAmount: 0,
385
+ totalQuantity: 3.7,
386
+ totalImportWithDiscount: 54.22,
387
+ totalImportWithoutDiscount: 54.22,
388
+ totalImporTaxes: 8.68,
389
+ shippingCost: 0,
390
+ });
391
+ });
392
+
393
+ it("should return 0 when adding two zeros", () => {
394
+ // Test case
395
+ expect(sum(0, 0)).toBe(0);
396
+ });
397
+
398
+ // Add more test cases as needed
399
+ });
@@ -1,60 +1,60 @@
1
- import { DiscountCodeTypes } from "../../enum";
2
- import { ICustomer } from "../../interfaces/Customer";
3
- import { IStore } from "../../interfaces/Store";
4
- import { calculateTotalTaxesIncluded } from "./calculateTotalTaxesIncluded";
5
- import { calculateTotalTaxesOverPrice } from "./calculateTotalTaxesOverPrice";
6
- import { getCreditApplied, getProductLineTotals, getShippingCost } from "./helpers";
7
-
8
- export const calculateOrderTotal = (
9
- order: any,
10
- selectedCustomer: { customer: ICustomer } | undefined,
11
- storeSettings: IStore,
12
- hasShippingCost: boolean,
13
- storeDiscounts: any[] = [],
14
- discountCodeObj: any
15
- ): any => {
16
- try {
17
- const appliedOrderDiscounts: any = {};
18
- const productTableCalculator = storeSettings?.taxesType === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
19
- const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
20
-
21
- // PRODUCT LINE TOTALS
22
- const {
23
- totalQuantity,
24
- totalImportWithDiscount,
25
- totalImportWithoutDiscount,
26
- totalImporTaxes,
27
- totalDiscountAmount
28
- } = getProductLineTotals(productTableImports);
29
-
30
- // DISCOUNT TOTAL AND SHIPPING SERVICE COST
31
- let discountCodeAmount = 0;
32
- let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
33
-
34
- if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
35
- const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr: any) => discountCodeObj.products.includes(curr._id));
36
- discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
37
- }
38
-
39
- // ORDER TOTAL CALCULATION AND CREDIT APPLIED
40
- let orderTotalWithOutCredit = +(totalImportWithDiscount + totalImporTaxes + shippingCost - discountCodeAmount).toFixed(2);
41
- let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
42
- let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
43
-
44
- return {
45
- ...order,
46
- totalQuantity,
47
- customerDiscount: order.discountCode ? 0 : selectedCustomer?.customer?.discount,
48
- productTotal: totalImportWithDiscount,
49
- taxesTotal: totalImporTaxes,
50
- total: orderTotal,
51
- creditApplied,
52
- productTotalWithoutDiscount: totalImportWithoutDiscount,
53
- totalDiscountAmount: totalDiscountAmount + discountCodeAmount,
54
- shippingServiceTotal: shippingCost,
55
- appliedOrderDiscounts
56
- };
57
- } catch (error) {
58
- throw error;
59
- }
60
- };
1
+ import { DiscountCodeTypes } from "../../enum";
2
+ import { ICustomer } from "../../interfaces/Customer";
3
+ import { IStore } from "../../interfaces/Store";
4
+ import { calculateTotalTaxesIncluded } from "./calculateTotalTaxesIncluded";
5
+ import { calculateTotalTaxesOverPrice } from "./calculateTotalTaxesOverPrice";
6
+ import { getCreditApplied, getProductLineTotals, getShippingCost } from "./helpers";
7
+
8
+ export const calculateOrderTotal = (
9
+ order: any,
10
+ selectedCustomer: { customer: ICustomer } | undefined,
11
+ storeSettings: IStore,
12
+ hasShippingCost: boolean,
13
+ storeDiscounts: any[] = [],
14
+ discountCodeObj: any
15
+ ): any => {
16
+ try {
17
+ const appliedOrderDiscounts: any = {};
18
+ const productTableCalculator = storeSettings?.taxesType === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
19
+ const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
20
+
21
+ // PRODUCT LINE TOTALS
22
+ const {
23
+ totalQuantity,
24
+ totalImportWithDiscount,
25
+ totalImportWithoutDiscount,
26
+ totalImporTaxes,
27
+ totalDiscountAmount
28
+ } = getProductLineTotals(productTableImports);
29
+
30
+ // DISCOUNT TOTAL AND SHIPPING SERVICE COST
31
+ let discountCodeAmount = 0;
32
+ let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
33
+
34
+ if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
35
+ const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr: any) => discountCodeObj.products.includes(curr._id));
36
+ discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
37
+ }
38
+
39
+ // ORDER TOTAL CALCULATION AND CREDIT APPLIED
40
+ let orderTotalWithOutCredit = +(totalImportWithDiscount + totalImporTaxes + shippingCost - discountCodeAmount).toFixed(2);
41
+ let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
42
+ let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
43
+
44
+ return {
45
+ ...order,
46
+ totalQuantity,
47
+ customerDiscount: order.discountCode ? 0 : selectedCustomer?.customer?.discount,
48
+ productTotal: totalImportWithDiscount,
49
+ taxesTotal: totalImporTaxes,
50
+ total: orderTotal,
51
+ creditApplied,
52
+ productTotalWithoutDiscount: totalImportWithoutDiscount,
53
+ totalDiscountAmount: totalDiscountAmount + discountCodeAmount,
54
+ shippingServiceTotal: shippingCost,
55
+ appliedOrderDiscounts
56
+ };
57
+ } catch (error) {
58
+ throw error;
59
+ }
60
+ };