Add cod_forn support for legacy supplier imports
This commit is contained in:
parent
a509c71189
commit
8ce48a7a27
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user