2017-08-04 16:28:16 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2020-05-24 12:32:36 +02:00
use Carbon\Carbon ;
2019-01-01 11:39:20 +01:00
use Modules\Anagrafiche\Anagrafica ;
2019-05-04 06:39:18 +02:00
use Modules\Articoli\Articolo as ArticoloOriginale ;
2019-01-01 11:39:20 +01:00
use Modules\Preventivi\Components\Articolo ;
2019-05-04 06:41:56 +02:00
use Modules\Preventivi\Components\Descrizione ;
2019-01-01 11:39:20 +01:00
use Modules\Preventivi\Components\Riga ;
2019-04-04 17:12:32 +02:00
use Modules\Preventivi\Components\Sconto ;
2019-01-01 11:39:20 +01:00
use Modules\Preventivi\Preventivo ;
2019-07-08 12:25:51 +02:00
use Modules\TipiIntervento\Tipo as TipoSessione ;
2019-01-01 11:39:20 +01:00
2017-08-04 16:28:16 +02:00
switch ( post ( 'op' )) {
case 'add' :
$idanagrafica = post ( 'idanagrafica' );
$nome = post ( 'nome' );
$idtipointervento = post ( 'idtipointervento' );
2019-11-20 23:22:43 +01:00
$data_bozza = post ( 'data_bozza' );
$id_sede = post ( 'idsede' );
2017-08-04 16:28:16 +02:00
2019-01-01 11:39:20 +01:00
$anagrafica = Anagrafica :: find ( $idanagrafica );
2019-01-02 10:08:08 +01:00
$tipo = TipoSessione :: find ( $idtipointervento );
2017-08-04 16:28:16 +02:00
2019-11-20 23:22:43 +01:00
$preventivo = Preventivo :: build ( $anagrafica , $tipo , $nome , $data_bozza , $id_sede );
2019-01-01 11:39:20 +01:00
$id_record = $preventivo -> id ;
2017-08-04 16:28:16 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Aggiunto preventivo numero _NUM_!' , [
2019-01-10 17:27:19 +01:00
'_NUM_' => $preventivo [ 'numero' ],
2018-07-07 13:56:22 +02:00
]));
2017-08-04 16:28:16 +02:00
break ;
case 'update' :
if ( isset ( $id_record )) {
2019-05-04 06:41:56 +02:00
$preventivo -> idstato = post ( 'idstato' );
$preventivo -> nome = post ( 'nome' );
$preventivo -> idanagrafica = post ( 'idanagrafica' );
$preventivo -> idsede = post ( 'idsede' );
$preventivo -> idagente = post ( 'idagente' );
$preventivo -> idreferente = post ( 'idreferente' );
$preventivo -> idpagamento = post ( 'idpagamento' );
$preventivo -> idporto = post ( 'idporto' );
$preventivo -> tempi_consegna = post ( 'tempi_consegna' );
$preventivo -> numero = post ( 'numero' );
$preventivo -> data_bozza = post ( 'data_bozza' );
$preventivo -> data_accettazione = post ( 'data_accettazione' );
$preventivo -> data_rifiuto = post ( 'data_rifiuto' );
$preventivo -> data_conclusione = post ( 'data_conclusione' );
$preventivo -> esclusioni = post ( 'esclusioni' );
$preventivo -> descrizione = post ( 'descrizione' );
$preventivo -> id_documento_fe = post ( 'id_documento_fe' );
$preventivo -> num_item = post ( 'num_item' );
$preventivo -> codice_cig = post ( 'codice_cig' );
$preventivo -> codice_cup = post ( 'codice_cup' );
$preventivo -> validita = post ( 'validita' );
$preventivo -> idtipointervento = post ( 'idtipointervento' );
$preventivo -> idiva = post ( 'idiva' );
2019-05-04 06:39:18 +02:00
$preventivo -> save ();
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Preventivo modificato correttamente!' ));
2017-08-04 16:28:16 +02:00
}
2019-05-04 06:39:18 +02:00
2017-08-04 16:28:16 +02:00
break ;
2018-12-23 14:01:59 +01:00
2018-12-07 10:47:44 +01:00
// Duplica preventivo
case 'copy' :
2020-01-15 11:48:45 +01:00
// Copia del preventivo
2019-04-19 20:52:02 +02:00
$new = $preventivo -> replicate ();
2020-05-24 12:32:36 +02:00
$new -> numero = Preventivo :: getNextNumero ( Carbon :: now () );
$new -> data_bozza = Carbon :: now ();
$new -> data_conclusione = Carbon :: now () -> addMonth ();
2020-04-01 16:37:22 +02:00
$new -> stato = 'Bozza' ;
2019-07-11 15:05:36 +02:00
$new -> save ();
$new -> master_revision = $new -> id ;
2019-04-19 20:52:02 +02:00
$new -> save ();
$id_record = $new -> id ;
2020-01-15 11:48:45 +01:00
// Copia delle righe
2019-04-19 20:52:02 +02:00
$righe = $preventivo -> getRighe ();
foreach ( $righe as $riga ) {
$new_riga = $riga -> replicate ();
$new_riga -> setParent ( $new );
$new_riga -> qta_evasa = 0 ;
$new_riga -> save ();
}
2018-12-28 08:07:56 +01:00
2018-12-07 10:47:44 +01:00
flash () -> info ( tr ( 'Preventivo duplicato correttamente!' ));
break ;
2018-12-23 14:01:59 +01:00
2017-08-04 16:28:16 +02:00
case 'addintervento' :
2018-07-19 15:33:32 +02:00
if ( post ( 'idintervento' ) !== null ) {
2017-08-04 16:28:16 +02:00
// Selezione costi da intervento
$idintervento = post ( 'idintervento' );
$rs = $dbo -> fetchArray ( 'SELECT * FROM in_interventi WHERE id=' . prepare ( $idintervento ));
$costo_km = $rs [ 0 ][ 'prezzo_km_unitario' ];
$costo_orario = $rs [ 0 ][ 'prezzo_ore_unitario' ];
2018-09-03 15:29:55 +02:00
$dbo -> update ( 'in_interventi' , [
'id_preventivo' => $id_record ,
], [ 'id' => $idintervento ]);
2017-08-04 16:28:16 +02:00
// Imposto il preventivo nello stato "In lavorazione" se inizio ad aggiungere interventi
$dbo -> query ( " UPDATE `co_preventivi` SET idstato=(SELECT `id` FROM `co_statipreventivi` WHERE `descrizione`='In lavorazione') WHERE `id`= " . prepare ( $id_record ));
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Intervento _NUM_ aggiunto!' , [
2017-09-10 14:35:41 +02:00
'_NUM_' => $rs [ 0 ][ 'codice' ],
2018-07-07 13:56:22 +02:00
]));
2017-08-04 16:28:16 +02:00
}
break ;
// Scollegamento intervento da preventivo
case 'unlink' :
if ( isset ( $_GET [ 'idpreventivo' ]) && isset ( $_GET [ 'idintervento' ])) {
2018-07-19 15:33:32 +02:00
$idintervento = get ( 'idintervento' );
2017-08-04 16:28:16 +02:00
2018-09-03 15:29:55 +02:00
$dbo -> update ( 'in_interventi' , [
'id_preventivo' => null ,
], [ 'id' => $idintervento ]);
2018-07-07 13:56:22 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Intervento _NUM_ rimosso!' , [
2017-09-10 14:35:41 +02:00
'_NUM_' => $idintervento ,
2018-07-07 13:56:22 +02:00
]));
2017-08-04 16:28:16 +02:00
}
break ;
2019-05-04 06:39:18 +02:00
// Eliminazione preventivo
2017-08-04 16:28:16 +02:00
case 'delete' :
2019-10-01 18:32:06 +02:00
try {
$preventivo -> delete ();
2017-08-04 16:28:16 +02:00
2019-10-01 18:32:06 +02:00
flash () -> info ( tr ( 'Preventivo 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 ;
2019-05-04 06:39:18 +02:00
case 'manage_articolo' :
if ( post ( 'idriga' ) != null ) {
$articolo = Articolo :: find ( post ( 'idriga' ));
} else {
$originale = ArticoloOriginale :: find ( post ( 'idarticolo' ));
$articolo = Articolo :: build ( $preventivo , $originale );
}
2017-08-04 16:28:16 +02:00
2018-07-19 15:33:32 +02:00
$qta = post ( 'qta' );
2017-08-04 16:28:16 +02:00
2019-05-04 06:39:18 +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-05-04 06:39:18 +02:00
try {
$articolo -> qta = $qta ;
} catch ( UnexpectedValueException $e ) {
flash () -> error ( tr ( 'Alcuni serial number sono già stati utilizzati!' ));
}
2017-08-04 16:28:16 +02:00
2019-05-04 06:39:18 +02:00
$articolo -> save ();
2018-02-19 11:25:56 +01:00
2019-05-04 06:39:18 +02:00
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Articolo modificato!' ));
2018-02-19 17:48:04 +01:00
} else {
2019-05-04 06:39:18 +02:00
flash () -> info ( tr ( 'Articolo aggiunto!' ));
2018-01-18 19:03:06 +01:00
}
2018-02-19 11:25:56 +01:00
2018-01-18 19:03:06 +01:00
break ;
2017-08-04 16:28:16 +02:00
2019-03-29 12:46:17 +01:00
case 'manage_sconto' :
if ( post ( 'idriga' ) != null ) {
2019-04-04 17:12:32 +02:00
$sconto = Sconto :: find ( post ( 'idriga' ));
2019-03-29 12:46:17 +01:00
} else {
2019-04-04 17:12:32 +02:00
$sconto = Sconto :: build ( $preventivo );
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
}
break ;
2019-05-04 06:39:18 +02:00
case 'manage_riga' :
if ( post ( 'idriga' ) != null ) {
$riga = Riga :: find ( post ( 'idriga' ));
} else {
$riga = Riga :: build ( $preventivo );
}
2017-08-04 16:28:16 +02:00
2018-07-19 15:33:32 +02:00
$qta = post ( 'qta' );
2019-05-04 06:39:18 +02:00
$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-05-04 06:39:18 +02:00
$riga -> qta = $qta ;
$riga -> save ();
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Riga modificata!' ));
2018-02-18 19:53:23 +01:00
} else {
2019-05-04 06:39:18 +02:00
flash () -> info ( tr ( 'Riga aggiunta!' ));
2018-01-18 19:03:06 +01:00
}
2017-08-04 16:28:16 +02:00
break ;
2019-05-04 06:39:18 +02:00
case 'manage_descrizione' :
if ( post ( 'idriga' ) != null ) {
$riga = Descrizione :: find ( post ( 'idriga' ));
} else {
$riga = Descrizione :: build ( $preventivo );
}
2017-08-04 16:28:16 +02:00
2019-05-04 06:39:18 +02:00
$riga -> descrizione = post ( 'descrizione' );
2018-10-30 10:27:44 +01:00
2019-05-04 06:39:18 +02:00
$riga -> save ();
2018-10-25 16:57:39 +02:00
2019-05-04 06:39:18 +02:00
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Riga descrittiva modificata!' ));
} else {
flash () -> info ( tr ( 'Riga descrittiva aggiunta!' ));
}
break ;
// Eliminazione riga
case 'delete_riga' :
$id_riga = post ( 'idriga' );
2020-01-17 17:31:07 +01:00
$type = post ( 'type' );
2019-05-04 06:39:18 +02:00
2020-01-17 17:31:07 +01:00
$riga = $preventivo -> getRiga ( $type , $id_riga );
if ( ! empty ( $riga )) {
2019-05-04 06:39:18 +02:00
$riga -> delete ();
2018-10-25 16:57:39 +02:00
2019-05-04 06:39:18 +02:00
flash () -> info ( tr ( 'Riga eliminata!' ));
}
break ;
case 'add_revision' :
2020-01-15 11:48:45 +01:00
// Rimozione flag default_revision dal record principale e dalle revisioni
$dbo -> query ( 'UPDATE co_preventivi SET default_revision=0 WHERE master_revision = ' . prepare ( $preventivo -> master_revision ));
// Copia del preventivo
$new = $preventivo -> replicate ();
2020-04-01 16:37:22 +02:00
$new -> stato = 'Bozza' ;
2020-01-15 11:48:45 +01:00
$new -> save ();
$new -> default_revision = 1 ;
2020-04-07 17:02:11 +02:00
$new -> numero_revision = $new -> ultima_revisione + 1 ;
2020-01-15 11:48:45 +01:00
$new -> save ();
$id_record = $new -> id ;
2018-10-25 16:57:39 +02:00
2020-01-15 11:48:45 +01:00
// Copia delle righe
$righe = $preventivo -> getRighe ();
foreach ( $righe as $riga ) {
$new_riga = $riga -> replicate ();
$new_riga -> setParent ( $new );
$new_riga -> qta_evasa = 0 ;
$new_riga -> save ();
}
2018-10-25 16:57:39 +02:00
2018-10-17 18:28:15 +02:00
flash () -> info ( tr ( 'Aggiunta nuova revisione!' ));
break ;
2019-08-26 10:50:54 +02:00
case 'update_position' :
$orders = explode ( ',' , $_POST [ 'order' ]);
$order = 0 ;
foreach ( $orders as $idriga ) {
$dbo -> query ( 'UPDATE `co_righe_preventivi` SET `order`=' . prepare ( $order ) . ' WHERE id=' . prepare ( $idriga ));
++ $order ;
}
break ;
2017-08-04 16:28:16 +02:00
}