2019-07-11 12:04:22 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2019-07-11 12:04:22 +02:00
2024-06-21 16:41:29 +02:00
use Models\Module ;
2024-06-25 09:40:41 +02:00
use Modules\Statistiche\Stats ;
2019-07-11 12:04:22 +02:00
include_once __DIR__ . '/../../core.php' ;
$calendar_id = filter ( 'calendar_id' );
$start = filter ( 'start' );
$end = filter ( 'end' );
switch ( filter ( 'op' )) {
case 'fatturato' :
2024-06-21 16:41:29 +02:00
if ( empty ( Module :: where ( 'name' , 'Vendita al banco' ) -> first () -> id )) {
2024-06-26 10:59:39 +02:00
$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` = \ ' entrata\ '
AND `co_tipidocumento` . `name` != \ ' 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
' );
2024-06-21 16:41:29 +02:00
} else {
$results = $dbo -> fetchArray ( '
2024-06-26 10:59:39 +02:00
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` = \ ' entrata\ '
AND `co_tipidocumento` . `name` != \ ' 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` )
UNION
SELECT
ROUND ( SUM ( `vb_righe_venditabanco` . `subtotale` - `vb_righe_venditabanco` . `sconto` ), 2 ) AS result ,
YEAR ( `vb_venditabanco` . `data` ) AS year ,
MONTH ( `vb_venditabanco` . `data` ) AS month
FROM
`vb_venditabanco`
INNER JOIN `vb_righe_venditabanco` ON `vb_righe_venditabanco` . `idvendita` = `vb_venditabanco` . `id`
GROUP BY
YEAR ( `vb_venditabanco` . `data` ), MONTH ( `vb_venditabanco` . `data` )
' );
2024-06-21 16:41:29 +02:00
}
2024-06-26 10:59:39 +02:00
2019-07-11 12:04:22 +02:00
$results = Stats :: monthly ( $results , $start , $end );
2024-06-26 10:59:39 +02:00
2019-07-11 12:04:22 +02:00
echo json_encode ([
'label' => tr ( 'Fatturato' ) . ' - ' . tr ( 'Periodo _NUM_' , [
2019-07-11 17:20:58 +02:00
'_NUM_' => $calendar_id ,
2019-07-11 12:04:22 +02:00
]),
2019-07-11 17:20:58 +02:00
'results' => $results ,
2019-07-11 12:04:22 +02:00
]);
break ;
2024-06-26 10:59:39 +02:00
2019-07-11 12:04:22 +02:00
case 'acquisti' :
2024-05-16 18:02:50 +02:00
$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' );
2019-07-11 12:04:22 +02:00
$results = Stats :: monthly ( $results , $start , $end );
echo json_encode ([
'label' => tr ( 'Acquisti' ) . ' - ' . tr ( 'Periodo _NUM_' , [
2019-07-11 17:20:58 +02:00
'_NUM_' => $calendar_id ,
2019-07-11 12:04:22 +02:00
]),
2019-07-11 17:20:58 +02:00
'results' => $results ,
2019-07-11 12:04:22 +02:00
]);
break ;
}