147 lines
6.2 KiB
PHP
Executable File
147 lines
6.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class GesconQaContatori extends Command
|
|
{
|
|
protected $signature = 'gescon:qa-contatori {--stabile= : Codice stabile (es. 0021) oppure mnemonico (es. 148) oppure ID} {--anno= : Provenienza esercizio/archivio (es. 0001/0003) per filtrare i record domain quando disponibile}';
|
|
|
|
protected $description = 'Contatori QA per controlli incrociati (domain vs gescon_import) su uno stabile';
|
|
|
|
public function handle(): int
|
|
{
|
|
$input = trim((string) ($this->option('stabile') ?? ''));
|
|
if ($input === '') {
|
|
$this->error('Manca --stabile= (es. --stabile=0021 oppure --stabile=148)');
|
|
return self::INVALID;
|
|
}
|
|
|
|
$annoOpt = trim((string) ($this->option('anno') ?? ''));
|
|
$anno = is_numeric($annoOpt) ? (int) $annoOpt : null;
|
|
|
|
// Lookup: prefer explicit codes over numeric ID.
|
|
$stabile = DB::table('stabili')->where('codice_stabile', $input)->first();
|
|
if (! $stabile) {
|
|
$stabile = DB::table('stabili')->where('cod_stabile', $input)->first();
|
|
}
|
|
|
|
// Only treat as ID if it's numeric and does NOT look like a legacy folder code (e.g. 0021).
|
|
if (! $stabile && ctype_digit($input) && ! str_starts_with($input, '0')) {
|
|
$stabile = DB::table('stabili')->where('id', (int) $input)->first();
|
|
}
|
|
|
|
if (! $stabile) {
|
|
$this->error('Stabile non trovato per: ' . $input);
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$stabileId = (int) $stabile->id;
|
|
$codiceStabile = (string) ($stabile->codice_stabile ?? '');
|
|
$codStabile = (string) ($stabile->cod_stabile ?? '');
|
|
|
|
$this->line('Stabile: #' . $stabileId . ' codice=' . $codiceStabile . ' mnemo=' . $codStabile . ' denom=' . (string) ($stabile->denominazione ?? ''));
|
|
if ($anno !== null) {
|
|
$this->line('Filtro anno/provenienza: ' . $anno);
|
|
}
|
|
|
|
// Domain counters
|
|
$domain = [];
|
|
$domain['unita_immobiliari'] = DB::table('unita_immobiliari')->where('stabile_id', $stabileId)->count();
|
|
$domain['palazzine'] = Schema::hasTable('palazzine')
|
|
? DB::table('palazzine')->where('stabile_id', $stabileId)->count()
|
|
: null;
|
|
$domain['casse'] = Schema::hasTable('casse')
|
|
? DB::table('casse')->where('stabile_id', $stabileId)->count()
|
|
: null;
|
|
$domain['dati_bancari'] = Schema::hasTable('dati_bancari')
|
|
? DB::table('dati_bancari')->where('stabile_id', $stabileId)->count()
|
|
: null;
|
|
|
|
// Rate emesse: join piano_rateizzazione -> stabile
|
|
if (Schema::hasTable('rate_emesse') && Schema::hasTable('piano_rateizzazione')) {
|
|
$rateQ = DB::table('rate_emesse as r')
|
|
->join('piano_rateizzazione as p', 'p.id', '=', 'r.piano_rateizzazione_id')
|
|
->where('p.stabile_id', $stabileId);
|
|
|
|
$domain['rate_emesse'] = (int) $rateQ->count();
|
|
|
|
$domain['rate_emesse_sum_addebitato'] = (string) (DB::table('rate_emesse as r')
|
|
->join('piano_rateizzazione as p', 'p.id', '=', 'r.piano_rateizzazione_id')
|
|
->where('p.stabile_id', $stabileId)
|
|
->sum('r.importo_addebitato_soggetto'));
|
|
|
|
$domain['rate_emesse_negative'] = (int) (DB::table('rate_emesse as r')
|
|
->join('piano_rateizzazione as p', 'p.id', '=', 'r.piano_rateizzazione_id')
|
|
->where('p.stabile_id', $stabileId)
|
|
->where('r.importo_addebitato_soggetto', '<', 0)
|
|
->count());
|
|
}
|
|
|
|
// Incassi domain
|
|
if (Schema::hasTable('incassi') && Schema::hasColumn('incassi', 'condominio_id')) {
|
|
$incQ = DB::table('incassi')->where('condominio_id', $stabileId);
|
|
if ($anno !== null && Schema::hasColumn('incassi', 'proviene_eserc')) {
|
|
$incQ->where('proviene_eserc', $anno);
|
|
}
|
|
|
|
$domain['incassi'] = (int) $incQ->count();
|
|
|
|
$incSumQ = DB::table('incassi')->where('condominio_id', $stabileId);
|
|
if ($anno !== null && Schema::hasColumn('incassi', 'proviene_eserc')) {
|
|
$incSumQ->where('proviene_eserc', $anno);
|
|
}
|
|
$domain['incassi_sum_euro'] = (string) $incSumQ->sum('importo_pagato_euro');
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->info('DOMAIN');
|
|
foreach ($domain as $k => $v) {
|
|
if ($v === null) {
|
|
continue;
|
|
}
|
|
$this->line(str_pad($k, 28) . ': ' . $v);
|
|
}
|
|
|
|
// Staging (gescon_import) counters
|
|
$this->newLine();
|
|
$this->info('GESCON_IMPORT');
|
|
|
|
try {
|
|
// In gescon_import, cod_stabile di solito contiene il codice cartella (es. 0021).
|
|
// Il mnemonico (es. 148) può vivere altrove: qui preferiamo codice_stabile.
|
|
$codForImport = $codiceStabile !== '' ? $codiceStabile : $codStabile;
|
|
|
|
if (Schema::connection('gescon_import')->hasTable('condomin')) {
|
|
$q = DB::connection('gescon_import')->table('condomin')->where('cod_stabile', $codForImport);
|
|
if ($anno !== null && Schema::connection('gescon_import')->hasColumn('condomin', 'legacy_year')) {
|
|
$q->where('legacy_year', $anno);
|
|
}
|
|
|
|
$this->line(str_pad('condomin', 28) . ': ' . (int) $q->count());
|
|
}
|
|
|
|
if (Schema::connection('gescon_import')->hasTable('incassi')) {
|
|
$q = DB::connection('gescon_import')->table('incassi')->where('cod_stabile', $codForImport);
|
|
|
|
$this->line(str_pad('incassi', 28) . ': ' . (int) $q->count());
|
|
|
|
$sum = (string) DB::connection('gescon_import')->table('incassi')
|
|
->where('cod_stabile', $codForImport)
|
|
->sum('importo_euro');
|
|
$this->line(str_pad('incassi_sum_euro', 28) . ': ' . $sum);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$this->warn('Impossibile leggere gescon_import: ' . $e->getMessage());
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->comment('Tip: usa i contatori per confrontare velocemente cosa ci aspettiamo vs cosa c\'è in DB.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|