Introduzione sistema per controlli di integrità logica dei dati

Base per effettuare controlli sullo stato dei dati del gestionale, con introduzione di un relativo script per controlli sui Conti Cliente e Fornitore.
This commit is contained in:
Dasc3er 2021-02-25 09:41:33 +01:00
parent 169067b217
commit 7ab35c7edf
5 changed files with 453 additions and 0 deletions

View File

@ -20,6 +20,8 @@
include_once __DIR__.'/../../core.php';
use Models\Cache;
use Modules\Aggiornamenti\Controlli\Controllo;
use Modules\Aggiornamenti\Controlli\PianoConti;
use Modules\Aggiornamenti\UpdateHook;
$id = post('id');
@ -42,5 +44,59 @@ switch (filter('op')) {
case 'upload':
include base_dir().'/modules/aggiornamenti/upload_modules.php';
break;
case 'controlli-disponibili':
$controlli = [
PianoConti::class,
];
$results = [];
foreach ($controlli as $key => $controllo) {
$results[] = [
'id' => $key,
'class' => $controllo,
'name' => (new $controllo())->getName(),
];
}
echo json_encode($results);
break;
case 'controlli-check':
$class = post('controllo');
// Controllo sulla classe
if (!is_subclass_of($class, Controllo::class)) {
echo json_encode([]);
return;
}
$manager = new $class();
$manager->check();
echo json_encode($manager->getResults());
break;
case 'controlli-action':
$class = post('controllo');
$records = post('records');
$params = post('params');
// Controllo sulla classe
if (!is_subclass_of($class, Controllo::class)) {
echo json_encode([]);
return;
}
$manager = new $class();
$result = $manager->solve($records, $params);
echo json_encode($result);
break;
}

View File

@ -0,0 +1,219 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
include_once __DIR__.'/../../core.php';
// Schermata di caricamento delle informazioni
echo '
<button class="btn btn-lg btn-block" onclick="avviaControlli(this);">
<i class="fa fa-cog"></i> '.tr('Avvia controlli').'
</button>
<div id="controlli"></div>
<div id="progress">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%">
<span>0%</span>
</div>
</div>
<hr>
<p class="text-center">'.tr('Operazione in corso').': <span id="operazione"></span></p>
</div>
<div class="alert alert-info" id="box-loading">
<i class="fa fa-spinner fa-spin"></i> '.tr('Caricamento in corso').'...
</div>
<div class="alert alert-success hidden" id="no-problems">
<i class="fa fa-check"></i> '.tr('Non sono stati rilevati problemi!').'
</div>
<script>
var content = $("#controlli");
var loader = $("#progress");
var progress = $("#box-loading");
$(document).ready(function () {
loader.hide();
progress.hide();
})
function avviaControlli(button) {
var restore = buttonLoading(button);
$.ajax({
url: globals.rootdir + "/actions.php",
type: "GET",
dataType: "JSON",
data: {
id_module: globals.id_module,
op: "controlli-disponibili",
},
success: async function(controlli) {
// Ripristino pulsante
buttonRestore(button, restore);
$(button).hide();
// Visualizzazione loader
loader.show();
progress.show();
let number = 0;
let total = controlli.length;
for (const controllo of controlli) {
// Informazione grafica di progressione
number ++;
let percentage = Math.round(number / total * 100);
percentage = percentage > 100 ? 100 : percentage;
setPercentage(percentage);
$("#operazione").text(controllo["name"]);
// Avvio dei controlli
await avviaControllo(controllo);
}
setPercentage(100);
// Visualizzazione loader
loader.hide();
progress.hide();
// Messaggio informativo in caso di nessun problema
if ($("#controlli").html() === "") {
$("#no-problems").removeClass("hidden");
}
},
error: function(xhr) {
alert("'.tr('Errore').': " + xhr.responseJSON.error.message);
buttonRestore(button, restore);
}
});
}
function avviaControllo(controllo) {
return $.ajax({
url: globals.rootdir + "/actions.php",
type: "POST",
dataType: "JSON",
data: {
id_module: globals.id_module,
op: "controlli-check",
controllo: controllo["class"],
},
success: function(records) {
if (records.length === 0) {
return;
}
let panel = initPanel(controllo);
for(const record of records) {
addRiga(controllo, panel, record);
}
$("#controlli").append(panel);
},
error: function(xhr, r, error) {
alert("'.tr('Errore').': " + error);
}
});
}
function eseguiAzione(controllo, records, params) {
return $.ajax({
url: globals.rootdir + "/actions.php",
type: "POST",
dataType: "JSON",
data: {
id_module: globals.id_module,
op: "controlli-action",
controllo: controllo["class"],
records: records,
params: params,
},
success: function(results) {
$("#controllo-" + controllo["id"] + "-" + records.id).remove();
},
error: function(xhr, r, error) {
alert("'.tr('Errore').': " + error);
}
});
}
function setPercentage(percent) {
$("#progress .progress-bar").width(percent + "%");
$("#progress .progress-bar span").text(percent + "%");
}
function initPanel(controllo) {
let panel = `<div class="box" id="controllo-` + controllo["id"] + `">
<div class="box-header with-border">
<h3 class="box-title">` + controllo["name"] + `</h3>
</div>
<div class="box-body">
<div class="table-responsive">
<table class="table table-striped table-hover table-condensed table-bordered">
<thead>
<tr>
<th width="15%">'.tr('Record').'</th>
<th>'.tr('Descrizione').'</th>
<th class="text-center" width="15%">'.tr('Opzioni').'</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>`;
return $(panel);
}
function addRiga(controllo, panel, record) {
let body = panel.find("tbody");
// Generazione riga
let riga = `<tr class="text-` + record.type + `" id="controllo-` + controllo["id"] + `-` + record.id + `">
<td>` + record.nome + `</td>
<td>` + record.descrizione + `</td>
<td></td>
</tr>`;
riga = $(riga);
// Generazione opzioni
const options_columns = riga.find("td").last();
record.options.forEach(function (option, id){
let button = `<button type="button" class="btn btn-` + option.color + `">
<i class="` + option.icon + `"></i> ` + option.name + `
</buttton>`;
button = $(button);
button.on("click", function () {
option.params.id = id;
eseguiAzione(controllo, record, option.params);
});
options_columns.append(button);
});
body.append(riga);
}
</script>';

