Browse Source

fix error mgmt fatture

FabioFratini 8 tháng trước cách đây
mục cha
commit
33b379058c
1 tập tin đã thay đổi với 45 bổ sung12 xóa
  1. 45 12
      app/Http/Livewire/RecordOUT.php

+ 45 - 12
app/Http/Livewire/RecordOUT.php

@@ -786,6 +786,16 @@ class RecordOUT extends Component
                     $xmlString = file_get_contents($receiptFile->getRealPath());
                     $xml = simplexml_load_string($xmlString);
 
+                    if (empty($xmlString)) {
+                        throw new \Exception("Il file è vuoto.");
+                    }
+
+                    // Verifica se il file sembra essere un XML
+                    if (strpos($xmlString, '<?xml') === false && strpos($xmlString, '<') === false) {
+                        throw new \Exception("Il file non sembra essere in formato XML.");
+                    }
+
+                    $xml = simplexml_load_string($xmlString);
                     if (!$xml) {
                         throw new \Exception("Impossibile analizzare il file XML");
                     }
@@ -824,15 +834,13 @@ class RecordOUT extends Component
 
                     Log::info("Fattura importata con successo: {$fatturaData['numeroFattura']}, Fornitore: {$fatturaData['denominazione']} OR {$fatturaData['cognome']}, {$fatturaData['nome']}");
                 } catch (\Exception $e) {
-                    $errorMsg = 'Errore durante l\'importazione della fattura';
-                    if (isset($fileName)) {
-                        $errorMsg .= " ($fileName)";
-                        $errorFiles[] = $fileName; // Aggiungiamo il nome del file alla lista degli errori
-                    }
-                    $errorMsg .= ': ' . $e->getMessage();
+                    $errorMsg = $e->getMessage();
+
+                    $friendlyErrorMsg = $this->getFriendlyErrorMessage($errorMsg);
 
-                    Log::error($errorMsg);
-                    $errorMessages[] = $errorMsg;
+                    Log::error("Errore originale: " . $errorMsg);
+                    $errorMessages[] = $friendlyErrorMsg;
+                    $errorFiles[] = $fileName;
                     $errorsCount++;
                 }
 
@@ -850,7 +858,6 @@ class RecordOUT extends Component
             ]);
         } finally {
             $this->emit('load-data-table');
-            // Non chiamiamo più closeImportModal() qui, perché la chiusura è gestita dal JavaScript
         }
     }
     /**
@@ -1367,7 +1374,7 @@ class RecordOUT extends Component
         $messageType = "success";
 
         if ($importCount > 0) {
-            $message .= "Importate $importCount nuove fatture.<br>";
+            $message .= "Importate correttamente n° $importCount nuove fatture.<br>";
             if (!empty($importedFiles)) {
                 $message .= "<details><summary>File importati (clicca per espandere)</summary><ul>";
                 foreach ($importedFiles as $file) {
@@ -1378,7 +1385,7 @@ class RecordOUT extends Component
         }
 
         if ($updateCount > 0) {
-            $message .= "Aggiornate $updateCount fatture esistenti.<br>";
+            $message .= "Aggiornate n° $updateCount fatture perchè già presenti.<br>";
             if (!empty($updatedFiles)) {
                 $message .= "<details><summary>File aggiornati (clicca per espandere)</summary><ul>";
                 foreach ($updatedFiles as $file) {
@@ -1389,7 +1396,7 @@ class RecordOUT extends Component
         }
 
         if ($errorsCount > 0) {
-            $message .= "$errorsCount fatture non sono state importate a causa di errori.<br>";
+            $message .= "Importazione fallita per n° $errorsCount fatture.<br>";
 
             // Aggiungi i dettagli degli errori se disponibili
             if (!empty($errorFiles)) {
@@ -1423,4 +1430,30 @@ class RecordOUT extends Component
         // Resetta i campi del form dopo l'importazione
         $this->reset(['receiptFiles']);
     }
+
+    private function getFriendlyErrorMessage($errorMessage) {
+        // Errore di parsing XML iniziale
+        if (strpos($errorMessage, "simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found") !== false) {
+            return "Il file non è in formato XML valido. Potrebbe essere danneggiato o in un formato diverso.";
+        }
+
+        // Errore di struttura XML
+        if (strpos($errorMessage, "FatturaElettronicaHeader o FatturaElettronicaBody non trovati") !== false) {
+            return "Il file non sembra essere una fattura elettronica valida. Mancano le sezioni principali.";
+        }
+
+        // Altri errori comuni di parsing XML
+        if (strpos($errorMessage, "parser error") !== false) {
+            return "Il file XML contiene errori di formattazione e non può essere letto correttamente.";
+        }
+
+        // Errori di struttura interna
+        if (strpos($errorMessage, "Undefined index") !== false ||
+            strpos($errorMessage, "Trying to get property") !== false) {
+            return "La fattura è incompleta o non contiene tutti i dati necessari.";
+        }
+
+        // Per altri errori, conserva il messaggio originale ma semplificalo
+        return $errorMessage;
+    }
 }