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
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' :
if ( ! empty ( $id_record )) {
$idstatoddt = post ( 'idstatoddt' );
$idpagamento = post ( 'idpagamento' );
2018-07-03 18:06:36 +02:00
2017-08-04 16:28:16 +02:00
$totale_imponibile = get_imponibile_ddt ( $id_record );
$totale_ddt = get_totale_ddt ( $id_record );
if ( $dir == 'uscita' ) {
2018-09-26 15:37:46 +02:00
$idrivalsainps = post ( 'id_rivalsa_inps' );
$idritenutaacconto = post ( 'id_ritenuta_acconto' );
2017-08-04 16:28:16 +02:00
$bollo = post ( 'bollo' );
} else {
$idrivalsainps = 0 ;
$idritenutaacconto = 0 ;
$bollo = 0 ;
}
2018-07-19 15:33:32 +02:00
$tipo_sconto = post ( 'tipo_sconto_generico' );
$sconto = post ( 'sconto_generico' );
2017-08-04 16:28:16 +02:00
// 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
2018-07-03 18:06:36 +02:00
$dbo -> update ( 'dt_ddt' , [
'data' => post ( 'data' ),
'numero_esterno' => post ( 'numero_esterno' ),
'note' => post ( 'note' ),
'note_aggiuntive' => post ( 'note_aggiuntive' ),
'idstatoddt' => $idstatoddt ,
'idpagamento' => $idpagamento ,
'idconto' => post ( 'idconto' ),
'idanagrafica' => post ( 'idanagrafica' ),
'idspedizione' => post ( 'idspedizione' ),
'idcausalet' => post ( 'idcausalet' ),
2019-05-29 19:17:57 +02:00
'idsede_partenza' => post ( 'idsede_partenza' ),
'idsede_destinazione' => post ( 'idsede_destinazione' ),
2018-07-03 18:06:36 +02:00
'idvettore' => post ( 'idvettore' ),
'idporto' => post ( 'idporto' ),
'idaspettobeni' => post ( 'idaspettobeni' ),
'idrivalsainps' => $idrivalsainps ,
'idritenutaacconto' => $idritenutaacconto ,
'n_colli' => post ( 'n_colli' ),
'bollo' => 0 ,
'rivalsainps' => 0 ,
'ritenutaacconto' => 0 ,
2019-07-22 16:13:38 +02:00
'id_documento_fe' => post ( 'id_documento_fe' ),
'codice_cup' => post ( 'codice_cup' ),
'codice_cig' => post ( 'codice_cig' ),
'num_item' => post ( 'num_item' ),
2018-07-03 18:06:36 +02:00
], [ 'id' => $id_record ]);
$query = 'SELECT descrizione FROM dt_statiddt WHERE id=' . prepare ( $idstatoddt );
$rs = $dbo -> fetchArray ( $query );
2017-08-04 16:28:16 +02:00
2018-07-03 18:06:36 +02:00
// 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 );
2017-08-04 16:28:16 +02:00
}
}
2018-07-03 18:06:36 +02:00
2019-05-29 19:17:57 +02:00
aggiorna_sedi_movimenti ( 'ddt' , $id_record );
2018-07-19 17:29:21 +02: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 ;
$articolo -> id_iva = post ( 'idiva' );
2017-08-04 16:28:16 +02:00
2019-07-23 15:39:00 +02:00
$articolo -> prezzo_unitario_acquisto = post ( 'prezzo_acquisto' ) ? : 0 ;
$articolo -> prezzo_unitario_vendita = post ( 'prezzo' );
$articolo -> sconto_unitario = post ( 'sconto' );
$articolo -> tipo_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 ;
$riga -> id_iva = post ( 'idiva' );
$riga -> prezzo_unitario_acquisto = post ( 'prezzo_acquisto' ) ? : 0 ;
$riga -> prezzo_unitario_vendita = post ( 'prezzo' );
$riga -> sconto_unitario = post ( 'sconto' );
$riga -> tipo_sconto = post ( 'tipo_sconto' );
$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
2019-02-14 17:49:58 +01:00
// Creazione della fattura al volo
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 ) {
if ( post ( 'evadere' )[ $riga -> id ] == 'on' ) {
$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 ()) {
$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' );
2017-08-04 16:28:16 +02:00
2019-10-01 18:32:06 +02:00
if ( ! empty ( $id_riga )) {
$riga = $ddt -> getRighe () -> find ( $id_riga );
2017-08-04 16:28:16 +02:00
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-10-01 18:32:06 +02:00
$dbo -> query ( 'DELETE FROM mg_movimenti WHERE idddt = ' . prepare ( $id_record ));
2018-06-26 09:41:43 +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
}
}