1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-12 08:26:20 +01:00
openstamanager/modules/impianti/actions.php

226 lines
8.9 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-03-22 15:52:24 +01:00
use Modules\Checklists\Check;
2023-10-27 15:09:49 +02:00
$op = post('op');
2024-04-18 17:44:05 +02:00
$upload_dir = base_dir().'/files/'.Module::find((new Module())->getByField('title', 'Anagrafiche', Models\Locale::getPredefined()->id))->directory;
$id_modulo_categorie_impianti = (new Module())->getByField('title', 'Categorie impianti', Models\Locale::getPredefined()->id);
switch ($op) {
// Aggiorno informazioni di base impianto
case 'update':
$matricola = post('matricola');
if (!empty($matricola)) {
2018-07-10 16:39:02 +02:00
$dbo->update('my_impianti', [
'idanagrafica' => post('idanagrafica'),
'nome' => post('nome'),
'matricola' => $matricola,
2018-10-19 10:53:47 +02:00
'id_categoria' => post('id_categoria') ?: null,
'id_sottocategoria' => post('id_sottocategoria') ?: null,
'id_marca' => post('id_marca') ?: null,
'id_modello' => post('id_modello') ?: null,
2018-07-10 16:39:02 +02:00
'descrizione' => post('descrizione'),
'idsede' => post('idsede'),
2023-10-27 15:35:40 +02:00
'data' => post('data') ?: null,
2018-07-10 16:39:02 +02:00
'proprietario' => post('proprietario'),
'palazzo' => post('palazzo'),
'ubicazione' => post('ubicazione'),
'idtecnico' => post('idtecnico'),
'scala' => post('scala'),
'piano' => post('piano'),
'interno' => post('interno'),
'occupante' => post('occupante'),
], ['id' => $id_record]);
2018-07-19 17:29:21 +02:00
flash()->info(tr('Informazioni salvate correttamente!'));
// Upload file
if (!empty($_FILES) && !empty($_FILES['immagine']['name'])) {
$upload = Uploads::upload($_FILES['immagine'], [
2018-07-10 16:39:02 +02:00
'name' => 'Immagine',
'id_module' => $id_module,
'id_record' => $id_record,
], [
'thumbnails' => true,
]);
$filename = $upload->filename;
2018-07-10 16:39:02 +02:00
if (!empty($filename)) {
$dbo->update('my_impianti', [
'immagine' => $filename,
], [
'id' => $id_record,
]);
} else {
2018-07-19 17:29:21 +02:00
flash()->warning(tr('Errore durante il caricamento del file in _DIR_!', [
'_DIR_' => $upload_dir,
]));
}
}
// Eliminazione file
if (post('delete_immagine') !== null) {
2018-07-18 15:20:10 +02:00
Uploads::delete($record['immagine'], [
2018-07-10 16:39:02 +02:00
'id_module' => $id_module,
'id_record' => $id_record,
]);
$dbo->update('my_impianti', [
'immagine' => null,
], [
'id' => $id_record,
]);
}
}
break;
2024-01-15 15:30:45 +01:00
// Aggiungo impianto
case 'add':
$matricola = post('matricola');
$idanagrafica = post('idanagrafica');
$nome = post('nome');
$idtecnico = post('idtecnico');
2019-01-10 18:41:25 +01:00
$idsede = post('idsede');
2023-10-27 15:09:49 +02:00
$id_categoria = post('id_categoria');
$id_sottocategoria = post('id_sottocategoria');
if (!empty($matricola)) {
2023-10-30 13:12:24 +01:00
$dbo->insert('my_impianti', [
'matricola' => $matricola,
'idanagrafica' => $idanagrafica,
'nome' => $nome,
'data' => date('Y-m-d'),
'idtecnico' => $idtecnico ?: 0,
'idsede' => $idsede ?: 0,
2023-11-02 17:11:07 +01:00
'id_categoria' => $id_categoria ?: null,
'id_sottocategoria' => $id_sottocategoria ?: null,
2023-10-30 13:12:24 +01:00
]);
$id_record = $dbo->lastInsertedID();
2018-06-26 09:41:43 +02:00
2024-03-05 16:01:45 +01:00
$checks_categoria = $dbo->fetchArray('SELECT * FROM zz_checks WHERE id_module = '.prepare($id_modulo_categorie_impianti).' AND id_record = '.prepare($id_categoria));
2023-10-27 15:09:49 +02:00
foreach ($checks_categoria as $check_categoria) {
2023-10-30 13:12:24 +01:00
$id_parent_new = null;
if ($check_categoria['id_parent']) {
$parent = $dbo->selectOne('zz_checks', '*', ['id' => $check_categoria['id_parent']]);
$id_parent_new = $dbo->selectOne('zz_checks', '*', ['content' => $parent['content'], 'id_module' => $id_module, 'id_record' => $id_record])['id'];
}
$check = Check::build($user, $structure, $id_record, $check_categoria['content'], $id_parent_new, $check_categoria['is_titolo'], $check_categoria['order']);
2023-10-27 15:09:49 +02:00
$check->id_plugin = null;
$check->note = $check_categoria['note'];
$check->save();
}
if (isAjaxRequest()) {
2018-06-26 14:30:26 +02:00
echo json_encode(['id' => $id_record, 'text' => $matricola.' - '.$nome]);
}
2018-07-19 17:29:21 +02:00
flash()->info(tr('Aggiunto nuovo impianto!'));
}
break;
2024-01-15 15:30:45 +01:00
// Carica i campi da compilare del componente
case 'load_componente':
$filename = post('filename');
$idarticolo = post('idarticolo');
// Se è stato specificato un idarticolo, carico il file .ini dal campo `contenuto` di quell'idarticolo
2024-02-29 15:10:55 +01:00
$rs = $dbo->fetchArray('SELECT `contenuto`, `componente_filename` FROM `mg_articoli` WHERE `id`='.prepare($idarticolo));
// Se i campi da caricare sono del componente già salvato leggo dal campo `contenuto`...
if ($rs[0]['componente_filename'] == $filename) {
$contenuto = $rs[0]['contenuto'];
}
// ...altrimenti carico dal file .ini
elseif (file_exists(base_dir().'/files/impianti/'.$filename)) {
$contenuto = file_get_contents(base_dir().'/files/impianti/'.$filename);
}
crea_form_componente($contenuto);
break;
2024-01-15 15:30:45 +01:00
// Duplica impianto
2019-05-03 16:31:58 +02:00
case 'copy':
2024-01-15 15:51:24 +01:00
$database->beginTransaction();
2019-05-03 16:31:58 +02:00
$dbo->query('CREATE TEMPORARY TABLE tmp SELECT * FROM my_impianti WHERE id= '.prepare($id_record));
$dbo->query('ALTER TABLE tmp DROP id');
$dbo->query('INSERT INTO my_impianti SELECT NULL,tmp. * FROM tmp');
$id_record = $dbo->lastInsertedID();
2019-05-04 03:29:43 +02:00
$dbo->query('DROP TEMPORARY TABLE tmp');
2019-05-03 16:31:58 +02:00
2019-05-04 03:29:43 +02:00
$dbo->query('UPDATE my_impianti SET matricola = CONCAT (matricola, " (copia)") WHERE id = '.prepare($id_record));
2019-05-03 16:31:58 +02:00
flash()->info(tr('Impianto duplicato correttamente!'));
2019-05-04 03:29:43 +02:00
2019-05-03 16:31:58 +02:00
break;
2024-01-15 15:30:45 +01:00
// Rimuovo impianto e scollego tutti i suoi componenti
case 'delete':
$dbo->query('DELETE FROM my_impianti WHERE id='.prepare($id_record));
2018-07-19 17:29:21 +02:00
flash()->info(tr('Impianto e relativi componenti eliminati!'));
break;
2023-10-27 15:09:49 +02:00
case 'sync_checklist':
2023-10-27 15:23:06 +02:00
Check::deleteLinked([
'id_module' => $id_module,
'id_record' => $id_record,
]);
2023-10-27 15:09:49 +02:00
2024-03-05 16:01:45 +01:00
$checks_categoria = $dbo->fetchArray('SELECT * FROM zz_checks WHERE id_module = '.prepare($id_modulo_categorie_impianti).' AND id_record = '.prepare(post('id_categoria')));
2023-10-27 15:09:49 +02:00
foreach ($checks_categoria as $check_categoria) {
2023-10-30 13:12:24 +01:00
$id_parent_new = null;
2023-11-02 17:11:07 +01:00
if ($check_categoria['id_parent']) {
$parent = $dbo->selectOne('zz_checks', '*', ['id' => $check_categoria['id_parent']]);
$id_parent_new = $dbo->selectOne('zz_checks', '*', ['content' => $parent['content'], 'id_module' => $id_module, 'id_record' => $id_record])['id'];
}
2023-10-30 13:12:24 +01:00
$check = Check::build($user, $structure, $id_record, $check_categoria['content'], $id_parent_new, $check_categoria['is_titolo'], $check_categoria['order']);
2023-10-27 15:09:49 +02:00
$check->id_plugin = null;
$check->note = $check_categoria['note'];
$check->save();
}
flash()->info(tr('Checklist importate correttamente!'));
break;
}
2018-07-10 16:39:02 +02:00
// Operazioni aggiuntive per l'immagine
if (filter('op') == 'rimuovi-allegato' && filter('filename') == $record['immagine']) {
2018-07-10 16:39:02 +02:00
$dbo->update('my_impianti', [
'immagine' => null,
], [
'id' => $id_record,
]);
} elseif (filter('op') == 'aggiungi-allegato' && filter('nome_allegato') == 'Immagine') {
2018-07-10 16:39:02 +02:00
$dbo->update('my_impianti', [
'immagine' => $upload->filename,
2018-07-10 16:39:02 +02:00
], [
'id' => $id_record,
]);
}