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 ;
2023-10-06 13:21:37 +02:00
use Modules\Iva\Aliquota ;
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' );
2022-11-28 09:27:25 +01:00
$id_segment = post ( 'id_segment' );
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
2022-11-28 09:27:25 +01:00
$preventivo = Preventivo :: build ( $anagrafica , $tipo , $nome , $data_bozza , $id_sede , $id_segment );
2023-08-04 14:54:28 +02:00
2024-03-27 15:18:09 +01:00
$preventivo -> esclusioni = setting ( 'Esclusioni default preventivi' );
2022-05-26 17:08:26 +02:00
$preventivo -> idstato = post ( 'idstato' );
$preventivo -> save ();
2019-01-01 11:39:20 +01:00
$id_record = $preventivo -> id ;
2017-08-04 16:28:16 +02:00
2022-05-26 17:08:26 +02:00
if ( isAjaxRequest ()) {
2023-04-06 13:14:55 +02:00
echo json_encode ([ 'id' => $id_record , 'text' => tr ( 'Preventivo' ) . ' ' . $preventivo -> numero . ' ' . tr ( 'del' ) . ' ' . dateFormat ( $preventivo -> data_bozza ) . ' - ' . $preventivo -> nome ]);
2022-05-26 17:08:26 +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' :
2024-04-12 16:59:19 +02:00
if ( ! empty ( $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' );
2021-12-13 10:45:11 +01:00
$preventivo -> informazioniaggiuntive = post ( 'informazioniaggiuntive' );
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
2024-01-15 15:30:45 +01:00
// Duplica preventivo
2018-12-07 10:47:44 +01:00
case 'copy' :
2020-01-15 11:48:45 +01:00
// Copia del preventivo
2019-04-19 20:52:02 +02:00
$new = $preventivo -> replicate ();
2022-11-28 09:27:25 +01:00
$new -> numero = Preventivo :: getNextNumero ( Carbon :: now (), $new -> id_segment );
2020-05-24 12:32:36 +02:00
$new -> data_bozza = Carbon :: now ();
2021-09-21 09:49:05 +02:00
2024-04-18 17:44:05 +02:00
$stato_preventivo = ( new Stato ()) -> getByField ( 'title' , 'Bozza' , Models\Locale :: getPredefined () -> id );
2021-09-21 09:49:05 +02:00
$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
2024-04-18 17:44:05 +02:00
$dbo -> query ( 'UPDATE `co_preventivi` SET `idstato`=(SELECT `id` FROM `co_statipreventivi` LEFT JOIN `co_statipreventivi_lang ON (`co_statipreventivi_lang`.`id_record` = `co_statipreventivi`.`id` AND `co_statipreventivi_lang`.`id_lang` = ' . prepare ( Models\Locale :: getDefault () -> id ) . " ) WHERE `title`='In lavorazione') WHERE `co_preventivi`.`id`= " . prepare ( $id_record ));
2017-08-04 16:28:16 +02:00
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 ;
2024-01-15 15:30:45 +01:00
// Scollegamento intervento da preventivo
2017-08-04 16:28:16 +02:00
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 ;
2024-01-15 15:30:45 +01: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 );
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
2024-04-18 17:44:05 +02:00
$articolo -> setTranslation ( 'title' , post ( 'descrizione' ));
2022-07-20 17:41:14 +02:00
$articolo -> note = post ( 'note' );
2019-05-04 06:39:18 +02:00
$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' ));
2022-05-19 17:42:41 +02:00
$articolo -> setProvvigione ( post ( 'provvigione' ), post ( 'tipo_provvigione' ));
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' );
2022-07-20 17:41:14 +02:00
$sconto -> note = post ( 'note' );
2020-09-29 15:27:00 +02:00
$sconto -> setScontoUnitario ( post ( 'sconto_unitario' ), post ( 'idiva' ));
2023-04-27 15:30:37 +02:00
$sconto -> confermato = setting ( 'Conferma automaticamente le quantità nei preventivi' );
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' );
2022-07-20 17:41:14 +02:00
$riga -> note = post ( 'note' );
2019-05-04 06:39:18 +02:00
$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' ));
2022-05-19 17:42:41 +02:00
$riga -> setProvvigione ( post ( 'provvigione' ), post ( 'tipo_provvigione' ));
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' );
2022-07-20 17:41:14 +02:00
$riga -> note = post ( 'note' );
2023-09-15 17:18:44 +02:00
$riga -> is_titolo = post ( 'is_titolo' );
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 ;
2024-01-15 15:30:45 +01:00
// Eliminazione riga
2019-05-04 06:39:18 +02:00
case 'delete_riga' :
2023-08-04 14:54:28 +02:00
$id_righe = ( array ) post ( 'righe' );
2022-04-01 09:58:53 +02:00
foreach ( $id_righe as $id_riga ) {
$riga = Articolo :: find ( $id_riga ) ? : Riga :: find ( $id_riga );
$riga = $riga ? : Descrizione :: find ( $id_riga );
$riga = $riga ? : Sconto :: find ( $id_riga );
$riga -> delete ();
2019-05-04 06:39:18 +02:00
2022-04-01 09:58:53 +02:00
$riga = null ;
}
2020-01-17 17:31:07 +01:00
2023-12-01 17:21:34 +01:00
if ( count ( $id_righe ) == 1 ) {
flash () -> info ( tr ( 'Riga eliminata!' ));
} else {
flash () -> info ( tr ( 'Righe eliminate!' ));
}
2022-04-01 09:58:53 +02:00
break ;
2024-01-15 15:30:45 +01:00
// Duplicazione riga
2022-04-01 09:58:53 +02:00
case 'copy_riga' :
2023-08-04 14:54:28 +02:00
$id_righe = ( array ) post ( 'righe' );
2022-04-01 09:58:53 +02:00
foreach ( $id_righe as $id_riga ) {
$riga = Articolo :: find ( $id_riga ) ? : Riga :: find ( $id_riga );
$riga = $riga ? : Descrizione :: find ( $id_riga );
$riga = $riga ? : Sconto :: find ( $id_riga );
2018-10-25 16:57:39 +02:00
2022-04-01 09:58:53 +02:00
$new_riga = $riga -> replicate ();
$new_riga -> setDocument ( $preventivo );
$new_riga -> qta_evasa = 0 ;
$new_riga -> save ();
$riga = null ;
2019-05-04 06:39:18 +02:00
}
2022-04-01 09:58:53 +02:00
flash () -> info ( tr ( 'Righe duplicate!' ));
2019-05-04 06:39:18 +02:00
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
2024-04-18 17:44:05 +02:00
$stato_preventivo = ( new Stato ()) -> getByField ( 'title' , 'Bozza' , Models\Locale :: getPredefined () -> id );
2021-09-21 09:49:05 +02:00
$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
}
2023-02-17 12:33:45 +01:00
break ;
case 'add_articolo' :
$id_articolo = post ( 'id_articolo' );
$barcode = post ( 'barcode' );
2023-04-27 12:17:36 +02:00
$dir = 'entrata' ;
2023-02-17 12:33:45 +01:00
if ( ! empty ( $barcode )) {
2023-08-04 14:54:28 +02:00
$id_articolo = $dbo -> selectOne ( 'mg_articoli' , 'id' , [ 'deleted_at' => null , 'attivo' => 1 , 'barcode' => $barcode ])[ 'id' ];
2023-02-17 12:33:45 +01:00
}
if ( ! empty ( $id_articolo )) {
$permetti_movimenti_sotto_zero = setting ( 'Permetti selezione articoli con quantità minore o uguale a zero in Documenti di Vendita' );
$qta_articolo = $dbo -> selectOne ( 'mg_articoli' , 'qta' , [ 'id' => $id_articolo ])[ 'qta' ];
$originale = ArticoloOriginale :: find ( $id_articolo );
2023-03-16 09:51:14 +01:00
$articolo = Articolo :: build ( $preventivo , $originale );
$qta = 1 ;
$articolo -> um = $originale -> um ;
$articolo -> qta = 1 ;
$articolo -> costo_unitario = $originale -> prezzo_acquisto ;
2023-04-27 15:30:37 +02:00
$articolo -> confermato = setting ( 'Conferma automaticamente le quantità nei preventivi' );
2023-03-16 09:51:14 +01:00
2023-10-06 13:21:37 +02:00
// L'aliquota dell'articolo ha precedenza solo se ha aliquota a 0, altrimenti anagrafica -> articolo -> impostazione
if ( $originale -> idiva_vendita ) {
$aliquota_articolo = floatval ( Aliquota :: find ( $originale -> idiva_vendita ) -> percentuale );
}
$id_iva = ( $preventivo -> anagrafica -> idiva_vendite && ( ! $originale -> idiva_vendita || $aliquota_articolo != 0 ) ? $preventivo -> anagrafica -> idiva_vendite : $originale -> idiva_vendita ) ? : setting ( 'Iva predefinita' );
2023-03-16 09:51:14 +01:00
$id_anagrafica = $preventivo -> idanagrafica ;
$prezzi_ivati = setting ( 'Utilizza prezzi di vendita comprensivi di IVA' );
2023-08-04 14:54:28 +02:00
2023-03-16 09:51:14 +01:00
// CALCOLO PREZZO UNITARIO
2023-12-29 16:15:38 +01:00
$prezzo_consigliato = getPrezzoConsigliato ( $id_anagrafica , $dir , $id_articolo );
$prezzo_unitario = $prezzo_consigliato [ 'prezzo_unitario' ];
$sconto = $prezzo_consigliato [ 'sconto' ];
2023-03-16 09:51:14 +01:00
$prezzo_unitario = $prezzo_unitario ? : ( $prezzi_ivati ? $originale -> prezzo_vendita_ivato : $originale -> prezzo_vendita );
$provvigione = $dbo -> selectOne ( 'an_anagrafiche' , 'provvigione_default' , [ 'idanagrafica' => $preventivo -> idagente ])[ 'provvigione_default' ];
2023-02-17 12:33:45 +01:00
2023-07-31 17:26:06 +02:00
// Aggiunta sconto combinato se è presente un piano di sconto nell'anagrafica
$piano_sconto = $dbo -> fetchOne ( 'SELECT prc_guadagno FROM an_anagrafiche INNER JOIN mg_piani_sconto ON an_anagrafiche.id_piano_sconto_vendite=mg_piani_sconto.id WHERE idanagrafica=' . prepare ( $id_anagrafica ));
if ( ! empty ( $piano_sconto )) {
$sconto = parseScontoCombinato ( $piano_sconto [ 'prc_guadagno' ] . '+' . $sconto );
}
2023-03-16 09:51:14 +01:00
$articolo -> setPrezzoUnitario ( $prezzo_unitario , $id_iva );
$articolo -> setSconto ( $sconto , 'PRC' );
$articolo -> setProvvigione ( $provvigione ? : 0 , 'PRC' );
$articolo -> save ();
2023-02-17 12:33:45 +01:00
2023-03-16 09:51:14 +01:00
flash () -> info ( tr ( 'Nuovo articolo aggiunto!' ));
2023-02-17 12:33:45 +01:00
} else {
$response [ 'error' ] = tr ( 'Nessun articolo corrispondente a magazzino' );
echo json_encode ( $response );
}
break ;
case 'update_inline' :
$id_riga = post ( 'riga_id' );
$riga = $riga ? : Riga :: find ( $id_riga );
$riga = $riga ? : Articolo :: find ( $id_riga );
2023-10-06 12:45:32 +02:00
$riga = $riga ? : Sconto :: find ( $id_riga );
2023-02-17 12:33:45 +01:00
if ( ! empty ( $riga )) {
2023-10-06 12:45:32 +02:00
if ( $riga -> isSconto ()) {
$riga -> setScontoUnitario ( post ( 'sconto' ), $riga -> idiva );
} else {
$riga -> qta = post ( 'qta' );
$riga -> setPrezzoUnitario ( post ( 'prezzo' ), $riga -> idiva );
$riga -> setSconto ( post ( 'sconto' ), post ( 'tipo_sconto' ));
$riga -> costo_unitario = post ( 'costo' ) ? : 0 ;
}
2023-02-17 12:33:45 +01:00
$riga -> save ();
2023-09-29 15:34:02 +02:00
flash () -> info ( tr ( 'Riga aggiornata!' ));
2023-02-17 12:33:45 +01:00
}
2019-08-26 10:50:54 +02:00
break ;
2023-04-27 14:35:02 +02:00
case 'edit-price' :
2023-09-25 16:53:28 +02:00
$righe = ( array ) post ( 'righe' );
2023-05-04 09:57:01 +02:00
$numero_totale = 0 ;
2023-08-04 14:54:28 +02:00
2023-04-27 14:35:02 +02:00
foreach ( $righe as $riga ) {
2024-01-15 15:30:45 +01:00
if ( $riga [ 'id' ] != null ) {
2023-04-28 14:44:35 +02:00
$articolo = Articolo :: find ( $riga [ 'id' ]);
}
2023-05-04 09:57:01 +02:00
2023-09-25 16:53:28 +02:00
if ( $articolo -> prezzo_unitario != $riga [ 'price' ]) {
2023-05-04 09:57:01 +02:00
$articolo -> setPrezzoUnitario ( $riga [ 'price' ], $articolo -> idiva );
$articolo -> save ();
++ $numero_totale ;
}
2023-04-28 14:44:35 +02:00
}
2023-05-04 09:07:34 +02:00
2023-05-04 09:57:01 +02:00
if ( $numero_totale > 1 ) {
flash () -> info ( tr ( '_NUM_ prezzi modificati!' , [
'_NUM_' => $numero_totale ,
]));
2023-08-04 14:54:28 +02:00
} elseif ( $numero_totale == 1 ) {
2023-05-04 09:57:01 +02:00
flash () -> info ( tr ( '_NUM_ prezzo modificato!' , [
'_NUM_' => $numero_totale ,
]));
} else {
flash () -> warning ( tr ( 'Nessun prezzo modificato!' ));
}
2023-04-27 14:35:02 +02:00
2023-05-04 09:57:01 +02:00
break ;
2023-09-25 16:53:28 +02:00
case 'update-price' :
$dir = 'entrata' ;
$id_anagrafica = $preventivo -> idanagrafica ;
$prezzi_ivati = setting ( 'Utilizza prezzi di vendita comprensivi di IVA' );
$numero_totale = 0 ;
$id_righe = ( array ) post ( 'righe' );
foreach ( $id_righe as $id_riga ) {
$riga = Articolo :: find ( $id_riga ) ? : Riga :: find ( $id_riga );
// CALCOLO PREZZO UNITARIO
$prezzo_unitario = 0 ;
$sconto = 0 ;
if ( $riga -> isArticolo ()) {
2023-12-29 16:15:38 +01:00
$id_articolo = $riga -> idarticolo ;
$prezzo_consigliato = getPrezzoConsigliato ( $id_anagrafica , $dir , $id_articolo );
$prezzo_unitario = $prezzo_consigliato [ 'prezzo_unitario' ];
$sconto = $prezzo_consigliato [ 'sconto' ];
2023-09-25 16:53:28 +02:00
$prezzo_unitario = $prezzo_unitario ? : ( $prezzi_ivati ? $riga -> articolo -> prezzo_vendita_ivato : $riga -> articolo -> prezzo_vendita );
$riga -> setPrezzoUnitario ( $prezzo_unitario , $riga -> idiva );
if ( $dir == 'entrata' ) {
$riga -> costo_unitario = $riga -> articolo -> prezzo_acquisto ;
}
}
// Aggiunta sconto combinato se è presente un piano di sconto nell'anagrafica
$piano_sconto = $dbo -> fetchOne ( 'SELECT prc_guadagno FROM an_anagrafiche INNER JOIN mg_piani_sconto ON an_anagrafiche.id_piano_sconto_vendite=mg_piani_sconto.id WHERE idanagrafica=' . prepare ( $id_anagrafica ));
if ( ! empty ( $piano_sconto )) {
$sconto = parseScontoCombinato ( $piano_sconto [ 'prc_guadagno' ] . '+' . $sconto );
}
$riga -> setSconto ( $sconto , 'PRC' );
$riga -> save ();
++ $numero_totale ;
}
if ( $numero_totale > 1 ) {
flash () -> info ( tr ( '_NUM_ prezzi modificati!' , [
'_NUM_' => $numero_totale ,
]));
} elseif ( $numero_totale == 1 ) {
flash () -> info ( tr ( '_NUM_ prezzo modificato!' , [
'_NUM_' => $numero_totale ,
]));
} else {
flash () -> warning ( tr ( 'Nessun prezzo modificato!' ));
}
break ;
2023-09-27 17:04:00 +02:00
}