openstamanager/modules/tipi_scadenze/actions.php

134 lines
5.8 KiB
PHP
Raw Normal View History

<?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/>.
*/
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;
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-04-19 16:44:08 +02:00
$nome_new = Tipo::where('id', '=', (new Tipo())->getByField('title', $descrizione))->where('id', '!=', $id_record)->first();
$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.'));
2024-04-18 17:44:05 +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((new Module())->getByField('title', 'Scadenzario', Models\Locale::getPredefined()->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
$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,
], [
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,
]);
flash()->info(tr('Salvataggio completato!'));
} else {
flash()->error(tr("E' già presente una tipologia di _TYPE_ con nome: _NOME_", [
'_TYPE_' => 'scadenza',
'_NOME_' => $nome,
]));
}
} else {
flash()->error(tr('Ci sono stati alcuni errori durante il salvataggio'));
}
break;
case 'add':
$descrizione = filter('descrizione');
$nome = filter('nome');
if (isset($nome)) {
2024-01-15 15:30:45 +01:00
// Se non esiste già un tipo di scadenza con lo stesso nome
2024-04-19 16:44:08 +02:00
if (empty(Tipo::where('id', '=', (new Tipo())->getByField('title', $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
$id_record = $dbo->lastInsertedID();
2024-04-19 16:44:08 +02:00
$tipo->setTranslation('title', $descrizione);
$tipo->save();
2024-01-15 15:30:45 +01:00
// Aggiungo anche il segmento
$dbo->insert('zz_segments', [
2024-04-18 17:44:05 +02:00
'id_module' => (new Module())->getByField('title', 'Scadenzario', Models\Locale::getPredefined()->id),
'clause' => 'co_scadenziario.tipo="'.$nome.'"',
'position' => 'WHR',
]);
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
]);
if (isAjaxRequest()) {
echo json_encode(['id' => $id_record, 'text' => $descrizione]);
}
flash()->info(tr('Aggiunta nuova tipologia di _TYPE_', [
'_TYPE_' => 'scadenza',
]));
} else {
flash()->error(tr("E' già presente una tipologia di _TYPE_ con nome: _NOME_", [
'_TYPE_' => 'scadenza',
'_NOME_' => $nome,
]));
}
} 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).')');
2024-04-12 16:59:19 +02:00
if ((!empty($id_record)) && empty($documenti)) {
$dbo->query('DELETE FROM `co_tipi_scadenze` WHERE `can_delete` = 1 AND `id`='.prepare($id_record));
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;
}