openstamanager/modules/primanota/movimenti.php

368 lines
10 KiB
PHP
Raw Normal View History

2019-07-31 18:22:35 +02:00
<?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/>.
*/
2019-07-31 18:22:35 +02:00
include_once __DIR__.'/../../core.php';
use Modules\Fatture\Fattura;
2019-07-31 18:22:35 +02:00
function renderRiga($id, $riga)
{
2019-07-31 18:22:35 +02:00
// Conto
echo '
<tr>
2020-08-10 10:20:22 +02:00
<input type="hidden" name="id_documento['.$id.']" value="'.$riga['iddocumento'].'">
<input type="hidden" name="id_scadenza['.$id.']" value="'.$riga['id_scadenza'].'">
<td>
{[ "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>').' ]}
</td>';
2019-07-31 18:22:35 +02:00
// Dare
echo '
<td>
{[ "type": "number", "name": "dare['.$id.']", "id": "dare'.$id.'", "value": "'.($riga['dare'] ?: 0).'" ]}
</td>';
2019-07-31 18:22:35 +02:00
// Avere
echo '
<td>
{[ "type": "number", "name": "avere['.$id.']", "id": "avere'.$id.'", "value": "'.($riga['avere'] ?: 0).'" ]}
</td>
</tr>';
$_SESSION['totale_dare'] += ($riga['dare'] ? $riga['dare'] : 0);
$_SESSION['totale_avere'] += ($riga['avere'] ? $riga['avere'] : 0);
2019-07-31 18:22:35 +02:00
}
2020-08-10 10:20:22 +02:00
function renderTabella($nome, $righe)
{
global $counter;
2020-08-10 10:20:22 +02:00
$prima_riga = $righe->first();
$id_documento = $prima_riga ? $prima_riga['iddocumento'] : null;
$id_scadenza = $prima_riga ? $prima_riga['id_scadenza'] : null;
echo '
2020-08-10 10:20:22 +02:00
<div class="raggruppamento_primanota" data-id_scadenza="'.$id_scadenza.'" data-id_documento="'.$id_documento.'">
<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').'
</button>
<h4>'.$nome.'</h4>
<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>
<tbody>';
foreach ($righe as $riga) {
renderRiga($counter++, $riga);
}
// Totale per controllare sbilancio
echo '
</tbody>
<tfoot>
2019-07-31 18:22:35 +02:00
<tr>
<td class="text-right"><b>'.tr('Totale').':</b></td>';
2019-07-31 18:22:35 +02:00
// Totale dare
echo '
<td class="text-right">
<span class="totale_dare"></span> '.currency().'
2019-07-31 18:22:35 +02:00
</td>';
// Totale avere
echo '
<td class="text-right">
<span class="totale_avere"></span> '.currency().'
2019-07-31 18:22:35 +02:00
</td>
</tr>';
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;
$movimenti = collect($movimenti);
$_SESSION['totale_dare'] = 0;
$_SESSION['totale_avere'] = 0;
// Elenco per documenti
$scadenze = $movimenti
->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'],
]);
2020-08-10 10:20:22 +02:00
renderTabella($nome, $righe);
}
// Elenco per scadenze
$scadenze = $movimenti
->where('iddocumento', '=', '')
->where('id_scadenza', '<>', '')
->groupBy('id_scadenza');
foreach ($scadenze as $id_scadenza => $righe) {
$nome = tr('Scadenza num. _ID_', [
'_ID_' => $id_scadenza,
]);
2020-08-10 10:20:22 +02:00
renderTabella($nome, $righe);
}
// Elenco generale
$movimenti_generali = $movimenti
->where('iddocumento', '=', '0')
->where('id_scadenza', '=', '');
if ($movimenti_generali->isEmpty()) {
$movimenti_generali->push([]);
$movimenti_generali->push([]);
}
$nome = tr('Generale');
renderTabella($nome, $movimenti_generali);
// Nuova riga
2019-07-31 18:22:35 +02:00
echo '
<table class="hide">
<tbody id="template">';
renderRiga('-id-', [
2020-08-10 10:20:22 +02:00
'iddocumento' => '-id_documento-',
'id_scadenza' => '-id_scadenza-',
]);
echo '
</tbody>
</table>';
2019-07-31 18:22:35 +02:00
// Nuova riga
echo '
<table class="table table-bordered">
<tr>
<th class="text-right">'.tr('Totale').'</th>
<th class="text-right" width="20%">'.moneyFormat($_SESSION['totale_dare']).'</th>
<th class="text-right" width="20%">'.moneyFormat($_SESSION['totale_avere']).'</th>
</tr>
</table>';
unset($_SESSION['totale_dare']);
unset($_SESSION['totale_avere']);
2019-07-31 18:22:35 +02:00
echo '
<script>
2020-10-06 12:29:14 +02:00
var formatted_zero = "'.numberFormat(0).'";
var n = '.$counter.';
2019-07-31 18:22:35 +02:00
function addRiga(btn) {
var raggruppamento = $(btn).parent();
cleanup_inputs();
var tabella = raggruppamento.find("tbody");
var content = $("#template").html();
2020-08-10 10:20:22 +02:00
content = content.replace("-id_scadenza-", raggruppamento.data("id_scadenza"))
.replace("-id_documento-", raggruppamento.data("id_documento"));
var text = replaceAll(content, "-id-", "" + n);
tabella.append(text);
restart_inputs();
n++;
}
/**
* Funzione per controllare lo stato dei conti della prima nota.
*
* @returns {boolean}
*/
function controllaConti() {
2020-10-06 12:29:14 +02:00
let continuare = true;
// Controlli sullo stato dei raggruppamenti
$(".raggruppamento_primanota").each(function() {
2020-10-06 12:29:14 +02:00
let bilancio = calcolaBilancio(this);
2020-10-06 12:29:14 +02:00
continuare &= bilancio === 0;
2019-07-31 18:22:35 +02:00
});
// Blocco degli input con valore non impostato
$("input[id*=dare], input[id*=avere]").each(function() {
2022-08-02 16:21:04 +02:00
let conto_relativo = $(this).parent().parent().parent().find("select").val();
if (!conto_relativo) {
$(this).prop("disabled", true);
}
if ($(this).val().toEnglish()){
2020-10-06 12:29:14 +02:00
continuare &= !!conto_relativo;
}
});
if (continuare) {
$("#add-submit").prop("disabled", false);
$("#modello-button").prop("disabled", false);
if (!$("#modals > div").get().length) {
$("#save, #save-close").removeAttr("disabled").removeClass("disabled");
}
} else {
$("#add-submit").prop("disabled", true);
$("#modello-button").prop("disabled", true);
if (!$("#modals > div").get().length) {
$("#save, #save-close").attr("disabled", "true").addClass("disabled");
}
}
return continuare;
}
2019-07-31 18:22:35 +02:00
/**
* Ad ogni modifica dell\'importo verifica che siano stati selezionati: il conto, la causale, la data.
* Inoltre aggiorna lo sbilancio.
*
* @param gruppo
* @returns {number}
*/
function calcolaBilancio(gruppo) {
2020-10-06 12:29:14 +02:00
let raggruppamento = $(gruppo);
2020-10-06 12:29:14 +02:00
let totale_dare = 0.00;
let totale_avere = 0.00;
// Calcolo il totale dare
raggruppamento.find("input[id*=dare]").each(function() {
2020-10-06 12:29:14 +02:00
totale_dare += input(this).get();
2019-07-31 18:22:35 +02:00
});
// Calcolo il totale avere
raggruppamento.find("input[id*=avere]").each(function() {
2020-10-06 12:29:14 +02:00
totale_avere += input(this).get();
});
2019-07-31 18:22:35 +02:00
2020-10-06 12:29:14 +02:00
totale_dare = parseFloat(totale_dare);
totale_avere = parseFloat(totale_avere);
// Visualizzazione dei totali
raggruppamento.find(".totale_dare").text(totale_dare.toLocale());
raggruppamento.find(".totale_avere").text(totale_avere.toLocale());
// Calcolo il bilancio
2020-10-06 12:29:14 +02:00
let bilancio = totale_dare.toFixed(2) - totale_avere.toFixed(2);
// Visualizzazione dello sbilancio eventuale
2020-10-06 12:29:14 +02:00
let sbilancio = raggruppamento.find(".sbilancio");
let valore_sbilancio = sbilancio.find(".money");
valore_sbilancio.text(bilancio.toLocale());
2019-07-31 18:22:35 +02:00
2020-10-06 12:29:14 +02:00
if (bilancio === 0) {
sbilancio.addClass("hide");
2019-07-31 18:22:35 +02:00
} else {
sbilancio.removeClass("hide");
2019-07-31 18:22:35 +02:00
}
return bilancio;
2019-07-31 18:22:35 +02:00
}
$(document).ready(function() {
controllaConti();
// Fix per l\'inizializzazione degli input
2019-07-31 18:22:35 +02:00
$("input[id*=dare], input[id*=avere]").each(function() {
2020-10-06 12:29:14 +02:00
if (input(this).get() === 0) {
2019-07-31 18:22:35 +02:00
$(this).prop("disabled", true);
} else {
$(this).prop("disabled", false);
}
});
// 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
$("select[id*=idconto]").click(function() {
$("input[id*=dare][value!=\'\'], input[id*=avere][value!=\'\']").keyup();
2019-07-31 18:22:35 +02:00
});
});
2019-07-31 18:22:35 +02:00
$(document).on("change", "select", function() {
2022-08-02 16:36:57 +02:00
let row = $(this).parent().parent().parent().parent();
2019-07-31 18:22:35 +02:00
2020-10-06 12:29:14 +02:00
if (row.find("input[disabled]").length > 1) {
row.find("input").prop("disabled", !$(this).val());
}
2022-08-02 16:36:57 +02: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();
controllaConti();
2022-08-02 16:36:57 +02:00
let button = $(this).parent().parent().find("button");
if ($(this).val()) {
button.removeClass("disabled");
} else {
button.addClass("disabled");
}
});
2019-07-31 18:22:35 +02:00
$(document).on("keyup change", "input[id*=dare]", function() {
2022-08-02 16:36:57 +02:00
let row = $(this).parent().parent().parent();
2019-07-31 18:22:35 +02:00
if (!$(this).prop("disabled")) {
row.find("input[id*=avere]").prop("disabled", $(this).val().toEnglish());
2019-07-31 18:22:35 +02:00
controllaConti();
}
});
2019-07-31 18:22:35 +02:00
$(document).on("keyup change", "input[id*=avere]", function() {
2022-08-02 16:36:57 +02:00
let row = $(this).parent().parent().parent();
if (!$(this).prop("disabled")) {
row.find("input[id*=dare]").prop("disabled", $(this).val().toEnglish());
controllaConti();
}
2019-07-31 18:22:35 +02:00
});
function visualizzaMovimenti(button) {
let id_conto = $(button).parent().parent().find("select").val();
openModal("'.tr('Ultimi 25 movimenti').'", "'.$module->fileurl('dettagli.php').'?id_module=" + globals.id_module + "&id_conto=" + id_conto);
}
2019-07-31 18:22:35 +02:00
</script>';