@@ -183,10 +183,18 @@ function renderTabella($nome, $righe, $totale_dare = null, $totale_avere = null)
183183
184184renderTabella ($ nome , $ movimenti_generali , $ totale_dare , $ totale_avere );
185185
186- // Nuova riga
186+ // Somma i totali dei movimenti generali
187+ foreach ($ movimenti_generali as $ riga ) {
188+ $ totale_dare += $ riga ['dare ' ] ?? 0 ;
189+ $ totale_avere += $ riga ['avere ' ] ?? 0 ;
190+ }
191+
192+ // Suffisso per distinguere add da edit
193+ $ suffix = empty ($ id_record ) ? '_add ' : '' ;
194+
187195echo '
188196<table class="hide">
189- <tbody id="template"> ' ;
197+ <tbody id="template ' . $ suffix . ' "> ' ;
190198
191199renderRiga ('-id- ' ,
192200 [
@@ -199,80 +207,61 @@ function renderTabella($nome, $righe, $totale_dare = null, $totale_avere = null)
199207
200208echo '
201209 </tbody>
202- </table> ' ;
210+ </table>
203211
204- // Nuova riga
205- $ suffix = empty ($ id_record ) ? '_add ' : '' ;
206- echo '
207212<table class="table table-bordered">
208213 <tr>
209214 <th class="text-right"> ' .tr ('Totale ' ).'</th>
210215 <th id="totale_dare ' .$ suffix .'" class="text-right" width="20%"> ' .moneyFormat ($ totale_dare ).'</th>
211216 <th id="totale_avere ' .$ suffix .'" class="text-right" width="20%"> ' .moneyFormat ($ totale_avere ).'</th>
212217 </tr>
213- </table> ' ;
218+ </table>
214219
215- echo '
216220<script>
217221var formatted_zero = " ' .numberFormat (0 ).'";
218222var n = ' .$ counter .';
219223
220224function addRiga(btn) {
221225 var raggruppamento = $(btn).parent();
226+ var isInModal = $(btn).closest("#modals").length > 0;
222227 cleanup_inputs();
223228
224229 var tabella = raggruppamento.find("tbody");
225- var content = $("#template").html();
226- content = content.replace("-id_scadenza-", raggruppamento.data("id_scadenza"))
230+ var templateId = isInModal ? "#template_add" : "#template";
231+ var content = $(templateId).html()
232+ .replace("-id_scadenza-", raggruppamento.data("id_scadenza"))
227233 .replace("-id_documento-", raggruppamento.data("id_documento"));
228234
229- var text = replaceAll(content, "-id-", "" + n);
230- tabella.append(text);
231-
235+ tabella.append(replaceAll(content, "-id-", "" + n));
232236 restart_inputs();
233237 n++;
234238}
235239
236240/**
237- * Funzione per controllare lo stato dei conti della prima nota.
238- *
239- * @returns {boolean}
241+ * Controlla lo stato dei conti della prima nota e abilita/disabilita i bottoni di submit.
240242*/
241- function controllaConti() {
243+ function controllaConti(element ) {
242244 let continuare = true;
245+ let isInModal = element ? $(element).closest("#modals").length > 0 : $("#modals > div").length > 0;
246+ let container = isInModal ? $("#modals > div") : $(document);
243247
244- // Controlli sullo stato dei raggruppamenti
245- $(".raggruppamento_primanota").each(function() {
246- let bilancio = calcolaBilancio(this);
247-
248- continuare &= bilancio === 0;
248+ // Controlli sullo stato dei raggruppamenti nel container corrente
249+ container.find(".raggruppamento_primanota").each(function() {
250+ continuare &= calcolaBilancio(this) === 0;
249251 });
250252
251- // Blocco degli input con valore non impostato
252- $("input[id*=dare], input[id*=avere]").each(function() {
253- let conto_relativo = $(this).parent().parent().parent().parent().find("select").val();
254-
255- if (!conto_relativo) {
256- $(this).prop("disabled", true);
257- }
258-
259- if ($(this).val().toEnglish()){
260- continuare &= !!conto_relativo;
261- }
253+ // Blocco degli input senza conto selezionato
254+ container.find("input[id*=dare], input[id*=avere]").each(function() {
255+ let conto = $(this).closest("tr").find("select").val();
256+ if (!conto) $(this).prop("disabled", true);
257+ if ($(this).val().toEnglish()) continuare &= !!conto;
262258 });
263259
264- if (continuare) {
265- $("#add-submit").prop("disabled", false);
266- $("#modello-button").prop("disabled", false);
267- if (!$("#modals > div").get().length) {
268- $("#save, #save-close").removeAttr("disabled").removeClass("disabled");
269- }
260+ // Gestione bottoni submit
261+ if (isInModal) {
262+ $("#modals > div #add-submit, #modals > div #modello-button").prop("disabled", !continuare);
270263 } else {
271- $("#add-submit").prop("disabled", true);
272- $("#modello-button").prop("disabled", true);
273- if (!$("#modals > div").get().length) {
274- $("#save, #save-close").attr("disabled", "true").addClass("disabled");
275- }
264+ $("#save, #save-close").prop("disabled", !continuare).toggleClass("disabled", !continuare);
276265 }
277266
278267 return continuare;
@@ -328,123 +317,85 @@ function calcolaBilancio(gruppo) {
328317$(document).ready(function() {
329318 controllaConti();
330319
331- // Fix per l \' inizializzazione degli input
320+ // Inizializzazione input: disabilita quelli a zero
332321 $("input[id*=dare], input[id*=avere]").each(function() {
333- if (input(this).get() === 0) {
334- $(this).prop("disabled", true);
335- } else {
336- $(this).prop("disabled", false);
337- }
322+ $(this).prop("disabled", input(this).get() === 0);
338323 });
339324
340- // Trigger dell \' evento keyup() per la prima volta, per eseguire i dovuti controlli nel caso siano predisposte delle righe in prima nota
325+ // Trigger iniziale per controlli
341326 $("input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
342327
343328 $("select[id*=conto]").click(function() {
344- $("input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
329+ $(this).closest(".raggruppamento_primanota").find( "input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
345330 });
346331});
347332
348- $(document).on("change", "select", function() {
349- let row = $(this).parent().parent().parent().parent( );
333+ $(document).on("change", "select[id*=conto] ", function() {
334+ let row = $(this).closest("tr" );
350335
351336 if (row.find("input[disabled]").length > 1) {
352337 row.find("input").prop("disabled", !$(this).val());
353338 }
354339
355- // Trigger dell \'evento keyup() per la prima volta, per eseguire i dovuti controlli nel caso siano predisposte delle righe in prima nota
356- $("input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
357-
358- controllaConti();
340+ $(this).closest(".raggruppamento_primanota").find("input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
341+ controllaConti(this);
359342
360- let button = $(this).parent().parent().find("button");
361- if ($(this).val()) {
362- button.removeClass("disabled");
363- } else {
364- button.addClass("disabled");
365- }
343+ $(this).closest("td").find("button").toggleClass("disabled", !$(this).val());
366344});
367345
368- $(document).on("keyup change", "input[id*=dare]", function() {
369- let row = $(this).parent().parent().parent();
346+ // Handler unificato per input dare/avere - disabilita il campo opposto e aggiorna totali
347+ $(document).on("keyup change", "input[id*=dare], input[id*=avere]", function() {
348+ if ($(this).prop("disabled")) return;
370349
371- if (!$(this).prop("disabled")) {
372- row.find("input[id*=avere]").prop("disabled", $(this).val().toEnglish());
373-
374- controllaConti();
375- }
376- });
377-
378- $(document).on("keyup change", "input[id*=avere]", function() {
379350 let row = $(this).parent().parent().parent();
351+ let isDare = this.id.includes("dare");
352+ let oppositeField = isDare ? "input[id*=avere]" : "input[id*=dare]";
380353
381- if (!$(this).prop("disabled")) {
382- row.find("input[id*=dare]").prop("disabled", $(this).val().toEnglish());
383-
384- controllaConti();
385- }
354+ row.find(oppositeField).prop("disabled", $(this).val().toEnglish());
355+ controllaConti(this);
356+ aggiornaTotali(this);
386357});
387358
388- // Funzione per aggiornare i totali dell \'edit
389- function aggiornaTotaliEdit() {
390- var totalDare = 0;
391- var totalAvere = 0;
392-
393- $("[id*=dare]:not([id*=_add_])").each(function() {
394- totalDare += parseFloat($(this).val().toEnglish()) || 0;
395- });
396-
397- $("[id*=avere]:not([id*=_add_])").each(function() {
398- totalAvere += parseFloat($(this).val().toEnglish()) || 0;
399- });
400-
401- $("#totale_dare").text(totalDare.toLocale());
402- $("#totale_avere").text(totalAvere.toLocale());
403- }
404-
405- // Funzione per aggiornare i totali dell \'add
406- function aggiornaTotaliAdd() {
407- var totalDare = 0;
408- var totalAvere = 0;
409-
410- $("[id*=dare_add_]").each(function() {
411- totalDare += parseFloat($(this).val().toEnglish()) || 0;
412- });
413-
414- $("[id*=avere_add_]").each(function() {
415- totalAvere += parseFloat($(this).val().toEnglish()) || 0;
416- });
417-
418- $("#totale_dare_add").text(totalDare.toLocale());
419- $("#totale_avere_add").text(totalAvere.toLocale());
359+ // Funzione unificata per aggiornare i totali in base al contesto
360+ function aggiornaTotali(element) {
361+ var isInModal = $(element).closest("#modals").length > 0;
362+ var totalDare = 0;
363+ var totalAvere = 0;
364+
365+ if (isInModal) {
366+ $("#modals [id*=dare_add_]").each(function() {
367+ totalDare += parseFloat($(this).val().toEnglish()) || 0;
368+ });
369+ $("#modals [id*=avere_add_]").each(function() {
370+ totalAvere += parseFloat($(this).val().toEnglish()) || 0;
371+ });
372+ $("#modals #totale_dare_add").text(totalDare.toLocale());
373+ $("#modals #totale_avere_add").text(totalAvere.toLocale());
374+ } else {
375+ $("[id*=dare]:not([id*=_add_])").not("#modals *").each(function() {
376+ totalDare += parseFloat($(this).val().toEnglish()) || 0;
377+ });
378+ $("[id*=avere]:not([id*=_add_])").not("#modals *").each(function() {
379+ totalAvere += parseFloat($(this).val().toEnglish()) || 0;
380+ });
381+ $("#totale_dare").text(totalDare.toLocale());
382+ $("#totale_avere").text(totalAvere.toLocale());
383+ }
420384}
421385
422- // Event handler per i campi dell \'edit
423- $(document).on("change", "[id*=dare]:not([id*=_add_]), [id*=avere]:not([id*=_add_])", function() {
424- aggiornaTotaliEdit();
425- });
426-
427- // Event handler per i campi dell \'add
428- $(document).on("change", "[id*=dare_add_], [id*=avere_add_]", function() {
429- aggiornaTotaliAdd();
430- });
431-
432386function visualizzaMovimenti(button) {
433387 let id_conto = $(button).parent().parent().parent().find("select").val();
434388 openModal(" ' .tr ('Ultimi 25 movimenti ' ).'", " ' .$ module ->fileurl ('dettagli.php ' ).'?id_module=" + globals.id_module + "&id_conto=" + id_conto);
435389}
436390
437391function deleteRiga(button) {
438- // Get the row
439392 let row = $(button).closest("tr");
393+ let isInModal = $(button).closest("#modals").length > 0;
440394
441- // Remove the row
442395 row.remove();
396+ controllaConti(button);
443397
444- // Recalculate totals and check balance
445- controllaConti();
446-
447- // Trigger change event to update totals
448- $("[id*=dare], [id*=avere]").trigger("change");
398+ // Aggiorna i totali usando un elemento fittizio per determinare il contesto
399+ aggiornaTotali(isInModal ? $("#modals [id*=dare]").first() : $("[id*=dare]:not([id*=_add_])").first());
449400}
450401</script> ' ;
0 commit comments