@jsdev_ninja/core 0.10.8 → 0.11.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.
@@ -104,49 +104,49 @@ export declare const ProductSchema: z.ZodObject<{
104
104
  id: string;
105
105
  companyId: string;
106
106
  storeId: string;
107
- tag: string;
108
107
  locales: {
109
108
  value: string;
110
109
  lang: "he";
111
110
  }[];
112
111
  depth: number;
113
112
  parentId?: string | null | undefined;
113
+ tag?: string | undefined;
114
114
  } & {
115
115
  children: ({
116
116
  id: string;
117
117
  companyId: string;
118
118
  storeId: string;
119
- tag: string;
120
119
  locales: {
121
120
  value: string;
122
121
  lang: "he";
123
122
  }[];
124
123
  depth: number;
125
124
  parentId?: string | null | undefined;
125
+ tag?: string | undefined;
126
126
  } & /*elided*/ any)[];
127
127
  }, z.ZodTypeDef, {
128
128
  id: string;
129
129
  companyId: string;
130
130
  storeId: string;
131
- tag: string;
132
131
  locales: {
133
132
  value: string;
134
133
  lang: "he";
135
134
  }[];
136
135
  depth: number;
137
136
  parentId?: string | null | undefined;
137
+ tag?: string | undefined;
138
138
  } & {
139
139
  children: ({
140
140
  id: string;
141
141
  companyId: string;
142
142
  storeId: string;
143
- tag: string;
144
143
  locales: {
145
144
  value: string;
146
145
  lang: "he";
147
146
  }[];
148
147
  depth: number;
149
148
  parentId?: string | null | undefined;
149
+ tag?: string | undefined;
150
150
  } & /*elided*/ any)[];
151
151
  }>, "many">;
152
152
  categories: z.ZodObject<{
@@ -223,25 +223,25 @@ export declare const ProductSchema: z.ZodObject<{
223
223
  id: string;
224
224
  companyId: string;
225
225
  storeId: string;
226
- tag: string;
227
226
  locales: {
228
227
  value: string;
229
228
  lang: "he";
230
229
  }[];
231
230
  depth: number;
232
231
  parentId?: string | null | undefined;
232
+ tag?: string | undefined;
233
233
  } & {
234
234
  children: ({
235
235
  id: string;
236
236
  companyId: string;
237
237
  storeId: string;
238
- tag: string;
239
238
  locales: {
240
239
  value: string;
241
240
  lang: "he";
242
241
  }[];
243
242
  depth: number;
244
243
  parentId?: string | null | undefined;
244
+ tag?: string | undefined;
245
245
  } & /*elided*/ any)[];
246
246
  })[];
247
247
  categories: {
@@ -309,25 +309,25 @@ export declare const ProductSchema: z.ZodObject<{
309
309
  id: string;
310
310
  companyId: string;
311
311
  storeId: string;
312
- tag: string;
313
312
  locales: {
314
313
  value: string;
315
314
  lang: "he";
316
315
  }[];
317
316
  depth: number;
318
317
  parentId?: string | null | undefined;
318
+ tag?: string | undefined;
319
319
  } & {
320
320
  children: ({
321
321
  id: string;
322
322
  companyId: string;
323
323
  storeId: string;
324
- tag: string;
325
324
  locales: {
326
325
  value: string;
327
326
  lang: "he";
328
327
  }[];
329
328
  depth: number;
330
329
  parentId?: string | null | undefined;
330
+ tag?: string | undefined;
331
331
  } & /*elided*/ any)[];
332
332
  })[];
