Skip to content

Commit f5c2e91

Browse files
committed
fix: import interventi
1 parent 237157f commit f5c2e91

1 file changed

Lines changed: 116 additions & 10 deletions

File tree

  • modules/interventi/src/Import

modules/interventi/src/Import/CSV.php

Lines changed: 116 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ public function import($record, $update_record = true, $add_record = true)
159159

160160
// Validazione dei campi obbligatori
161161
if (empty($record['codice']) || empty($record['data']) || empty($record['richiesta'])) {
162+
error_log('Campi obbligatori mancanti - Codice: '.($record['codice'] ?? 'vuoto').', Data: '.($record['data'] ?? 'vuoto').', Richiesta: '.($record['richiesta'] ?? 'vuoto'));
163+
return false;
164+
}
165+
166+
// Validazione formato data
167+
$data_richiesta = $record['data_richiesta'] ?? $record['data'];
168+
if (!$this->validaFormatoData($data_richiesta)) {
169+
error_log('Formato data non valido: '.$data_richiesta);
162170
return false;
163171
}
164172

@@ -168,6 +176,7 @@ public function import($record, $update_record = true, $add_record = true)
168176
// Ricerca dell'anagrafica cliente
169177
$anagrafica = $this->trovaAnagrafica($record);
170178
if (empty($anagrafica)) {
179+
error_log('Impossibile trovare o creare anagrafica per il record: '.json_encode($record));
171180
return false; // Non è possibile procedere senza un'anagrafica cliente
172181
}
173182

@@ -190,13 +199,26 @@ public function import($record, $update_record = true, $add_record = true)
190199

191200
// Trova o crea il tipo di intervento
192201
$tipo = $this->trovaTipoIntervento($record);
202+
if (empty($tipo)) {
203+
error_log('Impossibile trovare tipo intervento per il record: '.json_encode($record));
204+
return false;
205+
}
193206

194207
// Trova o crea lo stato dell'intervento
195208
$stato = $this->trovaStatoIntervento($record);
209+
if (empty($stato)) {
210+
error_log('Impossibile trovare stato intervento per il record: '.json_encode($record));
211+
return false;
212+
}
196213

