2019-11-22 18:25:33 +01: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-11-22 18:25:33 +01:00
2024-03-05 16:01:45 +01:00
use Models\Module ;
2024-03-22 15:52:24 +01:00
use Modules\Articoli\Articolo ;
use Util\Query ;
2020-04-07 16:10:00 +02:00
2019-11-22 18:25:33 +01:00
include_once __DIR__ . '/../../core.php' ;
2024-03-27 16:59:46 +01:00
$structure = Module :: find (( new Module ()) -> getByField ( 'name' , 'Articoli' , Models\Locale :: getPredefined () -> id ));
2020-04-07 16:10:00 +02:00
2019-11-22 18:25:33 +01:00
// Valori di ricerca
2020-04-07 16:10:00 +02:00
$where [ 'servizio' ] = '0' ;
2024-03-22 15:52:24 +01:00
foreach ( getSearchValues ( $id_module ) as $key => $value ) {
2024-03-02 23:04:03 +01:00
$where [ $key ] = $value ;
2019-11-22 18:25:33 +01:00
}
2020-04-07 16:10:00 +02:00
$period_end = $_SESSION [ 'period_end' ];
2019-11-22 18:25:33 +01:00
2020-04-07 16:10:00 +02:00
// RISULTATI VISIBILI
2024-01-15 15:30:45 +01:00
Query :: setSegments ( false );
2020-04-07 16:10:00 +02:00
$query = Query :: getQuery ( $structure , $where , 0 , []);
2019-11-22 18:25:33 +01:00
2024-03-05 16:01:45 +01:00
$query = Modules :: replaceAdditionals ( $structure -> id , $query );
2019-11-22 18:25:33 +01:00
2023-01-09 12:54:09 +01:00
// Modifiche alla query principale
2024-02-26 15:53:39 +01:00
$query = preg_replace ( '/FROM[\s\t\n]+`mg_articoli`/s' , 'FROM `mg_articoli` LEFT JOIN (SELECT `idarticolo`, SUM(`qta`) AS qta_totale FROM `mg_movimenti` WHERE `data` <=' . prepare ( $period_end ) . ' GROUP BY `idarticolo`) movimenti ON `movimenti`.`idarticolo`=`mg_articoli`.`id` ' , $query );
$query = preg_replace ( '/^SELECT/' , 'SELECT `movimenti`.`qta_totale`, ' , $query );
2023-01-09 12:54:09 +01:00
2021-10-22 17:14:13 +02:00
if ( post ( 'acquisto' ) == 'standard' ) {
2024-02-26 15:53:39 +01:00
$query = preg_replace ( '/^SELECT/' , 'SELECT `mg_articoli`.`prezzo_acquisto` AS acquisto, ' , $query );
2023-08-04 14:54:28 +02:00
$text = 'al prezzo presente nella scheda articolo' ;
} elseif ( post ( 'acquisto' ) == 'first' ) {
2024-02-26 15:53:39 +01:00
$query = preg_replace ( '/^SELECT/' , 'SELECT (SELECT (`prezzo_unitario`-`sconto_unitario`) AS acquisto FROM `co_righe_documenti` INNER JOIN `co_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id` INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id` WHERE `dir`="uscita" AND `idarticolo`=`mg_articoli`.`id` ORDER BY `co_righe_documenti`.`id` ASC LIMIT 0,1) AS acquisto, ' , $query );
2023-08-04 14:54:28 +02:00
$text = 'al primo articolo acquistato' ;
} elseif ( post ( 'acquisto' ) == 'last' ) {
2024-02-26 15:53:39 +01:00
$query = preg_replace ( '/^SELECT/' , 'SELECT (SELECT (`prezzo_unitario`-`sconto_unitario`) AS acquisto FROM `co_righe_documenti` INNER JOIN `co_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id` INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id` WHERE `dir`="uscita") AND `idarticolo`=`mg_articoli`.`id` ORDER BY `co_righe_documenti`.`id` DESC LIMIT 0,1) AS acquisto, ' , $query );
2021-10-22 17:14:13 +02:00
$text = " all'ultimo articolo acquistato " ;
} else {
2024-02-26 15:53:39 +01:00
$query = preg_replace ( '/^SELECT/' , 'SELECT (SELECT (SUM((`prezzo_unitario`-`sconto_unitario`)*`qta`)/SUM(`qta`)) AS acquisto FROM `co_righe_documenti` INNER JOIN `co_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id` INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id` WHERE dir="uscita" AND `idarticolo`=`mg_articoli`.`id`) AS acquisto, ' , $query );
2021-10-22 17:14:13 +02:00
$text = " alla media ponderata dell'articolo " ;
}
2020-04-07 16:10:00 +02:00
if ( post ( 'tipo' ) == 'nozero' ) {
2024-02-26 15:53:39 +01:00
$query = str_replace ( '2=2' , '2=2 AND `qta_totale` > 0' , $query );
2020-02-04 16:30:11 +01:00
}
2020-04-07 16:10:00 +02:00
$data = Query :: executeAndCount ( $query );
2019-11-22 18:25:33 +01:00
echo '
< h3 > '.tr(' Inventario al _DATE_ ' , [
'_DATE_' => Translator :: dateToLocale ( $period_end ),
], [ 'upper' => true ]) . ' </ h3 >
2021-10-22 17:14:13 +02:00
< p style = " color:#aaa; font-size:10px; " class = " text-right " >
2023-08-04 14:54:28 +02:00
'.tr(' Prezzo di acquisto calcolato in base _TEXT_ ' ,
2024-01-15 15:30:45 +01:00
[
'_TEXT_' => $text ,
]) . '
2021-10-22 17:14:13 +02:00
</ p >
2019-11-22 18:25:33 +01:00
< table class = " table table-bordered " >
< thead >
< tr >
< th class = " text-center " width = " 150 " > '.tr(' Codice ', [], [' upper ' => true]).' </ th >
2020-02-01 11:40:23 +01:00
< th class = " text-center " > '.tr(' Categoria ', [], [' upper ' => true]).' </ th >
2019-11-22 18:25:33 +01:00
< th class = " text-center " > '.tr(' Descrizione ', [], [' upper ' => true]).' </ th >
< th class = " text-center " width = " 70 " > '.tr(' Prezzo di vendita ', [], [' upper ' => true]).' </ th >
< th class = " text-center " width = " 70 " > '.tr(' Q . tà ', [], [' upper ' => true]).' </ th >
< th class = " text-center " width = " 70 " > '.tr(' Prezzo di acquisto ', [], [' upper ' => true]).' </ th >
< th class = " text-center " width = " 90 " > '.tr(' Valore totale ', [], [' upper ' => true]).' </ th >
</ tr >
</ thead >
2020-03-03 10:33:32 +01:00
2019-11-22 18:25:33 +01:00
< tbody > ' ;
2021-08-03 17:41:30 +02:00
$totale_qta = 0 ;
2019-11-22 18:25:33 +01:00
$totali = [];
2022-12-14 17:09:38 +01:00
2020-04-07 16:10:00 +02:00
foreach ( $data [ 'results' ] as $r ) {
2024-02-29 15:10:55 +01:00
$articolo = Articolo :: find ( $r [ 'id' ]);
$valore_magazzino = $articolo -> prezzo_acquisto * $articolo -> qta ;
2019-11-22 18:25:33 +01:00
echo '
< tr >
2024-02-29 15:10:55 +01:00
< td > '.$articolo->codice.' </ td >
2020-04-07 16:10:00 +02:00
< td > '.$r[' Categoria '].' </ td >
2024-03-19 18:18:11 +01:00
< td > '.$articolo->getTranslation(' name ').' </ td >
2024-02-29 15:10:55 +01:00
< td class = " text-right " > '.moneyFormat($articolo->prezzo_vendita).' </ td >
2024-03-04 12:28:16 +01:00
< td class = " text-right " > '.Translator::numberToLocale($articolo->qta).' '.$articolo->um.' </ td >
2024-02-29 15:10:55 +01:00
< td class = " text-right " > '.moneyFormat($articolo->prezzo_acquisto).' </ td >
2019-11-22 18:25:33 +01:00
< td class = " text-right " > '.moneyFormat($valore_magazzino).' </ td >
</ tr > ' ;
2022-12-22 11:59:33 +01:00
$totale_qta += $r [ 'Q.tà' ];
2019-11-22 18:25:33 +01:00
$totali [] = $valore_magazzino ;
}
// Totali
$totale_acquisto = sum ( $totali );
echo '
</ tbody >
< tr >
2020-02-14 11:03:16 +01:00
< td colspan = " 3 " class = " text-right border-top " >< b > '.tr(' Totale ', [], [' upper ' => true]).' :</ b ></ td >
2019-11-22 18:25:33 +01:00
< td class = " border-top " ></ td >
< td class = " text-right border-top " >< b > '.Translator::numberToLocale($totale_qta).' </ b ></ td >
< td class = " border-top " ></ td >
< td class = " text-right border-top " >< b > '.moneyFormat($totale_acquisto).' </ b ></ td >
</ tr >
</ table > ' ;