npm-pkg-hook 1.1.2 → 1.1.4
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/.eslintrc.js +132 -132
- package/.vscode/extensions.json +6 -0
- package/.vscode/settings.json +8 -0
- package/next.config.js +1 -0
- package/package.json +43 -46
- package/src/config/client/errors.js +1 -2
- package/src/hooks/index.js +20 -25
- package/src/hooks/useAnimationText/index.jsx +13 -11
- package/src/hooks/useCatWithProduct/index.js +4 -37
- package/src/hooks/useCatWithProduct/queries.js +16 -0
- package/src/hooks/useChartData/index.js +156 -182
- package/src/hooks/useChartData/useChartData/index.js +197 -0
- package/src/hooks/useChartData/useChartDataAllOrders/index.js +94 -0
- package/src/hooks/useCheckbox/index.js +2 -5
- package/src/hooks/useClients/index.js +8 -17
- package/src/hooks/useCreateProduct/index.js +64 -94
- package/src/hooks/useDrag/index.js +1 -0
- package/src/hooks/useEmployee/index.js +8 -11
- package/src/hooks/useFormTools/index.js +1 -1
- package/src/hooks/useFullScreenMode/index.js +8 -8
- package/src/hooks/useGoogleLogin/index.js +161 -0
- package/src/hooks/useGoogleLogin/loadScript.js +15 -0
- package/src/hooks/useGoogleLogin/removeScript.js +7 -0
- package/src/hooks/useHover/index.js +1 -1
- package/src/hooks/useImagesStore/index.js +144 -176
- package/src/hooks/useImagesStore/queries.js +28 -1
- package/src/hooks/useKeypress/index.js +1 -7
- package/src/hooks/useLogout/index.js +27 -22
- package/src/hooks/useManageQueryParams/index.js +1 -1
- package/src/hooks/useProductsFood/queriesStore.js +16 -3
- package/src/hooks/useReport/index.js +30 -26
- package/src/hooks/useReport/queries.js +32 -47
- package/src/hooks/useSales/index.js +21 -52
- package/src/hooks/useSales/queries.js +38 -48
- package/src/hooks/useSales/useGetSale.js +2 -16
- package/src/hooks/useSchedule/index.jsx +23 -0
- package/src/hooks/useUpdateExistingOrders/index.js +8 -8
- package/src/hooks/useUser/index.js +2 -2
- package/src/hooks/useUser/queries.js +40 -40
- package/src/hooks/useWindowSize/index.js +1 -1
- package/src/index.jsx +1 -2
- package/src/security/index.js +0 -0
|
@@ -2,196 +2,170 @@ import { useQuery } from '@apollo/client';
|
|
|
2
2
|
import { useState } from 'react';
|
|
3
3
|
import { SPANISH_MONTHS } from "../../utils/index";
|
|
4
4
|
import { GET_ALL_SALES } from "../useReport/queries";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const calculateSalesByMonth = (salesData) => {
|
|
8
|
-
try {
|
|
9
|
-
const result = Array.from({ length: 12 }, (_, mes) => ({
|
|
10
|
-
Mes: mes,
|
|
11
|
-
Year: new Date().getFullYear(),
|
|
12
|
-
totalProductsPrice: 0,
|
|
13
|
-
}));
|
|
14
|
-
|
|
15
|
-
salesData?.forEach((value) => {
|
|
16
|
-
const mes = new Date(value.pDatCre).getMonth();
|
|
17
|
-
result[mes].totalProductsPrice += value.totalProductsPrice;
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
return result;
|
|
21
|
-
} catch (error) {
|
|
22
|
-
return []
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// Función para llenar los meses faltantes
|
|
27
|
-
const fillMissingMonths = (data) => {
|
|
28
|
-
try {
|
|
29
|
-
const allMonths = Array.from({ length: 12 }, (_, i) => i);
|
|
30
|
-
const missingMonths = allMonths.filter(month => !data.some(data => data.Mes === month));
|
|
31
|
-
return data.concat(
|
|
32
|
-
missingMonths.map(element => ({
|
|
33
|
-
Mes: element,
|
|
34
|
-
totalProductsPrice: 0,
|
|
35
|
-
Year: '',
|
|
36
|
-
}))
|
|
37
|
-
).sort((a, b) => a.Mes - b.Mes);
|
|
38
|
-
} catch (error) {
|
|
39
|
-
return []
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Función para obtener los datos del gráfico
|
|
44
|
-
const getChartData = (asFilter, newResult, result, chartType) => ({
|
|
45
|
-
labels: asFilter
|
|
46
|
-
? newResult.map(data => SPANISH_MONTHS[data.Mes])
|
|
47
|
-
: result.map(data => SPANISH_MONTHS[data.Mes]),
|
|
48
|
-
datasets: [
|
|
49
|
-
{
|
|
50
|
-
label: `Ventas por meses del año ${asFilter ? chartTypeYear : ''}`,
|
|
51
|
-
data: asFilter
|
|
52
|
-
? newResult.map(data => data.totalProductsPrice)
|
|
53
|
-
: result.map(data => data.totalProductsPrice),
|
|
54
|
-
backgroundColor: [
|
|
55
|
-
'rgba(255, 99, 132, 0.2)',
|
|
56
|
-
'rgba(54, 162, 235, 0.2)',
|
|
57
|
-
'rgba(255, 206, 86, 0.2)',
|
|
58
|
-
'rgba(75, 192, 192, 0.2)',
|
|
59
|
-
'rgba(153, 102, 255, 0.2)',
|
|
60
|
-
'rgba(255, 159, 64, 0.2)'
|
|
61
|
-
],
|
|
62
|
-
borderColor: chartType === 'bar'
|
|
63
|
-
? [
|
|
64
|
-
'rgba(255, 99, 132, 1)',
|
|
65
|
-
'rgba(54, 162, 235, 1)',
|
|
66
|
-
'rgba(255, 206, 86, 1)',
|
|
67
|
-
'rgba(75, 192, 192, 1)',
|
|
68
|
-
'rgba(153, 102, 255, 1)',
|
|
69
|
-
'rgba(255, 159, 64, 1)'
|
|
70
|
-
]
|
|
71
|
-
: 'rgb(255 0 0)',
|
|
72
|
-
tension: 0.6,
|
|
73
|
-
fill: false,
|
|
74
|
-
borderWidth: 1,
|
|
75
|
-
barPercentage: 1,
|
|
76
|
-
barThickness: 50,
|
|
77
|
-
minBarLength: 3
|
|
78
|
-
}
|
|
79
|
-
]
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Función para agrupar los datos por año
|
|
83
|
-
const groupSalesByYear = (salesData = []) => {
|
|
84
|
-
const groupedData = {};
|
|
85
|
-
try {
|
|
86
|
-
salesData?.forEach((item) => {
|
|
87
|
-
const year = new Date(item.pDatCre).getFullYear();
|
|
88
|
-
if (!groupedData[year]) {
|
|
89
|
-
groupedData[year] = [];
|
|
90
|
-
}
|
|
91
|
-
groupedData[year].push(item);
|
|
92
|
-
});
|
|
93
|
-
return groupedData;
|
|
94
|
-
} catch (error) {
|
|
95
|
-
return groupedData
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// Función para obtener años únicos
|
|
100
|
-
const getUniqueYears = (salesData = []) => {
|
|
101
|
-
try {
|
|
102
|
-
const years = [];
|
|
103
|
-
salesData?.forEach((item) => {
|
|
104
|
-
const y = new Date(item.pDatCre).getFullYear();
|
|
105
|
-
if (!years.includes(y)) {
|
|
106
|
-
years.push(y);
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
return years.sort((a, b) => b - a);
|
|
110
|
-
} catch (error) {
|
|
111
|
-
return []
|
|
112
|
-
}
|
|
113
|
-
};
|
|
5
|
+
export * from './useChartData'
|
|
6
|
+
export * from './useChartDataAllOrders'
|
|
114
7
|
|
|
115
8
|
export const useChartData = ({ year }) => {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
9
|
+
// Construcción del nuevo array:
|
|
10
|
+
const { data, loading } = useQuery(GET_ALL_SALES)
|
|
11
|
+
const [chartType, setChartType] = useState('bar')
|
|
12
|
+
const [chartTypeYear, setChartTypeYear] = useState(new Date().getFullYear())
|
|
13
|
+
const [asFilter, setFilter] = useState(false)
|
|
14
|
+
const [newResult, setNewResult] = useState([])
|
|
15
|
+
let result = []
|
|
16
|
+
data?.getAllSalesStore?.length > 0 && data?.getAllSalesStore.reduce(function (res, value) {
|
|
17
|
+
// Creamos la posición del array para cada mes
|
|
18
|
+
let mes = new Date(value.pDatCre).getMonth()
|
|
19
|
+
let Year = new Date(value.pDatCre).getFullYear()
|
|
20
|
+
if (!res[mes]) {
|
|
21
|
+
res[mes] = { Mes: mes, Year }
|
|
22
|
+
// Inicializamos a 0 el valor de cada key
|
|
23
|
+
Object.keys(value).forEach((key) => {
|
|
24
|
+
if (key != 'pDatCre') {
|
|
25
|
+
res[mes][key] = 0
|
|
26
|
+
}
|
|
27
|
+
})
|
|
121
28
|
|
|
122
|
-
|
|
29
|
+
result.push(res[mes])
|
|
30
|
+
}
|
|
31
|
+
// Sumamos el valor de cada clave dentro de un bucle
|
|
32
|
+
Object.keys(value).forEach(function (key) {
|
|
33
|
+
if (key != 'pDatCre') {
|
|
34
|
+
res[mes]['totalProductsPrice'] += value['totalProductsPrice']
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
return res
|
|
38
|
+
}, {})
|
|
123
39
|
|
|
124
|
-
|
|
40
|
+
let allMonths = Array.from({ length: 12 }, (_, i) => { return i })
|
|
41
|
+
let missingMonths = allMonths.filter(month => { return !result.some(data => { return data.Mes === month }) })
|
|
125
42
|
|
|
126
|
-
|
|
43
|
+
for (const element of missingMonths) {
|
|
44
|
+
result.push({
|
|
45
|
+
'Mes': element,
|
|
46
|
+
'totalProductsPrice': 0,
|
|
47
|
+
'Year': ''
|
|
48
|
+
})
|
|
49
|
+
}
|
|
127
50
|
|
|
128
|
-
|
|
129
|
-
const years = getUniqueYears(data?.getAllSalesStore);
|
|
51
|
+
result.sort((a, b) => { return a.Mes - b.Mes })
|
|
130
52
|
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
53
|
+
const labelTitle = `Ventas por meses del año ${asFilter ? chartTypeYear : ''}`
|
|
54
|
+
// Resultado:
|
|
55
|
+
const dataChart = {
|
|
56
|
+
labels: asFilter ? newResult.map(data => {
|
|
57
|
+
return SPANISH_MONTHS[data.Mes]
|
|
58
|
+
}) : result.map(data => {
|
|
59
|
+
return SPANISH_MONTHS[data.Mes]
|
|
60
|
+
}),
|
|
61
|
+
datasets: [
|
|
62
|
+
{
|
|
63
|
+
label: labelTitle,
|
|
64
|
+
data: asFilter ? newResult.map(data => { return data.totalProductsPrice }) : result.map(data => { return data.totalProductsPrice }),
|
|
65
|
+
backgroundColor: [
|
|
66
|
+
'rgba(255, 99, 132, 0.2)',
|
|
67
|
+
'rgba(54, 162, 235, 0.2)',
|
|
68
|
+
'rgba(255, 206, 86, 0.2)',
|
|
69
|
+
'rgba(75, 192, 192, 0.2)',
|
|
70
|
+
'rgba(153, 102, 255, 0.2)',
|
|
71
|
+
'rgba(255, 159, 64, 0.2)'
|
|
72
|
+
],
|
|
73
|
+
borderColor: chartType === 'bar' ? [
|
|
74
|
+
'rgba(255, 99, 132, 1)',
|
|
75
|
+
'rgba(54, 162, 235, 1)',
|
|
76
|
+
'rgba(255, 206, 86, 1)',
|
|
77
|
+
'rgba(75, 192, 192, 1)',
|
|
78
|
+
'rgba(153, 102, 255, 1)',
|
|
79
|
+
'rgba(255, 159, 64, 1)'
|
|
80
|
+
] : 'rgb(255 0 0)',
|
|
81
|
+
tension: 0.6,
|
|
82
|
+
fill: false,
|
|
83
|
+
borderWidth: 1,
|
|
84
|
+
barPercentage: 1,
|
|
85
|
+
barThickness: 50,
|
|
86
|
+
minBarLength: 3,
|
|
87
|
+
}
|
|
88
|
+
]
|
|
150
89
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
mode: 'index',
|
|
164
|
-
intersect: false,
|
|
165
|
-
},
|
|
166
|
-
scales: {
|
|
167
|
-
x: {
|
|
168
|
-
stacked: true
|
|
90
|
+
const options = {
|
|
91
|
+
interaction: {
|
|
92
|
+
mode: 'index',
|
|
93
|
+
intersect: false,
|
|
94
|
+
},
|
|
95
|
+
scales: {
|
|
96
|
+
x: {
|
|
97
|
+
stacked: true
|
|
98
|
+
},
|
|
99
|
+
y: {
|
|
100
|
+
stacked: true
|
|
101
|
+
}
|
|
169
102
|
},
|
|
170
|
-
|
|
171
|
-
|
|
103
|
+
plugins: {
|
|
104
|
+
title: {
|
|
105
|
+
display: true,
|
|
106
|
+
text: labelTitle
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const groupedData = {}
|
|
111
|
+
|
|
112
|
+
data && data.getAllSalesStore.forEach((item) => {
|
|
113
|
+
const year = new Date(item.pDatCre).getFullYear()
|
|
114
|
+
if (!groupedData[year]) {
|
|
115
|
+
groupedData[year] = []
|
|
116
|
+
}
|
|
117
|
+
groupedData[year].push(item)
|
|
118
|
+
})
|
|
119
|
+
const years = []
|
|
120
|
+
data && data.getAllSalesStore.forEach((item) => {
|
|
121
|
+
const y = new Date(item.pDatCre).getFullYear()
|
|
122
|
+
if (!years.includes(y)) {
|
|
123
|
+
years.push(y)
|
|
172
124
|
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
125
|
+
})
|
|
126
|
+
const handleChangeYear = (value) => {
|
|
127
|
+
setFilter(true)
|
|
128
|
+
const currentYear = parseInt(value)
|
|
129
|
+
setChartTypeYear(currentYear || '')
|
|
130
|
+
if (result?.length > 0) {
|
|
131
|
+
const filterToYear = result.filter((elem) => {
|
|
132
|
+
return elem?.Year == currentYear
|
|
133
|
+
})
|
|
134
|
+
let missingNewMonths = allMonths.filter(month => { return !filterToYear.some(data => { return data.Mes === month }) })
|
|
135
|
+
for (const element of missingNewMonths) {
|
|
136
|
+
filterToYear.push({
|
|
137
|
+
'Mes': element,
|
|
138
|
+
'totalProductsPrice': 0,
|
|
139
|
+
'Year': ''
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
filterToYear.sort((a, b) => { return a.Mes - b.Mes })
|
|
143
|
+
setNewResult(filterToYear)
|
|
144
|
+
return filterToYear
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const cleanFilter = () => {
|
|
148
|
+
setFilter(false)
|
|
149
|
+
setChartTypeYear(new Date().getFullYear())
|
|
150
|
+
}
|
|
151
|
+
const sortYear = () => {
|
|
152
|
+
return years.sort((a, b) => { return b - a });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const organiceYears = sortYear()
|
|
156
|
+
return {
|
|
157
|
+
handleChangeYear,
|
|
158
|
+
setFilter,
|
|
159
|
+
cleanFilter,
|
|
160
|
+
setChartType,
|
|
161
|
+
years: organiceYears,
|
|
162
|
+
asFilter,
|
|
163
|
+
chartTypeYear,
|
|
164
|
+
options,
|
|
165
|
+
chartType,
|
|
166
|
+
labelTitle,
|
|
167
|
+
result,
|
|
168
|
+
dataChart,
|
|
169
|
+
loading
|
|
179
170
|
}
|
|
180
|
-
|
|
181
|
-
return {
|
|
182
|
-
handleChangeYear,
|
|
183
|
-
setFilter,
|
|
184
|
-
cleanFilter,
|
|
185
|
-
setChartType,
|
|
186
|
-
years: organiceYears,
|
|
187
|
-
asFilter,
|
|
188
|
-
chartTypeYear,
|
|
189
|
-
options,
|
|
190
|
-
chartType,
|
|
191
|
-
labelTitle,
|
|
192
|
-
result: filledResult,
|
|
193
|
-
dataChart,
|
|
194
|
-
loading,
|
|
195
|
-
};
|
|
196
|
-
};
|
|
197
|
-
|
|
171
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { useQuery } from '@apollo/client';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { SPANISH_MONTHS } from "../../../utils/index";
|
|
4
|
+
import { GET_ALL_SALES } from "../../useReport/queries";
|
|
5
|
+
|
|
6
|
+
// Función para calcular el total de ventas por mes
|
|
7
|
+
const calculateSalesByMonth = (salesData) => {
|
|
8
|
+
try {
|
|
9
|
+
const result = Array.from({ length: 12 }, (_, mes) => ({
|
|
10
|
+
Mes: mes,
|
|
11
|
+
Year: new Date().getFullYear(),
|
|
12
|
+
totalProductsPrice: 0,
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
salesData?.forEach((value) => {
|
|
16
|
+
const mes = new Date(value.pDatCre).getMonth();
|
|
17
|
+
result[mes].totalProductsPrice += value.totalProductsPrice;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return result;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return []
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Función para llenar los meses faltantes
|
|
27
|
+
const fillMissingMonths = (data) => {
|
|
28
|
+
try {
|
|
29
|
+
const allMonths = Array.from({ length: 12 }, (_, i) => i);
|
|
30
|
+
const missingMonths = allMonths.filter(month => !data.some(data => data.Mes === month));
|
|
31
|
+
return data.concat(
|
|
32
|
+
missingMonths.map(element => ({
|
|
33
|
+
Mes: element,
|
|
34
|
+
totalProductsPrice: 0,
|
|
35
|
+
Year: '',
|
|
36
|
+
}))
|
|
37
|
+
).sort((a, b) => a.Mes - b.Mes);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return []
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Función para obtener los datos del gráfico
|
|
44
|
+
const getChartData = (asFilter, newResult, result, chartType) => ({
|
|
45
|
+
labels: asFilter
|
|
46
|
+
? newResult.map(data => SPANISH_MONTHS[data.Mes])
|
|
47
|
+
: result.map(data => SPANISH_MONTHS[data.Mes]),
|
|
48
|
+
datasets: [
|
|
49
|
+
{
|
|
50
|
+
label: `Ventas por meses del año ${asFilter ? chartTypeYear : ''}`,
|
|
51
|
+
data: asFilter
|
|
52
|
+
? newResult.map(data => data.totalProductsPrice)
|
|
53
|
+
: result.map(data => data.totalProductsPrice),
|
|
54
|
+
backgroundColor: [
|
|
55
|
+
'rgba(255, 99, 132, 0.2)',
|
|
56
|
+
'rgba(54, 162, 235, 0.2)',
|
|
57
|
+
'rgba(255, 206, 86, 0.2)',
|
|
58
|
+
'rgba(75, 192, 192, 0.2)',
|
|
59
|
+
'rgba(153, 102, 255, 0.2)',
|
|
60
|
+
'rgba(255, 159, 64, 0.2)'
|
|
61
|
+
],
|
|
62
|
+
borderColor: chartType === 'bar'
|
|
63
|
+
? [
|
|
64
|
+
'rgba(255, 99, 132, 1)',
|
|
65
|
+
'rgba(54, 162, 235, 1)',
|
|
66
|
+
'rgba(255, 206, 86, 1)',
|
|
67
|
+
'rgba(75, 192, 192, 1)',
|
|
68
|
+
'rgba(153, 102, 255, 1)',
|
|
69
|
+
'rgba(255, 159, 64, 1)'
|
|
70
|
+
]
|
|
71
|
+
: 'rgb(255 0 0)',
|
|
72
|
+
tension: 0.6,
|
|
73
|
+
fill: false,
|
|
74
|
+
borderWidth: 1,
|
|
75
|
+
barPercentage: 1,
|
|
76
|
+
barThickness: 50,
|
|
77
|
+
minBarLength: 3
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Función para agrupar los datos por año
|
|
83
|
+
const groupSalesByYear = (salesData = []) => {
|
|
84
|
+
const groupedData = {};
|
|
85
|
+
try {
|
|
86
|
+
salesData?.forEach((item) => {
|
|
87
|
+
const year = new Date(item.pDatCre).getFullYear();
|
|
88
|
+
if (!groupedData[year]) {
|
|
89
|
+
groupedData[year] = [];
|
|
90
|
+
}
|
|
91
|
+
groupedData[year].push(item);
|
|
92
|
+
});
|
|
93
|
+
return groupedData;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return groupedData
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Función para obtener años únicos
|
|
100
|
+
const getUniqueYears = (salesData = []) => {
|
|
101
|
+
try {
|
|
102
|
+
const years = [];
|
|
103
|
+
salesData?.forEach((item) => {
|
|
104
|
+
const y = new Date(item.pDatCre).getFullYear();
|
|
105
|
+
if (!years.includes(y)) {
|
|
106
|
+
years.push(y);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return years.sort((a, b) => b - a);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return []
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const useChartData = ({ year }) => {
|
|
116
|
+
const { data, loading } = useQuery(GET_ALL_SALES);
|
|
117
|
+
const [chartType, setChartType] = useState('Line');
|
|
118
|
+
const [chartTypeYear, setChartTypeYear] = useState(new Date().getFullYear());
|
|
119
|
+
const [asFilter, setFilter] = useState(false);
|
|
120
|
+
const [newResult, setNewResult] = useState([]);
|
|
121
|
+
|
|
122
|
+
const result = calculateSalesByMonth(data?.getAllSalesStore);
|
|
123
|
+
|
|
124
|
+
const filledResult = fillMissingMonths(result);
|
|
125
|
+
|
|
126
|
+
const dataChart = getChartData(asFilter, newResult, filledResult, chartType);
|
|
127
|
+
|
|
128
|
+
const groupedData = groupSalesByYear(data?.getAllSalesStore);
|
|
129
|
+
const years = getUniqueYears(data?.getAllSalesStore);
|
|
130
|
+
|
|
131
|
+
const handleChangeYear = (value) => {
|
|
132
|
+
setFilter(true);
|
|
133
|
+
const currentYear = parseInt(value);
|
|
134
|
+
setChartTypeYear(currentYear || '');
|
|
135
|
+
|
|
136
|
+
if (filledResult?.length > 0) {
|
|
137
|
+
const filterToYear = filledResult.filter((elem) => elem?.Year === currentYear);
|
|
138
|
+
const missingNewMonths = allMonths.filter(month => !filterToYear.some(data => data.Mes === month));
|
|
139
|
+
|
|
140
|
+
const newFilteredResult = filterToYear.concat(
|
|
141
|
+
missingNewMonths.map(element => ({
|
|
142
|
+
Mes: element,
|
|
143
|
+
totalProductsPrice: 0,
|
|
144
|
+
Year: '',
|
|
145
|
+
}))
|
|
146
|
+
).sort((a, b) => a.Mes - b.Mes);
|
|
147
|
+
|
|
148
|
+
setNewResult(newFilteredResult);
|
|
149
|
+
return newFilteredResult;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const cleanFilter = () => {
|
|
154
|
+
setFilter(false);
|
|
155
|
+
setChartTypeYear(new Date().getFullYear());
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const sortYear = () => years.sort((a, b) => b - a);
|
|
159
|
+
const labelTitle = `Ventas por meses del año ${asFilter ? chartTypeYear : ''}`
|
|
160
|
+
const organiceYears = sortYear();
|
|
161
|
+
const options = {
|
|
162
|
+
interaction: {
|
|
163
|
+
mode: 'index',
|
|
164
|
+
intersect: false,
|
|
165
|
+
},
|
|
166
|
+
scales: {
|
|
167
|
+
x: {
|
|
168
|
+
stacked: true
|
|
169
|
+
},
|
|
170
|
+
y: {
|
|
171
|
+
stacked: true
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
plugins: {
|
|
175
|
+
title: {
|
|
176
|
+
display: true,
|
|
177
|
+
text: labelTitle
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
handleChangeYear,
|
|
183
|
+
setFilter,
|
|
184
|
+
cleanFilter,
|
|
185
|
+
setChartType,
|
|
186
|
+
years: organiceYears,
|
|
187
|
+
asFilter,
|
|
188
|
+
chartTypeYear,
|
|
189
|
+
options,
|
|
190
|
+
chartType,
|
|
191
|
+
labelTitle,
|
|
192
|
+
result: filledResult,
|
|
193
|
+
dataChart,
|
|
194
|
+
loading,
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useGetAllSales } from "../../useSales/useGetAllSales"
|
|
2
|
+
|
|
3
|
+
export const useChartDataAllOrders = ({
|
|
4
|
+
onScreen = false
|
|
5
|
+
} = {}) => {
|
|
6
|
+
const { data, loading } = useGetAllSales({
|
|
7
|
+
onScreen
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
// Objeto para mapear los códigos de estado a sus nombres
|
|
11
|
+
const statusMap = {
|
|
12
|
+
1: 'ACEPTA',
|
|
13
|
+
2: 'PROCESSING',
|
|
14
|
+
3: 'READY',
|
|
15
|
+
4: 'CONCLUDES',
|
|
16
|
+
5: 'RECHAZADOS'
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Objeto para almacenar los totales por estado
|
|
20
|
+
const totalsByStatus = {};
|
|
21
|
+
|
|
22
|
+
// Inicializar totales en 0 para todos los estados
|
|
23
|
+
for (const status in statusMap) {
|
|
24
|
+
totalsByStatus[status] = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Procesar los datos y acumular los totales por estado
|
|
28
|
+
data?.forEach(item => {
|
|
29
|
+
const status = item.pSState;
|
|
30
|
+
const totalPrice = item.totalProductsPrice;
|
|
31
|
+
totalsByStatus[status] += totalPrice;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Preparar los datos en el formato requerido por Chart.js
|
|
35
|
+
const chartLabels = [];
|
|
36
|
+
const chartData = [];
|
|
37
|
+
|
|
38
|
+
// Iterar a través del statusMap para llenar los datos
|
|
39
|
+
for (const status in statusMap) {
|
|
40
|
+
const statusLabel = statusMap[status];
|
|
41
|
+
chartLabels?.push(statusLabel);
|
|
42
|
+
chartData?.push(totalsByStatus[status] || 0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const chartJsData = {
|
|
46
|
+
labels: chartLabels,
|
|
47
|
+
datasets: [{
|
|
48
|
+
tension: 2,
|
|
49
|
+
fill: false,
|
|
50
|
+
borderWidth: 1,
|
|
51
|
+
barPercentage: 1,
|
|
52
|
+
barThickness: 50,
|
|
53
|
+
minBarLength: 3,
|
|
54
|
+
label: '',
|
|
55
|
+
data: chartData,
|
|
56
|
+
backgroundColor: [
|
|
57
|
+
'#FF5733', // Reddish color
|
|
58
|
+
'#FFC300', // Yellowish color
|
|
59
|
+
'#FF8C42', // Orange color
|
|
60
|
+
'#138D75', // Gold color
|
|
61
|
+
'#ff0000', // Tomato color
|
|
62
|
+
],
|
|
63
|
+
}],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const defaultOptions = {
|
|
67
|
+
animation: {
|
|
68
|
+
animateRotate: true,
|
|
69
|
+
animateScale: true,
|
|
70
|
+
},
|
|
71
|
+
responsive: true,
|
|
72
|
+
maintainAspectRatio: false,
|
|
73
|
+
legend: {
|
|
74
|
+
position: 'bottom',
|
|
75
|
+
},
|
|
76
|
+
tooltips: {
|
|
77
|
+
callbacks: {
|
|
78
|
+
label: (tooltipItem, data) => {
|
|
79
|
+
const dataset = data?.datasets[tooltipItem.datasetIndex];
|
|
80
|
+
const total = dataset?.data?.reduce((previousValue, currentValue) => previousValue + currentValue);
|
|
81
|
+
const currentValue = dataset?.data[tooltipItem?.index];
|
|
82
|
+
const percentage = ((currentValue / total) * 100).toFixed(2);
|
|
83
|
+
return `${data?.labels[tooltipItem?.index]}: ${currentValue} (${percentage}%)`;
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
loading,
|
|
91
|
+
data: loading ? [] : chartJsData,
|
|
92
|
+
option: loading ? [] : defaultOptions
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -15,7 +15,7 @@ import { useCallback, useState } from 'react'
|
|
|
15
15
|
* - clearAll (callback)
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
export const useCheckboxState = (elem, selectedIds = [], disabledIds = []
|
|
18
|
+
export const useCheckboxState = (elem, selectedIds = [], disabledIds = []) => {
|
|
19
19
|
const numTotalItems = elem?.length
|
|
20
20
|
const [checkedItems, setCheckedItems] = useState(new Set(selectedIds))
|
|
21
21
|
const [disabledItems, setDisabledItems] = useState(new Set(disabledIds))
|
|
@@ -23,17 +23,14 @@ export const useCheckboxState = (elem, selectedIds = [], disabledIds = [], calBa
|
|
|
23
23
|
const handleChangeCheck = useCallback((event, id) => {
|
|
24
24
|
const target = event.target
|
|
25
25
|
setCheckedItems(prevState => {
|
|
26
|
-
|
|
27
26
|
const newState = new Set(prevState)
|
|
28
27
|
if (target.checked) {
|
|
29
28
|
newState.add(id)
|
|
30
29
|
} else {
|
|
31
30
|
newState.delete(id)
|
|
32
31
|
}
|
|
33
|
-
calBack([...newState])
|
|
34
32
|
return newState
|
|
35
33
|
})
|
|
36
|
-
// eslint-disable-next-line
|
|
37
34
|
}, [])
|
|
38
35
|
|
|
39
36
|
const setAll = useCallback(
|
|
@@ -109,9 +106,9 @@ export const useCheckboxState = (elem, selectedIds = [], disabledIds = [], calBa
|
|
|
109
106
|
disabledItems,
|
|
110
107
|
handleChangeCheck,
|
|
111
108
|
toggleAll,
|
|
112
|
-
setCheckedItems,
|
|
113
109
|
selectAll,
|
|
114
110
|
clearAll,
|
|
111
|
+
setCheckedItems,
|
|
115
112
|
enableCheckboxes,
|
|
116
113
|
disableCheckboxes
|
|
117
114
|
}
|