333
333
  categories: {
@@ -448,49 +448,49 @@ export declare const NewProductSchema: z.ZodObject<z.objectUtil.extendShape<{
448
448
  id: string;
449
449
  companyId: string;
450
450
  storeId: string;
451
- tag: string;
452
451
  locales: {
453
452
  value: string;
454
453
  lang: "he";
455
454
  }[];
456
455
  depth: number;
457
456
  parentId?: string | null | undefined;
457
+ tag?: string | undefined;
458
458
  } & {
459
459
  children: ({
460
460
  id: string;
461
461
  companyId: string;
462
462
  storeId: string;
463
- tag: string;
464
463
  locales: {
465
464
  value: string;
466
465
  lang: "he";
467
466
  }[];
468
467
  depth: number;
469
468
  parentId?: string | null | undefined;
469
+ tag?: string | undefined;
470
470
  } & /*elided*/ any)[];
471
471
  }, z.ZodTypeDef, {
472
472
  id: string;
473
473
  companyId: string;
474
474
  storeId: string;
475
- tag: string;
476
475
  locales: {
477
476
  value: string;
478
477
  lang: "he";
479
478
  }[];
480
479
  depth: number;
481
480
  parentId?: string | null | undefined;
481
+ tag?: string | undefined;
482
482
  } & {
483
483
  children: ({
484
484
  id: string;
485
485
  companyId: string;
486
486
  storeId: string;
487
- tag: string;
488
487
  locales: {
489
488
  value: string;
490
489
  lang: "he";
491
490
  }[];
492
491
  depth: number;
493
492
  parentId?: string | null | undefined;
493
+ tag?: string | undefined;
494
494
  } & /*elided*/ any)[];
495
495
  }>, "many">;
496
496
  categories: z.ZodObject<{
@@ -569,25 +569,25 @@ export declare const NewProductSchema: z.ZodObject<z.objectUtil.extendShape<{
569
569
  id: string;
570
570
  companyId: string;
571
571
  storeId: string;
572
- tag: string;
573
572
  locales: {
574
573
  value: string;
575
574
  lang: "he";
576
575
  }[];
577
576
  depth: number;
578
577
  parentId?: string | null | undefined;
578
+ tag?: string | undefined;
579
579
  } & {
580
580
  children: ({
581
581
  id: string;
582
582
  companyId: string;
583
583
  storeId: string;
584
- tag: string;
585
584
  locales: {
586
585
  value: string;
587
586
  lang: "he";
588
587
  }[];
589
588
  depth: number;
590
589
  parentId?: string | null | undefined;
590
+ tag?: string | undefined;
591
591
  } & /*elided*/ any)[];
592
592
  })[];
593
593
  categories: {
@@ -656,25 +656,25 @@ export declare const NewProductSchema: z.ZodObject<z.objectUtil.extendShape<{
656
656
  id: string;
657
657
  companyId: string;
658
658
  storeId: string;
659
- tag: string;
660
659
  locales: {
661
660
  value: string;
662
661
  lang: "he";
663
662
  }[];
664
663
  depth: number;
665
664
  parentId?: string | null | undefined;
665
+ tag?: string | undefined;
666
666
  } & {
667
667
  children: ({
668
668
  id: string;
669
669
  companyId: string;
670
670
  storeId: string;
671
- tag: string;
672
671
  locales: {
673
672
  value: string;
674
673
  lang: "he";
675
674
  }[];
676
675
  depth: number;
677
676
  parentId?: string | null | undefined;
677
+ tag?: string | undefined;
678
678
  } & /*elided*/ any)[];
679
679
  })[];
680
680
  categories: {
@@ -1 +1 @@
1
- {"root":["../lib/index.tsx","../lib/entities/address.ts","../lib/entities/atoms.ts","../lib/entities/cart.ts","../lib/entities/category.ts","../lib/entities/company.ts","../lib/entities/discount.ts","../lib/entities/favoriteproduct.ts","../lib/entities/locale.ts","../lib/entities/order.ts","../lib/entities/product.ts","../lib/entities/profile.ts","../lib/entities/store.ts","../lib/entities/index.ts","../lib/firebase-api/app.ts","../lib/firebase-api/index.ts"],"version":"5.7.3"}
1
+ {"root":["../lib/index.tsx","../lib/entities/address.ts","../lib/entities/atoms.ts","../lib/entities/cart.ts","../lib/entities/category.ts","../lib/entities/company.ts","../lib/entities/discount.ts","../lib/entities/favoriteproduct.ts","../lib/entities/locale.ts","../lib/entities/order.ts","../lib/entities/product.ts","../lib/entities/profile.ts","../lib/entities/store.ts","../lib/entities/index.ts","../lib/firebase-api/app.ts","../lib/firebase-api/index.ts","../lib/utils/index.ts"],"version":"5.7.3"}
@@ -0,0 +1,111 @@
1
+ import { TCart, TDiscount, TStore } from "../entities";
2
+ export declare function getCartCost({ cart, discounts, store, }: {
3
+ cart: TCart["items"];
4
+ discounts: TDiscount[];
5
+ store: TStore;
6
+ }): {
7
+ discount: number;
8
+ cost: number;
9
+ finalCost: number;
10
+ vat: number;
11
+ items: {
12
+ amount: number;
13
+ product: {
14
+ type: "Product";
15
+ id: string;
16
+ companyId: string;
17
+ storeId: string;
18
+ objectID: string;
19
+ sku: string;
20
+ name: {
21
+ value: string;
22
+ lang: "he";
23
+ }[];
24
+ description: {
25
+ value: string;
26
+ lang: "he";
27
+ }[];
28
+ isPublished: boolean;
29
+ vat: boolean;
30
+ priceType: {
31
+ value: number;
32
+ type: "unit" | "kg" | "gram" | "liter" | "ml";
33
+ };
34
+ price: number;
35
+ currency: "ILS";
36
+ discount: {
37
+ value: number;
38
+ type: "number" | "percent" | "none";
39
+ };
40
+ weight: {
41
+ value: number;
42
+ unit: "kg" | "gram" | "none";
43
+ };
44
+ volume: {
45
+ value: number;
46
+ unit: "liter" | "ml" | "none";
47
+ };
48
+ images: {
49
+ id: string;
50
+ url: string;
51
+ }[];
52
+ manufacturer: string;
53
+ brand: string;
54
+ importer: string;
55
+ supplier: string;
56
+ ingredients: {
57
+ value: string;
58
+ lang: "he";
59
+ }[];
60
+ created_at: number;
61
+ updated_at: number;
62
+ categoryIds: string[];
63
+ categoryList: ({
64
+ id: string;
65
+ companyId: string;
66
+ storeId: string;
67
+ locales: {
68
+ value: string;
69
+ lang: "he";
70
+ }[];
71
+ depth: number;
72
+ parentId?: string | null | undefined;
73
+ tag?: string | undefined;
74
+ } & {
75
+ children: ({
76
+ id: string;
77
+ companyId: string;
78
+ storeId: string;
79
+ locales: {
80
+ value: string;
81
+ lang: "he";
82
+ }[];
83
+ depth: number;
84
+ parentId?: string | null | undefined;
85
+ tag?: string | undefined;
86
+ } & /*elided*/ any)[];
87
+ })[];
88
+ categories: {
89
+ lvl0: string[];
90
+ lvl1: string[];
91
+ lvl2: string[];
92
+ lvl3: string[];
93
+ lvl4: string[];
94
+ };
95
+ categoryNames: string[];
96
+ purchasePrice?: number | undefined;
97
+ profitPercentage?: number | undefined;
98
+ isDiscountable?: boolean | undefined;
99
+ };
100
+ originalPrice: number;
101
+ finalPrice: number;
102
+ finalDiscount: number;
103
+ }[];
104
+ };
105
+ export declare function calculateCartPrice(items: TCart["items"]): {
106
+ cost: number;
107
+ discount: number;
108
+ vat: number;
109
+ finalCost: number;
110
+ };
111
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAY,MAAM,EAAE,MAAM,aAAa,CAAC;AA6BjE,wBAAgB,WAAW,CAAC,EAC3B,IAAI,EACJ,SAAS,EACT,KAAK,GACL,EAAE;IACF,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACrB,SAAS,EAAE,SAAS,EAAE,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;CACd;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyIA;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC;;;;;EA4BvD"}
@@ -0,0 +1,149 @@
1
+ const CONFIG = {
2
+ VAT: 18,
3
+ };
4
+ function calculateDiscount(product) {
5
+ if (product.discount?.type === "percent") {
6
+ return (product.price * (product.discount.value ?? 100)) / 100;
7
+ }
8
+ if (product.discount?.type === "number") {
9
+ return product.price - (product.discount.value ?? 0);
10
+ }
11
+ return 0;
12
+ }
13
+ function getPriceAfterDiscount(product) {
14
+ if (product.discount?.type === "percent") {
15
+ const dscountAmount = (product.price * product.discount.value) / 100;
16
+ return product.price - dscountAmount;
17
+ }
18
+ if (product.discount?.type === "number") {
19
+ const dscountAmount = product.price - product.discount.value;
20
+ return dscountAmount;
21
+ }
22
+ return product.price;
23
+ }
24
+ // main
25
+ export function getCartCost({ cart, discounts, store, }) {
26
+ const { isVatIncludedInPrice } = store;
27
+ let result = cart.map((item) => {
28
+ return {
29
+ amount: item.amount,
30
+ product: { ...item.product },
31
+ originalPrice: item.product.price,
32
+ finalPrice: item.product.price,
33
+ finalDiscount: 0,
34
+ };
35
+ });
36
+ const activeDiscounts = discounts.filter((discount) => {
37
+ if (discount.variant.variantType === "bundle") {
38
+ const productsTotal = cart?.reduce((total, item) => {
39
+ if (discount.variant.productsId.includes(item.product.id)) {
40
+ total += item.amount;
41
+ return total;
42
+ }
43
+ return total;
44
+ }, 0) ?? 0;
45
+ if (productsTotal >= discount.variant.requiredQuantity) {
46
+ // const times = Math.floor(productsTotal / discount.variant.requiredQuantity);
47
+ // console.log("yes", times, discount.variant.discountPrice);
48
+ return true;
49
+ }
50
+ }
51
+ return false;
52
+ });
53
+ console.log("activeDiscounts", activeDiscounts);
54
+ activeDiscounts.forEach((discount) => {
55
+ if (discount.variant.variantType === "bundle") {
56
+ // get all products in cart
57
+ const products = cart.filter((item) => discount.variant.productsId.includes(item.product.id));
58
+ const productsTotal = products?.reduce((total, item) => {
59
+ if (discount.variant.productsId.includes(item.product.id)) {
60
+ total += item.amount;
61
+ return total;
62
+ }
63
+ return total;
64
+ }, 0) ?? 0;
65
+ const times = Math.floor(productsTotal / discount.variant.requiredQuantity);
66
+ const price = getPriceAfterDiscount(products[0]?.product);
67
+ const _discount = calculateDiscount(products[0]?.product);
68
+ console.log("price", price, _discount);
69
+ const discountPrice = Number((discount.variant.discountPrice / discount.variant.requiredQuantity).toFixed(2)) * 1;
70
+ console.log("discountPrice", discountPrice);
71
+ const totalDiscount = (price * discount.variant.requiredQuantity - discount.variant.discountPrice) * times;
72
+ const originalPrice = productsTotal * price;
73
+ const discountPriceFinal = originalPrice - totalDiscount;
74
+ console.log("totalDiscount", totalDiscount);
75
+ console.log("discountPriceFinal", discountPriceFinal, originalPrice);
76
+ const averagePrice = Number((discountPriceFinal / productsTotal).toFixed(2));
77
+ console.log("averagePrice", averagePrice);
78
+ result = result.map((item) => {
79
+ if (discount.variant.productsId.includes(item.product.id)) {
80
+ return {
81
+ ...item,
82
+ finalPrice: averagePrice,
83
+ originalPrice: item.product.price,
84
+ finalDiscount: item.product.price - averagePrice,
85
+ };
86
+ }
87
+ return item;
88
+ });
89
+ console.log("averagePrice", averagePrice);
90
+ console.log("yes", times, discount.variant.requiredQuantity, discount.variant.discountPrice);
91
+ console.log("dis", productsTotal, products);
92
+ // find average price
93
+ }
94
+ });
95
+ console.log("result", result);
96
+ const cartDetails = result.reduce((acc, item) => {
97
+ const { product, amount, finalPrice, finalDiscount } = item;
98
+ console.log("isVatIncludedInPrice", isVatIncludedInPrice);
99
+ let productVatValue = 0;
100
+ if (product.vat) {
101
+ if (isVatIncludedInPrice) {
102
+ const vat_amount = finalPrice * (CONFIG.VAT / (100 + CONFIG.VAT));
103
+ productVatValue = Number(vat_amount.toFixed(2));
104
+ productVatValue = productVatValue * amount;
105
+ acc.vat += Number(productVatValue.toFixed(2));
106
+ }
107
+ else {
108
+ productVatValue = (finalPrice * CONFIG.VAT) / 100;
109
+ productVatValue = productVatValue * amount;
110
+ acc.vat += Number(productVatValue.toFixed(2));
111
+ }
112
+ }
113
+ console.log("finalDiscount", finalDiscount);
114
+ acc.cost += amount * finalPrice;
115
+ acc.discount += finalDiscount ? amount * finalDiscount : finalDiscount;
116
+ acc.finalCost += amount * finalPrice + (isVatIncludedInPrice ? 0 : productVatValue);
117
+ return acc;
118
+ }, {
119
+ discount: 0,
120
+ cost: 0,
121
+ finalCost: 0,
122
+ vat: 0,
123
+ });
124
+ console.log("cartDetails", cartDetails);
125
+ return { items: result, ...cartDetails };
126
+ }
127
+ export function calculateCartPrice(items) {
128
+ return (items ?? []).reduce((acc, item) => {
129
+ const { product, amount } = item;
130
+ const productPrice = getPriceAfterDiscount(product);
131
+ const discount = calculateDiscount(product);
132
+ const realPrice = product.price - discount;
133
+ let productVatValue = 0;
134
+ if (product.vat) {
135
+ productVatValue = (realPrice * CONFIG.VAT) / 100;
136
+ productVatValue = productVatValue * amount;
137
+ acc.vat += productVatValue;
138
+ }
139
+ acc.cost += amount * product.price;
140
+ acc.discount += discount ? amount * discount : discount;
141
+ acc.finalCost += amount * productPrice + productVatValue;
142
+ return acc;
143
+ }, {
144
+ cost: 0,
145
+ discount: 0,
146
+ vat: 0,
147
+ finalCost: 0,
148
+ });
149
+ }
@@ -6,7 +6,7 @@ export const BaseCategorySchema = z.object({
6
6
  companyId: z.string().min(1),
7
7
  storeId: z.string().min(1),
8
8
  parentId: z.string().nullish(),
9
- tag: z.string().min(1),
9
+ tag: z.string().optional(),
10
10
  locales: z.array(LocaleSchema),
11
11
  depth: z.number(),
12
12
  });
@@ -0,0 +1,205 @@
1
+ import { TCart, TDiscount, TProduct, TStore } from "../entities";
2
+
3
+ const CONFIG = {
4
+ VAT: 18,
5
+ };
6
+
7
+ function calculateDiscount(product: TProduct) {
8
+ if (product.discount?.type === "percent") {
9
+ return (product.price * (product.discount.value ?? 100)) / 100;
10
+ }
11
+ if (product.discount?.type === "number") {
12
+ return product.price - (product.discount.value ?? 0);
13
+ }
14
+ return 0;
15
+ }
16
+
17
+ function getPriceAfterDiscount(product: TProduct) {
18
+ if (product.discount?.type === "percent") {
19
+ const dscountAmount = (product.price * product.discount.value) / 100;
20
+ return product.price - dscountAmount;
21
+ }
22
+ if (product.discount?.type === "number") {
23
+ const dscountAmount = product.price - product.discount.value;
24
+ return dscountAmount;
25
+ }
26
+ return product.price;
27
+ }
28
+
29
+ // main
30
+ export function getCartCost({
31
+ cart,
32
+ discounts,
33
+ store,
34
+ }: {
35
+ cart: TCart["items"];
36
+ discounts: TDiscount[];
37
+ store: TStore;
38
+ }) {
39
+ const { isVatIncludedInPrice } = store;
40
+ let result = cart.map((item) => {
41
+ return {
42
+ amount: item.amount,
43
+ product: { ...item.product },
44
+ originalPrice: item.product.price,
45
+ finalPrice: item.product.price,
46
+ finalDiscount: 0,
47
+ };
48
+ });
49
+
50
+ const activeDiscounts = discounts.filter((discount) => {
51
+ if (discount.variant.variantType === "bundle") {
52
+ const productsTotal =
53
+ cart?.reduce((total, item) => {
54
+ if (discount.variant.productsId.includes(item.product.id)) {
55
+ total += item.amount;
56
+ return total;
57
+ }
58
+ return total;
59
+ }, 0) ?? 0;
60
+ if (productsTotal >= discount.variant.requiredQuantity) {
61
+ // const times = Math.floor(productsTotal / discount.variant.requiredQuantity);
62
+ // console.log("yes", times, discount.variant.discountPrice);
63
+ return true;
64
+ }
65
+ }
66
+ return false;
67
+ });
68
+ console.log("activeDiscounts", activeDiscounts);
69
+
70
+ activeDiscounts.forEach((discount) => {
71
+ if (discount.variant.variantType === "bundle") {
72
+ // get all products in cart
73
+ const products = cart.filter((item) =>
74
+ discount.variant.productsId.includes(item.product.id)
75
+ );
76
+ const productsTotal =
77
+ products?.reduce((total, item) => {
78
+ if (discount.variant.productsId.includes(item.product.id)) {
79
+ total += item.amount;
80
+ return total;
81
+ }
82
+ return total;
83
+ }, 0) ?? 0;
84
+
85
+ const times = Math.floor(productsTotal / discount.variant.requiredQuantity);
86
+ const price = getPriceAfterDiscount(products[0]?.product);
87
+ const _discount = calculateDiscount(products[0]?.product);
88
+ console.log("price", price, _discount);
89
+ const discountPrice =
90
+ Number(
91
+ (discount.variant.discountPrice / discount.variant.requiredQuantity).toFixed(2)
92
+ ) * 1;
93
+
94
+ console.log("discountPrice", discountPrice);
95
+ const totalDiscount =
96
+ (price * discount.variant.requiredQuantity - discount.variant.discountPrice) * times;
97
+
98
+ const originalPrice = productsTotal * price;
99
+ const discountPriceFinal = originalPrice - totalDiscount;
100
+
101
+ console.log("totalDiscount", totalDiscount);
102
+
103
+ console.log("discountPriceFinal", discountPriceFinal, originalPrice);
104
+
105
+ const averagePrice = Number((discountPriceFinal / productsTotal).toFixed(2));
106
+ console.log("averagePrice", averagePrice);
107
+
108
+ result = result.map((item) => {
109
+ if (discount.variant.productsId.includes(item.product.id)) {
110
+ return {
111
+ ...item,
112
+ finalPrice: averagePrice,
113
+ originalPrice: item.product.price,
114
+ finalDiscount: item.product.price - averagePrice,
115
+ };
116
+ }
117
+ return item;
118
+ });
119
+
120
+ console.log("averagePrice", averagePrice);
121
+
122
+ console.log(
123
+ "yes",
124
+ times,
125
+ discount.variant.requiredQuantity,
126
+ discount.variant.discountPrice
127
+ );
128
+
129
+ console.log("dis", productsTotal, products);
130
+
131
+ // find average price
132
+ }
133
+ });
134
+
135
+ console.log("result", result);
136
+
137
+ const cartDetails = result.reduce(
138
+ (acc, item) => {
139
+ const { product, amount, finalPrice, finalDiscount } = item;
140
+ console.log("isVatIncludedInPrice", isVatIncludedInPrice);
141
+
142
+ let productVatValue: number = 0;
143
+ if (product.vat) {
144
+ if (isVatIncludedInPrice) {
145
+ const vat_amount = finalPrice * (CONFIG.VAT / (100 + CONFIG.VAT));
146
+ productVatValue = Number(vat_amount.toFixed(2));
147
+ productVatValue = productVatValue * amount;
148
+
149
+ acc.vat += Number(productVatValue.toFixed(2));
150
+ } else {
151
+ productVatValue = (finalPrice * CONFIG.VAT) / 100;
152
+ productVatValue = productVatValue * amount;
153
+ acc.vat += Number(productVatValue.toFixed(2));
154
+ }
155
+ }
156
+ console.log("finalDiscount", finalDiscount);
157
+
158
+ acc.cost += amount * finalPrice;
159
+ acc.discount += finalDiscount ? amount * finalDiscount : finalDiscount;
160
+ acc.finalCost += amount * finalPrice + (isVatIncludedInPrice ? 0 : productVatValue);
161
+
162
+ return acc;
163
+ },
164
+ {
165
+ discount: 0,
166
+ cost: 0,
167
+ finalCost: 0,
168
+ vat: 0,
169
+ }
170
+ );
171
+
172
+ console.log("cartDetails", cartDetails);
173
+
174
+ return { items: result, ...cartDetails };
175
+ }
176
+
177
+ export function calculateCartPrice(items: TCart["items"]) {
178
+ return (items ?? []).reduce(
179
+ (acc, item) => {
180
+ const { product, amount } = item;
181
+ const productPrice = getPriceAfterDiscount(product);
182
+ const discount = calculateDiscount(product);
183
+
184
+ const realPrice = product.price - discount;
185
+
186
+ let productVatValue: number = 0;
187
+ if (product.vat) {
188
+ productVatValue = (realPrice * CONFIG.VAT) / 100;
189
+ productVatValue = productVatValue * amount;
190
+ acc.vat += productVatValue;
191
+ }
192
+ acc.cost += amount * product.price;
193
+ acc.discount += discount ? amount * discount : discount;
194
+ acc.finalCost += amount * productPrice + productVatValue;
195
+
196
+ return acc;
197
+ },
198
+ {
199
+ cost: 0,
200
+ discount: 0,
201
+ vat: 0,
202
+ finalCost: 0,
203
+ }
204
+ );
205
+ }