2017-08-04 16:28:16 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
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-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-01-02 14:15:16 +01:00
$preventivo = Preventivo :: build ( $anagrafica , $tipo , $nome );
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' :
2019-04-19 20:52:02 +02:00
$new = $preventivo -> replicate ();
2019-05-10 17:54:18 +02:00
$new -> numero = Preventivo :: getNextNumero ();
2019-04-19 20:52:02 +02:00
$new -> idstato = 1 ;
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 ;
$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-05-04 06:39:18 +02:00
$preventivo -> delete ();
2017-08-04 16:28:16 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Preventivo eliminato!' ));
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
2019-05-04 06:39:18 +02:00
$articolo -> id_iva = post ( 'idiva' );
2017-08-04 16:28:16 +02:00
2019-09-05 18:40:39 +02:00
$articolo -> prezzo_unitario_acquisto = post ( 'prezzo_acquisto' ) ? : 0 ;
2019-05-04 06:39:18 +02:00
$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-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 ;
$riga -> id_iva = post ( 'idiva' );
2019-09-05 18:40:39 +02:00
$riga -> prezzo_unitario_acquisto = post ( 'prezzo_acquisto' ) ? : 0 ;
2019-05-04 06:39:18 +02:00
$riga -> prezzo_unitario_vendita = post ( 'prezzo' );
$riga -> sconto_unitario = post ( 'sconto' );
$riga -> tipo_sconto = post ( 'tipo_sconto' );
$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' );
if ( $id_riga !== null ) {
$riga = Descrizione :: find ( $id_riga ) ? : Riga :: find ( $id_riga );
$riga = $riga ? $riga : Articolo :: find ( $id_riga );
$riga = $riga ? $riga : Sconto :: find ( $id_riga );
$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' :
2018-10-17 18:28:15 +02:00
//Copio il preventivo
$rs_preventivo = $dbo -> fetchArray ( " SELECT * FROM co_preventivi WHERE id=' " . $id_record . " ' " );
2018-10-25 16:57:39 +02:00
2018-10-17 18:28:15 +02:00
//Tolgo il flag default_revision da tutte le revisioni e dal record_principale
2018-10-25 16:57:39 +02:00
$dbo -> query ( 'UPDATE co_preventivi SET default_revision=0 WHERE master_revision=' . prepare ( $rs_preventivo [ 0 ][ 'master_revision' ]));
2018-10-17 18:28:15 +02:00
$preventivo = [
'numero' => $rs_preventivo [ 0 ][ 'numero' ],
'nome' => $rs_preventivo [ 0 ][ 'nome' ],
'idagente' => $rs_preventivo [ 0 ][ 'idagente' ],
'data_bozza' => $rs_preventivo [ 0 ][ 'data_bozza' ],
'data_accettazione' => $rs_preventivo [ 0 ][ 'data_accettazione' ],
'data_rifiuto' => $rs_preventivo [ 0 ][ 'data_rifiuto' ],
'data_conclusione' => $rs_preventivo [ 0 ][ 'data_conclusione' ],
'data_pagamento' => $rs_preventivo [ 0 ][ 'data_pagamento' ],
'budget' => $rs_preventivo [ 0 ][ 'budget' ],
'descrizione' => $rs_preventivo [ 0 ][ 'descrizione' ],
'idstato' => $rs_preventivo [ 0 ][ 'idstato' ],
'validita' => $rs_preventivo [ 0 ][ 'validita' ],
'tempi_consegna' => $rs_preventivo [ 0 ][ 'tempi_consegna' ],
'idanagrafica' => $rs_preventivo [ 0 ][ 'idanagrafica' ],
'esclusioni' => $rs_preventivo [ 0 ][ 'esclusioni' ],
'idreferente' => $rs_preventivo [ 0 ][ 'idreferente' ],
'idpagamento' => $rs_preventivo [ 0 ][ 'idpagamento' ],
'idporto' => $rs_preventivo [ 0 ][ 'idporto' ],
'idtipointervento' => $rs_preventivo [ 0 ][ 'idtipointervento' ],
'idiva' => $rs_preventivo [ 0 ][ 'idiva' ],
'costo_diritto_chiamata' => $rs_preventivo [ 0 ][ 'costo_diritto_chiamata' ],
'ore_lavoro' => $rs_preventivo [ 0 ][ 'ore_lavoro' ],
'costo_orario' => $rs_preventivo [ 0 ][ 'costo_orario' ],
'costo_km' => $rs_preventivo [ 0 ][ 'costo_km' ],
'master_revision' => $rs_preventivo [ 0 ][ 'master_revision' ],
'default_revision' => '1' ,
];
2018-10-25 16:57:39 +02:00
2018-10-17 18:28:15 +02:00
$dbo -> insert ( 'co_preventivi' , $preventivo );
$id_record_new = $dbo -> lastInsertedID ();
2018-10-25 16:57:39 +02:00
$rs_righe_preventivo = $dbo -> fetchArray ( 'SELECT * FROM co_righe_preventivi WHERE idpreventivo=' . prepare ( $id_record ));
for ( $i = 0 ; $i < sizeof ( $rs_righe_preventivo ); ++ $i ) {
2018-10-17 18:28:15 +02:00
$righe_preventivo = [
'idpreventivo' => $id_record_new ,
'idarticolo' => $rs_righe_preventivo [ $i ][ 'idarticolo' ],
'is_descrizione' => $rs_righe_preventivo [ $i ][ 'is_descrizione' ],
'idiva' => $rs_righe_preventivo [ $i ][ 'idiva' ],
'desc_iva' => $rs_righe_preventivo [ $i ][ 'desc_iva' ],
'iva' => $rs_righe_preventivo [ $i ][ 'iva' ],
'iva_indetraibile' => $rs_righe_preventivo [ $i ][ 'iva_indetraibile' ],
'descrizione' => $rs_righe_preventivo [ $i ][ 'descrizione' ],
'subtotale' => $rs_righe_preventivo [ $i ][ 'subtotale' ],
'sconto' => $rs_righe_preventivo [ $i ][ 'sconto' ],
'sconto_unitario' => $rs_righe_preventivo [ $i ][ 'sconto_unitario' ],
'tipo_sconto' => $rs_righe_preventivo [ $i ][ 'tipo_sconto' ],
'um' => $rs_righe_preventivo [ $i ][ 'um' ],
'qta' => $rs_righe_preventivo [ $i ][ 'qta' ],
'order' => $rs_righe_preventivo [ $i ][ 'order' ],
];
$dbo -> insert ( 'co_righe_preventivi' , $righe_preventivo );
}
2018-10-25 16:57:39 +02:00
2018-10-17 18:28:15 +02:00
$id_record = $id_record_new ;
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
}