Skip to content

Commit 2ffe4c8

Browse files
committed
feat: gestione caricamento documenti collegati via ajax
1 parent 40b10b1 commit 2ffe4c8

8 files changed

Lines changed: 1931 additions & 179 deletions

File tree

ajax_documenti_collegati.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
5+
* Copyright (C) DevCode s.r.l.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
use Common\DocumentiCollegati;
22+
use Models\Module;
23+
24+
// Pulisci l'output buffer prima di tutto
25+
while (ob_get_level() > 0) {
26+
ob_end_clean();
27+
}
28+
29+
include_once __DIR__.'/core.php';
30+
31+
// Abilita error reporting per debug
32+
error_reporting(E_ALL);
33+
ini_set('display_errors', 0);
34+
35+
// Verifica che l'id_record sia valido
36+
if (empty($id_record) || !is_numeric($id_record)) {
37+
$count_only = isset($_GET['count_only']) && $_GET['count_only'] == '1';
38+
39+
if ($count_only) {
40+
header('Content-Type: application/json');
41+
echo json_encode(['count' => 0, 'error' => 'ID record non valido']);
42+
} else {
43+
echo '<div class="alert alert-warning">'.tr('ID record non valido').'</div>';
44+
}
45+
exit;
46+
}
47+
48+
// Verifica che id_module sia presente
49+
if (empty($id_module)) {
50+
$count_only = isset($_GET['count_only']) && $_GET['count_only'] == '1';
51+
52+
if ($count_only) {
53+
header('Content-Type: application/json');
54+
echo json_encode(['count' => 0, 'error' => 'ID modulo non valido']);
55+
} else {
56+
echo '<div class="alert alert-warning">'.tr('ID modulo non valido').'</div>';
57+
}
58+
exit;
59+
}
60+
61+
// Verifica se è richiesto solo il conteggio
62+
$count_only = isset($_GET['count_only']) && $_GET['count_only'] == '1';
63+
64+
try {
65+
// Recupera informazioni sul modulo corrente
66+
$module = Module::find($id_module);
67+
if (empty($module)) {
68+
if ($count_only) {
69+
header('Content-Type: application/json');
70+
echo json_encode(['count' => 0, 'error' => 'Modulo non trovato']);
71+
} else {
72+
echo '<div class="alert alert-warning">'.tr('Modulo non trovato').'</div>';
73+
}
74+
exit;
75+
}
76+
77+
$module_name = $module->name;
78+
79+
// Determina il tipo di record in base al nome del modulo
80+
$tipo_record = 'intervento'; // Default
81+
82+
switch ($module_name) {
83+
case 'Interventi':
84+
$tipo_record = 'intervento';
85+
break;
86+
case 'Fatture di vendita':
87+
$tipo_record = 'fattura_vendita';
88+
break;
89+
case 'Fatture di acquisto':
90+
$tipo_record = 'fattura_acquisto';
91+
break;
92+
case 'Contratti':
93+
$tipo_record = 'contratto';
94+
break;
95+
case 'Preventivi':
96+
$tipo_record = 'preventivo';
97+
break;
98+
case 'Ordini cliente':
99+
case 'Ordini fornitore':
100+
$tipo_record = 'ordine';
101+
break;
102+
case 'Ddt in entrata':
103+
case 'Ddt in uscita':
104+
$tipo_record = 'ddt';
105+
break;
106+
default:
107+
// Per altri moduli, usa il default
108+
$tipo_record = 'intervento';
109+
break;
110+
}
111+
112+
// Gestisci la richiesta AJAX
113+
DocumentiCollegati::handleAjaxRequest($id_record, $tipo_record, $count_only);
114+
} catch (Exception $e) {
115+
// Gestione errori generici
116+
if ($count_only) {
117+
header('Content-Type: application/json');
118+
echo json_encode(['count' => 0, 'error' => 'Errore: '.$e->getMessage()]);
119+
} else {
120+
echo '<div class="alert alert-danger">'.tr('Errore nel caricamento dei documenti collegati').': '.$e->getMessage().'</div>';
121+
}
122+
exit;
123+
}