View File

@ -156,6 +156,10 @@ function database(button) {
openModal("'.tr('Controllo del database').'", "'.$module->fileurl('database.php').'?id_module='.$id_module.'");
}
function controlli(button) {
openModal("'.tr('Controlli del gestionale').'", "'.$module->fileurl('controlli.php').'?id_module='.$id_module.'");
}
function search(button) {
let restore = buttonLoading(button);
@ -217,6 +221,10 @@ function search(button) {
<button type="button" class="btn btn-info btn-block" onclick="database(this)">
<i class="fa fa-database"></i> '.tr('Controlla database').'
</button>
<button type="button" class="btn btn-block" onclick="controlli(this)">
<i class="fa fa-cog"></i> '.tr('Controlla gestionale').'
</button>
</div>
</div>
</div>

View File

@ -0,0 +1,65 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
namespace Modules\Aggiornamenti\Controlli;
abstract class Controllo
{
public $results = [];
public function addResult($record)
{
$this->results[] = array_merge($record, [
'type' => $this->getType($record),
'options' => $this->getOptions($record),
]);
}
public function getResults()
{
return $this->results;
}
public function getOptions($record)
{
return [];
}
abstract public function getName();
abstract public function getType($record);
abstract public function check();
public function solve($records, $params = [])
{
if (!isset($records[0])) {
return $this->execute($records, $params);
}
$results = [];
foreach ($records as $record) {
$results[$record['id']] = $this->execute($record, $params);
}
return $results;
}
abstract public function execute($records, $params = []);
}

View File

@ -0,0 +1,105 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* 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/>.
*/
namespace Modules\Aggiornamenti\Controlli;
use Modules\Anagrafiche\Anagrafica;
class PianoConti extends Controllo
{
public function getName()
{
return tr('Piano dei Conti collegato alle anagrafiche');
}
public function getType($record)
{
return 'warning';
}
public function getOptions($record)
{
return [
[
'name' => tr('Risolvi'),
'icon' => 'fa fa-check',
'color' => 'success',
'params' => [],
],
];
}
public function check()
{
$database = database();
/**
* Verifico se serve creare un conto per eventuali nuovi clienti o fornitori.
*/
$anagrafiche_interessate = $database->fetchArray('SELECT
an_anagrafiche.idanagrafica AS id,
an_anagrafiche.idconto_cliente,
an_anagrafiche.idconto_fornitore,
an_anagrafiche.ragione_sociale,
GROUP_CONCAT(an_tipianagrafiche.descrizione) AS tipi_anagrafica
FROM an_anagrafiche
INNER JOIN an_tipianagrafiche_anagrafiche ON an_tipianagrafiche_anagrafiche.idanagrafica = an_anagrafiche.idanagrafica
INNER JOIN an_tipianagrafiche ON an_tipianagrafiche.idtipoanagrafica = an_tipianagrafiche_anagrafiche.idtipoanagrafica
WHERE idconto_cliente = 0 OR idconto_cliente IS NULL OR idconto_fornitore = 0 OR idconto_fornitore IS NULL
GROUP BY an_anagrafiche.idanagrafica');
foreach ($anagrafiche_interessate as $anagrafica) {
$tipi = explode(',', $anagrafica['tipi_anagrafica']);
$cliente = in_array('Cliente', $tipi) && empty($anagrafica['idconto_cliente']);
$fornitore = in_array('Fornitore', $tipi) && empty($anagrafica['idconto_fornitore']);
$descrizione = null;
if ($cliente && $fornitore) {
$descrizione = tr("L'anagrafica corrente non ha impostati i conti relativi al Piano dei Conti");
} elseif ($cliente) {
$descrizione = tr("L'anagrafica corrente non ha impostati il conto Cliente relativo al Piano dei Conti");
} elseif ($fornitore) {
$descrizione = tr("L'anagrafica corrente non ha impostati il conto Fornitore relativo al Piano dei Conti");
}
if (!empty($descrizione)) {
$this->addResult([
'id' => $anagrafica['id'],
'nome' => $anagrafica['ragione_sociale'],
'descrizione' => $descrizione,
]);
}
}
}
public function execute($record, $params = [])
{
$anagrafica = Anagrafica::find($record['id']);
if ($anagrafica->isTipo('Cliente')) {
Anagrafica::fixCliente($anagrafica);
}
if ($anagrafica->isTipo('Fornitore')) {
Anagrafica::fixFornitore($anagrafica);
}
return true;
}
}