Skip to content

Commit 092ca39

Browse files
committed
fix: grafico vendite e acquisti in Statistiche
1 parent 03f504b commit 092ca39

6 files changed

Lines changed: 105 additions & 64 deletions

File tree

assets/src/js/functions/dates.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ function start_datepickers() {
115115
});
116116
}
117117

118-
function start_complete_calendar(id, callback) {
118+
function start_complete_calendar(id, callback, options = {}) {
119+
var startDate = options.startDate ? moment(options.startDate) : moment(globals.start_date);
120+
var endDate = options.endDate ? moment(options.endDate) : moment(globals.end_date);
121+
119122
var ranges = {};
120123
ranges[globals.translations.today] = [moment(), moment()];
121124
ranges[globals.translations.firstThreemester] = [moment("01", "MM"), moment("03", "MM").endOf('month')];
@@ -144,8 +147,8 @@ function start_complete_calendar(id, callback) {
144147
toLabel: globals.translations.to,
145148
},
146149
ranges: ranges,
147-
startDate: globals.start_date_formatted,
148-
endDate: globals.end_date_formatted,
150+
startDate: startDate,
151+
endDate: endDate,
149152
applyClass: 'btn btn-success btn-sm',
150153
cancelClass: 'btn btn-danger btn-sm',
151154
linkedCalendars: false

modules/statistiche/actions.php

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
$calendar_id = filter('calendar_id');
2727
$start = filter('start');
2828
$end = filter('end');
29+
$locale_id = prepare(Models\Locale::getDefault()->id);
30+
$prepared_start = prepare($start);
31+
$prepared_end = prepare($end);
2932

3033
switch (filter('op')) {
3134
case 'fatturato':
32-
if (empty(Module::where('name', 'Vendita al banco')->first()->id)) {
35+
if (!Module::where('name', 'Vendita al banco')->exists()) {
3336
$results = $dbo->fetchArray('
3437
SELECT
3538
ROUND(SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))), 2) AS result,
@@ -38,13 +41,13 @@
3841
FROM
3942
`co_documenti`
4043
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
41-
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
44+
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = '.$locale_id.')
4245
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
4346
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
4447
WHERE
4548
`co_tipidocumento`.`dir`=\'entrata\'
4649
AND `co_tipidocumento`.`name`!=\'Bozza\'
47-
AND `co_documenti`.`data` BETWEEN '.prepare($start).' AND '.prepare($end).'
50+
AND `co_documenti`.`data` BETWEEN '.$prepared_start.' AND '.$prepared_end.'
4851
AND `is_fiscale`=1
4952
AND `zz_segments`.`autofatture`=0
5053
GROUP BY
@@ -55,35 +58,48 @@
5558
} else {
5659
$results = $dbo->fetchArray('
5760
SELECT
58-
ROUND(SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))), 2) AS result,
59-
YEAR(`co_documenti`.`data`) AS year,
60-
MONTH(`co_documenti`.`data`) AS month
61-
FROM
62-
`co_documenti`
63-
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
64-
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
65-
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
66-
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
67-
WHERE
68-
`co_tipidocumento`.`dir`=\'entrata\'
69-
AND `co_tipidocumento`.`name`!=\'Bozza\'
70-
AND `co_documenti`.`data` BETWEEN '.prepare($start).' AND '.prepare($end).'
71-
AND `is_fiscale`=1
72-
AND `zz_segments`.`autofatture`=0
73-
GROUP BY
74-
YEAR(`co_documenti`.`data`), MONTH(`co_documenti`.`data`)
75-
76-
UNION
77-
78-
SELECT
79-
ROUND(SUM(`vb_righe_venditabanco`.`subtotale` - `vb_righe_venditabanco`.`sconto`), 2) AS result,
80-
YEAR(`vb_venditabanco`.`data`) AS year,
81-
MONTH(`vb_venditabanco`.`data`) AS month
61+
ROUND(SUM(`movimenti`.`result`), 2) AS result,
62+
`movimenti`.`year`,
63+
`movimenti`.`month`
8264
FROM
83-
`vb_venditabanco`
84-
INNER JOIN `vb_righe_venditabanco` ON `vb_righe_venditabanco`.`idvendita`=`vb_venditabanco`.`id`
65+
(
66+
SELECT
67+
ROUND(SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))), 2) AS result,
68+
YEAR(`co_documenti`.`data`) AS year,
69+
MONTH(`co_documenti`.`data`) AS month
70+
FROM
71+
`co_documenti`
72+
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
73+
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = '.$locale_id.')
74+
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
75+
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
76+
WHERE
77+
`co_tipidocumento`.`dir`=\'entrata\'
78+
AND `co_tipidocumento`.`name`!=\'Bozza\'
79+
AND `co_documenti`.`data` BETWEEN '.$prepared_start.' AND '.$prepared_end.'
80+
AND `is_fiscale`=1
81+
AND `zz_segments`.`autofatture`=0
82+
GROUP BY
83+
YEAR(`co_documenti`.`data`), MONTH(`co_documenti`.`data`)
84+
85+
UNION ALL
86+
87+
SELECT
88+
ROUND(SUM(`vb_righe_venditabanco`.`subtotale` - `vb_righe_venditabanco`.`sconto`), 2) AS result,
89+
YEAR(`vb_venditabanco`.`data`) AS year,
90+
MONTH(`vb_venditabanco`.`data`) AS month
91+
FROM
92+
`vb_venditabanco`
93+
INNER JOIN `vb_righe_venditabanco` ON `vb_righe_venditabanco`.`idvendita`=`vb_venditabanco`.`id`
94+
WHERE
95+
`vb_venditabanco`.`data` BETWEEN '.$prepared_start.' AND '.$prepared_end.'
96+
GROUP BY
97+
YEAR(`vb_venditabanco`.`data`), MONTH(`vb_venditabanco`.`data`)
98+
) AS `movimenti`
8599
GROUP BY
86-
YEAR(`vb_venditabanco`.`data`), MONTH(`vb_venditabanco`.`data`)
100+
`movimenti`.`year`, `movimenti`.`month`
101+
ORDER BY
102+
`movimenti`.`year` ASC, `movimenti`.`month` ASC
87103
');
88104
}
89105

