166 lines
6.9 KiB
PHP
166 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Fornitore;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class GesconImportFornitoriFiscalDetailsCommand extends Command
|
|
{
|
|
protected $signature = 'gescon:import-fornitori-fiscal-details {--mdb= : Percorso opzionale Fornitori.mdb}';
|
|
|
|
protected $description = 'Importa dati fiscali avanzati (Trib 1019/1020, Regime Fiscale, Cassa Prof, PEC, IBAN) da Fornitori.mdb';
|
|
|
|
public function handle(): int
|
|
{
|
|
$this->info('Avvio importazione dati fiscali fornitori (Trib 1019/1020 & Regime Fiscale)...');
|
|
|
|
$candidateMdbs = [
|
|
$this->option('mdb'),
|
|
'/mnt/gescon-archives/gescon/dbc/Fornitori.mdb',
|
|
'/mnt/gescon-archives/id/temp/fornitori.mdb',
|
|
];
|
|
|
|
$mdbPath = null;
|
|
foreach ($candidateMdbs as $p) {
|
|
if ($p && file_exists($p) && is_readable($p)) {
|
|
$mdbPath = $p;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (! $mdbPath) {
|
|
$this->error('File Fornitori.mdb non trovato.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->line("Utilizzo file MDB: {$mdbPath}");
|
|
|
|
$proc = new \Symfony\Component\Process\Process(['mdb-export', $mdbPath, 'Fornitori']);
|
|
$proc->run();
|
|
|
|
if (! $proc->isSuccessful()) {
|
|
$this->error('Impossibile eseguire mdb-export su Fornitori.mdb.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$csvContent = $proc->getOutput();
|
|
$stream = fopen('php://memory', 'r+');
|
|
fwrite($stream, $csvContent);
|
|
rewind($stream);
|
|
|
|
$header = fgetcsv($stream);
|
|
if (! $header) {
|
|
fclose($stream);
|
|
$this->error('Header CSV non valido.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$updated = 0;
|
|
while (($data = fgetcsv($stream)) !== false) {
|
|
if (count($data) === count($header)) {
|
|
$row = array_combine($header, $data);
|
|
$piva = trim((string) ($row['p_iva'] ?? ''));
|
|
$cf = trim((string) ($row['cod_fisc'] ?? ''));
|
|
$ragSoc = trim((string) ($row['Denominazione'] ?? '')) ?: trim(trim((string) ($row['cognome'] ?? '')) . ' ' . trim((string) ($row['nome'] ?? '')));
|
|
$trib = trim((string) ($row['Trib_1019_1020'] ?? ''));
|
|
$regime = trim((string) ($row['Regime_fiscale'] ?? ''));
|
|
$cassaProf = trim((string) ($row['Perc_cassa_prof'] ?? ''));
|
|
$iban = trim((string) ($row['Cod_IBAN'] ?? ''));
|
|
$pec = trim((string) ($row['PEC_Fornitore'] ?? ''));
|
|
$intestCC = trim((string) ($row['Intestaz_CC_esatta'] ?? ''));
|
|
$oldId = (int) ($row['id_fornitore'] ?? 0);
|
|
$codForn = trim((string) ($row['cod_forn'] ?? ''));
|
|
|
|
if (! $piva && ! $cf && ! $ragSoc) {
|
|
continue;
|
|
}
|
|
|
|
$query = Fornitore::query();
|
|
if ($piva !== '') {
|
|
$query->where('partita_iva', $piva);
|
|
} elseif ($cf !== '') {
|
|
$query->where('codice_fiscale', $cf);
|
|
} else {
|
|
$query->where('ragione_sociale', $ragSoc);
|
|
}
|
|
|
|
$fornitore = $query->first();
|
|
|
|
if (! $fornitore && $ragSoc !== '') {
|
|
$admin = \App\Models\Amministratore::query()->where('id', 13)->first()
|
|
?: \App\Models\Amministratore::query()->first();
|
|
$adminId = $admin?->id ?: 13;
|
|
|
|
$cleanOldId = null;
|
|
if ($oldId > 0) {
|
|
$existsOldId = Fornitore::query()
|
|
->where('amministratore_id', $adminId)
|
|
->where('old_id', $oldId)
|
|
->exists();
|
|
if (! $existsOldId) {
|
|
$cleanOldId = $oldId;
|
|
}
|
|
}
|
|
|
|
$fornitore = Fornitore::query()->create([
|
|
'amministratore_id' => $adminId,
|
|
'codice_univoco' => strtoupper(\Illuminate\Support\Str::random(8)),
|
|
'ragione_sociale' => $ragSoc,
|
|
'partita_iva' => $piva,
|
|
'codice_fiscale' => $cf,
|
|
'email' => trim((string) ($row['Indir_Email'] ?? '')),
|
|
'pec' => $pec,
|
|
'telefono' => trim((string) ($row['Telef_1'] ?? '')),
|
|
'cellulare' => trim((string) ($row['Cellulare'] ?? '')),
|
|
'indirizzo' => trim((string) ($row['indirizzo'] ?? '')),
|
|
'cap' => trim((string) ($row['cap'] ?? '')),
|
|
'citta' => trim((string) ($row['citta'] ?? '')),
|
|
'provincia' => trim((string) ($row['pr'] ?? '')),
|
|
'old_id' => $cleanOldId,
|
|
]);
|
|
}
|
|
|
|
if ($fornitore) {
|
|
$existingData = is_array($fornitore->dati_fiscali_json ?? null) ? $fornitore->dati_fiscali_json : [];
|
|
$fiscalData = array_merge($existingData, [
|
|
'trib_1019_1020' => $trib,
|
|
'regime_fiscale' => $regime,
|
|
'perc_cassa_prof' => $cassaProf,
|
|
'intestazione_cc_esatta'=> $intestCC,
|
|
'cod_forn_legacy' => $codForn,
|
|
'id_fornitore_legacy' => $oldId,
|
|
]);
|
|
|
|
$updatePayload = [
|
|
'pec' => $pec ?: $fornitore->pec,
|
|
'iban' => $iban ?: $fornitore->iban,
|
|
'dati_fiscali_json'=> $fiscalData,
|
|
];
|
|
|
|
if ($oldId > 0 && ! $fornitore->old_id) {
|
|
$existsOldId = Fornitore::query()
|
|
->where('amministratore_id', $fornitore->amministratore_id)
|
|
->where('old_id', $oldId)
|
|
->exists();
|
|
if (! $existsOldId) {
|
|
$updatePayload['old_id'] = $oldId;
|
|
}
|
|
}
|
|
|
|
$fornitore->update($updatePayload);
|
|
|
|
$updated++;
|
|
$this->line(" ✓ Fornitore #{$fornitore->id} [{$fornitore->ragione_sociale}]: Trib={$trib}, Regime={$regime}, IBAN={$iban}");
|
|
}
|
|
}
|
|
}
|
|
fclose($stream);
|
|
|
|
$this->info("Importazione dati fiscali fornitori completata per {$updated} fornitori!");
|
|
return self::SUCCESS;
|
|
}
|
|
}
|