2019-07-31 18:22:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include_once __DIR__.'/../../core.php';
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
use Modules\Fatture\Fattura;
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
function renderRiga($id, $riga)
|
|
|
|
{
|
2019-07-31 18:22:35 +02:00
|
|
|
// Conto
|
|
|
|
echo '
|
2019-11-22 17:15:17 +01:00
|
|
|
<tr>
|
|
|
|
<input type="hidden" name="id_scadenza['.$id.']" value="'.$riga['id_scadenza'].'">
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
<td>
|
|
|
|
{[ "type": "select", "name": "idconto['.$id.']", "id": "conto'.$id.'", "value": "'.($riga['id_conto'] ?: '').'", "ajax-source": "conti" ]}
|
|
|
|
</td>';
|
2019-07-31 18:22:35 +02:00
|
|
|
|
|
|
|
// Dare
|
|
|
|
echo '
|
2019-11-22 17:15:17 +01:00
|
|
|
<td>
|
|
|
|
{[ "type": "number", "name": "dare['.$id.']", "id": "dare'.$id.'", "value": "'.($riga['dare'] ?: 0).'" ]}
|
|
|
|
</td>';
|
2019-07-31 18:22:35 +02:00
|
|
|
|
|
|
|
// Avere
|
|
|
|
echo '
|
2019-11-22 17:15:17 +01:00
|
|
|
<td>
|
|
|
|
{[ "type": "number", "name": "avere['.$id.']", "id": "avere'.$id.'", "value": "'.($riga['avere'] ?: 0).'" ]}
|
|
|
|
</td>
|
|
|
|
</tr>';
|
2019-07-31 18:22:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
function renderTabella($nome, $righe, $id_scadenza_default = null)
|
|
|
|
{
|
|
|
|
global $counter;
|
|
|
|
|
|
|
|
echo '
|
|
|
|
<div class="raggruppamento_primanota" data-id_scadenza="'.$id_scadenza_default.'">
|
|
|
|
<button class="btn btn-info btn-xs pull-right" type="button" onclick="addRiga(this)">
|
2019-11-25 19:30:50 +01:00
|
|
|
<i class="fa fa-plus"></i> '.tr('Aggiungi riga').'
|
2019-11-22 17:15:17 +01:00
|
|
|
</button>
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
<h4>'.$nome.'</h4>
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
<table class="table table-striped table-condensed table-hover table-bordered scadenze">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>'.tr('Conto').'</th>
|
|
|
|
<th width="20%">'.tr('Dare').'</th>
|
|
|
|
<th width="20%">'.tr('Avere').'</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
<tbody>';
|
|
|
|
|
|
|
|
foreach ($righe as $riga) {
|
|
|
|
renderRiga($counter++, $riga);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Totale per controllare sbilancio
|
|
|
|
echo '
|
|
|
|
</tbody>
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
<tfoot>
|
2019-07-31 18:22:35 +02:00
|
|
|
<tr>
|
2020-07-06 13:19:20 +02:00
|
|
|
<td class="text-right"><b>'.tr('Totale').':</b></td>';
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Totale dare
|
|
|
|
echo '
|
2020-07-06 13:19:20 +02:00
|
|
|
<td class="text-right">
|
2019-11-22 17:15:17 +01:00
|
|
|
<span class="totale_dare"></span> '.currency().'
|
2019-07-31 18:22:35 +02:00
|
|
|
</td>';
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Totale avere
|
|
|
|
echo '
|
2020-07-06 13:19:20 +02:00
|
|
|
<td class="text-right">
|
2019-11-22 17:15:17 +01:00
|
|
|
<span class="totale_avere"></span> '.currency().'
|
2019-07-31 18:22:35 +02:00
|
|
|
</td>
|
|
|
|
</tr>';
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
echo '
|
|
|
|
</tfoot>
|
|
|
|
</table>';
|
|
|
|
|
|
|
|
// Verifica dello sbilancio
|
|
|
|
echo '
|
|
|
|
<div class="alert alert-warning hide sbilancio">
|
|
|
|
<i class="fa fa-warning"></i> '.tr('Sbilancio di _MONEY_', [
|
|
|
|
'_MONEY_' => '<span class="money"></span> '.currency(),
|
|
|
|
]).'
|
|
|
|
</div>
|
|
|
|
</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$counter = 0;
|
2019-12-11 16:23:51 +01:00
|
|
|
$movimenti = collect($movimenti);
|
2019-11-22 17:15:17 +01:00
|
|
|
|
|
|
|
// Elenco per documenti
|
2019-12-11 16:23:51 +01:00
|
|
|
$scadenze = $movimenti
|
2019-11-22 17:15:17 +01:00
|
|
|
->where('iddocumento', '<>', '')
|
|
|
|
->groupBy('iddocumento');
|
|
|
|
foreach ($scadenze as $id_documento => $righe) {
|
|
|
|
$documento = Fattura::find($id_documento);
|
|
|
|
|
|
|
|
$nome = tr('Documento num. _NUM_', [
|
|
|
|
'_NUM_' => $documento['numero_esterno'] ?: $documento['numero'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
renderTabella($nome, $righe, $righe->first()['id_scadenza']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Elenco per scadenze
|
2019-12-11 16:23:51 +01:00
|
|
|
$scadenze = $movimenti
|
2019-11-22 17:15:17 +01:00
|
|
|
->where('iddocumento', '=', '')
|
|
|
|
->where('id_scadenza', '<>', '')
|
|
|
|
->groupBy('id_scadenza');
|
|
|
|
foreach ($scadenze as $id_scadenza => $righe) {
|
|
|
|
$nome = tr('Scadenza num. _ID_', [
|
|
|
|
'_ID_' => $id_scadenza,
|
|
|
|
]);
|
|
|
|
|
|
|
|
renderTabella($nome, $righe, $righe->first()['id_scadenza']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Elenco generale
|
2019-12-11 16:23:51 +01:00
|
|
|
$movimenti_generali = $movimenti
|
2019-11-22 17:15:17 +01:00
|
|
|
->where('iddocumento', '=', '')
|
|
|
|
->where('id_scadenza', '=', '');
|
2019-12-11 16:23:51 +01:00
|
|
|
if ($movimenti_generali->isEmpty()) {
|
|
|
|
$movimenti_generali->push([]);
|
|
|
|
$movimenti_generali->push([]);
|
2019-11-22 17:15:17 +01:00
|
|
|
}
|
|
|
|
$nome = tr('Generale');
|
|
|
|
|
2019-12-11 16:23:51 +01:00
|
|
|
renderTabella($nome, $movimenti_generali);
|
2019-11-22 17:15:17 +01:00
|
|
|
|
|
|
|
// Nuova riga
|
2019-07-31 18:22:35 +02:00
|
|
|
echo '
|
2019-11-22 17:15:17 +01:00
|
|
|
<table class="hide">
|
|
|
|
<tbody id="template">';
|
|
|
|
|
|
|
|
renderRiga('-id-', [
|
|
|
|
'id_scadenza' => '-id_scadenza-',
|
|
|
|
]);
|
|
|
|
|
|
|
|
echo '
|
|
|
|
</tbody>
|
|
|
|
</table>';
|
2019-07-31 18:22:35 +02:00
|
|
|
|
|
|
|
echo '
|
|
|
|
<script>
|
|
|
|
var formatted_zero = "'.Translator::numberToLocale(0).'";
|
2019-11-22 17:15:17 +01:00
|
|
|
var n = '.$counter.';
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
function addRiga(btn) {
|
|
|
|
var raggruppamento = $(btn).parent();
|
|
|
|
cleanup_inputs();
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
var tabella = raggruppamento.find("tbody");
|
|
|
|
var content = $("#template").html();
|
|
|
|
content = content.replace("-id_scadenza-", raggruppamento.data("id_scadenza"));
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
var text = replaceAll(content, "-id-", "" + n);
|
|
|
|
tabella.append(text);
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
restart_inputs();
|
2020-02-16 16:45:45 +01:00
|
|
|
n++;
|
2019-11-22 17:15:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Funzione per controllare lo stato dei conti della prima nota.
|
2020-07-06 13:19:20 +02:00
|
|
|
*
|
2019-11-22 17:15:17 +01:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function controllaConti() {
|
|
|
|
var continuare = true;
|
|
|
|
|
|
|
|
// Controlli sullo stato dei raggruppamenti
|
|
|
|
$(".raggruppamento_primanota").each(function() {
|
|
|
|
var bilancio = calcolaBilancio(this);
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
continuare &= bilancio == 0;
|
2019-07-31 18:22:35 +02:00
|
|
|
});
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Blocco degli input con valore non impostato
|
|
|
|
$("input[id*=dare], input[id*=avere]").each(function() {
|
|
|
|
var conto_relativo = $(this).parent().parent().find("select").val();
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
if (!conto_relativo) {
|
|
|
|
$(this).prop("disabled", true);
|
2020-07-06 13:19:20 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
if ($(this).val().toEnglish()){
|
|
|
|
continuare &= conto_relativo ? true : false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (continuare) {
|
|
|
|
$("#add-submit").removeClass("hide");
|
|
|
|
$("#btn_crea_modello").removeClass("hide");
|
2020-07-06 13:19:20 +02:00
|
|
|
} else {
|
2019-11-22 17:15:17 +01:00
|
|
|
$("#add-submit").addClass("hide");
|
|
|
|
$("#btn_crea_modello").addClass("hide");
|
|
|
|
}
|
|
|
|
|
|
|
|
return continuare;
|
|
|
|
}
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
/**
|
|
|
|
* Ad ogni modifica dell\'importo verifica che siano stati selezionati: il conto, la causale, la data.
|
|
|
|
* Inoltre aggiorna lo sbilancio.
|
2020-07-06 13:19:20 +02:00
|
|
|
*
|
2019-11-22 17:15:17 +01:00
|
|
|
* @param gruppo
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
function calcolaBilancio(gruppo) {
|
|
|
|
var raggruppamento = $(gruppo);
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
var totale_dare = 0.00;
|
|
|
|
var totale_avere = 0.00;
|
|
|
|
|
|
|
|
// Calcolo il totale dare
|
|
|
|
raggruppamento.find("input[id*=dare]").each(function() {
|
2019-07-31 18:22:35 +02:00
|
|
|
valore = $(this).val() ? $(this).val().toEnglish() : 0;
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
totale_dare += valore;
|
2019-07-31 18:22:35 +02:00
|
|
|
});
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Calcolo il totale avere
|
|
|
|
raggruppamento.find("input[id*=avere]").each(function() {
|
|
|
|
valore = $(this).val() ? $(this).val().toEnglish() : 0;
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
totale_avere += valore;
|
|
|
|
});
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Visualizzazione dei totali
|
|
|
|
raggruppamento.find(".totale_dare").text(totale_dare.toLocale());
|
|
|
|
raggruppamento.find(".totale_avere").text(totale_avere.toLocale());
|
|
|
|
|
|
|
|
// Calcolo il bilancio
|
2020-01-02 16:10:33 +01:00
|
|
|
var bilancio = totale_dare.toFixed(2) - totale_avere.toFixed(2);
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Visualizzazione dello sbilancio eventuale
|
|
|
|
var sbilancio = raggruppamento.find(".sbilancio");
|
|
|
|
var valore_sbilancio = sbilancio.find(".money");
|
|
|
|
valore_sbilancio.text(bilancio.toLocale());
|
2019-07-31 18:22:35 +02:00
|
|
|
|
|
|
|
if (bilancio == 0) {
|
2019-11-22 17:15:17 +01:00
|
|
|
sbilancio.addClass("hide");
|
2019-07-31 18:22:35 +02:00
|
|
|
} else {
|
2019-11-22 17:15:17 +01:00
|
|
|
sbilancio.removeClass("hide");
|
2019-07-31 18:22:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
return bilancio;
|
2019-07-31 18:22:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
$(document).ready(function() {
|
|
|
|
controllaConti();
|
2020-07-06 13:19:20 +02:00
|
|
|
|
|
|
|
// Fix per l\'inizializzazione degli input
|
2019-07-31 18:22:35 +02:00
|
|
|
$("input[id*=dare], input[id*=avere]").each(function() {
|
|
|
|
if ($(this).val() == formatted_zero) {
|
|
|
|
$(this).prop("disabled", true);
|
|
|
|
} else {
|
|
|
|
$(this).prop("disabled", false);
|
|
|
|
}
|
|
|
|
});
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
// Trigger dell\'evento keyup() per la prima volta, per eseguire i dovuti controlli nel caso siano predisposte delle righe in prima nota
|
|
|
|
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
$("select[id*=idconto]").click(function() {
|
|
|
|
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
|
2019-07-31 18:22:35 +02:00
|
|
|
});
|
2019-11-22 17:15:17 +01:00
|
|
|
});
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
$(document).on("change", "select", function() {
|
|
|
|
var row = $(this).parent().parent();
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
if ($(this).parent().parent().find("input[disabled]").length != 1) {
|
|
|
|
row.find("input").prop("disabled", $(this).val() ? false : true);
|
|
|
|
}
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
controllaConti();
|
|
|
|
});
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
$(document).on("keyup change", "input[id*=dare]", function() {
|
|
|
|
var row = $(this).parent().parent();
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
if (!$(this).prop("disabled")) {
|
|
|
|
row.find("input[id*=avere]").prop("disabled", $(this).val() ? true : false);
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
controllaConti();
|
|
|
|
}
|
|
|
|
});
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
$(document).on("keyup change", "input[id*=avere]", function() {
|
|
|
|
var row = $(this).parent().parent();
|
2020-07-06 13:19:20 +02:00
|
|
|
|
2019-11-22 17:15:17 +01:00
|
|
|
if (!$(this).prop("disabled")) {
|
|
|
|
row.find("input[id*=dare]").prop("disabled", $(this).val() ? true : false);
|
|
|
|
|
|
|
|
controllaConti();
|
|
|
|
}
|
2019-07-31 18:22:35 +02:00
|
|
|
});
|
|
|
|
</script>';
|