netgescon-day0/routes/console.php

156 lines
5.1 KiB
PHP
Executable File

<?php
use App\Models\Amministratore;
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Facades\Schema;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');
// Manutenzione automatica (no comandi manuali per gli operatori)
Schedule::call(function (): void {
if (! Schema::hasTable('amministratori') || ! Schema::hasTable('rubrica_universale')) {
return;
}
$ids = Amministratore::query()
->where('attivo', true)
->pluck('id')
->map(fn($v) => (int) $v)
->filter(fn(int $v) => $v > 0)
->values();
foreach ($ids as $adminId) {
try {
Artisan::call('gescon:rubrica-dedup', [
'amministratore_id' => $adminId,
'--delete-empties' => true,
]);
} catch (\Throwable) {
// ignore
}
}
})->dailyAt('02:10')->name('netgescon:auto-rubrica-dedup');
Schedule::call(function (): void {
if (! Schema::hasTable('contabilita_fatture_fornitori') || ! Schema::hasColumn('contabilita_fatture_fornitori', 'stabile_id')) {
return;
}
if (! Schema::hasColumn('contabilita_fatture_fornitori', 'fattura_elettronica_id')) {
return;
}
// Processa solo gli stabili che hanno ancora fatture contabili senza fornitore_id.
$stabileIds = DB::table('contabilita_fatture_fornitori')
->whereNull('fornitore_id')
->whereNotNull('fattura_elettronica_id')
->distinct()
->pluck('stabile_id')
->map(fn($v) => (int) $v)
->filter(fn(int $v) => $v > 0)
->values();
foreach ($stabileIds as $stabileId) {
try {
Artisan::call('gescon:fatture-fix-fornitore-link', [
'stabile_id' => $stabileId,
]);
} catch (\Throwable) {
// ignore
}
}
})->hourly()->name('netgescon:auto-fatture-fornitore-link');
Schedule::call(function (): void {
if (! Schema::hasTable('stabili') || ! Schema::hasColumn('stabili', 'cod_stabile')) {
return;
}
if (! Schema::hasTable('migration_datasets') || ! Schema::hasTable('migration_records')) {
return;
}
$datasetId = DB::table('migration_datasets')->where('key', 'stabili')->orderByDesc('id')->value('id');
if (! $datasetId) {
return;
}
$select = ['id', 'cod_stabile'];
if (Schema::hasColumn('stabili', 'codice_stabile')) {
$select[] = 'codice_stabile';
}
if (Schema::hasColumn('stabili', 'codice_interno')) {
$select[] = 'codice_interno';
}
$stabili = DB::table('stabili')
->select($select)
->where(function ($q) {
$q->whereNull('cod_stabile')->orWhere('cod_stabile', '');
})
->limit(250)
->get();
foreach ($stabili as $stabile) {
$key = null;
if (property_exists($stabile, 'codice_stabile') && is_string($stabile->codice_stabile) && trim($stabile->codice_stabile) !== '') {
$key = trim($stabile->codice_stabile);
} elseif (property_exists($stabile, 'codice_interno') && is_string($stabile->codice_interno) && trim($stabile->codice_interno) !== '') {
$key = trim($stabile->codice_interno);
}
if (! $key) {
continue;
}
try {
$rec = DB::table('migration_records')
->where('dataset_id', $datasetId)
->where(function ($q) use ($key) {
$q->where('legacy_key', $key)->orWhere('legacy_code', $key);
})
->orderByDesc('id')
->first();
if (! $rec || ! is_string($rec->data)) {
continue;
}
$decoded = json_decode($rec->data, true);
if (! is_array($decoded)) {
continue;
}
$candidate = $decoded['nome_directory'] ?? $decoded['internet_cod_stab'] ?? $decoded['codstab'] ?? $decoded['cod_stabile'] ?? null;
$candidate = is_string($candidate) ? trim($candidate) : (is_numeric($candidate) ? (string) $candidate : null);
if ($candidate === null || $candidate === '') {
continue;
}
DB::table('stabili')->where('id', $stabile->id)->update(['cod_stabile' => $candidate]);
} catch (\Throwable) {
// ignore
}
}
})->dailyAt('02:25')->name('netgescon:auto-stabili-cod-stabile-backfill');
Schedule::command('istat:sync-indici')
->monthlyOn(7, '06:10')
->when(function (): bool {
if (! Schema::hasTable('istat_indici_mensili')) {
return false;
}
$url = trim((string) config('services.istat.api_url', ''));
return $url !== '';
})
->name('netgescon:auto-istat-sync');
Schedule::command('google:sync-rubrica --max-pages=10')
->hourlyAt(20)
->name('netgescon:auto-google-rubrica-sync');
Schedule::command('google:push-rubrica --limit=300')
->hourlyAt(25)
->name('netgescon:auto-google-rubrica-push');