@@ -99,7 +115,28 @@
99115
break;
100116

101117
case 'acquisti':
102-
$results = $dbo->fetchArray('SELECT ROUND(SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))), 2) AS result, YEAR(`co_documenti`.`data`) AS year, MONTH(`co_documenti`.`data`) AS month FROM `co_documenti` INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id` LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id` INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id` WHERE `co_tipidocumento`.`dir`=\'uscita\' AND `co_tipidocumento_lang`.`title`!=\'Bozza\' AND `co_documenti`.`data` BETWEEN '.prepare($start).' AND '.prepare($end).' AND `is_fiscale`=1 AND `zz_segments`.`autofatture`=0 GROUP BY YEAR(`co_documenti`.`data`), MONTH(`co_documenti`.`data`) ORDER BY YEAR(`co_documenti`.`data`) ASC, MONTH(`co_documenti`.`data`) ASC');
118+
$results = $dbo->fetchArray('
119+
SELECT
120+
ROUND(SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))), 2) AS result,
121+
YEAR(`co_documenti`.`data`) AS year,
122+
MONTH(`co_documenti`.`data`) AS month
123+
FROM
124+
`co_documenti`
125+
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
126+
LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id` = `co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang` = '.$locale_id.')
127+
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
128+
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
129+
WHERE
130+
`co_tipidocumento`.`dir`=\'uscita\'
131+
AND `co_tipidocumento`.`name`!=\'Bozza\'
132+
AND `co_documenti`.`data` BETWEEN '.$prepared_start.' AND '.$prepared_end.'
133+
AND `is_fiscale`=1
134+
AND `zz_segments`.`autofatture`=0
135+
GROUP BY
136+
YEAR(`co_documenti`.`data`), MONTH(`co_documenti`.`data`)
137+
ORDER BY
138+
YEAR(`co_documenti`.`data`) ASC, MONTH(`co_documenti`.`data`) ASC
139+
');
103140

104141
$results = Stats::monthly($results, $start, $end);
105142

modules/statistiche/edit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@
198198
url: "'.str_replace('edit.php', '', $structure->fileurl('edit.php')).'",
199199
id_module: globals.id_module,
200200
id_record: globals.id_record,
201-
start_date: globals.start_date,
202-
end_date: globals.end_date,
201+
start_date: "'.$start.'",
202+
end_date: "'.$end.'",
203203
}
204204
var manager = new Manager(info);
205205

modules/statistiche/js/functions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ function add_calendar() {
5858
var end = end.format("YYYY-MM-DD");
5959

6060
manager.update(name, start, end);
61+
}, {
62+
startDate: manager.info.start_date,
63+
endDate: manager.info.end_date,
6164
});
6265

6366
// Inizializzazone calendario

modules/statistiche/js/stats/line_chart.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class LineChart extends Stat {
9999
color = color.substr(0, 7);
100100

101101
chart.data.datasets.splice(dataset_index, 1);
102-
chart.data.labels.splice(dataset_index, 1);
103102
}
104103

105104
while (chart.data.labels.length > max_length) {

modules/statistiche/src/Stats.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,34 @@ class Stats
2424
{
2525
public static function monthly($original, $start, $end)
2626
{
27-
// Copia dei dati
28-
$array = new \ArrayObject($original);
29-
$data = $array->getArrayCopy();
30-
31-
// Ordinamento
32-
array_multisort(array_column($data, 'year'), SORT_ASC,
33-
array_column($data, 'month'), SORT_ASC,
34-
$data);
35-
36-
// Differenza delle date in mesi
37-
$d1 = new \DateTime($start);
38-
$d2 = new \DateTime($end);
39-
$count = $d1->diff($d2)->m + ($d1->diff($d2)->y * 12) + 1;
40-
41-
$year = $d1->format('Y');
42-
$month = intval($d1->format('m')) - 1;
43-
for ($i = 0; $i < $count; ++$i) {
44-
$year = $month >= 12 ? $year + 1 : $year;
45-
$month = $month % 12;
46-
47-
if (!isset($data[$i]) || intval($data[$i]->month) != $month + 1) {
48-
array_splice($data, $i, 0, [[
49-
'result' => 0,
50-
'year' => $year,
51-
'month' => $month + 1,
52-
]]);
27+
$start_date = (new \DateTimeImmutable($start));
28+
$end_date = (new \DateTimeImmutable($end));
29+
30+
if ($end_date < $start_date) {
31+
return [];
32+
}
33+
34+
$grouped = [];
35+
foreach ($original as $row) {
36+
$year = (int) ($row['year'] ?? 0);
37+
$month = (int) ($row['month'] ?? 0);
38+
39+
if ($year < 1 || $month < 1 || $month > 12) {
40+
continue;
5341
}
5442

55-
++$month;
43+
$key = sprintf('%04d-%02d', $year, $month);
44+
$grouped[$key] = round(($grouped[$key] ?? 0) + (float) ($row['result'] ?? 0), 2);
45+
}
46+
47+
$data = [];
48+
for ($cursor = $start_date; $cursor <= $end_date; $cursor = $cursor->modify('+1 month')) {
49+
$key = $cursor->format('Y-m');
50+
$data[] = [
51+
'result' => round((float) ($grouped[$key] ?? 0), 2),
52+
'year' => (int) $cursor->format('Y'),
53+
'month' => (int) $cursor->format('m'),
54+
];
5655
}
5756

5857
return $data;

0 commit comments

Comments
 (0)