modules/contratti/edit.php

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -587,38 +587,86 @@ function aggiungiRigaOre(idTipoIntervento, titoloTipo) {
587587

588588
// Collegamenti diretti
589589
// Fatture o interventi collegati a questo contratto
590-
if (!empty($elementi)) {
591-
echo '
592-
<div class="card card-warning collapsable collapsed-card">
590+
echo '
591+
<div class="card card-warning collapsable collapsed-card" id="documenti-collegati-card">
593592
<div class="card-header with-border">
594-
<h3 class="card-title"><i class="fa fa-warning"></i> '.tr('Documenti collegati: _NUM_', [
595-
'_NUM_' => count($elementi),
596-
]).'</h3>
593+
<h3 class="card-title"><i class="fa fa-warning"></i> <span id="documenti-collegati-title">'.tr('Documenti collegati').'</span></h3>
597594
<div class="card-tools pull-right">
598-
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fa fa-plus"></i></button>
595+
<button type="button" class="btn btn-tool" data-card-widget="collapse" id="documenti-collegati-toggle"><i class="fa fa-plus"></i></button>
596+
</div>
597+
</div>
598+
<div class="card-body" id="documenti-collegati-body">
599+
<div class="text-center" id="documenti-collegati-loading">
600+
<i class="fa fa-spinner fa-spin"></i> '.tr('Caricamento documenti collegati in corso').'
599601
</div>
602+
<div id="documenti-collegati-content" style="display: none;"></div>
600603
</div>
601-
<div class="card-body">
602-
<ul>';
603-
604-
// Elenco attività o contratti collegati
605-
foreach ($elementi as $elemento) {
606-
$descrizione = tr('_DOC_ num. _NUM_ del _DATE_ _STATO_', [
607-
'_DOC_' => $elemento['tipo_documento'],
608-
'_NUM_' => !empty($elemento['numero_esterno']) ? $elemento['numero_esterno'] : $elemento['numero'],
609-
'_DATE_' => Translator::dateToLocale($elemento['data']),
610-
'_STATO_' => (!empty($elemento['stato_documento']) ? '('.$elemento['stato_documento'].')' : ''),
611-
]);
604+
</div>
612605
613-
echo '
614-
<li>'.Modules::link($elemento['modulo'], $elemento['id'], $descrizione).'</li>';
606+
<script type="text/javascript">
607+
// Funzioni per i documenti collegati
608+
var documentiCaricati = false;
609+
610+
function caricaConteggioDocumenti() {
611+
$.get(globals.rootdir + "/ajax_documenti_collegati.php", {
612+
id_module: globals.id_module,
613+
id_record: globals.id_record,
614+
count_only: 1
615+
})
616+
.done(function(data) {
617+
var title = $("#documenti-collegati-title");
618+
if (data.count > 0) {
619+
title.html("'.tr('Documenti collegati').' (" + data.count + ")");
620+
} else {
621+
title.html("'.tr('Documenti collegati').'");
622+
}
623+
})
624+
.fail(function() {
625+
var title = $("#documenti-collegati-title");
626+
title.html("'.tr('Documenti collegati').'");
627+
});
615628
}
616629
617-
echo '
618-
</ul>
619-
</div>
620-
</div>';
621-
}
630+
function caricaDocumentiCollegati() {
631+
$("#documenti-collegati-loading").show();
632+
$("#documenti-collegati-content").hide();
633+
634+
$.get(globals.rootdir + "/ajax_documenti_collegati.php", {
635+
id_module: globals.id_module,
636+
id_record: globals.id_record
637+
})
638+
.done(function(data) {
639+
$("#documenti-collegati-loading").hide();
640+
$("#documenti-collegati-content").html(data).show();
641+
documentiCaricati = true;
642+
})
643+
.fail(function() {
644+
$("#documenti-collegati-loading").hide();
645+
$("#documenti-collegati-content").html("<div class=\"alert alert-danger\">'.tr('Errore durante il caricamento dei documenti collegati').'</div>").show();
646+
});
647+
}
648+
649+
$(document).ready(function() {
650+
// Carica il conteggio dei documenti collegati
651+
caricaConteggioDocumenti();
652+
653+
// Carica i documenti quando la card viene espansa
654+
$("#documenti-collegati-card").on("expanded.lte.cardwidget", function() {
655+
if (!documentiCaricati) {
656+
caricaDocumentiCollegati();
657+
}
658+
});
659+
660+
// Aggiorna l\'icona quando la card viene espansa/collassata
661+
$("#documenti-collegati-card").on("expanded.lte.cardwidget", function() {
662+
$("#documenti-collegati-toggle i").removeClass("fa-plus").addClass("fa-minus");
663+
});
664+
665+
$("#documenti-collegati-card").on("collapsed.lte.cardwidget", function() {
666+
$("#documenti-collegati-toggle i").removeClass("fa-minus").addClass("fa-plus");
667+
});
668+
});
669+
</script>';
622670

