|
26 | 26 | $calendar_id = filter('calendar_id'); |
27 | 27 | $start = filter('start'); |
28 | 28 | $end = filter('end'); |
| 29 | +$locale_id = prepare(Models\Locale::getDefault()->id); |
| 30 | +$prepared_start = prepare($start); |
| 31 | +$prepared_end = prepare($end); |
29 | 32 |
|
30 | 33 | switch (filter('op')) { |
31 | 34 | case 'fatturato': |
32 | | - if (empty(Module::where('name', 'Vendita al banco')->first()->id)) { |
| 35 | + if (!Module::where('name', 'Vendita al banco')->exists()) { |
33 | 36 | $results = $dbo->fetchArray(' |
34 | 37 | SELECT |
35 | 38 | 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 | 41 | FROM |
39 | 42 | `co_documenti` |
40 | 43 | 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.') |
42 | 45 | INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id` |
43 | 46 | INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id` |
44 | 47 | WHERE |
45 | 48 | `co_tipidocumento`.`dir`=\'entrata\' |
46 | 49 | 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.' |
48 | 51 | AND `is_fiscale`=1 |
49 | 52 | AND `zz_segments`.`autofatture`=0 |
50 | 53 | GROUP BY |
|
55 | 58 | } else { |
56 | 59 | $results = $dbo->fetchArray(' |
57 | 60 | 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` |
82 | 64 | 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` |
85 | 99 | 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 |
87 | 103 | '); |
88 | 104 | } |
89 | 105 |
|
|
99 | 115 | break; |
100 | 116 |
|
101 | 117 | 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 | + '); |
103 | 140 |
|
104 | 141 | $results = Stats::monthly($results, $start, $end); |
105 | 142 |
|
|
0 commit comments