277 lines
8.2 KiB
PHP
Executable File
277 lines
8.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GesconTempReport extends Command
|
|
{
|
|
protected $signature = 'gescon:temp-report
|
|
{--format=table : Formato output: table, json, csv}
|
|
{--export-csv : Esporta operazioni in CSV}';
|
|
|
|
protected $description = 'Genera report dal database temporaneo GESCON';
|
|
|
|
private $tempDbName = 'gescon_temp_development';
|
|
|
|
public function handle()
|
|
{
|
|
$format = $this->option('format');
|
|
$exportCsv = $this->option('export-csv');
|
|
|
|
$this->info("📊 REPORT DATABASE TEMPORANEO GESCON");
|
|
|
|
if (!$this->verifyTempConnection()) {
|
|
$this->error("❌ Database temporaneo non trovato. Esegui prima 'gescon:temp-db create'");
|
|
return 1;
|
|
}
|
|
|
|
$this->showGeneralStats();
|
|
$this->showFornitoriStats();
|
|
$this->showVociSpesaStats();
|
|
$this->showOperazioniStats();
|
|
|
|
if ($exportCsv) {
|
|
$this->exportOperazioniCsv();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private function showGeneralStats()
|
|
{
|
|
$this->info("\n🔍 STATISTICHE GENERALI");
|
|
|
|
$stats = DB::connection('gescon_temp')->select("
|
|
SELECT
|
|
'Fornitori Totali' as item, COUNT(*) as valore FROM temp_fornitori
|
|
UNION ALL
|
|
SELECT
|
|
'Voci Spesa' as item, COUNT(*) as valore FROM temp_voci_spesa
|
|
UNION ALL
|
|
SELECT
|
|
'Operazioni Totali' as item, COUNT(*) as valore FROM temp_operazioni
|
|
UNION ALL
|
|
SELECT
|
|
'Operazioni Positive' as item, COUNT(*) as valore FROM temp_operazioni WHERE importo_euro > 0
|
|
UNION ALL
|
|
SELECT
|
|
'Operazioni Negative' as item, COUNT(*) as valore FROM temp_operazioni WHERE importo_euro < 0
|
|
");
|
|
|
|
$headers = ['Categoria', 'Valore'];
|
|
$rows = [];
|
|
foreach ($stats as $stat) {
|
|
$rows[] = [$stat->item, number_format($stat->valore)];
|
|
}
|
|
|
|
$this->table($headers, $rows);
|
|
}
|
|
|
|
private function showFornitoriStats()
|
|
{
|
|
$this->info("\n👥 TOP 10 FORNITORI PIÙ ATTIVI");
|
|
|
|
$fornitori = DB::connection('gescon_temp')->select("
|
|
SELECT
|
|
f.ragione_sociale,
|
|
f.categoria_attivita,
|
|
COUNT(o.id) as operazioni,
|
|
SUM(ABS(o.importo_euro)) as importo_totale
|
|
FROM temp_fornitori f
|
|
JOIN temp_operazioni o ON f.cod_forn = o.cod_for
|
|
GROUP BY f.cod_forn, f.ragione_sociale, f.categoria_attivita
|
|
ORDER BY COUNT(o.id) DESC
|
|
LIMIT 10
|
|
");
|
|
|
|
$headers = ['Fornitore', 'Categoria', 'Operazioni', 'Importo Tot.'];
|
|
$rows = [];
|
|
foreach ($fornitori as $forn) {
|
|
$rows[] = [
|
|
substr($forn->ragione_sociale, 0, 30),
|
|
substr($forn->categoria_attivita ?? 'N/A', 0, 20),
|
|
$forn->operazioni,
|
|
'€' . number_format($forn->importo_totale, 2)
|
|
];
|
|
}
|
|
|
|
$this->table($headers, $rows);
|
|
}
|
|
|
|
private function showVociSpesaStats()
|
|
{
|
|
$this->info("\n📋 TOP 10 VOCI SPESA PIÙ UTILIZZATE");
|
|
|
|
$voci = DB::connection('gescon_temp')->select("
|
|
SELECT
|
|
v.cod_spe,
|
|
v.descrizione,
|
|
v.tipo_voce,
|
|
COUNT(o.id) as operazioni,
|
|
SUM(ABS(o.importo_euro)) as importo_totale
|
|
FROM temp_voci_spesa v
|
|
JOIN temp_operazioni o ON v.cod_spe = o.cod_spe
|
|
GROUP BY v.cod_spe, v.descrizione, v.tipo_voce
|
|
ORDER BY COUNT(o.id) DESC
|
|
LIMIT 10
|
|
");
|
|
|
|
$headers = ['Codice', 'Descrizione', 'Tipo', 'Operazioni', 'Importo Tot.'];
|
|
$rows = [];
|
|
foreach ($voci as $voce) {
|
|
$rows[] = [
|
|
$voce->cod_spe,
|
|
substr($voce->descrizione, 0, 35),
|
|
$voce->tipo_voce,
|
|
$voce->operazioni,
|
|
'€' . number_format($voce->importo_totale, 2)
|
|
];
|
|
}
|
|
|
|
$this->table($headers, $rows);
|
|
}
|
|
|
|
private function showOperazioniStats()
|
|
{
|
|
$this->info("\n💰 ANALISI FINANZIARIA");
|
|
|
|
$financial = DB::connection('gescon_temp')->select("
|
|
SELECT
|
|
'Entrate (positive)' as tipo,
|
|
COUNT(*) as operazioni,
|
|
SUM(importo_euro) as totale
|
|
FROM temp_operazioni
|
|
WHERE importo_euro > 0
|
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
'Uscite (negative)' as tipo,
|
|
COUNT(*) as operazioni,
|
|
SUM(ABS(importo_euro)) as totale
|
|
FROM temp_operazioni
|
|
WHERE importo_euro < 0
|
|
");
|
|
|
|
$headers = ['Tipo', 'Operazioni', 'Totale'];
|
|
$rows = [];
|
|
foreach ($financial as $fin) {
|
|
$rows[] = [
|
|
$fin->tipo,
|
|
number_format($fin->operazioni),
|
|
'€' . number_format($fin->totale, 2)
|
|
];
|
|
}
|
|
|
|
$this->table($headers, $rows);
|
|
|
|
// Operazioni per mese
|
|
$this->info("\n📅 OPERAZIONI PER MESE (2024-2025)");
|
|
|
|
$monthly = DB::connection('gescon_temp')->select("
|
|
SELECT
|
|
DATE_FORMAT(data_spesa, '%Y-%m') as mese,
|
|
COUNT(*) as operazioni,
|
|
SUM(ABS(importo_euro)) as volume
|
|
FROM temp_operazioni
|
|
WHERE data_spesa IS NOT NULL
|
|
GROUP BY DATE_FORMAT(data_spesa, '%Y-%m')
|
|
ORDER BY mese DESC
|
|
LIMIT 12
|
|
");
|
|
|
|
$headers = ['Mese', 'Operazioni', 'Volume'];
|
|
$rows = [];
|
|
foreach ($monthly as $month) {
|
|
$rows[] = [
|
|
$month->mese,
|
|
number_format($month->operazioni),
|
|
'€' . number_format($month->volume, 2)
|
|
];
|
|
}
|
|
|
|
$this->table($headers, $rows);
|
|
}
|
|
|
|
private function exportOperazioniCsv()
|
|
{
|
|
$this->info("\n📤 EXPORT OPERAZIONI IN CSV");
|
|
|
|
$operazioni = DB::connection('gescon_temp')->select("
|
|
SELECT
|
|
data_spesa,
|
|
cod_spe,
|
|
descrizione_spesa,
|
|
cod_for,
|
|
ragione_sociale_fornitore,
|
|
importo_euro,
|
|
numero_fattura,
|
|
data_fattura,
|
|
note,
|
|
anno_gestione
|
|
FROM temp_operazioni
|
|
ORDER BY data_spesa DESC
|
|
");
|
|
|
|
$csvFile = '/tmp/operazioni_gescon_' . date('Y-m-d') . '.csv';
|
|
$handle = fopen($csvFile, 'w');
|
|
|
|
// Header CSV
|
|
fputcsv($handle, [
|
|
'Data Spesa',
|
|
'Cod. Spesa',
|
|
'Descrizione Spesa',
|
|
'Cod. Fornitore',
|
|
'Ragione Sociale',
|
|
'Importo Euro',
|
|
'N. Fattura',
|
|
'Data Fattura',
|
|
'Note',
|
|
'Anno Gestione'
|
|
]);
|
|
|
|
// Dati
|
|
foreach ($operazioni as $op) {
|
|
fputcsv($handle, [
|
|
$op->data_spesa,
|
|
$op->cod_spe,
|
|
$op->descrizione_spesa,
|
|
$op->cod_for,
|
|
$op->ragione_sociale_fornitore,
|
|
$op->importo_euro,
|
|
$op->numero_fattura,
|
|
$op->data_fattura,
|
|
$op->note,
|
|
$op->anno_gestione
|
|
]);
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
$this->info("✅ CSV esportato: {$csvFile}");
|
|
$this->info("📊 Record esportati: " . count($operazioni));
|
|
}
|
|
|
|
private function verifyTempConnection()
|
|
{
|
|
try {
|
|
config(['database.connections.gescon_temp' => [
|
|
'driver' => 'mysql',
|
|
'host' => '127.0.0.1',
|
|
'database' => $this->tempDbName,
|
|
'username' => 'netgescon_user',
|
|
'password' => 'NetGescon2024!',
|
|
'charset' => 'utf8mb4',
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
]]);
|
|
|
|
DB::connection('gescon_temp')->select('SELECT 1');
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|