2023-05-05 18:28:36 +02:00
< ? php
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
* Copyright ( C ) DevCode s . r . l .
*
* 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 />.
*/
include_once __DIR__ . '/../../core.php' ;
2024-03-05 16:01:45 +01:00
use Models\Module ;
2023-05-05 18:28:36 +02:00
2024-06-11 16:58:27 +02:00
$module = Module :: where ( 'name' , 'Fatture di vendita' ) -> first ();
2023-05-05 18:28:36 +02:00
$module_query = '
SELECT
2024-02-13 15:49:40 +01:00
`numero_esterno` ,
`an_anagrafiche` . `ragione_sociale` ,
SUM ( `prezzo_unitario` * `qta` ) as \ ' Totale\ ' ,
`provvigione_percentuale` ,
`provvigione`
2023-05-05 18:28:36 +02:00
FROM
`co_documenti`
LEFT JOIN `an_anagrafiche` ON `co_documenti` . `idanagrafica` = `an_anagrafiche` . `idanagrafica`
LEFT JOIN `co_tipidocumento` ON `co_documenti` . `idtipodocumento` = `co_tipidocumento` . `id`
LEFT JOIN ( SELECT `iddocumento` , SUM ( `subtotale` - `sconto` ) AS `totale_imponibile` , SUM ( `iva` ) AS `iva` FROM `co_righe_documenti` GROUP BY `iddocumento` ) AS righe ON `co_documenti` . `id` = `righe` . `iddocumento`
LEFT JOIN `co_statidocumento` ON `co_documenti` . `idstatodocumento` = `co_statidocumento` . `id`
2024-03-22 15:52:24 +01:00
LEFT JOIN `co_statidocumento_lang` ON ( `co_statidocumento_lang` . `id_record` = `co_statidocumento` . `id` AND `co_statidocumento_lang` . `id_lang` = '.prepare(Models\Locale::getDefault()->id).' )
2024-02-13 15:49:40 +01:00
LEFT JOIN `an_anagrafiche` as agenti ON `agenti` . `idanagrafica` = `co_documenti` . `idagente`
LEFT JOIN `co_righe_documenti` ON `co_righe_documenti` . `iddocumento` = `co_documenti` . `id`
2023-05-05 18:28:36 +02:00
WHERE
2024-02-13 15:49:40 +01:00
1 = 1 AND `provvigione` > 0
2023-05-05 18:28:36 +02:00
GROUP BY
2024-02-13 15:49:40 +01:00
`co_documenti` . `id`
2023-05-05 18:28:36 +02:00
HAVING
2 = 2
ORDER BY
`co_documenti` . `data` DESC ' ;
2023-08-04 14:54:28 +02:00
if ( ! empty ( get ( 'date_start' ))) {
2023-05-05 18:28:36 +02:00
$module_query = str_replace ( '1=1' , '1=1 AND DATE_FORMAT(`data`, "%Y%m%d") >= "' . date ( 'Ymd' , strtotime ( get ( 'date_start' ))) . '"' , $module_query );
$date_start = get ( 'date_start' );
}
2023-08-04 14:54:28 +02:00
if ( ! empty ( get ( 'date_end' ))) {
2023-05-05 18:28:36 +02:00
$module_query = str_replace ( '1=1' , '1=1 AND DATE_FORMAT(`data`, "%Y%m%d") <= "' . date ( 'Ymd' , strtotime ( get ( 'date_end' ))) . '"' , $module_query );
$date_end = get ( 'date_end' );
}
2024-04-18 17:44:05 +02:00
$module_query = str_replace ( '1=1' , '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` = "Pagato")' , $module_query );
2023-05-05 18:28:36 +02:00
2023-08-04 14:54:28 +02:00
if ( get ( 'is_emessa' ) == 'true' && get ( 'is_parz_pagata' ) == 'true' ) {
2024-04-18 17:44:05 +02:00
$module_query = str_replace ( '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` = "Pagato")' , '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` IN ("Pagato", "Emessa", "Parzialmente pagato"))' , $module_query );
2023-08-04 14:54:28 +02:00
} elseif ( get ( 'is_emessa' ) == 'true' ) {
2024-04-18 17:44:05 +02:00
$module_query = str_replace ( '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` = "Pagato")' , '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` IN ("Pagato", "Emessa"))' , $module_query );
2023-08-04 14:54:28 +02:00
} elseif ( get ( 'is_parz_pagata' ) == 'true' ) {
2024-04-18 17:44:05 +02:00
$module_query = str_replace ( '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` = "Pagato")' , '1=1 AND `co_documenti`.`idstatodocumento` IN (SELECT `id_record` FROM `co_statidocumento_lang` WHERE `title` IN ("Pagato", "Parzialmente pagato"))' , $module_query );
2023-05-05 18:28:36 +02:00
}
2024-02-13 15:49:40 +01:00
$module_query = str_replace ( '1=1' , '1=1 AND `co_documenti`.`idagente`=' . prepare ( $id_record ), $module_query );
2023-08-04 14:54:28 +02:00
$records = $dbo -> fetchArray ( $module_query );