2019-07-09 16:43:08 +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-09 16:43:08 +02:00
include_once __DIR__ . '/../../core.php' ;
$dir = $_GET [ 'dir' ];
2020-07-01 18:33:21 +02:00
$id_sezionale = filter ( 'id_sezionale' );
2020-07-06 14:16:16 +02:00
$sezionale = $dbo -> fetchOne ( 'SELECT name FROM zz_segments WHERE id = ' . $id_sezionale )[ 'name' ];
2020-07-01 18:10:00 +02:00
2020-07-02 17:30:09 +02:00
$date_start = filter ( 'date_start' );
$date_end = filter ( 'date_end' );
2019-07-09 16:43:08 +02:00
2019-07-09 18:29:40 +02:00
$tipo = $dir == 'entrata' ? 'vendite' : 'acquisti' ;
2019-07-09 16:43:08 +02:00
$v_iva = [];
$v_totale = [];
$query = ' SELECT * ,
2020-10-09 18:08:24 +02:00
co_movimenti . id AS idmovimenti , co_documenti . id AS id ,
IF ( numero = " " , numero_esterno , numero ) AS numero ,
2021-05-05 14:16:57 +02:00
( SELECT IF ( co_tipidocumento . reversed = 0 , SUM ( subtotale - sconto ), ( SUM ( subtotale - sconto ) *- 1 )) FROM co_righe_documenti AS righe2 WHERE righe2 . iddocumento = co_documenti . id AND righe2 . idiva = co_righe_documenti . idiva GROUP BY iddocumento ) AS subtotale ,
( SELECT IF ( co_tipidocumento . reversed = 0 , SUM ( subtotale - sconto + iva + rivalsainps - ritenutaacconto ), ( SUM ( subtotale - sconto + iva + rivalsainps - ritenutaacconto ) *- 1 )) FROM co_righe_documenti AS righe2 WHERE righe2 . iddocumento = co_documenti . id AND righe2 . idiva = co_righe_documenti . idiva GROUP BY iddocumento ) AS totale ,
( SELECT IF ( co_tipidocumento . reversed = 0 , SUM ( iva + iva_rivalsainps ), ( SUM ( iva + iva_rivalsainps ) *- 1 )) FROM co_righe_documenti AS righe2 WHERE righe2 . iddocumento = co_documenti . id AND righe2 . idiva = co_righe_documenti . idiva GROUP BY iddocumento ) AS iva ,
2020-10-09 18:08:24 +02:00
an_anagrafiche . ragione_sociale ,
an_anagrafiche . codice AS codice_anagrafica
FROM co_movimenti
INNER JOIN co_documenti ON co_movimenti . iddocumento = co_documenti . id
2019-07-09 16:43:08 +02:00
INNER JOIN co_righe_documenti ON co_documenti . id = co_righe_documenti . iddocumento
INNER JOIN co_tipidocumento ON co_documenti . idtipodocumento = co_tipidocumento . id
INNER JOIN co_iva ON co_righe_documenti . idiva = co_iva . id
INNER JOIN an_anagrafiche ON an_anagrafiche . idanagrafica = co_documenti . idanagrafica
2021-10-28 16:11:53 +02:00
WHERE dir = '.prepare($dir).' AND idstatodocumento NOT IN ( SELECT id FROM co_statidocumento WHERE descrizione = " Bozza " OR descrizione = " Annullata " ) AND is_descrizione = 0 AND co_documenti . data_competenza >= '.prepare($date_start).' AND co_documenti . data_competenza <= '.prepare($date_end).' AND '.((!empty($id_sezionale)) ? ' co_documenti . id_segment = '.prepare($id_sezionale).' ' : ' 1 = 1 ').'
2019-07-09 16:43:08 +02:00
GROUP BY co_documenti . id , co_righe_documenti . idiva
2022-03-11 15:05:31 +01:00
ORDER BY CAST ( co_documenti . numero_esterno AS UNSIGNED ) ' ;
2019-07-09 16:43:08 +02:00
$records = $dbo -> fetchArray ( $query );
2019-07-10 11:53:35 +02:00
2022-03-02 16:38:58 +01:00
if ( empty ( get ( 'notdefinitiva' ))) {
$page = $dbo -> fetchOne ( 'SELECT first_page FROM co_stampecontabili WHERE dir=' . prepare ( filter ( 'dir' )) . ' AND date_start=' . prepare ( filter ( 'date_start' )) . ' AND date_end=' . prepare ( filter ( 'date_end' )))[ 'first_page' ];
}
2019-07-10 11:53:35 +02:00
// Sostituzioni specifiche
$custom = [
'tipo' => $tipo ,
2020-10-16 08:31:10 +02:00
];