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 ;
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-03-07 17:51:41 +01:00
$nome_new = $dbo -> fetchOne ( 'SELECT * 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 ( setting ( 'Lingua' )) . ') WHERE `name` = ' . prepare ( $nome ) . ' AND `co_tipi_scadenze_lang`.`id_record` != ' . prepare ( $id_record ));
if ( empty ( $nome_new )) {
2024-01-15 15:30:45 +01:00
// nome_prev
2024-03-07 17:51:41 +01:00
$nome_prev = $dbo -> fetchOne ( 'SELECT `name` AS nome_prev 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 ( setting ( 'Lingua' )) . '") WHERE `co_tipi_scadenze`.`id`=' . prepare ( $id_record ))[ 'nome_prev' ];
2019-06-21 15:50:28 +02:00
2024-03-07 17:51:41 +01:00
$dbo -> update ( 'co_tipi_scadenze_lang' , [
'name' => $nome ,
'description' => $descrizione ,
], [ 'id_record' => $id_record , 'id_lang' => setting ( 'Lingua' )]);
2019-06-21 13:04:46 +02: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 . '"' ,
'name' => 'Scadenzario ' . $nome ,
], [
'clause' => 'co_scadenziario.tipo="' . $nome_prev . '"' ,
'name' => 'Scadenzario ' . $nome_prev ,
2024-03-05 16:01:45 +01:00
'id_module' => ( new Module ()) -> getByName ( 'Scadenzario' ) -> id_record ,
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-03-07 17:51:41 +01:00
if ( $dbo -> fetchNum ( 'SELECT * 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 ( setting ( 'Lingua' )) . '") WHERE `name`=' . prepare ( $nome )) == 0 ) {
2019-06-21 15:50:28 +02:00
$dbo -> insert ( 'co_tipi_scadenze' , [
2024-03-07 17:51:41 +01:00
'created_at' => 'NOW()' ,
2019-06-21 13:04:46 +02:00
]);
$id_record = $dbo -> lastInsertedID ();
2024-03-07 17:51:41 +01:00
$dbo -> insert ( 'co_tipi_scadenze_lang' , [
'name' => $nome ,
'description' => $descrizione ,
'id_record' => $id_record ,
'id_lang' => setting ( 'Lingua' )
]);
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-03-05 16:01:45 +01:00
'id_module' => ( new Module ()) -> getByName ( 'Scadenzario' ) -> id_record ,
2019-06-21 15:50:28 +02:00
'name' => 'Scadenzario ' . $nome ,
'clause' => 'co_scadenziario.tipo="' . $nome . '"' ,
2019-06-29 11:01:26 +02:00
'position' => 'WHR' ,
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-03-07 17:51:41 +01:00
$documenti = $dbo -> fetchNum ( 'SELECT `id` FROM `co_scadenziario` WHERE `tipo` = (SELECT `name` 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 ( setting ( 'Lingua' )) . '") WHERE `co_tipi_scadenze`.`id` = ' . prepare ( $id_record ) . ')' );
2019-06-21 13:04:46 +02:00
if ( isset ( $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 ;
}