2017-08-04 16:28:16 +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 />.
*/
2017-08-04 16:28:16 +02:00
include_once __DIR__ . '/../../core.php' ;
2019-10-01 18:01:00 +02:00
use Modules\Articoli\Articolo ;
2017-08-04 16:28:16 +02:00
/**
* Funzione per inserire i movimenti di magazzino .
2019-10-01 18:01:00 +02:00
*
2019-10-01 18:32:06 +02:00
* @ deprecated 2.4 . 11
2017-08-04 16:28:16 +02:00
*/
2018-06-23 15:41:32 +02:00
function add_movimento_magazzino ( $id_articolo , $qta , $array = [], $descrizone = '' , $data = '' )
2017-08-04 16:28:16 +02:00
{
2018-09-20 12:05:22 +02:00
$dbo = database ();
2017-08-04 16:28:16 +02:00
if ( empty ( $qta )) {
return false ;
}
2018-02-09 14:25:40 +01:00
2018-06-23 15:41:32 +02:00
$nome = null ;
$tipo = null ;
$numero = null ;
// Informazioni articolo
2018-05-11 15:11:01 +02:00
$manuale = 0 ;
2017-08-04 16:28:16 +02:00
2017-09-15 15:03:27 +02:00
// Ddt
2017-08-04 16:28:16 +02:00
if ( ! empty ( $array [ 'idddt' ])) {
2017-08-08 14:04:46 +02:00
$rs = $dbo -> fetchArray ( 'SELECT numero, numero_esterno, dt_tipiddt.descrizione AS tipo, dt_tipiddt.dir FROM dt_ddt LEFT JOIN dt_tipiddt ON dt_tipiddt.id = dt_ddt.idtipoddt WHERE dt_ddt.id=' . prepare ( $array [ 'idddt' ]));
2017-08-04 16:28:16 +02:00
$numero = ( ! empty ( $rs [ 0 ][ 'numero_esterno' ])) ? $rs [ 0 ][ 'numero_esterno' ] : $rs [ 0 ][ 'numero' ];
$tipo = strtolower ( $rs [ 0 ][ 'tipo' ]);
2018-06-23 15:41:32 +02:00
2018-05-11 15:11:01 +02:00
$rs_data = $dbo -> fetchArray ( " SELECT data FROM dt_ddt WHERE id=' " . $array [ 'idddt' ] . " ' " );
$data = $rs_data [ 0 ][ 'data' ];
2017-08-04 16:28:16 +02:00
}
// Fattura
elseif ( ! empty ( $array [ 'iddocumento' ])) {
$rs = $dbo -> fetchArray ( 'SELECT numero, numero_esterno, co_tipidocumento.descrizione AS tipo, co_tipidocumento.dir FROM co_documenti LEFT JOIN co_tipidocumento ON co_tipidocumento.id = co_documenti.idtipodocumento WHERE co_documenti.id=' . prepare ( $array [ 'iddocumento' ]));
$numero = ( ! empty ( $rs [ 0 ][ 'numero_esterno' ])) ? $rs [ 0 ][ 'numero_esterno' ] : $rs [ 0 ][ 'numero' ];
$tipo = strtolower ( $rs [ 0 ][ 'tipo' ]);
2018-06-23 15:41:32 +02:00
2018-05-11 15:11:01 +02:00
$rs_data = $dbo -> fetchArray ( " SELECT data FROM co_documenti WHERE id=' " . $array [ 'iddocumento' ] . " ' " );
$data = $rs_data [ 0 ][ 'data' ];
2017-08-04 16:28:16 +02:00
}
// Intervento
elseif ( ! empty ( $array [ 'idintervento' ])) {
2018-06-23 15:41:32 +02:00
$rs_data = $dbo -> fetchArray ( 'SELECT IFNULL(MAX(orario_fine), data_richiesta) AS data, codice FROM in_interventi LEFT JOIN in_interventi_tecnici ON in_interventi.id=in_interventi_tecnici.idintervento WHERE in_interventi.id = ' . prepare ( $array [ 'idintervento' ]));
2018-05-17 11:37:23 +02:00
$data = $rs_data [ 0 ][ 'data' ];
2018-06-23 15:41:32 +02:00
$codice_intervento = $rs_data [ 0 ][ 'codice' ];
2017-09-04 12:02:29 +02:00
$movimento = ( $qta > 0 ) ? tr ( 'Ripristino articolo da intervento _NUM_' ) : tr ( 'Scarico magazzino per intervento _NUM_' );
2018-05-17 11:37:23 +02:00
$numero = $codice_intervento ;
2017-08-04 16:28:16 +02:00
}
// Manuale
else {
2018-05-11 15:11:01 +02:00
$manuale = 1 ;
2017-08-04 16:28:16 +02:00
$movimento = ! empty ( $descrizone ) ? $descrizone : '' ;
$descrizone = '' ;
if ( empty ( $movimento )) {
2017-09-04 12:02:29 +02:00
$movimento = ( $qta > 0 ) ? tr ( 'Carico magazzino' ) : tr ( 'Scarico magazzino' );
2017-08-04 16:28:16 +02:00
}
2018-06-23 15:41:32 +02:00
if ( $data == '' ) {
2018-05-11 15:11:01 +02:00
$data = date ( 'Y-m-d' );
}
2017-08-04 16:28:16 +02:00
}
// Descrizione di default
if ( empty ( $movimento )) {
2019-10-01 18:32:06 +02:00
$carico = ( ! empty ( $rs [ 0 ][ 'dir' ]) && $rs [ 0 ][ 'dir' ] == 'entrata' ) ? tr ( 'Ripristino articolo da _TYPE_ numero _NUM_' ) : tr ( 'Carico magazzino da _TYPE_ numero _NUM_' );
$scarico = ( ! empty ( $rs [ 0 ][ 'dir' ]) && $rs [ 0 ][ 'dir' ] == 'uscita' ) ? tr ( 'Rimozione articolo da _TYPE_ numero _NUM_' ) : tr ( 'Scarico magazzino per _TYPE_ numero _NUM_' );
2017-08-04 16:28:16 +02:00
$movimento = ( $qta > 0 ) ? $carico : $scarico ;
}
// Completamento della descrizione
$movimento .= $descrizone ;
$movimento = str_replace ([ '_NAME_' , '_TYPE_' , '_NUM_' ], [ $nome , $tipo , $numero ], $movimento );
2018-06-23 15:41:32 +02:00
// Movimento il magazzino solo se l'articolo non è un servizio
2019-10-01 18:01:00 +02:00
$articolo = Articolo :: find ( $id_articolo );
2017-08-04 16:28:16 +02:00
2019-10-01 18:01:00 +02:00
// Movimentazione effettiva
if ( empty ( $array [ 'idintervento' ])) {
return $articolo -> movimenta ( $qta , $movimento , $data , $manuale , $array );
} else {
return $articolo -> registra ( $qta , $movimento , $data , $manuale , $array );
2017-08-04 16:28:16 +02:00
}
return true ;
}
2019-05-29 19:17:57 +02:00
/**
* Funzione per aggiornare le sedi nei movimenti di magazzino .
*/
2019-06-04 20:45:40 +02:00
function aggiorna_sedi_movimenti ( $module , $id )
{
2019-05-29 19:17:57 +02:00
$dbo = database ();
2019-06-04 20:45:40 +02:00
if ( $module == 'ddt' ) {
2019-05-29 19:17:57 +02:00
$rs = $dbo -> fetchArray ( 'SELECT idsede_partenza, idsede_destinazione, dir FROM dt_ddt INNER JOIN dt_tipiddt ON dt_tipiddt.id = dt_ddt.idtipoddt WHERE dt_ddt.id=' . prepare ( $id ));
2021-02-05 13:27:18 +01:00
$idsede = ( $rs [ 0 ][ 'dir' ] == 'uscita' ) ? $rs [ 0 ][ 'idsede_destinazione' ] : $rs [ 0 ][ 'idsede_partenza' ];
2019-06-04 20:45:40 +02:00
2021-02-05 16:05:45 +01:00
$dbo -> query ( 'UPDATE mg_movimenti SET idsede=' . prepare ( $idsede ) . ' WHERE reference_type=' . prepare ( 'Modules\DDT\DDT' ) . ' AND reference_id=' . prepare ( $id ));
2019-06-04 20:45:40 +02:00
} elseif ( $module == 'documenti' ) {
2019-05-29 19:17:57 +02:00
$rs = $dbo -> fetchArray ( 'SELECT idsede_partenza, idsede_destinazione, dir FROM co_documenti INNER JOIN co_tipidocumento ON co_tipidocumento.id = co_documenti.idtipodocumento WHERE co_documenti.id=' . prepare ( $id ));
2021-02-05 13:27:18 +01:00
$idsede = ( $rs [ 0 ][ 'dir' ] == 'uscita' ) ? $rs [ 0 ][ 'idsede_destinazione' ] : $rs [ 0 ][ 'idsede_partenza' ];
2019-06-04 20:45:40 +02:00
2021-02-05 16:05:45 +01:00
$dbo -> query ( 'UPDATE mg_movimenti SET idsede=' . prepare ( $idsede ) . ' WHERE reference_type=' . prepare ( 'Modules\Fatture\Fattura' ) . ' AND reference_id=' . prepare ( $id ));
2019-06-04 20:45:40 +02:00
} elseif ( $module == 'interventi' ) {
2019-05-29 19:17:57 +02:00
$rs = $dbo -> fetchArray ( 'SELECT idsede_partenza, idsede_destinazione FROM in_interventi WHERE in_interventi.id=' . prepare ( $id ));
2021-02-05 13:27:18 +01:00
$idsede = $rs [ 0 ][ 'idsede_partenza' ];
2019-05-29 19:17:57 +02:00
2021-02-05 16:05:45 +01:00
$dbo -> query ( 'UPDATE mg_movimenti SET idsede=' . prepare ( $idsede ) . ' WHERE reference_type=' . prepare ( 'Modules\Interventi\Intervento' ) . ' AND reference_id=' . prepare ( $id ));
2019-05-29 19:17:57 +02:00
}
}