2019-06-21 13:04:46 +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 />.
*/
2019-06-21 13:04:46 +02:00
include_once __DIR__ . '/../../core.php' ;
2024-03-05 16:01:45 +01:00
use Models\Module ;
2024-04-19 16:44:08 +02:00
use Modules\Scadenzario\Tipo ;
2019-06-21 13:04:46 +02:00
switch ( filter ( 'op' )) {
case 'update' :
$descrizione = filter ( 'descrizione' );
$nome = filter ( 'nome' );
if ( isset ( $nome )) {
2024-01-15 15:30:45 +01:00
// Se non esiste già una tipo di scadenza con lo stesso nome
2024-06-11 16:58:27 +02:00
$nome_new = Tipo :: where ( 'name' , $descrizione ) -> where ( 'id' , '!=' , $id_record ) -> first ();
2024-04-19 16:44:08 +02:00
$nome_prev = $tipo -> getTranslation ( 'title' );
2024-03-07 17:51:41 +01:00
if ( empty ( $nome_new )) {
2024-01-15 15:30:45 +01:00
// nome_prev
2024-04-19 16:44:08 +02:00
$tipo -> setTranslation ( 'title' , $descrizione );
if ( Models\Locale :: getDefault () -> id == Models\Locale :: getPredefined () -> id ) {
$tipo -> name = $nome ;
}
$tipo -> save ();
flash () -> info ( tr ( 'Salvataggio completato.' ));
2019-06-21 13:04:46 +02:00
2024-06-11 16:58:27 +02:00
$segmento = $dbo -> fetchOne ( 'SELECT `zz_segments`.`id` FROM `zz_segments` LEFT JOIN `zz_segments_lang` ON (`zz_segments_lang`.`id_record` = `zz_segments`.`id` AND `zz_segments_lang`.`id_lang` = ' . prepare ( Models\Locale :: getDefault () -> id ) . ') WHERE `id_module` = ' . prepare ( Module :: where ( 'name' , 'Scadenzario' ) -> first () -> id ) . ' AND `clause` = "co_scadenziario.tipo=\'' . $nome_prev . '\'" AND `zz_segments_lang`.`title` = "Scadenzario ' . $nome_prev . '"' )[ 'id' ];
2024-03-11 10:40:42 +01:00
2024-01-15 15:30:45 +01:00
// aggiorno anche il segmento
2019-06-21 15:50:28 +02:00
$dbo -> update ( 'zz_segments' , [
'clause' => 'co_scadenziario.tipo="' . $nome . '"' ,
2024-03-11 10:40:42 +01:00
], [
'id' => $segmento ,
]);
2024-03-22 15:52:24 +01:00
2024-03-11 10:40:42 +01:00
$dbo -> update ( 'zz_segments_lang' , [
2024-04-19 16:44:08 +02:00
'title' => 'Scadenzario ' . $nome ,
2019-06-21 15:50:28 +02:00
], [
2024-03-11 10:40:42 +01:00
'id_record' => $segmento ,
2024-03-22 15:52:24 +01:00
'id_lang' => Models\Locale :: getDefault () -> id ,
2019-06-21 15:50:28 +02:00
]);
2019-06-21 13:04:46 +02:00
flash () -> info ( tr ( 'Salvataggio completato!' ));
} else {
2019-06-21 15:50:28 +02:00
flash () -> error ( tr ( " E' già presente una tipologia di _TYPE_ con nome: _NOME_ " , [
2019-06-21 13:04:46 +02:00
'_TYPE_' => 'scadenza' ,
2019-06-29 11:01:26 +02:00
'_NOME_' => $nome ,
2019-06-21 13:04:46 +02:00
]));
}
} else {
2020-03-03 10:33:32 +01:00
flash () -> error ( tr ( 'Ci sono stati alcuni errori durante il salvataggio' ));
2019-06-21 13:04:46 +02:00
}
break ;
case 'add' :
$descrizione = filter ( 'descrizione' );
$nome = filter ( 'nome' );
2019-06-21 15:50:28 +02:00
if ( isset ( $nome )) {
2024-01-15 15:30:45 +01:00
// Se non esiste già un tipo di scadenza con lo stesso nome
2024-06-11 16:58:27 +02:00
if ( empty ( Tipo :: where ( 'name' , $descrizione ) -> where ( 'id' , '!=' , $id_record ) -> first ())) {
2024-04-23 09:56:36 +02:00
$tipo = Tipo :: build ();
2024-04-19 16:44:08 +02:00
if ( Models\Locale :: getDefault () -> id == Models\Locale :: getPredefined () -> id ) {
$tipo -> name = $nome ;
}
$tipo -> save ();
2024-04-23 09:56:36 +02:00
2019-06-21 13:04:46 +02:00
$id_record = $dbo -> lastInsertedID ();
2024-04-19 16:44:08 +02:00
$tipo -> setTranslation ( 'title' , $descrizione );
$tipo -> save ();
2019-06-29 11:01:26 +02:00
2024-01-15 15:30:45 +01:00
// Aggiungo anche il segmento
2019-06-21 15:50:28 +02:00
$dbo -> insert ( 'zz_segments' , [
2024-06-11 16:58:27 +02:00
'id_module' => Module :: where ( 'name' , 'Scadenzario' ) -> first () -> id ,
2019-06-21 15:50:28 +02:00
'clause' => 'co_scadenziario.tipo="' . $nome . '"' ,
2019-06-29 11:01:26 +02:00
'position' => 'WHR' ,
2019-06-21 15:50:28 +02:00
]);
2024-03-13 17:32:07 +01:00
$id_segment = $dbo -> lastInsertedID ();
$dbo -> insert ( 'zz_segments_lang' , [
2024-04-19 16:44:08 +02:00
'title' => 'Scadenzario ' . $nome ,
2024-03-13 17:32:07 +01:00
'id_record' => $id_segment ,
2024-03-22 15:52:24 +01:00
'id_lang' => Models\Locale :: getDefault () -> id ,
2024-03-11 10:40:42 +01:00
]);
2019-06-21 15:50:28 +02:00
2019-06-21 13:04:46 +02:00
if ( isAjaxRequest ()) {
2022-02-06 10:06:48 +01:00
echo json_encode ([ 'id' => $id_record , 'text' => $descrizione ]);
2019-06-21 13:04:46 +02:00
}
flash () -> info ( tr ( 'Aggiunta nuova tipologia di _TYPE_' , [
'_TYPE_' => 'scadenza' ,
]));
} else {
2019-06-21 15:50:28 +02:00
flash () -> error ( tr ( " E' già presente una tipologia di _TYPE_ con nome: _NOME_ " , [
2019-06-21 13:04:46 +02:00
'_TYPE_' => 'scadenza' ,
2019-06-29 11:01:26 +02:00
'_NOME_' => $nome ,
2019-06-21 13:04:46 +02:00
]));
}
} else {
flash () -> error ( tr ( 'Ci sono stati alcuni errori durante il salvataggio' ));
}
break ;
case 'delete' :
2024-04-18 17:44:05 +02:00
$documenti = $dbo -> fetchNum ( 'SELECT `id` FROM `co_scadenziario` WHERE `tipo` = (SELECT `title` FROM `co_tipi_scadenze` LEFT JOIN `co_tipi_scadenze_lang` ON (`co_tipi_scadenze_lang`.`id_record` = `co_tipi_scadenze`.`id` AND `co_tipi_scadenze_lang`.`id_lang` = ' . prepare ( Models\Locale :: getDefault () -> id ) . ') WHERE `co_tipi_scadenze`.`id` = ' . prepare ( $id_record ) . ')' );
2019-06-21 13:04:46 +02:00
2024-04-12 16:59:19 +02:00
if (( ! empty ( $id_record )) && empty ( $documenti )) {
2019-06-21 15:50:28 +02:00
$dbo -> query ( 'DELETE FROM `co_tipi_scadenze` WHERE `can_delete` = 1 AND `id`=' . prepare ( $id_record ));
2019-06-21 13:04:46 +02:00
flash () -> info ( tr ( 'Tipologia di _TYPE_ eliminata con successo.' , [
'_TYPE_' => 'scadenza' ,
]));
} else {
flash () -> error ( tr ( 'Sono presenti delle scadenze collegate a questo tipo di scadenza' ));
}
break ;
}