diff --git a/app/Console/Commands/GesconImportFornitoriLegacyMdb.php b/app/Console/Commands/GesconImportFornitoriLegacyMdb.php index 5f15d93..3078826 100644 --- a/app/Console/Commands/GesconImportFornitoriLegacyMdb.php +++ b/app/Console/Commands/GesconImportFornitoriLegacyMdb.php @@ -223,6 +223,7 @@ public function handle(): int $fornitorePayload = [ 'amministratore_id' => $amministratoreId, + 'cod_forn' => $codForn, 'rubrica_id' => $rubrica->id, 'codice_univoco' => (is_string($rubricaCode) && $rubricaCode !== '') ? $rubricaCode : null, 'ragione_sociale' => $ragione ?? $rubrica->ragione_sociale, @@ -256,6 +257,12 @@ public function handle(): int ->where('old_id', $legacyId) ->first(); } + if (! $existing && $codForn !== null && Schema::hasColumn('fornitori', 'cod_forn')) { + $existing = Fornitore::query() + ->where('amministratore_id', $amministratoreId) + ->where('cod_forn', $codForn) + ->first(); + } if (! $existing && ! empty($fornitorePayload['rubrica_id'])) { $existing = Fornitore::query() ->where('amministratore_id', $amministratoreId) diff --git a/app/Console/Commands/ImportLegacyFornitoriTagsCommand.php b/app/Console/Commands/ImportLegacyFornitoriTagsCommand.php index 8312b50..90a13f9 100644 --- a/app/Console/Commands/ImportLegacyFornitoriTagsCommand.php +++ b/app/Console/Commands/ImportLegacyFornitoriTagsCommand.php @@ -250,6 +250,11 @@ private function resolveLocalFornitore(object $legacy): ?Fornitore $cf = trim((string) ($legacy->codice_fiscale ?? '')); $ragione = trim((string) ($legacy->ragione_sociale ?? '')); + $byLegacyCode = $this->resolveLocalFornitoreByLegacyIds($legacyId, $legacyCod, $piva, $cf, $this->normalizeComparableString($ragione)); + if ($byLegacyCode instanceof Fornitore) { + return $byLegacyCode; + } + if ($piva !== '') { $found = Fornitore::query()->where('partita_iva', $piva)->first(); if ($found instanceof Fornitore) { @@ -280,20 +285,49 @@ private function resolveLocalFornitore(object $legacy): ?Fornitore } } - return $this->resolveLocalFornitoreByLegacyIds($legacyId, $legacyCod, $piva, $cf, $normalizedRagione); + return null; } private function resolveLocalFornitoreByLegacyIds(int $legacyId, string $legacyCod, string $piva, string $cf, string $normalizedRagione): ?Fornitore { - $candidateIds = collect([ - $legacyId > 0 ? $legacyId : null, + $candidateCodes = collect([ ($legacyCod !== '' && ctype_digit($legacyCod)) ? (int) $legacyCod : null, + $legacyId > 0 ? $legacyId : null, ]) ->filter(fn($value): bool => is_int($value) && $value > 0) ->unique() ->values(); - foreach ($candidateIds as $candidateId) { + if (Schema::hasColumn('fornitori', 'cod_forn')) { + foreach ($candidateCodes as $candidateCode) { + $matches = Fornitore::query() + ->where('cod_forn', (int) $candidateCode) + ->get(['id', 'cod_forn', 'partita_iva', 'codice_fiscale', 'ragione_sociale']); + + if ($matches->count() === 1) { + return $matches->first(); + } + + $filtered = $matches->filter(function (Fornitore $fornitore) use ($piva, $cf, $normalizedRagione): bool { + if ($piva !== '' && trim((string) ($fornitore->partita_iva ?? '')) === $piva) { + return true; + } + + if ($cf !== '' && trim((string) ($fornitore->codice_fiscale ?? '')) === $cf) { + return true; + } + + return $normalizedRagione !== '' + && $this->normalizeComparableString((string) ($fornitore->ragione_sociale ?? '')) === $normalizedRagione; + })->values(); + + if ($filtered->count() === 1) { + return $filtered->first(); + } + } + } + + foreach ($candidateCodes as $candidateId) { $matches = Fornitore::query() ->where('old_id', (int) $candidateId) ->get(['id', 'partita_iva', 'codice_fiscale', 'ragione_sociale']); @@ -361,6 +395,11 @@ private function extractKeywordTags(string $input): array } $keywords = [ + 'informat' => 'informatica', + 'assist' => 'assistenza', + 'computer' => 'pc', + ' pc ' => 'pc', + 'apple' => 'apple', 'idr' => 'idraulico', 'termoidraul' => 'termoidraulico', 'elettric' => 'elettricista', @@ -425,6 +464,10 @@ private function canonicalizeTag(string $raw): ?string { $clean = trim(mb_strtolower($raw), " \t\n\r\0\x0B-_,.;:"); $clean = preg_replace('/\s+/', ' ', $clean) ?? ''; + if ($clean === 'pc') { + return 'pc'; + } + if ($clean === '' || mb_strlen($clean) < 3) { return null; } @@ -438,6 +481,10 @@ private function canonicalizeTag(string $raw): ?string } $map = [ + 'informat' => 'informatica', + 'assist' => 'assistenza', + 'computer' => 'pc', + 'apple' => 'apple', 'idr' => 'idraulico', 'idraul' => 'idraulico', 'elett' => 'elettricista', diff --git a/app/Models/Fornitore.php b/app/Models/Fornitore.php index 5b9a6c3..f2161ec 100755 --- a/app/Models/Fornitore.php +++ b/app/Models/Fornitore.php @@ -17,6 +17,7 @@ class Fornitore extends Model protected $fillable = [ 'amministratore_id', + 'cod_forn', 'rubrica_id', 'codice_univoco', 'ragione_sociale', @@ -57,6 +58,7 @@ class Fornitore extends Model protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime', + 'cod_forn' => 'integer', 'escludi_righe_fe' => 'boolean', 'fe_features' => 'array', 'operational_config' => 'array', diff --git a/database/migrations/2026_04_14_150000_add_cod_forn_to_fornitori_table.php b/database/migrations/2026_04_14_150000_add_cod_forn_to_fornitori_table.php new file mode 100644 index 0000000..db168e4 --- /dev/null +++ b/database/migrations/2026_04_14_150000_add_cod_forn_to_fornitori_table.php @@ -0,0 +1,56 @@ +unsignedBigInteger('cod_forn')->nullable()->after('old_id'); + } + }); + + DB::table('fornitori') + ->whereNull('cod_forn') + ->whereNotNull('old_id') + ->update(['cod_forn' => DB::raw('old_id')]); + + $indexes = collect(DB::select("SHOW INDEX FROM fornitori")) + ->pluck('Key_name'); + + Schema::table('fornitori', function (Blueprint $table) use ($indexes): void { + if (! $indexes->contains('fornitori_amministratore_cod_forn_index')) { + $table->index(['amministratore_id', 'cod_forn'], 'fornitori_amministratore_cod_forn_index'); + } + }); + } + + public function down(): void + { + if (! Schema::hasTable('fornitori')) { + return; + } + + $indexes = collect(DB::select("SHOW INDEX FROM fornitori")) + ->pluck('Key_name'); + + Schema::table('fornitori', function (Blueprint $table) use ($indexes): void { + if ($indexes->contains('fornitori_amministratore_cod_forn_index')) { + $table->dropIndex('fornitori_amministratore_cod_forn_index'); + } + + if (Schema::hasColumn('fornitori', 'cod_forn')) { + $table->dropColumn('cod_forn'); + } + }); + } +}; \ No newline at end of file