197214
// Crea o aggiorna l'intervento
198215
if (empty($intervento)) {
199216
$intervento = Intervento::build($anagrafica, $tipo, $stato, $record['data_richiesta']);
217+
218+
// Imposta il codice personalizzato se diverso da quello generato automaticamente
219+
if ($intervento->codice != $record['codice']) {
220+
$intervento->codice = $record['codice'];
221+
}
200222
} else {
201223
// Aggiorna i campi dell'intervento esistente
202224
$intervento->idtipointervento = $tipo->id;
@@ -223,8 +245,8 @@ public function import($record, $update_record = true, $add_record = true)
223245

224246
return true;
225247
} catch (\Exception $e) {
226-
// Registra l'errore in un log
227-
error_log('Errore durante l\'importazione dell\'intervento: '.$e->getMessage());
248+
// Registra l'errore in un log con più dettagli
249+
error_log('Errore durante l\'importazione dell\'intervento: '.$e->getMessage().' - Record: '.json_encode($record).' - Stack trace: '.$e->getTraceAsString());
228250

229251
return false;
230252
}
@@ -305,11 +327,27 @@ protected function trovaImpianto($matricola)
305327
*/
306328
protected function trovaTipoIntervento($record)
307329
{
308-
if (empty($record['tipo'])) {
309-
return TipoIntervento::where('codice', 'GEN')->first();
330+
$tipo = null;
331+
332+
if (!empty($record['tipo'])) {
333+
$tipo = TipoIntervento::where('codice', $record['tipo'])->first();
310334
}
311335

312-
return TipoIntervento::where('codice', $record['tipo'])->first();
336+
// Se non trovato, cerca il tipo "GEN" (Generico)
337+
if (empty($tipo)) {
338+
$tipo = TipoIntervento::where('codice', 'GEN')->first();
339+
}
340+
341+
// Se ancora non trovato, prende il primo tipo disponibile
342+
if (empty($tipo)) {
343+
$tipo = TipoIntervento::first();
344+
}
345+
346+
if (empty($tipo)) {
347+
error_log('Nessun tipo intervento trovato nel database');
348+
}
349+
350+
return $tipo;
313351
}
314352

315353
/**
@@ -321,11 +359,32 @@ protected function trovaTipoIntervento($record)
321359
*/
322360
protected function trovaStatoIntervento($record)
323361
{
324-
if (empty($record['stato'])) {
325-
return Stato::where('name', 'Completato')->first();
362+
$stato = null;
363+
364+
if (!empty($record['stato'])) {
365+
$stato = Stato::where('name', $record['stato'])->first();
366+
}
367+
368+
// Se non trovato, cerca lo stato "Completato"
369+
if (empty($stato)) {
370+
$stato = Stato::where('name', 'Completato')->first();
326371
}
327372

328-
return Stato::where('name', $record['stato'])->first();
373+
// Se ancora non trovato, cerca lo stato "Da programmare"
374+
if (empty($stato)) {
375+
$stato = Stato::where('name', 'Da programmare')->first();
376+
}
377+
378+
// Se ancora non trovato, prende il primo stato disponibile
379+
if (empty($stato)) {
380+
$stato = Stato::first();
381+
}
382+
383+
if (empty($stato)) {
384+
error_log('Nessuno stato intervento trovato nel database');
385+
}
386+
387+
return $stato;
329388
}
330389

331390
/**
@@ -447,6 +506,12 @@ protected function creaAnagrafica($record)
447506
$ragione_sociale = 'Cliente importato '.date('Y-m-d H:i:s');
448507
}
449508

509+
// Verifica che la ragione sociale non sia vuota
510+
if (empty($ragione_sociale)) {
511+
error_log('Impossibile determinare la ragione sociale per il record: '.json_encode($record));
512+
return null;
513+
}
514+
450515
// Crea la nuova anagrafica
451516
$anagrafica = Anagrafica::build($ragione_sociale);
452517

@@ -460,6 +525,11 @@ protected function creaAnagrafica($record)
460525
$anagrafica->codice_fiscale = $record['codice_fiscale'];
461526
}
462527

528+
// Imposta un telefono fittizio se mancante (richiesto per le anagrafiche)
529+
if (empty($anagrafica->telefono) && empty($anagrafica->piva)) {
530+
$anagrafica->telefono = '000000000'; // Telefono fittizio per soddisfare i vincoli
531+
}
532+
463533
// Assegna il tipo "Cliente" all'anagrafica
464534
$tipo_cliente = TipoAnagrafica::where('name', 'Cliente')->first();
465535
if (!empty($tipo_cliente)) {
@@ -469,10 +539,12 @@ protected function creaAnagrafica($record)
469539
// Salva l'anagrafica
470540
$anagrafica->save();
471541

542+
error_log('Anagrafica creata con successo: ID '.$anagrafica->id.', Ragione sociale: '.$ragione_sociale);
543+
472544
return $anagrafica;
473545
} catch (\Exception $e) {
474-
// Registra l'errore
475-
error_log('Errore durante la creazione dell\'anagrafica: '.$e->getMessage());
546+
// Registra l'errore con più dettagli
547+
error_log('Errore durante la creazione dell\'anagrafica: '.$e->getMessage().' - Record: '.json_encode($record).' - Stack trace: '.$e->getTraceAsString());
476548

477549
return null;
478550
}
@@ -545,4 +617,38 @@ protected function creaRigaIntervento($intervento, $record)
545617
return null;
546618
}
547619
}
620+
621+
/**
622+
* Valida il formato della data.
623+
*
624+
* @param string $data Data da validare
625+
*
626+
* @return bool True se il formato è valido, false altrimenti
627+
*/
628+
protected function validaFormatoData($data)
629+
{
630+
if (empty($data)) {
631+
return false;
632+
}
633+
634+
// Prova diversi formati di data comuni
635+
$formati = [
636+
'Y-m-d',
637+
'd/m/Y',
638+
'd-m-Y',
639+
'Y/m/d',
640+
'Y-m-d H:i:s',
641+
'd/m/Y H:i:s',
642+
];
643+
644+
foreach ($formati as $formato) {
645+
$date = \DateTime::createFromFormat($formato, $data);
646+
if ($date && $date->format($formato) === $data) {
647+
return true;
648+
}
649+
}
650+
651+
// Prova anche con strtotime
652+
return strtotime($data) !== false;
653+
}
548654
}

0 commit comments

Comments
 (0)