225 lines
8.7 KiB
PHP
225 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Fornitore;
|
|
use App\Models\RubricaRuolo;
|
|
use App\Models\RubricaUniversale;
|
|
use App\Models\Stabile;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GesconCollegaFornitoreStabili extends Command
|
|
{
|
|
protected $signature = 'gescon:collega-fornitore-stabili
|
|
{fornitore_id : ID del fornitore}
|
|
{stabile_ids* : Uno o più ID di stabili}
|
|
{--apply : Applica davvero (default: dry-run)}';
|
|
|
|
protected $description = 'Collega un fornitore (rubrica) a uno o più stabili tramite rubrica_ruoli (ruolo fornitore).';
|
|
|
|
public function handle(): int
|
|
{
|
|
$apply = (bool) $this->option('apply');
|
|
|
|
$fornitoreId = (int) $this->argument('fornitore_id');
|
|
$stabileIds = array_values(array_unique(array_map('intval', (array) $this->argument('stabile_ids'))));
|
|
$stabileIds = array_values(array_filter($stabileIds, fn(int $id) => $id > 0));
|
|
|
|
if ($fornitoreId <= 0 || empty($stabileIds)) {
|
|
$this->error('Parametri non validi.');
|
|
return 1;
|
|
}
|
|
|
|
$fornitore = Fornitore::query()->find($fornitoreId);
|
|
if (! $fornitore) {
|
|
$this->error("Fornitore #{$fornitoreId} non trovato");
|
|
return 1;
|
|
}
|
|
|
|
$stabili = Stabile::query()->whereIn('id', $stabileIds)->get();
|
|
if ($stabili->count() !== count($stabileIds)) {
|
|
$missing = array_diff($stabileIds, $stabili->pluck('id')->map(fn($id) => (int) $id)->all());
|
|
$this->error('Stabili non trovati: ' . implode(', ', $missing));
|
|
return 1;
|
|
}
|
|
|
|
$this->info(($apply ? 'APPLY' : 'DRY-RUN') . ' - collegamento fornitore ↔ stabili');
|
|
|
|
$runner = function () use ($fornitore, $stabili, $apply): void {
|
|
$rubrica = $this->ensureRubricaForFornitore($fornitore, $apply);
|
|
if (! $rubrica) {
|
|
throw new \RuntimeException('Impossibile risolvere/creare la rubrica del fornitore');
|
|
}
|
|
|
|
foreach ($stabili as $stabile) {
|
|
$this->ensureRubricaForStabile($stabile, $apply);
|
|
|
|
$exists = RubricaRuolo::query()
|
|
->where('rubrica_id', (int) $rubrica->id)
|
|
->where('ruolo_standard', 'fornitore')
|
|
->where('stabile_id', (int) $stabile->id)
|
|
->whereNull('unita_immobiliare_id')
|
|
->where('is_attivo', true)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
$this->line("- OK già presente: fornitore su stabile #{$stabile->id} ({$stabile->codice_stabile})");
|
|
continue;
|
|
}
|
|
|
|
$this->line("- CREA ruolo: fornitore su stabile #{$stabile->id} ({$stabile->codice_stabile})");
|
|
|
|
if ($apply) {
|
|
RubricaRuolo::query()->create([
|
|
'rubrica_id' => (int) $rubrica->id,
|
|
'ruolo_standard' => 'fornitore',
|
|
'ruolo_custom' => null,
|
|
'stabile_id' => (int) $stabile->id,
|
|
'unita_immobiliare_id' => null,
|
|
'data_inizio' => null,
|
|
'data_fine' => null,
|
|
'is_preferito' => false,
|
|
'is_attivo' => true,
|
|
'meta' => [
|
|
'source' => 'gescon:collega-fornitore-stabili',
|
|
'fornitore_id' => (int) $fornitore->id,
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
};
|
|
|
|
if ($apply) {
|
|
DB::transaction(function () use ($runner): void {
|
|
$runner();
|
|
});
|
|
} else {
|
|
$runner();
|
|
}
|
|
|
|
$this->info('Fatto.');
|
|
return 0;
|
|
}
|
|
|
|
private function ensureRubricaForFornitore(Fornitore $fornitore, bool $apply): ?RubricaUniversale
|
|
{
|
|
if ($fornitore->rubrica_id) {
|
|
$rubrica = RubricaUniversale::query()->find((int) $fornitore->rubrica_id);
|
|
if ($rubrica) {
|
|
if ($apply && $fornitore->codice_univoco !== $rubrica->codice_univoco) {
|
|
$fornitore->forceFill(['codice_univoco' => $rubrica->codice_univoco])->saveQuietly();
|
|
}
|
|
|
|
$this->line("- Fornitore #{$fornitore->id} già collegato a Rubrica #{$rubrica->id} ({$rubrica->codice_univoco})");
|
|
return $rubrica;
|
|
}
|
|
}
|
|
|
|
$piva = trim((string) ($fornitore->partita_iva ?? ''));
|
|
$cf = trim((string) ($fornitore->codice_fiscale ?? ''));
|
|
|
|
$rubrica = null;
|
|
if ($piva !== '') {
|
|
$rubrica = RubricaUniversale::query()->where('partita_iva', $piva)->first();
|
|
}
|
|
if (! $rubrica && $cf !== '') {
|
|
$rubrica = RubricaUniversale::query()->where('codice_fiscale', $cf)->first();
|
|
}
|
|
|
|
if (! $rubrica) {
|
|
$ragione = trim((string) ($fornitore->ragione_sociale ?? ''));
|
|
$nome = trim((string) ($fornitore->nome ?? ''));
|
|
$cognome = trim((string) ($fornitore->cognome ?? ''));
|
|
$tipo = $ragione !== '' ? 'persona_giuridica' : 'persona_fisica';
|
|
|
|
$this->line("- CREA Rubrica per fornitore #{$fornitore->id}");
|
|
|
|
if (! $apply) {
|
|
return new RubricaUniversale();
|
|
}
|
|
|
|
$rubrica = RubricaUniversale::query()->create([
|
|
'tipo_contatto' => $tipo,
|
|
'ragione_sociale' => $ragione !== '' ? $ragione : null,
|
|
'nome' => $tipo === 'persona_fisica' ? ($nome !== '' ? $nome : null) : null,
|
|
'cognome' => $tipo === 'persona_fisica' ? ($cognome !== '' ? $cognome : null) : null,
|
|
'codice_fiscale' => $cf !== '' ? $cf : null,
|
|
'partita_iva' => $piva !== '' ? $piva : null,
|
|
'indirizzo' => $fornitore->indirizzo ?? null,
|
|
'civico' => $fornitore->civico ?? null,
|
|
'cap' => $fornitore->cap ?? null,
|
|
'citta' => $fornitore->citta ?? null,
|
|
'provincia' => $fornitore->provincia ?? null,
|
|
'nazione' => 'Italia',
|
|
'email' => $fornitore->email ?? null,
|
|
'pec' => $fornitore->pec ?? null,
|
|
'categoria' => 'fornitore',
|
|
'stato' => 'attivo',
|
|
'data_inserimento' => now()->toDateString(),
|
|
'data_ultima_modifica' => now()->toDateString(),
|
|
'note' => 'Auto-link da fornitore',
|
|
]);
|
|
} else {
|
|
$this->line("- Trovata Rubrica esistente #{$rubrica->id} per fornitore #{$fornitore->id}");
|
|
}
|
|
|
|
if ($apply && $rubrica->exists) {
|
|
$fornitore->forceFill([
|
|
'rubrica_id' => (int) $rubrica->id,
|
|
'codice_univoco' => $rubrica->codice_univoco,
|
|
])->saveQuietly();
|
|
}
|
|
|
|
return $rubrica;
|
|
}
|
|
|
|
private function ensureRubricaForStabile(Stabile $stabile, bool $apply): void
|
|
{
|
|
if ($stabile->rubrica_id) {
|
|
$this->line("- Stabile #{$stabile->id} già collegato a Rubrica #{$stabile->rubrica_id}");
|
|
return;
|
|
}
|
|
|
|
$cf = trim((string) ($stabile->codice_fiscale ?? ''));
|
|
$den = trim((string) ($stabile->denominazione ?? ''));
|
|
|
|
$rubrica = null;
|
|
if ($cf !== '') {
|
|
$rubrica = RubricaUniversale::query()->where('codice_fiscale', $cf)->first();
|
|
}
|
|
if (! $rubrica && $den !== '') {
|
|
$rubrica = RubricaUniversale::query()->where('ragione_sociale', $den)->first();
|
|
}
|
|
|
|
if (! $rubrica) {
|
|
$this->line("- CREA Rubrica per stabile #{$stabile->id}");
|
|
if (! $apply) {
|
|
return;
|
|
}
|
|
|
|
$rubrica = RubricaUniversale::query()->create([
|
|
'tipo_contatto' => 'persona_giuridica',
|
|
'ragione_sociale' => $den !== '' ? $den : ('Condominio #' . (int) $stabile->id),
|
|
'codice_fiscale' => $cf !== '' ? $cf : null,
|
|
'indirizzo' => $stabile->indirizzo ?? null,
|
|
'cap' => $stabile->cap ?? null,
|
|
'citta' => $stabile->citta ?? null,
|
|
'provincia' => $stabile->provincia ?? null,
|
|
'nazione' => 'Italia',
|
|
'categoria' => 'cliente',
|
|
'stato' => 'attivo',
|
|
'data_inserimento' => now()->toDateString(),
|
|
'data_ultima_modifica' => now()->toDateString(),
|
|
'note' => 'Auto-link da stabile',
|
|
]);
|
|
} else {
|
|
$this->line("- Trovata Rubrica esistente #{$rubrica->id} per stabile #{$stabile->id}");
|
|
}
|
|
|
|
if ($apply && $rubrica?->exists) {
|
|
$stabile->forceFill(['rubrica_id' => (int) $rubrica->id])->saveQuietly();
|
|
}
|
|
}
|
|
}
|