Add cod_forn support for legacy supplier imports

This commit is contained in:
michele 2026-04-14 16:18:40 +00:00
parent a509c71189
commit 8ce48a7a27
4 changed files with 116 additions and 4 deletions

View File

@ -223,6 +223,7 @@ public function handle(): int
$fornitorePayload = [ $fornitorePayload = [
'amministratore_id' => $amministratoreId, 'amministratore_id' => $amministratoreId,
'cod_forn' => $codForn,
'rubrica_id' => $rubrica->id, 'rubrica_id' => $rubrica->id,
'codice_univoco' => (is_string($rubricaCode) && $rubricaCode !== '') ? $rubricaCode : null, 'codice_univoco' => (is_string($rubricaCode) && $rubricaCode !== '') ? $rubricaCode : null,
'ragione_sociale' => $ragione ?? $rubrica->ragione_sociale, 'ragione_sociale' => $ragione ?? $rubrica->ragione_sociale,
@ -256,6 +257,12 @@ public function handle(): int
->where('old_id', $legacyId) ->where('old_id', $legacyId)
->first(); ->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'])) { if (! $existing && ! empty($fornitorePayload['rubrica_id'])) {
$existing = Fornitore::query() $existing = Fornitore::query()
->where('amministratore_id', $amministratoreId) ->where('amministratore_id', $amministratoreId)

View File

@ -250,6 +250,11 @@ private function resolveLocalFornitore(object $legacy): ?Fornitore
$cf = trim((string) ($legacy->codice_fiscale ?? '')); $cf = trim((string) ($legacy->codice_fiscale ?? ''));
$ragione = trim((string) ($legacy->ragione_sociale ?? '')); $ragione = trim((string) ($legacy->ragione_sociale ?? ''));
$byLegacyCode = $this->resolveLocalFornitoreByLegacyIds($legacyId, $legacyCod, $piva, $cf, $this->normalizeComparableString($ragione));
if ($byLegacyCode instanceof Fornitore) {
return $byLegacyCode;
}
if ($piva !== '') { if ($piva !== '') {
$found = Fornitore::query()->where('partita_iva', $piva)->first(); $found = Fornitore::query()->where('partita_iva', $piva)->first();
if ($found instanceof Fornitore) { 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 private function resolveLocalFornitoreByLegacyIds(int $legacyId, string $legacyCod, string $piva, string $cf, string $normalizedRagione): ?Fornitore
{ {
$candidateIds = collect([ $candidateCodes = collect([
$legacyId > 0 ? $legacyId : null,
($legacyCod !== '' && ctype_digit($legacyCod)) ? (int) $legacyCod : null, ($legacyCod !== '' && ctype_digit($legacyCod)) ? (int) $legacyCod : null,
$legacyId > 0 ? $legacyId : null,
]) ])
->filter(fn($value): bool => is_int($value) && $value > 0) ->filter(fn($value): bool => is_int($value) && $value > 0)
->unique() ->unique()
->values(); ->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() $matches = Fornitore::query()
->where('old_id', (int) $candidateId) ->where('old_id', (int) $candidateId)
->get(['id', 'partita_iva', 'codice_fiscale', 'ragione_sociale']); ->get(['id', 'partita_iva', 'codice_fiscale', 'ragione_sociale']);
@ -361,6 +395,11 @@ private function extractKeywordTags(string $input): array
} }
$keywords = [ $keywords = [
'informat' => 'informatica',
'assist' => 'assistenza',
'computer' => 'pc',
' pc ' => 'pc',
'apple' => 'apple',
'idr' => 'idraulico', 'idr' => 'idraulico',
'termoidraul' => 'termoidraulico', 'termoidraul' => 'termoidraulico',
'elettric' => 'elettricista', 'elettric' => 'elettricista',
@ -425,6 +464,10 @@ private function canonicalizeTag(string $raw): ?string
{ {
$clean = trim(mb_strtolower($raw), " \t\n\r\0\x0B-_,.;:"); $clean = trim(mb_strtolower($raw), " \t\n\r\0\x0B-_,.;:");
$clean = preg_replace('/\s+/', ' ', $clean) ?? ''; $clean = preg_replace('/\s+/', ' ', $clean) ?? '';
if ($clean === 'pc') {
return 'pc';
}
if ($clean === '' || mb_strlen($clean) < 3) { if ($clean === '' || mb_strlen($clean) < 3) {
return null; return null;
} }
@ -438,6 +481,10 @@ private function canonicalizeTag(string $raw): ?string
} }
$map = [ $map = [
'informat' => 'informatica',
'assist' => 'assistenza',
'computer' => 'pc',
'apple' => 'apple',
'idr' => 'idraulico', 'idr' => 'idraulico',
'idraul' => 'idraulico', 'idraul' => 'idraulico',
'elett' => 'elettricista', 'elett' => 'elettricista',

View File

@ -17,6 +17,7 @@ class Fornitore extends Model
protected $fillable = [ protected $fillable = [
'amministratore_id', 'amministratore_id',
'cod_forn',
'rubrica_id', 'rubrica_id',
'codice_univoco', 'codice_univoco',
'ragione_sociale', 'ragione_sociale',
@ -57,6 +58,7 @@ class Fornitore extends Model
protected $casts = [ protected $casts = [
'created_at' => 'datetime', 'created_at' => 'datetime',
'updated_at' => 'datetime', 'updated_at' => 'datetime',
'cod_forn' => 'integer',
'escludi_righe_fe' => 'boolean', 'escludi_righe_fe' => 'boolean',
'fe_features' => 'array', 'fe_features' => 'array',
'operational_config' => 'array', 'operational_config' => 'array',

View File

@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('fornitori')) {
return;
}
Schema::table('fornitori', function (Blueprint $table): void {
if (! Schema::hasColumn('fornitori', 'cod_forn')) {
$table->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');
}
});
}
};