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' ;
2018-12-29 14:24:27 +01:00
use Modules\Anagrafiche\Anagrafica ;
2019-07-23 18:04:01 +02:00
use Modules\Articoli\Articolo as ArticoloOriginale ;
2018-12-29 14:24:27 +01:00
use Modules\DDT\Components\Articolo ;
use Modules\DDT\Components\Descrizione ;
use Modules\DDT\Components\Riga ;
2019-04-04 17:12:32 +02:00
use Modules\DDT\Components\Sconto ;
2018-12-29 14:24:27 +01:00
use Modules\DDT\DDT ;
use Modules\DDT\Tipo ;
2017-09-22 15:19:59 +02:00
$module = Modules :: get ( $id_module );
2017-08-04 16:28:16 +02:00
if ( $module [ 'name' ] == 'Ddt di vendita' ) {
$dir = 'entrata' ;
} else {
$dir = 'uscita' ;
}
switch ( post ( 'op' )) {
case 'add' :
$idanagrafica = post ( 'idanagrafica' );
2018-07-19 15:33:32 +02:00
$data = post ( 'data' );
2018-12-29 14:24:27 +01:00
$id_tipo = post ( 'idtipoddt' );
2017-08-04 16:28:16 +02:00
2018-12-29 14:24:27 +01:00
$anagrafica = Anagrafica :: find ( $idanagrafica );
$tipo = Tipo :: find ( $id_tipo );
2017-08-04 16:28:16 +02:00
2019-01-02 14:15:16 +01:00
$ddt = DDT :: build ( $anagrafica , $tipo , $data );
2018-12-29 14:24:27 +01:00
$id_record = $ddt -> id ;
2017-08-04 16:28:16 +02:00
2020-02-01 11:02:23 +01:00
$ddt -> idcausalet = post ( 'idcausalet' );
$ddt -> save ();
2018-12-29 14:24:27 +01:00
flash () -> info ( tr ( 'Aggiunto ddt in _TYPE_ numero _NUM_!' , [
'_TYPE_' => $dir ,
2019-01-19 10:33:57 +01:00
'_NUM_' => $ddt -> numero ,
2018-12-29 14:24:27 +01:00
]));
2017-08-04 16:28:16 +02:00
break ;
case 'update' :
2021-04-06 15:10:55 +02:00
if ( isset ( $id_record )) {
$idstatoddt = post ( 'idstatoddt' );
$idpagamento = post ( 'idpagamento' );
$numero_esterno = post ( 'numero_esterno' );
$id_anagrafica = post ( 'idanagrafica' );
if ( $dir == 'uscita' ) {
$idrivalsainps = post ( 'id_rivalsa_inps' );
$idritenutaacconto = post ( 'id_ritenuta_acconto' );
$bollo = post ( 'bollo' );
} else {
$idrivalsainps = 0 ;
$idritenutaacconto = 0 ;
$bollo = 0 ;
2017-08-04 16:28:16 +02:00
}
2021-04-06 15:10:55 +02:00
$tipo_sconto = post ( 'tipo_sconto_generico' );
$sconto = post ( 'sconto_generico' );
// Leggo la descrizione del pagamento
$query = 'SELECT descrizione FROM co_pagamenti WHERE id=' . prepare ( $idpagamento );
$rs = $dbo -> fetchArray ( $query );
$pagamento = $rs [ 0 ][ 'descrizione' ];
$ddt -> data = post ( 'data' );
$ddt -> numero_esterno = $numero_esterno ;
$ddt -> note = post ( 'note' );
$ddt -> note_aggiuntive = post ( 'note_aggiuntive' );
$ddt -> idstatoddt = $idstatoddt ;
$ddt -> idpagamento = $idpagamento ;
$ddt -> idconto = post ( 'idconto' );
$ddt -> idanagrafica = $id_anagrafica ;
$ddt -> idreferente = post ( 'idreferente' );
$ddt -> idspedizione = post ( 'idspedizione' );
$ddt -> idcausalet = post ( 'idcausalet' );
$ddt -> idsede_partenza = post ( 'idsede_partenza' );
$ddt -> idsede_destinazione = post ( 'idsede_destinazione' );
$ddt -> idvettore = post ( 'idvettore' );
$ddt -> data_ora_trasporto = post ( 'data_ora_trasporto' ) ? : null ;
$ddt -> idporto = post ( 'idporto' );
$ddt -> idaspettobeni = post ( 'idaspettobeni' );
$ddt -> idrivalsainps = $idrivalsainps ;
$ddt -> idritenutaacconto = $idritenutaacconto ;
$ddt -> n_colli = post ( 'n_colli' );
$ddt -> peso = post ( 'peso' );
$ddt -> volume = post ( 'volume' );
$ddt -> peso_manuale = post ( 'peso_manuale' );
$ddt -> volume_manuale = post ( 'volume_manuale' );
$ddt -> bollo = 0 ;
$ddt -> rivalsainps = 0 ;
$ddt -> ritenutaacconto = 0 ;
$ddt -> id_documento_fe = post ( 'id_documento_fe' );
$ddt -> codice_cup = post ( 'codice_cup' );
$ddt -> codice_cig = post ( 'codice_cig' );
$ddt -> num_item = post ( 'num_item' );
$ddt -> setScontoFinale ( post ( 'sconto_finale' ), post ( 'tipo_sconto_finale' ));
$ddt -> save ();
$query = 'SELECT descrizione FROM dt_statiddt WHERE id=' . prepare ( $idstatoddt );
$rs = $dbo -> fetchArray ( $query );
// Ricalcolo inps, ritenuta e bollo (se l'ddt non è stato evaso)
if ( $dir == 'entrata' ) {
if ( $rs [ 0 ][ 'descrizione' ] != 'Pagato' ) {
ricalcola_costiagg_ddt ( $id_record );
}
} else {
if ( $rs [ 0 ][ 'descrizione' ] != 'Pagato' ) {
ricalcola_costiagg_ddt ( $id_record , $idrivalsainps , $idritenutaacconto , $bollo );
}
2020-02-28 12:04:12 +01:00
}
2018-07-03 18:06:36 +02:00
2021-04-06 15:10:55 +02:00
aggiorna_sedi_movimenti ( 'ddt' , $id_record );
// Controllo sulla presenza di DDT con lo stesso numero secondario
$direzione = $ddt -> direzione ;
if ( $direzione == 'uscita' and ! empty ( $numero_esterno )) {
$count = DDT :: where ( 'numero_esterno' , $numero_esterno )
-> where ( 'id' , '!=' , $id_record )
-> where ( 'idanagrafica' , '=' , $id_anagrafica )
-> whereHas ( 'tipo' , function ( $query ) use ( $direzione ) {
$query -> where ( 'dir' , '=' , $direzione );
}) -> count ();
if ( ! empty ( $count )) {
flash () -> warning ( tr ( 'Esiste già un DDT con lo stesso numero secondario e la stessa anagrafica collegata!' ));
}
2020-03-02 16:13:07 +01:00
}
2021-04-06 15:10:55 +02:00
flash () -> info ( tr ( 'Ddt modificato correttamente!' ));
2017-08-04 16:28:16 +02:00
}
2020-02-28 12:04:12 +01:00
2017-08-04 16:28:16 +02:00
break ;
2020-07-20 14:40:11 +02:00
case 'manage_barcode' :
foreach ( post ( 'qta' ) as $id_articolo => $qta ) {
if ( $id_articolo == '-id-' ) {
continue ;
}
// Dati di input
$sconto = post ( 'sconto' )[ $id_articolo ];
$tipo_sconto = post ( 'tipo_sconto' )[ $id_articolo ];
$prezzo_unitario = post ( 'prezzo_unitario' )[ $id_articolo ];
$id_dettaglio_fornitore = post ( 'id_dettaglio_fornitore' )[ $id_articolo ];
$id_iva = $originale -> idiva_vendita ? $originale -> idiva_vendita : setting ( 'Iva predefinita' );
// Creazione articolo
$originale = ArticoloOriginale :: find ( $id_articolo );
$articolo = Articolo :: build ( $ddt , $originale );
$articolo -> id_dettaglio_fornitore = $id_dettaglio_fornitore ? : null ;
$articolo -> setPrezzoUnitario ( $prezzo_unitario , $id_iva );
if ( $dir == 'entrata' ) {
$articolo -> costo_unitario = $originale -> prezzo_acquisto ;
}
$articolo -> setSconto ( $sconto , $tipo_sconto );
$articolo -> qta = $qta ;
$articolo -> save ();
}
flash () -> info ( tr ( 'Articoli aggiunti!' ));
break ;
2019-07-23 15:39:00 +02:00
case 'manage_articolo' :
if ( post ( 'idriga' ) != null ) {
$articolo = Articolo :: find ( post ( 'idriga' ));
} else {
$originale = ArticoloOriginale :: find ( post ( 'idarticolo' ));
$articolo = Articolo :: build ( $ddt , $originale );
2020-07-06 13:32:43 +02:00
$articolo -> id_dettaglio_fornitore = post ( 'id_dettaglio_fornitore' ) ? : null ;
2019-07-23 15:39:00 +02:00
}
2017-08-04 16:28:16 +02:00
2019-07-23 15:39:00 +02:00
$articolo -> descrizione = post ( 'descrizione' );
$articolo -> um = post ( 'um' ) ? : null ;
2017-08-04 16:28:16 +02:00
2020-02-14 18:31:07 +01:00
$articolo -> costo_unitario = post ( 'costo_unitario' ) ? : 0 ;
2020-02-17 19:11:40 +01:00
$articolo -> setPrezzoUnitario ( post ( 'prezzo_unitario' ), post ( 'idiva' ));
2020-02-14 18:31:07 +01:00
$articolo -> setSconto ( post ( 'sconto' ), post ( 'tipo_sconto' ));
2017-08-04 16:28:16 +02:00
2019-07-23 15:39:00 +02:00
try {
$articolo -> qta = post ( 'qta' );
} catch ( UnexpectedValueException $e ) {
flash () -> error ( tr ( 'Alcuni serial number sono già stati utilizzati!' ));
2017-08-04 16:28:16 +02:00
}
2019-07-23 15:39:00 +02:00
$articolo -> save ();
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Articolo modificato!' ));
2018-02-19 17:48:04 +01:00
} else {
2019-07-23 15:39:00 +02:00
flash () -> info ( tr ( 'Articolo aggiunto!' ));
2017-08-04 16:28:16 +02:00
}
2018-01-27 12:46:38 +01:00
2018-02-19 17:48:04 +01:00
// Ricalcolo inps, ritenuta e bollo
2019-03-29 12:46:17 +01:00
ricalcola_costiagg_ddt ( $id_record );
break ;
case 'manage_sconto' :
if ( post ( 'idriga' ) != null ) {
2019-04-04 17:12:32 +02:00
$sconto = Sconto :: find ( post ( 'idriga' ));
2018-02-19 17:48:04 +01:00
} else {
2019-04-04 17:12:32 +02:00
$sconto = Sconto :: build ( $ddt );
2018-01-18 19:03:06 +01:00
}
2018-02-19 17:48:04 +01:00
2019-03-29 12:46:17 +01:00
$sconto -> descrizione = post ( 'descrizione' );
2020-09-29 15:27:00 +02:00
$sconto -> setScontoUnitario ( post ( 'sconto_unitario' ), post ( 'idiva' ));
2019-03-29 12:46:17 +01:00
$sconto -> save ();
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Sconto/maggiorazione modificato!' ));
} else {
2019-07-11 17:20:58 +02:00
flash () -> info ( tr ( 'Sconto/maggiorazione aggiunto!' ));
2019-03-29 12:46:17 +01:00
}
// Ricalcolo inps, ritenuta e bollo
ricalcola_costiagg_ddt ( $id_record );
2018-01-18 19:03:06 +01:00
break ;
2017-08-04 16:28:16 +02:00
2019-07-23 15:39:00 +02:00
case 'manage_riga' :
if ( post ( 'idriga' ) != null ) {
$riga = Riga :: find ( post ( 'idriga' ));
} else {
$riga = Riga :: build ( $ddt );
}
$riga -> descrizione = post ( 'descrizione' );
$riga -> um = post ( 'um' ) ? : null ;
2020-02-14 18:31:07 +01:00
$riga -> costo_unitario = post ( 'costo_unitario' ) ? : 0 ;
2020-02-17 19:11:40 +01:00
$riga -> setPrezzoUnitario ( post ( 'prezzo_unitario' ), post ( 'idiva' ));
2020-02-14 18:31:07 +01:00
$riga -> setSconto ( post ( 'sconto' ), post ( 'tipo_sconto' ));
2019-07-23 15:39:00 +02:00
$riga -> qta = post ( 'qta' );
$riga -> save ();
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Riga modificata!' ));
} else {
flash () -> info ( tr ( 'Riga aggiunta!' ));
}
// Ricalcolo inps, ritenuta e bollo
ricalcola_costiagg_ddt ( $id_record );
break ;
case 'manage_descrizione' :
if ( post ( 'idriga' ) != null ) {
$riga = Descrizione :: find ( post ( 'idriga' ));
} else {
$riga = Descrizione :: build ( $ddt );
}
$riga -> descrizione = post ( 'descrizione' );
$riga -> save ();
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Riga descrittiva modificata!' ));
} else {
flash () -> info ( tr ( 'Riga descrittiva aggiunta!' ));
}
break ;
2020-07-02 15:27:18 +02:00
// Aggiunta di un documento in ddt
2019-02-14 17:49:58 +01:00
case 'add_ordine' :
2020-07-02 15:27:18 +02:00
case 'add_documento' :
$class = post ( 'class' );
$id_documento = post ( 'id_documento' );
// Individuazione del documento originale
if ( ! is_subclass_of ( $class , \Common\Document :: class )) {
return ;
}
$documento = $class :: find ( $id_documento );
2017-08-04 16:28:16 +02:00
2020-09-09 11:44:14 +02:00
// Individuazione sede
2020-07-31 10:30:38 +02:00
$id_sede = ( $documento -> direzione == 'entrata' ) ? $documento -> idsede_destinazione : $documento -> idsede_partenza ;
$id_sede = $id_sede ? : $documento -> idsede ;
$id_sede = $id_sede ? : 0 ;
2020-02-07 17:35:26 +01:00
// Creazione del ddt al volo
2019-02-14 17:49:58 +01:00
if ( post ( 'create_document' ) == 'on' ) {
2020-07-06 13:19:20 +02:00
$tipo = Tipo :: where ( 'dir' , $documento -> direzione ) -> first ();
2017-08-04 16:28:16 +02:00
2020-07-02 15:27:18 +02:00
$ddt = DDT :: build ( $documento -> anagrafica , $tipo , post ( 'data' ));
$ddt -> idpagamento = $documento -> idpagamento ;
2019-07-22 16:13:38 +02:00
2020-07-02 15:27:18 +02:00
$ddt -> id_documento_fe = $documento -> id_documento_fe ;
$ddt -> codice_cup = $documento -> codice_cup ;
$ddt -> codice_cig = $documento -> codice_cig ;
$ddt -> num_item = $documento -> num_item ;
2020-07-31 10:30:38 +02:00
$ddt -> idsede_destinazione = $id_sede ;
2019-07-22 16:13:38 +02:00
2020-09-09 11:44:14 +02:00
$ddt -> idcausalet = post ( 'id_causale_trasporto' );
2021-04-20 17:12:04 +02:00
$ddt -> idreferente = $documento -> idreferente ;
2020-09-09 11:44:14 +02:00
2019-02-14 17:49:58 +01:00
$ddt -> save ();
2017-08-04 16:28:16 +02:00
2019-02-14 17:49:58 +01:00
$id_record = $ddt -> id ;
}
2017-08-04 16:28:16 +02:00
2021-04-06 15:10:55 +02:00
if ( ! empty ( $documento -> sconto_finale )) {
$ddt -> sconto_finale = $documento -> sconto_finale ;
2021-04-12 15:51:27 +02:00
} elseif ( ! empty ( $documento -> sconto_finale_percentuale )) {
2021-04-06 15:10:55 +02:00
$ddt -> sconto_finale_percentuale = $documento -> sconto_finale_percentuale ;
}
$ddt -> save ();
2020-07-02 15:27:18 +02:00
$righe = $documento -> getRighe ();
2019-02-14 17:49:58 +01:00
foreach ( $righe as $riga ) {
2020-02-07 17:35:26 +01:00
if ( post ( 'evadere' )[ $riga -> id ] == 'on' and ! empty ( post ( 'qta_da_evadere' )[ $riga -> id ])) {
2019-02-14 17:49:58 +01:00
$qta = post ( 'qta_da_evadere' )[ $riga -> id ];
2017-08-04 16:28:16 +02:00
2020-07-19 15:11:50 +02:00
$copia = $riga -> copiaIn ( $ddt , $qta );
2019-02-15 10:37:08 +01:00
2017-09-05 17:31:58 +02:00
// Aggiornamento seriali dalla riga dell'ordine
2019-02-14 17:49:58 +01:00
if ( $copia -> isArticolo ()) {
2021-03-29 10:07:45 +02:00
if ( $documento -> tipo -> dir == 'uscita' ) {
2021-03-23 10:31:17 +01:00
$originale = ArticoloOriginale :: find ( $riga -> idarticolo );
$id_iva = $originale -> idiva_vendita ? $originale -> idiva_vendita : setting ( 'Iva predefinita' );
$copia -> setPrezzoUnitario ( $originale -> prezzo_vendita , $id_iva );
}
2019-02-14 17:49:58 +01:00
$serials = is_array ( post ( 'serial' )[ $riga -> id ]) ? post ( 'serial' )[ $riga -> id ] : [];
2017-08-04 16:28:16 +02:00
2019-02-14 17:49:58 +01:00
$copia -> serials = $serials ;
2017-08-04 16:28:16 +02:00
}
2019-02-26 21:20:05 +01:00
$copia -> save ();
2017-08-04 16:28:16 +02:00
}
}
2020-09-09 11:44:14 +02:00
// Modifica finale dello stato
if ( post ( 'create_document' ) == 'on' ) {
$ddt -> idstatoddt = post ( 'id_stato' );
$ddt -> save ();
}
2017-08-04 16:28:16 +02:00
ricalcola_costiagg_ddt ( $id_record );
2019-02-14 17:49:58 +01:00
2020-07-02 15:27:18 +02:00
// Messaggio informativo
$message = tr ( '_DOC_ aggiunto!' , [
'_DOC_' => $documento -> getReference (),
]);
flash () -> info ( $message );
2019-02-14 17:49:58 +01:00
2017-08-04 16:28:16 +02:00
break ;
2019-02-26 20:55:00 +01:00
2017-08-04 16:28:16 +02:00
// Scollegamento riga generica da ddt
2019-10-01 18:32:06 +02:00
case 'delete_riga' :
2020-07-06 13:19:20 +02:00
$id_riga = post ( 'riga_id' );
$type = post ( 'riga_type' );
2017-08-04 16:28:16 +02:00
2020-01-17 17:31:07 +01:00
$riga = $ddt -> getRiga ( $type , $id_riga );
2017-08-04 16:28:16 +02:00
2020-01-17 17:31:07 +01:00
if ( ! empty ( $riga )) {
2019-10-01 18:32:06 +02:00
try {
$riga -> delete ();
2017-08-04 16:28:16 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Riga rimossa!' ));
2019-10-01 18:32:06 +02:00
} catch ( InvalidArgumentException $e ) {
flash () -> error ( tr ( 'Alcuni serial number sono già stati utilizzati!' ));
2017-08-04 16:28:16 +02:00
}
}
2019-10-01 18:32:06 +02:00
ricalcola_costiagg_ddt ( $id_record );
2017-08-04 16:28:16 +02:00
break ;
// eliminazione ddt
case 'delete' :
2019-07-23 15:39:00 +02:00
try {
$ddt -> delete ();
2017-08-04 16:28:16 +02:00
2019-07-23 15:39:00 +02:00
flash () -> info ( tr ( 'Ddt eliminato!' ));
} catch ( InvalidArgumentException $e ) {
flash () -> error ( tr ( 'Sono stati utilizzati alcuni serial number nel documento: impossibile procedere!' ));
2017-08-04 16:28:16 +02:00
}
break ;
case 'add_serial' :
2020-08-10 09:54:01 +02:00
$articolo = Articolo :: find ( post ( 'idriga' ));
2017-08-04 16:28:16 +02:00
2018-07-19 15:33:32 +02:00
$serials = ( array ) post ( 'serial' );
2020-08-10 09:54:01 +02:00
$articolo -> serials = $serials ;
2017-09-05 17:31:58 +02:00
2017-08-04 16:28:16 +02:00
break ;
2018-10-30 10:27:44 +01:00
case 'update_position' :
2020-08-06 11:34:32 +02:00
$order = explode ( ',' , post ( 'order' , true ));
2017-08-04 16:28:16 +02:00
2020-08-06 11:34:32 +02:00
foreach ( $order as $i => $id_riga ) {
2021-02-23 11:40:27 +01:00
$dbo -> query ( 'UPDATE `dt_righe_ddt` SET `order` = ' . prepare ( $i + 1 ) . ' WHERE id=' . prepare ( $id_riga ));
2018-10-30 10:27:44 +01:00
}
break ;
2017-08-04 16:28:16 +02:00
}
2018-01-23 18:05:29 +01:00
// Aggiornamento stato degli ordini presenti in questa fattura in base alle quantità totali evase
2018-07-08 18:11:17 +02:00
if ( ! empty ( $id_record ) && setting ( 'Cambia automaticamente stato ordini fatturati' )) {
2018-02-06 09:55:45 +01:00
$rs = $dbo -> fetchArray ( 'SELECT idordine FROM dt_righe_ddt WHERE idddt=' . prepare ( $id_record ));
2018-01-27 12:46:38 +01:00
2018-02-06 09:55:45 +01:00
for ( $i = 0 ; $i < sizeof ( $rs ); ++ $i ) {
$dbo -> query ( 'UPDATE or_ordini SET idstatoordine=(SELECT id FROM or_statiordine WHERE descrizione="' . get_stato_ordine ( $rs [ $i ][ 'idordine' ]) . '") WHERE id = ' . prepare ( $rs [ $i ][ 'idordine' ]));
2018-01-23 18:05:29 +01:00
}
}