|
|
@@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Log;
|
|
|
use SimpleXMLElement;
|
|
|
use Livewire\WithFileUploads;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Schema;
|
|
|
|
|
|
|
|
|
class RecordOUT extends Component
|
|
|
@@ -163,7 +164,6 @@ class RecordOUT extends Component
|
|
|
$this->reset(['selectedCausal', 'receiptFiles']);
|
|
|
$this->resetValidation();
|
|
|
$this->dispatchBrowserEvent('closeModal');
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public function hydrate()
|
|
|
@@ -492,7 +492,7 @@ class RecordOUT extends Component
|
|
|
{
|
|
|
$this->emit('refresh');
|
|
|
$this->validate();
|
|
|
- Log::info("Rows: pipo" );
|
|
|
+ Log::info("Rows: pipo");
|
|
|
|
|
|
try {
|
|
|
\App\Models\Record::whereId($this->dataId)->update([
|
|
|
@@ -508,10 +508,10 @@ class RecordOUT extends Component
|
|
|
$tot = 0;
|
|
|
|
|
|
$existingRows = \App\Models\RecordRow::where('record_id', $this->dataId)
|
|
|
- ->select('id', 'quantita', 'numero_linea')
|
|
|
- ->get()
|
|
|
- ->keyBy('id')
|
|
|
- ->toArray();
|
|
|
+ ->select('id', 'quantita', 'numero_linea')
|
|
|
+ ->get()
|
|
|
+ ->keyBy('id')
|
|
|
+ ->toArray();
|
|
|
|
|
|
\App\Models\RecordRow::where('record_id', $this->dataId)->delete();
|
|
|
|
|
|
@@ -591,7 +591,7 @@ class RecordOUT extends Component
|
|
|
\App\Models\Record::find($id)->delete();
|
|
|
session()->flash('success', "Movimento eliminato");
|
|
|
} catch (\Exception $e) {
|
|
|
- $this->emit('flash-error','Errore (' . $e->getMessage() . ')');
|
|
|
+ $this->emit('flash-error', 'Errore (' . $e->getMessage() . ')');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -602,7 +602,7 @@ class RecordOUT extends Component
|
|
|
\App\Models\Record::find($id)->delete();
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
- $this->emit('flash-error','Errore (' . $e->getMessage() . ')');
|
|
|
+ $this->emit('flash-error', 'Errore (' . $e->getMessage() . ')');
|
|
|
}
|
|
|
$this->multipleAction = '';
|
|
|
}
|
|
|
@@ -751,13 +751,15 @@ class RecordOUT extends Component
|
|
|
public function importReceipts()
|
|
|
{
|
|
|
$this->validate([
|
|
|
- 'receiptFiles.*' => 'required|mimes:xml|max:2048',
|
|
|
+ //'receiptFiles.*' => 'required|mimes:xml|max:2048',
|
|
|
'selectedCausal' => 'required|exists:causals,id',
|
|
|
]);
|
|
|
|
|
|
try {
|
|
|
$importCount = 0;
|
|
|
+ $updateCount = 0;
|
|
|
$errorsCount = 0;
|
|
|
+ $errorMessages = [];
|
|
|
$totalFiles = count($this->receiptFiles);
|
|
|
|
|
|
// disabilita select
|
|
|
@@ -765,6 +767,8 @@ class RecordOUT extends Component
|
|
|
|
|
|
foreach ($this->receiptFiles as $index => $receiptFile) {
|
|
|
try {
|
|
|
+ $fileName = $receiptFile->getClientOriginalName();
|
|
|
+ Log::info("Elaborazione file: " . $fileName);
|
|
|
// Carica e analizza il file XML
|
|
|
$xmlString = file_get_contents($receiptFile->getRealPath());
|
|
|
$xml = simplexml_load_string($xmlString);
|
|
|
@@ -783,32 +787,58 @@ class RecordOUT extends Component
|
|
|
$paymentMethodId = $this->findPaymentMethod($fatturaData['modalitaPagamento']);
|
|
|
|
|
|
// Crea il record principale
|
|
|
- $record = $this->createRecord($supplier->id, $paymentMethodId, $fatturaData);
|
|
|
+ $isUpdate = false;
|
|
|
+ $existingRecord = \App\Models\Record::where('supplier_id', $supplier->id)
|
|
|
+ ->where('numero_fattura', $fatturaData['numeroFattura'])
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($existingRecord) {
|
|
|
+ $record = $this->updateRecord($existingRecord, $paymentMethodId, $fatturaData);
|
|
|
+ $isUpdate = true;
|
|
|
+ $updateCount++;
|
|
|
+ Log::info("Fattura aggiornata con successo: {$fatturaData['numeroFattura']}, Fornitore: {$supplier->name}");
|
|
|
+ } else {
|
|
|
+ // Crea un nuovo record
|
|
|
+ $record = $this->createRecord($supplier->id, $paymentMethodId, $fatturaData);
|
|
|
+ $importCount++;
|
|
|
+ Log::info("Fattura importata con successo: {$fatturaData['numeroFattura']}, Fornitore: {$supplier->name}");
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
// Crea il record row
|
|
|
$this->createRecordRow($record->id, $fatturaData);
|
|
|
|
|
|
- $importCount++;
|
|
|
+ //$importCount++;
|
|
|
Log::info("Fattura importata con successo: {$fatturaData['numeroFattura']}, Fornitore: {$fatturaData['denominazione']} OR {$fatturaData['cognome']}, {$fatturaData['nome']}");
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Errore durante l\'importazione della fattura: ' . $e->getMessage());
|
|
|
+ $errorMsg = 'Errore durante l\'importazione della fattura';
|
|
|
+ if (isset($fileName)) {
|
|
|
+ $errorMsg .= " ($fileName)";
|
|
|
+ }
|
|
|
+ $errorMsg .= ': ' . $e->getMessage();
|
|
|
+
|
|
|
+ Log::error($errorMsg);
|
|
|
+ $errorMessages[] = $errorMsg;
|
|
|
$errorsCount++;
|
|
|
}
|
|
|
|
|
|
$progress = ($index + 1) / $totalFiles * 100;
|
|
|
$this->emit('update-progress', $progress);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- $this->showResultMessages($importCount, $errorsCount);
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('Errore durante l\'importazione dei file XML: ' . $e->getMessage());
|
|
|
- $this->emit('flash-error', 'Errore durante l\'importazione: ' . $e->getMessage());
|
|
|
+ $this->showResultMessages($importCount, $updateCount, $errorsCount);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $errorMsg = 'Errore durante l\'importazione dei file XML: ' . $e->getMessage();
|
|
|
+ Log::error($errorMsg);
|
|
|
+ $this->emit('show-import-result', [
|
|
|
+ 'message' => $errorMsg,
|
|
|
+ 'type' => 'error'
|
|
|
+ ]);
|
|
|
} finally {
|
|
|
- $this->closeImportModal();
|
|
|
$this->emit('load-data-table');
|
|
|
-
|
|
|
+ // Non chiamiamo più closeImportModal() qui, perché la chiusura è gestita dal JavaScript
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
/**
|
|
|
* Estrae i dati dalla fattura elettronica XML
|
|
|
@@ -1007,66 +1037,83 @@ class RecordOUT extends Component
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $defaultPaymentMethod = DB::table('payment_methods')->where('default', 1)->first()
|
|
|
- ?? DB::table('payment_methods')->first();
|
|
|
+ // Cerca il metodo di pagamento predefinito - corretto per evitare l'errore della colonna 'default'
|
|
|
+ // Verifica se esiste una colonna 'is_default' o simile
|
|
|
+ $possibleDefaultColumns = ['is_default', 'is_default_method', 'default_method', 'primary'];
|
|
|
+ $defaultPaymentMethod = null;
|
|
|
+
|
|
|
+ foreach ($possibleDefaultColumns as $column) {
|
|
|
+ if (Schema::hasColumn('payment_methods', $column)) {
|
|
|
+ $defaultPaymentMethod = DB::table('payment_methods')->where($column, 1)->first();
|
|
|
+ if ($defaultPaymentMethod) {
|
|
|
+ Log::info("Usando metodo di pagamento predefinito (colonna $column) ID: {$defaultPaymentMethod->id}");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Se non è stato trovato un metodo predefinito, prendi il primo disponibile
|
|
|
+ if (!$defaultPaymentMethod) {
|
|
|
+ $defaultPaymentMethod = DB::table('payment_methods')->first();
|
|
|
+ if ($defaultPaymentMethod) {
|
|
|
+ Log::info("Usando il primo metodo di pagamento disponibile ID: {$defaultPaymentMethod->id}");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
if ($defaultPaymentMethod) {
|
|
|
- Log::info("Usando metodo di pagamento predefinito ID: {$defaultPaymentMethod->id}");
|
|
|
return $defaultPaymentMethod->id;
|
|
|
}
|
|
|
|
|
|
throw new \Exception("Nessun metodo di pagamento disponibile nel sistema");
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* Crea un record nella tabella records
|
|
|
*/
|
|
|
private function createRecord($supplierId, $paymentMethodId, $fatturaData)
|
|
|
{
|
|
|
- $existingRecord = \App\Models\Record::where('supplier_id', $supplierId)
|
|
|
- ->where('numero_fattura', $fatturaData['numeroFattura'])
|
|
|
- ->first();
|
|
|
-
|
|
|
- if ($existingRecord) {
|
|
|
- Log::info("Trovato record esistente con ID: " . $existingRecord->id . " per fornitore ID: " . $supplierId . " e numero fattura: " . $fatturaData['numeroFattura']);
|
|
|
-
|
|
|
- $existingRecord->payment_method_id = $paymentMethodId;
|
|
|
- $existingRecord->date = $fatturaData['dataDocumento'];
|
|
|
- $existingRecord->data_pagamento = $fatturaData['dataDocumento'];
|
|
|
- $existingRecord->type = 'OUT';
|
|
|
- $existingRecord->commercial = 1;
|
|
|
- $existingRecord->corrispettivo_fiscale = 0;
|
|
|
- $existingRecord->deleted = 0;
|
|
|
- $existingRecord->financial_movement = 1;
|
|
|
- $existingRecord->amount = $fatturaData['importoTotale'];
|
|
|
- $existingRecord->tipo_documento = $this->mapTipoDocumento($fatturaData['tipoDocumento']);
|
|
|
- $existingRecord->is_ricevuta = true;
|
|
|
-
|
|
|
- if (isset($fatturaData['condizioniPagamento']) && !empty($fatturaData['condizioniPagamento'])) {
|
|
|
- $existingRecord->condizioni_pagamento = $this->mapCondizioniPagamento($fatturaData['condizioniPagamento']);
|
|
|
- }
|
|
|
- if (isset($fatturaData['iban']) && !empty($fatturaData['iban'])) {
|
|
|
- $existingRecord->IBAN = $fatturaData['iban'];
|
|
|
- }
|
|
|
- if (isset($fatturaData['bic']) && !empty($fatturaData['bic'])) {
|
|
|
- $existingRecord->BIC = $fatturaData['bic'];
|
|
|
- }
|
|
|
- if (isset($fatturaData['dataScadenza']) && !empty($fatturaData['dataScadenza'])) {
|
|
|
- $existingRecord->data_scadenza = $fatturaData['dataScadenza'];
|
|
|
- }
|
|
|
+ $record = new \App\Models\Record();
|
|
|
+ $record->supplier_id = $supplierId;
|
|
|
+ $record->payment_method_id = $paymentMethodId;
|
|
|
+ $record->date = $fatturaData['dataDocumento'];
|
|
|
+ $record->data_pagamento = $fatturaData['dataDocumento'];
|
|
|
+ $record->numero_fattura = $fatturaData['numeroFattura'];
|
|
|
+ $record->type = 'OUT';
|
|
|
+ $record->commercial = 1;
|
|
|
+ $record->corrispettivo_fiscale = 0;
|
|
|
+ $record->deleted = 0;
|
|
|
+ $record->financial_movement = 1;
|
|
|
+ $record->amount = $fatturaData['importoTotale'];
|
|
|
+ $record->tipo_documento = $this->mapTipoDocumento($fatturaData['tipoDocumento']);
|
|
|
|
|
|
- $existingRecord->save();
|
|
|
+ $record->is_ricevuta = true;
|
|
|
+ if (isset($fatturaData['condizioniPagamento']) && !empty($fatturaData['condizioniPagamento'])) {
|
|
|
+ $record->condizioni_pagamento = $this->mapCondizioniPagamento($fatturaData['condizioniPagamento']);
|
|
|
+ }
|
|
|
+ if (isset($fatturaData['iban']) && !empty($fatturaData['iban'])) {
|
|
|
+ $record->IBAN = $fatturaData['iban'];
|
|
|
+ }
|
|
|
+ if (isset($fatturaData['bic']) && !empty($fatturaData['bic'])) {
|
|
|
+ $record->BIC = $fatturaData['bic'];
|
|
|
+ }
|
|
|
+ if (isset($fatturaData['dataScadenza']) && !empty($fatturaData['dataScadenza'])) {
|
|
|
+ $record->data_scadenza = $fatturaData['dataScadenza'];
|
|
|
+ }
|
|
|
+ $record->save();
|
|
|
|
|
|
- Log::info("Record esistente aggiornato con ID: " . $existingRecord->id);
|
|
|
+ Log::info("Nuovo record creato con ID: " . $record->id);
|
|
|
|
|
|
- return $existingRecord;
|
|
|
- }
|
|
|
+ return $record;
|
|
|
+ }
|
|
|
|
|
|
- $record = new \App\Models\Record();
|
|
|
- $record->supplier_id = $supplierId;
|
|
|
+ /**
|
|
|
+ * Aggiorna un record esistente nella tabella records
|
|
|
+ */
|
|
|
+ private function updateRecord($record, $paymentMethodId, $fatturaData)
|
|
|
+ {
|
|
|
$record->payment_method_id = $paymentMethodId;
|
|
|
$record->date = $fatturaData['dataDocumento'];
|
|
|
$record->data_pagamento = $fatturaData['dataDocumento'];
|
|
|
- $record->numero_fattura = $fatturaData['numeroFattura'];
|
|
|
$record->type = 'OUT';
|
|
|
$record->commercial = 1;
|
|
|
$record->corrispettivo_fiscale = 0;
|
|
|
@@ -1090,11 +1137,12 @@ class RecordOUT extends Component
|
|
|
}
|
|
|
$record->save();
|
|
|
|
|
|
- Log::info("Record creato con ID: " . $record->id);
|
|
|
+ Log::info("Record esistente aggiornato con ID: " . $record->id);
|
|
|
|
|
|
return $record;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* Crea un record row per il record specificato
|
|
|
*/
|
|
|
@@ -1295,16 +1343,51 @@ class RecordOUT extends Component
|
|
|
return $condizioniPagamento[$codice] ?? $codice;
|
|
|
}
|
|
|
|
|
|
- private function showResultMessages($importCount, $errorsCount)
|
|
|
+ private function showResultMessages($importCount, $updateCount, $errorsCount)
|
|
|
{
|
|
|
+ $message = "";
|
|
|
+ $messageType = "success";
|
|
|
+
|
|
|
if ($importCount > 0) {
|
|
|
- $message = "Importate $importCount fatture con successo.";
|
|
|
- if ($errorsCount > 0) {
|
|
|
- $message .= " $errorsCount fatture non sono state importate.";
|
|
|
+ $message .= "Importate $importCount nuove fatture.<br>";
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($updateCount > 0) {
|
|
|
+ $message .= "Aggiornate $updateCount fatture esistenti.<br>";
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($errorsCount > 0) {
|
|
|
+ $message .= "$errorsCount fatture non sono state importate a causa di errori.<br>";
|
|
|
+
|
|
|
+ // Aggiungi i dettagli degli errori se disponibili
|
|
|
+ if (!empty($errorMessages)) {
|
|
|
+ $message .= "<details><summary>Dettagli errori (clicca per espandere)</summary><ul>";
|
|
|
+ foreach ($errorMessages as $errorMsg) {
|
|
|
+ $message .= "<li>" . htmlspecialchars($errorMsg) . "</li>";
|
|
|
+ }
|
|
|
+ $message .= "</ul></details>";
|
|
|
}
|
|
|
- session()->flash('message', $message);
|
|
|
+
|
|
|
+ if ($importCount == 0 && $updateCount == 0) {
|
|
|
+ $messageType = "error";
|
|
|
+ } else {
|
|
|
+ $messageType = "warning";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($importCount > 0 || $updateCount > 0) {
|
|
|
+ $this->emit('show-import-result', [
|
|
|
+ 'message' => $message,
|
|
|
+ 'type' => $messageType
|
|
|
+ ]);
|
|
|
} else {
|
|
|
- $this->emit('flash-error','Nessuna fattura importata. Controlla i file XML e riprova.');
|
|
|
+ $this->emit('show-import-result', [
|
|
|
+ 'message' => 'Nessuna fattura importata. Controlla i file XML e riprova.',
|
|
|
+ 'type' => 'error'
|
|
|
+ ]);
|
|
|
}
|
|
|
+
|
|
|
+ // Resetta i campi del form dopo l'importazione
|
|
|
+ $this->reset(['receiptFiles']);
|
|
|
}
|
|
|
}
|