623671
if (!empty($elementi)) {
624672
echo '

modules/ddt/edit.php

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,25 @@
493493
494494
{( "name": "log_email", "id_module": "$id_module$", "id_record": "$id_record$" )}
495495
496-
{( "name": "log_sms", "id_module": "$id_module$", "id_record": "$id_record$" )}
496+
{( "name": "log_sms", "id_module": "$id_module$", "id_record": "$id_record$" )}';
497497

498+
?>
499+
<div class="card card-warning collapsable collapsed-card" id="documenti-collegati-card">
500+
<div class="card-header with-border">
501+
<h3 class="card-title"><i class="fa fa-warning"></i> <span id="documenti-collegati-title"><?php echo tr('Documenti collegati') ?></span></h3>
502+
<div class="card-tools pull-right">
503+
<button type="button" class="btn btn-tool" data-card-widget="collapse" id="documenti-collegati-toggle"><i class="fa fa-plus"></i></button>
504+
</div>
505+
</div>
506+
<div class="card-body" id="documenti-collegati-body">
507+
<div class="text-center" id="documenti-collegati-loading">
508+
<i class="fa fa-spinner fa-spin"></i> <?php echo tr('Caricamento documenti collegati in corso') ?>
509+
</div>
510+
<div id="documenti-collegati-content" style="display: none;"></div>
511+
</div>
512+
</div>
513+
<?php
514+
echo'
498515
<script>
499516
async function saveForm() {
500517
// Salvataggio via AJAX
@@ -630,41 +647,71 @@ function caricaRighe(id_riga) {
630647
return false;
631648
}
632649
});
633-
</script>';
634650
635-
// Collegamenti diretti
636-
// Fatture collegate a questo ddt
637-
if (!empty($elementi)) {
638-
echo '
639-
<div class="card card-warning collapsable collapsed-card">
640-
<div class="card-header with-border">
641-
<h3 class="card-title"><i class="fa fa-warning"></i> '.tr('Documenti collegati: _NUM_', [
642-
'_NUM_' => count($elementi),
643-
]).'</h3>
644-
<div class="card-tools pull-right">
645-
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fa fa-plus"></i></button>
646-
</div>
647-
</div>
648-
<div class="card-body">
649-
<ul>';
651+
// Funzioni per i documenti collegati
652+
var documentiCaricati = false;
650653
651-
foreach ($elementi as $elemento) {
652-
$descrizione = tr('_DOC_ num. _NUM_ del _DATE_ _STATO_', [
653-
'_DOC_' => $elemento['tipo_documento'],
654-
'_NUM_' => !empty($elemento['numero_esterno']) ? $elemento['numero_esterno'] : $elemento['numero'],
655-
'_DATE_' => Translator::dateToLocale($elemento['data']),
656-
'_STATO_' => (!empty($elemento['stato_documento']) ? '('.$elemento['stato_documento'].')' : ''),
657-
]);
654+
function caricaConteggioDocumenti() {
655+
$.get(globals.rootdir + "/ajax_documenti_collegati.php", {
656+
id_module: globals.id_module,
657+
id_record: globals.id_record,
658+
count_only: 1
659+
})
660+
.done(function(data) {
661+
var title = $("#documenti-collegati-title");
662+
if (data.count > 0) {
663+
title.html("'.tr('Documenti collegati').' (" + data.count + ")");
664+
} else {
665+
title.html("'.tr('Documenti collegati').'");
666+
}
667+
})
668+
.fail(function() {
669+
var title = $("#documenti-collegati-title");
670+
title.html("'.tr('Documenti collegati').'");
671+
});
672+
}
658673
659-
echo '
660-
<li>'.Modules::link($elemento['modulo'], $elemento['id'], $descrizione).'</li>';
674+
function caricaDocumentiCollegati() {
675+
$("#documenti-collegati-loading").show();
676+
$("#documenti-collegati-content").hide();
677+
678+
$.get(globals.rootdir + "/ajax_documenti_collegati.php", {
679+
id_module: globals.id_module,
680+
id_record: globals.id_record
681+
})
682+
.done(function(data) {
683+
$("#documenti-collegati-loading").hide();
684+
$("#documenti-collegati-content").html(data).show();
685+
documentiCaricati = true;
686+
})
687+
.fail(function() {
688+
$("#documenti-collegati-loading").hide();
689+
$("#documenti-collegati-content").html("<div class=\"alert alert-danger\">'.tr('Errore durante il caricamento dei documenti collegati').'</div>").show();
690+
});
661691
}
662692
663-
echo '
664-
</ul>
665-
</div>
666-
</div>';
667-
}
693+
$(document).ready(function() {
694+
// Carica il conteggio dei documenti collegati
695+
caricaConteggioDocumenti();
696+
697+
// Carica i documenti quando la card viene espansa
698+
$("#documenti-collegati-card").on("expanded.lte.cardwidget", function() {
699+
if (!documentiCaricati) {
700+
caricaDocumentiCollegati();
701+
}
702+
});
703+
704+
// Aggiorna l\'icona quando la card viene espansa/collassata
705+
$("#documenti-collegati-card").on("expanded.lte.cardwidget", function() {
706+
$("#documenti-collegati-toggle i").removeClass("fa-plus").addClass("fa-minus");
707+
});
708+
709+
$("#documenti-collegati-card").on("collapsed.lte.cardwidget", function() {
710+
$("#documenti-collegati-toggle i").removeClass("fa-minus").addClass("fa-plus");
711+
});
712+
});
713+
</script>';
714+
668715

669716
if (!empty($elementi)) {
670717
echo '

0 commit comments

Comments
 (0)