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' :
2020-02-28 12:04:12 +01:00
$idstatoddt = post ( 'idstatoddt' );
$idpagamento = post ( 'idpagamento' );
$numero_esterno = post ( 'numero_esterno' );
$id_anagrafica = post ( 'idanagrafica' );
2021-02-18 18:48:44 +01:00
2020-02-28 12:04:12 +01:00
if ( $dir == 'uscita' ) {
$idrivalsainps = post ( 'id_rivalsa_inps' );
$idritenutaacconto = post ( 'id_ritenuta_acconto' );
$bollo = post ( 'bollo' );
} else {
$idrivalsainps = 0 ;
$idritenutaacconto = 0 ;
$bollo = 0 ;
}
2021-02-18 18:48:44 +01: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 ,
2021-02-11 15:05:29 +01:00
'idreferente' => post ( 'idreferente' ),
2020-02-28 12:04:12 +01:00
'idspedizione' => post ( 'idspedizione' ),
'idcausalet' => post ( 'idcausalet' ),
'idsede_partenza' => post ( 'idsede_partenza' ),
'idsede_destinazione' => post ( 'idsede_destinazione' ),
'idvettore' => post ( 'idvettore' ),
2020-06-12 13:03:52 +02:00
'data_ora_trasporto' => post ( 'data_ora_trasporto' ) ? : null ,
2020-02-28 12:04:12 +01:00
'idporto' => post ( 'idporto' ),
'idaspettobeni' => post ( 'idaspettobeni' ),
'idrivalsainps' => $idrivalsainps ,
'idritenutaacconto' => $idritenutaacconto ,
'n_colli' => post ( 'n_colli' ),
2020-07-08 08:45:55 +02:00
'peso' => post ( 'peso' ),
'volume' => post ( 'volume' ),
2021-01-12 17:40:04 +01:00
'peso_manuale' => post ( 'peso_manuale' ),
'volume_manuale' => post ( 'volume_manuale' ),
2020-02-28 12:04:12 +01:00
'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 ;
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' );
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
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 ()) {
$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
}
}