123 lines
5.3 KiB
PHP
Executable File
123 lines
5.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services\QuickImport;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class QuickStabiliImporter
|
|
{
|
|
private string $tenantId;
|
|
public function __construct(string $tenantId)
|
|
{
|
|
$this->tenantId = $tenantId;
|
|
}
|
|
|
|
/**
|
|
* Import/Upsert singolo Stabile da mapping semplice.
|
|
* @param string $legacyCode Codice legacy stabile (cartella / codice)
|
|
* @param array|null $mappingMeta
|
|
*/
|
|
public function import(string $legacyCode, ?array $mappingMeta): array
|
|
{
|
|
// 1. Recupera record legacy di riferimento (dataset 'stabili')
|
|
$legacyData = $this->loadLegacyData($legacyCode);
|
|
if (!$legacyData) {
|
|
return ['ok' => false, 'error' => 'Legacy data non trovato per codice ' . $legacyCode];
|
|
}
|
|
$meta = $mappingMeta ?? [];
|
|
$map = isset($meta['mapping']) && is_array($meta['mapping']) ? $meta['mapping'] : [];
|
|
$attributes = $this->buildAttributes($legacyData, $map);
|
|
if (!isset($attributes['denominazione']) && isset($legacyData['denominazione'])) {
|
|
$attributes['denominazione'] = $legacyData['denominazione'];
|
|
}
|
|
$now = now();
|
|
$fillable = array_values(array_filter([
|
|
'denominazione',
|
|
'indirizzo',
|
|
'numero_civico',
|
|
'cap',
|
|
'comune_cod',
|
|
'provincia',
|
|
'regione',
|
|
// Catasto
|
|
Schema::hasColumn('stabili', 'codice_comune_catasto') ? 'codice_comune_catasto' : null,
|
|
Schema::hasColumn('stabili', 'codice_catastale_comune') ? 'codice_catastale_comune' : null,
|
|
Schema::hasColumn('stabili', 'foglio') ? 'foglio' : (Schema::hasColumn('stabili', 'foglio_catasto') ? 'foglio_catasto' : null),
|
|
Schema::hasColumn('stabili', 'particella') ? 'particella' : (Schema::hasColumn('stabili', 'particella_catasto') ? 'particella_catasto' : (Schema::hasColumn('stabili', 'mappale') ? 'mappale' : null)),
|
|
Schema::hasColumn('stabili', 'subalterno') ? 'subalterno' : null,
|
|
Schema::hasColumn('stabili', 'sezione') ? 'sezione' : null,
|
|
Schema::hasColumn('stabili', 'categoria_catastale') ? 'categoria_catastale' : (Schema::hasColumn('stabili', 'categoria') ? 'categoria' : null),
|
|
Schema::hasColumn('stabili', 'classe') ? 'classe' : (Schema::hasColumn('stabili', 'classe_catastale') ? 'classe_catastale' : null),
|
|
Schema::hasColumn('stabili', 'consistenza') ? 'consistenza' : (Schema::hasColumn('stabili', 'consistenza_catastale') ? 'consistenza_catastale' : null),
|
|
Schema::hasColumn('stabili', 'rendita_catastale') ? 'rendita_catastale' : null,
|
|
// Fiscali
|
|
'codice_fiscale_condominio',
|
|
'partita_iva_condominio',
|
|
'cod_fisc_amministratore',
|
|
'codice_fiscale',
|
|
'anno_costruzione',
|
|
'numero_piani'
|
|
]));
|
|
$row = DB::table('stabili')
|
|
->where('codice_stabile', $legacyCode)
|
|
->orWhere('codice_interno', $legacyCode)
|
|
->first();
|
|
$isNew = !$row;
|
|
$data = [];
|
|
foreach ($attributes as $k => $v) {
|
|
if (in_array($k, $fillable, true)) $data[$k] = $v;
|
|
}
|
|
if (!isset($data['codice_stabile'])) $data['codice_stabile'] = $legacyCode;
|
|
if (!isset($data['codice_interno'])) $data['codice_interno'] = $legacyCode;
|
|
if (Schema::hasColumn('stabili', 'tenant_id')) {
|
|
$data['tenant_id'] = $this->tenantId;
|
|
}
|
|
$data['updated_at'] = $now;
|
|
if ($isNew) $data['created_at'] = $now;
|
|
if ($isNew) {
|
|
$id = DB::table('stabili')->insertGetId($data);
|
|
} else {
|
|
DB::table('stabili')->where('id', $row->id)->update($data);
|
|
$id = $row->id;
|
|
}
|
|
return ['ok' => true, 'created' => $isNew ? 1 : 0, 'updated' => $isNew ? 0 : 1, 'stabile_id' => $id, 'legacy_code' => $legacyCode, 'applied_fields' => array_keys($attributes)];
|
|
}
|
|
|
|
private function loadLegacyData(string $legacyCode): ?array
|
|
{
|
|
try {
|
|
$dataset = DB::table('migration_datasets')->where('key', 'stabili')->orderByDesc('id')->first();
|
|
if (!$dataset) return null;
|
|
$rec = DB::table('migration_records')
|
|
->where('dataset_id', $dataset->id)
|
|
->where(function ($q) use ($legacyCode) {
|
|
$q->where('legacy_key', $legacyCode)->orWhere('legacy_code', $legacyCode);
|
|
})
|
|
->orderBy('id')->first();
|
|
if (!$rec) return null;
|
|
$raw = $rec->data;
|
|
$decoded = is_string($raw) ? json_decode($raw, true) : (array)$raw;
|
|
if (!is_array($decoded)) $decoded = [];
|
|
return $decoded;
|
|
} catch (\Throwable $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private function buildAttributes(array $legacy, array $map): array
|
|
{
|
|
$out = [];
|
|
foreach ($map as $legacyKey => $target) {
|
|
if (!is_string($target)) continue;
|
|
if (strpos($target, 'stabile.') !== 0) continue;
|
|
$field = substr($target, 8);
|
|
if ($field === '') continue;
|
|
$value = $legacy[$legacyKey] ?? null;
|
|
if ($value === null || $value === '') continue;
|
|
$out[$field] = $value;
|
|
}
|
|
return $out;
|
|
}
|
|
}
|