2424
2525function renderRiga ($ id , $ riga , $ totale_dare = null , $ totale_avere = null )
2626{
27+ global $ id_record ;
28+
29+ // Determina se siamo nell'add (non c'è $id_record) o nell'edit
30+ $ suffix = empty ($ id_record ) ? '_add ' : '' ;
31+
2732 // Conto
2833 echo '
2934 <tr>
30- <input type="hidden" name="id_documento[ ' .$ id .']" value=" ' .$ riga ['iddocumento ' ].'">
31- <input type="hidden" name="id_scadenza[ ' .$ id .']" value=" ' .$ riga ['id_scadenza ' ].'">
35+ <input type="hidden" name="id_documento ' . $ suffix . ' [ ' .$ id .']" value=" ' .$ riga ['iddocumento ' ].'">
36+ <input type="hidden" name="id_scadenza ' . $ suffix . ' [ ' .$ id .']" value=" ' .$ riga ['id_scadenza ' ].'">
3237
3338 <td class="text-center" style="width:40px;">
3439 <button type="button" class="btn btn-danger btn-xs" onclick="deleteRiga(this)">
@@ -37,19 +42,19 @@ function renderRiga($id, $riga, $totale_dare = null, $totale_avere = null)
3742 </td>
3843
3944 <td>
40- {[ "type": "select", "name": "idconto[ ' .$ id .']", "id": "conto ' .$ id .'", "value": " ' .($ riga ['id_conto ' ] ?: '' ).'", "ajax-source": "conti", "icon-after": ' .json_encode ('<button type="button" onclick="visualizzaMovimenti(this)" class="btn btn-info ' .($ riga ['id_conto ' ] ? '' : 'disabled ' ).'"><i class="fa fa-eye"></i></button> ' ).' ]}
45+ {[ "type": "select", "name": "idconto ' . $ suffix . ' [ ' .$ id .']", "id": "conto ' . $ suffix . ' _ ' .$ id .'", "value": " ' .($ riga ['id_conto ' ] ?: '' ).'", "ajax-source": "conti", "icon-after": ' .json_encode ('<button type="button" onclick="visualizzaMovimenti(this)" class="btn btn-info ' .($ riga ['id_conto ' ] ? '' : 'disabled ' ).'"><i class="fa fa-eye"></i></button> ' ).' ]}
4146 </td> ' ;
4247
4348 // Dare
4449 echo '
4550 <td>
46- {[ "type": "number", "name": "dare[ ' .$ id .']", "id": "dare ' .$ id .'", "value": " ' .($ riga ['dare ' ] ?: 0 ).'" ]}
51+ {[ "type": "number", "name": "dare ' . $ suffix . ' [ ' .$ id .']", "id": "dare ' . $ suffix . ' _ ' .$ id .'", "value": " ' .($ riga ['dare ' ] ?: 0 ).'" ]}
4752 </td> ' ;
4853
4954 // Avere
5055 echo '
5156 <td>
52- {[ "type": "number", "name": "avere[ ' .$ id .']", "id": "avere ' .$ id .'", "value": " ' .($ riga ['avere ' ] ?: 0 ).'" ]}
57+ {[ "type": "number", "name": "avere ' . $ suffix . ' [ ' .$ id .']", "id": "avere ' . $ suffix . ' _ ' .$ id .'", "value": " ' .($ riga ['avere ' ] ?: 0 ).'" ]}
5358 </td>
5459 </tr> ' ;
5560
@@ -197,12 +202,13 @@ function renderTabella($nome, $righe, $totale_dare = null, $totale_avere = null)
197202</table> ' ;
198203
199204// Nuova riga
205+ $ suffix = empty ($ id_record ) ? '_add ' : '' ;
200206echo '
201207<table class="table table-bordered">
202208 <tr>
203209 <th class="text-right"> ' .tr ('Totale ' ).'</th>
204- <th id="totale_dare" class="text-right" width="20%"> ' .moneyFormat ($ totale_dare ).'</th>
205- <th id="totale_avere" class="text-right" width="20%"> ' .moneyFormat ($ totale_avere ).'</th>
210+ <th id="totale_dare ' . $ suffix . ' " class="text-right" width="20%"> ' .moneyFormat ($ totale_dare ).'</th>
211+ <th id="totale_avere ' . $ suffix . ' " class="text-right" width="20%"> ' .moneyFormat ($ totale_avere ).'</th>
206212 </tr>
207213</table> ' ;
208214
@@ -334,7 +340,7 @@ function calcolaBilancio(gruppo) {
334340 // Trigger dell \'evento keyup() per la prima volta, per eseguire i dovuti controlli nel caso siano predisposte delle righe in prima nota
335341 $("input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
336342
337- $("select[id*=idconto ]").click(function() {
343+ $("select[id*=conto ]").click(function() {
338344 $("input[id*=dare][value!= \'\'], input[id*=avere][value!= \'\']").keyup();
339345 });
340346});
@@ -379,20 +385,48 @@ function calcolaBilancio(gruppo) {
379385 }
380386});
381387
382- $(document).on("change", "[id*=dare], [id*=avere]", function() {
388+ // Funzione per aggiornare i totali dell \'edit
389+ function aggiornaTotaliEdit() {
383390 var totalDare = 0;
384391 var totalAvere = 0;
385392
386- $("[id*=dare]").each(function() {
393+ $("[id*=dare]:not([id*=_add_]) ").each(function() {
387394 totalDare += parseFloat($(this).val().toEnglish()) || 0;
388395 });
389396
390- $("[id*=avere]").each(function() {
397+ $("[id*=avere]:not([id*=_add_]) ").each(function() {
391398 totalAvere += parseFloat($(this).val().toEnglish()) || 0;
392399 });
393400
394401 $("#totale_dare").text(totalDare.toLocale());
395402 $("#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());
420+ }
421+
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();
396430});
397431
398432function visualizzaMovimenti(button) {
0 commit comments