1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-08 06:31:20 +01:00
openstamanager/modules/impianti/plugins/my_impianti.interventi.php

335 lines
14 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';
2023-10-27 15:09:49 +02:00
use Modules\Checklists\Check;
$matricole = (array) post('matricole');
2023-10-27 15:09:49 +02:00
$modulo_impianti = Modules::get('Impianti');
$checklist_module = Modules::get('Checklists');
// Salvo gli impianti selezionati
2020-08-17 16:25:01 +02:00
if (filter('op') == 'link_impianti') {
$matricole_old = $dbo->fetchArray('SELECT * FROM my_impianti_interventi WHERE idintervento = '.prepare($id_record));
$matricole_old = array_column($matricole_old, 'idimpianto');
// Individuazione delle matricole mancanti
foreach ($matricole_old as $matricola) {
if (!in_array($matricola, $matricole)) {
$dbo->query('DELETE FROM my_impianti_interventi WHERE idintervento='.prepare($id_record).' AND idimpianto = '.prepare($matricola));
2023-10-27 15:09:49 +02:00
Check::deleteLinked([
'id_module' => $id_module,
'id_record' => $id_record,
'id_module_from' => $modulo_impianti['id'],
2023-11-08 11:08:47 +01:00
'id_record_from' => $matricola,
2023-10-27 15:09:49 +02:00
]);
$components = $dbo->fetchArray('SELECT * FROM my_componenti WHERE id_impianto = '.prepare($matricola));
if (!empty($components)) {
foreach ($components as $component) {
$dbo->query('DELETE FROM my_componenti_interventi WHERE id_componente = '.prepare($component['id']).' AND id_intervento = '.prepare($id_record));
}
}
}
}
foreach ($matricole as $matricola) {
if (!in_array($matricola, $matricole_old)) {
$dbo->query('INSERT INTO my_impianti_interventi(idimpianto, idintervento) VALUES('.prepare($matricola).', '.prepare($id_record).')');
2023-10-27 15:09:49 +02:00
2023-11-08 11:08:47 +01:00
$checks_impianti = $dbo->fetchArray('SELECT * FROM zz_checks WHERE id_module = '.prepare($modulo_impianti['id']).' AND id_record = '.prepare($matricola));
2023-10-27 15:09:49 +02:00
foreach ($checks_impianti as $check_impianto) {
2023-10-30 13:12:24 +01:00
$id_parent_new = null;
if ($check_impianto['id_parent']) {
$parent = $dbo->selectOne('zz_checks', '*', ['id' => $check_impianto['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_impianto['content'], $id_parent_new, $check_impianto['is_titolo'], $check_impianto['order'], $modulo_impianti['id'], $matricola);
2023-10-27 15:09:49 +02:00
$check->id_module = $id_module;
2023-10-30 13:12:24 +01:00
$check->id_plugin = $id_plugin;
2023-10-27 15:09:49 +02:00
$check->note = $check_impianto['note'];
$check->save();
}
}
}
2018-07-19 17:29:21 +02:00
flash()->info(tr('Informazioni impianti salvate!'));
} elseif (filter('op') == 'link_componenti') {
2018-07-19 15:33:32 +02:00
$components = (array) post('componenti');
2019-05-17 19:17:12 +02:00
$id_impianto = post('id_impianto');
$dbo->query('DELETE FROM my_componenti_interventi WHERE id_componente IN (SELECT id FROM my_componenti WHERE id_impianto = '.prepare($id_impianto).') AND id_intervento = '.prepare($id_record));
foreach ($components as $component) {
2019-05-17 19:17:12 +02:00
$dbo->query('INSERT INTO my_componenti_interventi(id_componente, id_intervento) VALUES ('.prepare($component).', '.prepare($id_record).')');
}
2018-07-19 17:29:21 +02:00
flash()->info(tr('Informazioni componenti salvate!'));
}
2020-09-07 14:52:46 +02:00
// Blocco della modifica impianti se l'intervento è completato
$dati_intervento = $dbo->fetchArray('SELECT in_statiintervento.is_completato FROM in_statiintervento INNER JOIN in_interventi ON in_statiintervento.idstatointervento = in_interventi.idstatointervento WHERE in_interventi.id='.prepare($id_record));
$is_completato = $dati_intervento[0]['is_completato'];
2020-09-07 14:52:46 +02:00
if ($is_completato) {
$readonly = 'readonly';
$disabled = 'disabled';
} else {
$readonly = '';
$disabled = '';
}
2023-10-27 15:09:49 +02:00
/*
* Aggiunta impianti all'intervento
*/
// Elenco impianti collegati all'intervento
$impianti = $dbo->fetchArray('SELECT idimpianto FROM my_impianti_interventi WHERE idintervento='.prepare($id_record));
$impianti = !empty($impianti) ? array_column($impianti, 'idimpianto') : [];
2023-10-27 15:09:49 +02:00
// Elenco sedi
$sedi = $dbo->fetchArray('SELECT id, nomesede, citta FROM an_sedi WHERE idanagrafica='.prepare($record['idanagrafica'])." UNION SELECT 0, 'Sede legale', '' ORDER BY id");
echo '
2023-11-07 16:30:49 +01:00
<form action="'.base_path().'/editor.php?id_module='.$id_module.'&id_record='.$id_record.'&op=link_impianti" method="post">
<input type="hidden" name="backto" value="record-edit">
<div class="row">
<div class="col-md-12">
{[ "type": "select", "name": "matricole[]", "label": "'.tr('Impianti').'", "multiple": 1, "value": "'.implode(',', $impianti).'", "ajax-source": "impianti-cliente", "select-options": {"idanagrafica": '.$record['idanagrafica'].', "idsede_destinazione": '.($record['idsede_destinazione'] ?: '""').'}, "extra": "'.$readonly.'", "icon-after": "add|'.$modulo_impianti['id'].'|id_anagrafica='.$record['idanagrafica'].'" ]}
2023-10-27 15:09:49 +02:00
</div>
2023-11-07 16:30:49 +01:00
</div>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-success pull-right" '.$disabled.'><i class="fa fa-check"></i> '.tr('Salva impianti').'</button>
2023-10-27 15:09:49 +02:00
</div>
2023-11-07 16:30:49 +01:00
</div>
</form>
<br>';
2023-11-07 16:30:49 +01:00
if (!empty($impianti)) {
// IMPIANTI
echo '
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">'.tr('Impianti soggetti ad intervento').'</h3>
</div>
<div class="box-body">
<table class="table table-hover table-condensed table-striped">
<tr>
<th class="text-center" width="1%"></th>
<th class="text-center" width="10%">'.tr('Matricola').'</th>
<th class="text-center" width="20%">'.tr('Nome').'</th>
<th class="text-center" width="8%">'.tr('Data').'</th>
<th class="text-center">'.tr('Descrizione').'</th>
<th class="text-center" width="25%">'.tr("Componenti soggetti all'intervento").'</th>
<th class="text-center" width="5%">Checklist</th>
</tr>';
2023-11-08 11:08:47 +01:00
$impianti_collegati = $dbo->fetchArray('SELECT * FROM my_impianti_interventi INNER JOIN my_impianti ON my_impianti_interventi.idimpianto = my_impianti.id WHERE idintervento = '.prepare($id_record));
foreach ($impianti_collegati as $impianto) {
$checks = Check::where('id_module_from', $modulo_impianti['id'])->where('id_record_from', $impianto['id'])->where('id_module', $id_module)->where('id_record', $id_record)->where('id_parent', null)->get();
$type = 'muted';
$class = 'disabled';
$icon = 'circle-o';
$icon2 = 'remove';
if (sizeof($checks)) {
$class = '';
$icon = 'plus';
$checks_not_verified = $checks->where('checked_at', null)->count();
$type = $checks_not_verified ? 'warning' : 'success';
$icon2 = $checks_not_verified ? 'clock-o' : 'check';
}
echo '
2023-11-07 16:30:49 +01:00
<tr>
<td class="text-left">
<button type="button" class="btn btn-xs btn-default '.$class.'" onclick="toggleDettagli(this)">
<i class="fa fa-'.$icon.'"></i>
</button>
</td>
<td>'.$impianto['matricola'].'</td>
<td>'.Modules::link('Impianti', $impianto['id'], $impianto['nome']).'</td>
<td class="text-center">'.Translator::dateToLocale($impianto['data']).'</td>
<td>'.$impianto['descrizione'].'</td>
<td>
<form action="'.base_path().'/editor.php?id_module='.$id_module.'&id_record='.$id_record.'&op=link_componenti&matricola='.$impianto['id'].'" method="post">
<input type="hidden" name="backto" value="record-edit">
<input type="hidden" name="id_impianto" value="'.$impianto['id'].'">';
2023-11-08 11:08:47 +01:00
$inseriti = $dbo->fetchArray('SELECT * FROM my_componenti_interventi WHERE id_intervento = '.prepare($id_record));
$ids = array_column($inseriti, 'id_componente');
2023-11-07 16:30:49 +01:00
2023-11-08 11:08:47 +01:00
echo '
2023-11-07 16:30:49 +01:00
{[ "type": "select", "multiple": 1, "name": "componenti[]", "id": "componenti_'.$impianto['id'].'", "ajax-source": "componenti", "select-options": {"matricola": '.$impianto['id'].'}, "value": "'.implode(',', $ids).'", "readonly": "'.!empty($readonly).'", "disabled": "'.!empty($disabled).'", "icon-after": "<button type=\"submit\" class=\"btn btn-success\" '.$disabled.'> <i class=\"fa fa-check\"></i> '.tr('Salva').'</button>" ]}
</form>
</td>
<td class="text-center"><i class="fa fa-'.$icon2.' fa-2x text-'.$type.'"></i></td>
</tr>
<tr style="display: none">
<td colspan="7">
<table class="table">
<tbody class="sort check-impianto" data-sonof="0">';
2023-11-08 11:08:47 +01:00
foreach ($checks as $check) {
echo renderChecklist($check);
}
echo '
2023-11-07 16:30:49 +01:00
</tbody>
</table>
</td>
</tr>';
2023-11-08 11:08:47 +01:00
}
echo '
2023-11-07 16:30:49 +01:00
</table>
</div>
</div>';
} else {
2023-10-27 15:09:49 +02:00
echo '
2023-11-07 16:30:49 +01:00
<div class="alert alert-info text-center">
<i class="fa fa-info-circle"></i> '.tr('Nessun impianto collegato a questo intervento').'
</div>';
}
2023-10-27 15:09:49 +02:00
echo '
<script>
function toggleDettagli(trigger) {
const tr = $(trigger).closest("tr");
const dettagli = tr.next();
2023-10-27 15:09:49 +02:00
if (dettagli.css("display") === "none"){
dettagli.show(500);
$(trigger).children().removeClass("fa-plus");
$(trigger).children().addClass("fa-minus");
} else {
dettagli.hide(500);
$(trigger).children().removeClass("fa-minus");
$(trigger).children().addClass("fa-plus");
}
}
</script>';
echo '
2023-10-27 15:09:49 +02:00
<script>$(document).ready(init)</script>
2019-05-17 19:17:12 +02:00
2023-10-27 15:09:49 +02:00
<script>
2019-05-17 19:17:12 +02:00
2023-10-27 15:09:49 +02:00
$(document).ready(function(){
$("[data-toggle=\'tooltip\']").tooltip();
});
2023-10-27 15:09:49 +02:00
sortable("#tab_checks .sort", {
axis: "y",
handle: ".handle",
cursor: "move",
dropOnEmpty: true,
scroll: true,
});
sortable_table = sortable("#tab_checks .sort").length;
for(i=0; i<sortable_table; i++){
sortable("#tab_checks .sort")[i].addEventListener("sortupdate", function(e) {
var sonof = $(this).data("sonof");
let order = $(this).find(".sonof_"+sonof+"[data-id]").toArray().map(a => $(a).data("id"))
$.post("'.$checklist_module->fileurl('ajax.php').'", {
op: "update_position",
order: order.join(","),
});
});
}
$("textarea[name=\'note_checklist\']").keyup(function() {
$(this).parent().parent().parent().find(".save-nota").removeClass("btn-default");
$(this).parent().parent().parent().find(".save-nota").addClass("btn-success");
});
function saveNota(id) {
$.post("'.$checklist_module->fileurl('ajax.php').'", {
op: "save_note",
note: $("#note_" + id).val(),
id: id
}, function() {
alertPush();
$("#note_" + id).parent().parent().parent().find(".save-nota").removeClass("btn-success");
$("#note_" + id).parent().parent().parent().find(".save-nota").addClass("btn-default");
});
}
$(".check-impianto .checkbox").click(function(){
if($(this).is(":checked")){
$.post("'.$checklist_module->fileurl('ajax.php').'", {
op: "save_checkbox",
id: $(this).attr("data-id"),
},function(result){
});
$(this).parent().parent().find(".text").css("text-decoration", "line-through");
parent = $(this).attr("data-id");
$("tr.sonof_"+parent).find("input[type=checkbox]").each(function(){
if(!$(this).is(":checked")){
$(this).click();
}
});
$(this).parent().parent().find(".verificato").removeClass("hidden");
$(this).parent().parent().find(".verificato").text("'.tr('Verificato da _USER_ il _DATE_', [
'_USER_' => $user->username,
'_DATE_' => dateFormat(date('Y-m-d')).' '.date('H:i'),
]).'");
}else{
$.post("'.$checklist_module->fileurl('ajax.php').'", {
op: "remove_checkbox",
id: $(this).attr("data-id"),
},function(result){
});
$(this).parent().parent().find(".text").css("text-decoration", "none");
parent = $(this).attr("data-id");
$("tr.sonof_"+parent).find("input[type=checkbox]").each(function(){
if($(this).is(":checked")){
$(this).click();
}
});
$(this).parent().parent().find(".verificato").addClass("hidden");
}
})
function delete_check(id){
if(confirm("Eliminare questa checklist?")){
$.post("'.$checklist_module->fileurl('ajax.php').'", {
op: "delete_check",
id: id,
}, function(){
location.reload();
});
}
}
function edit_check(id){
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_record="+id, 1);
}
</script>';