2017-08-04 16:28:16 +02:00
< ? php
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' :
2020-02-28 12:04:12 +01:00
$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
2020-02-28 12:04:12 +01: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' ];
// Query di aggiornamento
$dbo -> update ( 'dt_ddt' , [
'data' => post ( 'data' ),
'numero_esterno' => $numero_esterno ,
'note' => post ( 'note' ),
'note_aggiuntive' => post ( 'note_aggiuntive' ),
'idstatoddt' => $idstatoddt ,
'idpagamento' => $idpagamento ,
'idconto' => post ( 'idconto' ),
'idanagrafica' => $id_anagrafica ,
'idspedizione' => post ( 'idspedizione' ),
'idcausalet' => post ( 'idcausalet' ),
'idsede_partenza' => post ( 'idsede_partenza' ),
'idsede_destinazione' => post ( 'idsede_destinazione' ),
'idvettore' => post ( 'idvettore' ),
'data_ora_trasporto' => post ( 'data_ora_trasporto' ),
'idporto' => post ( 'idporto' ),
'idaspettobeni' => post ( 'idaspettobeni' ),
'idrivalsainps' => $idrivalsainps ,
'idritenutaacconto' => $idritenutaacconto ,
'n_colli' => post ( 'n_colli' ),
'bollo' => 0 ,
'rivalsainps' => 0 ,
'ritenutaacconto' => 0 ,
'id_documento_fe' => post ( 'id_documento_fe' ),
'codice_cup' => post ( 'codice_cup' ),
'codice_cig' => post ( 'codice_cig' ),
'num_item' => post ( 'num_item' ),
], [ 'id' => $id_record ]);
$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 );
2017-08-04 16:28:16 +02:00
}
2020-02-28 12:04:12 +01:00
} else {
if ( $rs [ 0 ][ 'descrizione' ] != 'Pagato' ) {
ricalcola_costiagg_ddt ( $id_record , $idrivalsainps , $idritenutaacconto , $bollo );
}
}
2018-07-03 18:06:36 +02:00
2020-02-28 12:04:12 +01:00
aggiorna_sedi_movimenti ( 'ddt' , $id_record );
// Controllo sulla presenza di DDT con lo stesso numero secondario
2020-03-02 16:13:07 +01:00
$direzione = $ddt -> direzione ;
2020-04-27 12:26:27 +02:00
if ( $direzione == 'uscita' and ! empty ( $numero_esterno )) {
2020-03-02 16:13:07 +01:00
$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!' ));
}
2017-08-04 16:28:16 +02:00
}
2020-02-28 12:04:12 +01:00
flash () -> info ( tr ( 'Ddt modificato correttamente!' ));
2017-08-04 16:28:16 +02:00
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 );
}
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' );
$sconto -> id_iva = post ( 'idiva' );
$sconto -> sconto_unitario = post ( 'sconto_unitario' );
$sconto -> tipo_sconto = 'UNT' ;
$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 ;
2019-02-14 17:49:58 +01:00
// Aggiunta di un ordine in ddt
case 'add_ordine' :
2019-07-23 15:39:00 +02:00
$ordine = \Modules\Ordini\Ordine :: find ( post ( 'id_documento' ));
2017-08-04 16:28:16 +02:00
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' ) {
$tipo = Tipo :: where ( 'dir' , $dir ) -> first ();
2017-08-04 16:28:16 +02:00
2019-02-15 10:37:08 +01:00
$ddt = DDT :: build ( $ordine -> anagrafica , $tipo , post ( 'data' ));
2019-02-14 17:49:58 +01:00
$ddt -> idpagamento = $ordine -> idpagamento ;
2019-07-22 16:13:38 +02:00
$ddt -> id_documento_fe = $ordine -> id_documento_fe ;
$ddt -> codice_cup = $ordine -> codice_cup ;
$ddt -> codice_cig = $ordine -> codice_cig ;
$ddt -> num_item = $ordine -> num_item ;
2019-10-13 16:42:09 +02:00
$ddt -> idsede_destinazione = $ordine -> idsede ;
2019-07-22 16:13:38 +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
2019-02-14 17:49:58 +01:00
$righe = $ordine -> getRighe ();
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
2019-02-14 17:49:58 +01: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 ()) {
2020-02-24 19:03:47 +01:00
//$copia->movimenta($copia->qta);
2017-09-13 09:33:45 +02:00
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
}
}
ricalcola_costiagg_ddt ( $id_record );
2019-02-14 17:49:58 +01:00
flash () -> info ( tr ( 'Ordine _NUM_ aggiunto!' , [
'_NUM_' => $ordine -> numero ,
]));
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' :
$id_riga = post ( 'idriga' );
2020-01-17 17:31:07 +01:00
$type = post ( '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' :
2018-07-19 15:33:32 +02:00
$idriga = post ( 'idriga' );
$idarticolo = post ( 'idarticolo' );
2017-08-04 16:28:16 +02:00
2018-07-19 15:33:32 +02:00
$serials = ( array ) post ( 'serial' );
2017-09-05 17:31:58 +02:00
foreach ( $serials as $key => $value ) {
if ( empty ( $value )) {
unset ( $serials [ $key ]);
}
2017-08-04 16:28:16 +02:00
}
2017-09-05 17:31:58 +02:00
$dbo -> sync ( 'mg_prodotti' , [ 'id_riga_ddt' => $idriga , 'dir' => $dir , 'id_articolo' => $idarticolo ], [ 'serial' => $serials ]);
2017-08-04 16:28:16 +02:00
break ;
2018-10-30 10:27:44 +01:00
case 'update_position' :
2018-11-30 16:10:15 +01:00
$orders = explode ( ',' , $_POST [ 'order' ]);
2018-10-30 10:27:44 +01:00
$order = 0 ;
2017-08-04 16:28:16 +02:00
2018-11-30 16:10:15 +01:00
foreach ( $orders as $idriga ) {
2018-10-30 10:27:44 +01:00
$dbo -> query ( 'UPDATE `dt_righe_ddt` SET `order`=' . prepare ( $order ) . ' WHERE id=' . prepare ( $idriga ));
2018-11-30 16:10:15 +01:00
++ $order ;
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
}
}