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-04-29 09:07:06 +02: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' ;
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 ;
2021-09-21 09:49:05 +02:00
use Modules\Preventivi\Stato ;
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' );
2021-04-29 09:07:06 +02:00
$preventivo -> condizioni_fornitura = post ( 'condizioni_fornitura' );
2020-07-07 17:32:03 +02:00
// Informazioni sulle date del documento
2021-04-29 09:07:06 +02:00
$preventivo -> data_bozza = post ( 'data_bozza' ) ? : null ;
$preventivo -> data_rifiuto = post ( 'data_rifiuto' ) ? : null ;
2020-07-07 17:32:03 +02:00
// Dati relativi alla validità del documento
$preventivo -> validita = post ( 'validita' );
$preventivo -> tipo_validita = post ( 'tipo_validita' );
2021-04-29 09:07:06 +02:00
$preventivo -> data_accettazione = post ( 'data_accettazione' ) ? : null ;
$preventivo -> data_conclusione = post ( 'data_conclusione' ) ? : null ;
2020-07-07 17:32:03 +02:00
2019-05-04 06:41:56 +02:00
$preventivo -> esclusioni = post ( 'esclusioni' );
2021-04-29 09:07:06 +02:00
$preventivo -> garanzia = post ( 'garanzia' );
2019-05-04 06:41:56 +02:00
$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 -> idtipointervento = post ( 'idtipointervento' );
$preventivo -> idiva = post ( 'idiva' );
2021-04-06 15:10:55 +02:00
$preventivo -> setScontoFinale ( post ( 'sconto_finale' ), post ( 'tipo_sconto_finale' ));
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-06-09 16:59:26 +02:00
$new -> numero = Preventivo :: getNextNumero ( Carbon :: now ());
2020-05-24 12:32:36 +02:00
$new -> data_bozza = Carbon :: now ();
$new -> data_conclusione = Carbon :: now () -> addMonth ();
2021-09-21 09:49:05 +02:00
$stato_preventivo = Stato :: where ( 'descrizione' , '=' , 'Bozza' ) -> first ();
$new -> stato () -> associate ( $stato_preventivo );
2019-07-11 15:05:36 +02:00
$new -> save ();
$new -> master_revision = $new -> id ;
2021-10-22 18:38:26 +02:00
$new -> descrizione_revision = '' ;
$new -> numero_revision = 0 ;
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 ();
2020-09-22 20:28:37 +02:00
$new_riga -> setDocument ( $new );
2019-04-19 20:52:02 +02:00
$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!' ));
2021-09-20 17:49:30 +02:00
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 ;
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 ( $preventivo , $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-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 );
2020-07-06 13:32:43 +02:00
$articolo -> id_dettaglio_fornitore = post ( 'id_dettaglio_fornitore' ) ? : null ;
2019-05-04 06:39:18 +02:00
}
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 ;
2021-07-02 10:17:09 +02:00
$articolo -> data_evasione = post ( 'data_evasione' ) ? : null ;
$articolo -> ora_evasione = post ( 'ora_evasione' ) ? : null ;
$articolo -> confermato = post ( 'confermato' ) ? : 0 ;
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
2021-07-02 10:17:09 +02:00
// Impostare data evasione su tutte le righe
if ( post ( 'data_evasione_all' ) == 1 ) {
$righe = $preventivo -> getRighe () -> where ( 'is_descrizione' , '=' , '0' );
foreach ( $righe as $riga ) {
$riga -> data_evasione = post ( 'data_evasione' ) ? : null ;
$riga -> ora_evasione = post ( 'ora_evasione' ) ? : null ;
$riga -> save ();
}
}
// Impostare confermato su tutte le righe
if ( post ( 'confermato_all' ) == 1 ) {
$righe = $preventivo -> getRighe () -> where ( 'is_descrizione' , '=' , '0' );
foreach ( $righe as $riga ) {
$riga -> confermato = post ( 'confermato' ) ? : 0 ;
$riga -> save ();
}
}
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' );
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
}
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 ;
2021-07-02 10:17:09 +02:00
$riga -> data_evasione = post ( 'data_evasione' ) ? : null ;
$riga -> ora_evasione = post ( 'ora_evasione' ) ? : null ;
$riga -> confermato = post ( 'confermato' ) ? : 0 ;
2019-05-04 06:39:18 +02:00
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 ();
2021-07-02 10:17:09 +02:00
// Impostare data evasione su tutte le righe
if ( post ( 'data_evasione_all' ) == 1 ) {
$righe = $preventivo -> getRighe () -> where ( 'is_descrizione' , '=' , '0' );
foreach ( $righe as $riga ) {
$riga -> data_evasione = post ( 'data_evasione' ) ? : null ;
$riga -> ora_evasione = post ( 'ora_evasione' ) ? : null ;
$riga -> save ();
}
}
// Impostare confermato su tutte le righe
if ( post ( 'confermato_all' ) == 1 ) {
$righe = $preventivo -> getRighe () -> where ( 'is_descrizione' , '=' , '0' );
foreach ( $righe as $riga ) {
$riga -> confermato = post ( 'confermato' ) ? : 0 ;
$riga -> save ();
}
}
2019-05-04 06:39:18 +02:00
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' :
2020-07-06 13:19:20 +02:00
$id_riga = post ( 'riga_id' );
$type = post ( 'riga_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 ();
2021-09-21 09:49:05 +02:00
$stato_preventivo = Stato :: where ( 'descrizione' , '=' , 'Bozza' ) -> first ();
$new -> stato () -> associate ( $stato_preventivo );
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 ;
2021-04-26 13:24:35 +02:00
$new -> descrizione_revision = post ( 'descrizione' );
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 ();
2020-09-22 20:28:37 +02:00
$new_riga -> setDocument ( $new );
2020-01-15 11:48:45 +01:00
$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' :
2020-08-06 11:34:32 +02:00
$order = explode ( ',' , post ( 'order' , true ));
2019-08-26 10:50:54 +02:00
2020-08-06 11:34:32 +02:00
foreach ( $order as $i => $id_riga ) {
2021-04-29 09:07:06 +02:00
$dbo -> query ( 'UPDATE `co_righe_preventivi` SET `order` = ' . prepare ( $i + 1 ) . ' WHERE id=' . prepare ( $id_riga ));
2019-08-26 10:50:54 +02:00
}
break ;
2017-08-04 16:28:16 +02:00
}