Explorar el Código

fix uscite allegato

FabioFratini hace 7 meses
padre
commit
3d0efa10df
Se han modificado 2 ficheros con 73 adiciones y 12 borrados
  1. 72 11
      app/Http/Livewire/RecordOUT.php
  2. 1 1
      resources/views/livewire/records_out.blade.php

+ 72 - 11
app/Http/Livewire/RecordOUT.php

@@ -143,6 +143,7 @@ class RecordOUT extends Component
         $this->numero_fattura = null;
         $this->type = 'OUT';
         $this->commercial = 1;
+        $this->dataId = null;
         $this->rows = array();
         $this->rows[] = array(
             'causal_id' => null,
@@ -155,7 +156,6 @@ class RecordOUT extends Component
         );
         $this->emit('load-data-table');
     }
-
     public function getCausale($records, $indentation)
     {
         foreach ($records as $record) {
@@ -371,6 +371,7 @@ class RecordOUT extends Component
         $this->emit('setEdit', true);
     }
 
+
     public function store()
     {
         $this->emit('refresh');
@@ -379,7 +380,6 @@ class RecordOUT extends Component
             $this->numero_fattura = 'USC-' . date('Ymd');
         }
 
-
         if (empty($this->data_pagamento) || $this->data_pagamento == '1970-01-01') {
             $this->data_pagamento = null;
             Log::info("Setting data_pagamento to NULL in store");
@@ -390,45 +390,77 @@ class RecordOUT extends Component
         }
 
         $this->validate();
+
         try {
+            // DEBUG: Check if we have an attachment
+            Log::info("STORE DEBUG - Has attachment?: " . ($this->attachment ? 'YES' : 'NO'));
+            if ($this->attachment) {
+                Log::info("STORE DEBUG - Attachment original name: " . $this->attachment->getClientOriginalName());
+            }
+
+            // Create the record first WITHOUT attachment
             $record = \App\Models\Record::create([
                 'member_id' => $this->member_id,
                 'supplier_id' => $this->supplier_id,
                 'payment_method_id' => $this->payment_method_id,
                 'date' => $this->date,
                 'data_pagamento' => $this->data_pagamento,
-                'attachment' => '',
+                'attachment' => '', // Empty initially
                 'type' => $this->type,
                 'amount' => $this->currencyToDouble($this->amount),
                 'commercial' => $this->commercial,
                 'numero_fattura' => $this->numero_fattura,
                 'is_paid' => $is_paid,
             ]);
-            Log::info("Record data being inserted: " . json_encode($record));
 
+            Log::info("STORE DEBUG - Record created with ID: " . $record->id);
             $this->dataId = $record->id;
+
+            // Create record folders after record is created
             $this->recordFileService->createRecordFolders($record->id, 'OUT');
+            Log::info("STORE DEBUG - Folders created for record ID: " . $record->id);
+
+            // Now handle attachment upload if exists
             if ($this->attachment) {
                 try {
+                    Log::info("STORE DEBUG - Starting attachment upload...");
                     $attachmentPath = $this->recordFileService->uploadAttachment($this->attachment, $record->id, 'OUT');
+                    Log::info("STORE DEBUG - Attachment uploaded to path: " . $attachmentPath);
+
+                    // Use DB query builder instead of model update to ensure it works
+                    $updateResult = \DB::table('records')
+                        ->where('id', $record->id)
+                        ->update(['attachment' => $attachmentPath]);
 
-                    $record->update(['attachment' => $attachmentPath]);
+                    Log::info("STORE DEBUG - DB update result: " . ($updateResult ? 'SUCCESS' : 'FAILED'));
+
+                    // Verify the update worked by fetching fresh from database
+                    $updatedRecord = \App\Models\Record::where('id', $record->id)->first();
+                    Log::info("STORE DEBUG - Verified attachment in DB: '" . $updatedRecord->attachment . "'");
+
+                    // Also refresh the model instance
+                    $record->refresh();
+                    Log::info("STORE DEBUG - Model after refresh: '" . $record->attachment . "'");
 
                     Log::info("Attachment uploaded and record updated: " . $attachmentPath);
                 } catch (\Exception $ex) {
-                    Log::error("Error uploading attachment: " . $ex->getMessage());
+                    Log::error("STORE ERROR - Error uploading attachment: " . $ex->getMessage());
+                    Log::error("STORE ERROR - Exception trace: " . $ex->getTraceAsString());
+                    session()->flash('warning', 'Record created but attachment upload failed: ' . $ex->getMessage());
                 }
+            } else {
+                Log::info("STORE DEBUG - No attachment to upload");
             }
 
+            // Create record rows
             $tot = 0;
             foreach ($this->rows as $row) {
                 foreach ($row["when"] as $x => $y) {
                     $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
                 }
                 $imponibile = isset($row["imponibile"]) ? $this->currencyToDouble($row["imponibile"]) : null;
-                Log::info("Imponibile store: " . $imponibile);
                 $aliquota_iva = isset($row["aliquota_iva"]) ? floatval(str_replace('%', '', $row["aliquota_iva"])) : null;
-                Log::info("Aliquota IVA store: " . $aliquota_iva);
+
                 \App\Models\RecordRow::create([
                     'record_id' => $this->dataId,
                     'causal_id' => $row["causal_id"],
@@ -441,6 +473,8 @@ class RecordOUT extends Component
                 ]);
                 $tot += $this->currencyToDouble($row["amount"]);
             }
+
+            // Update final amount
             $record->amount = $tot;
             $record->save();
 
@@ -449,6 +483,8 @@ class RecordOUT extends Component
             $this->add = false;
             $this->emit('setEdit', false);
         } catch (\Exception $ex) {
+            Log::error("STORE ERROR - General error: " . $ex->getMessage());
+            Log::error("STORE ERROR - Exception trace: " . $ex->getTraceAsString());
             $this->emit('flash-error', 'Errore (' . $ex->getMessage() . ')');
         }
     }
@@ -477,19 +513,38 @@ class RecordOUT extends Component
         $this->emit('setEdit', true);
         $this->emit('load-select');
         $this->emit('hide-search');
+
         try {
             $record = \App\Models\Record::findOrFail($id);
             if (!$record) {
                 $this->emit('flash-error', 'Movimento non trovato');
             } else {
+                // DEBUG: Let's see what's in the database
+                Log::info("EDIT DEBUG - Record ID: {$record->id}");
+                Log::info("EDIT DEBUG - Database attachment value: '" . $record->attachment . "'");
+                Log::info("EDIT DEBUG - Database attachment type: " . gettype($record->attachment));
+                Log::info("EDIT DEBUG - Is attachment empty?: " . (empty($record->attachment) ? 'YES' : 'NO'));
+                Log::info("EDIT DEBUG - Is attachment null?: " . (is_null($record->attachment) ? 'YES' : 'NO'));
+
                 $this->member_id = $record->member_id;
                 $this->supplier_id = $record->supplier_id;
                 $this->payment_method_id = $record->payment_method_id;
                 $this->date = date("Y-m-d", strtotime($record->date));
-                $this->data_pagamento =  $record->data_pagamento;
+                $this->data_pagamento = $record->data_pagamento;
                 $this->type = $record->type;
                 $this->numero_fattura = $record->numero_fattura;
-                $this->attachment_old = $record->attachment;
+
+                // Set attachment_old - handle different cases
+                if (!empty($record->attachment) && $record->attachment !== null && $record->attachment !== '') {
+                    $this->attachment_old = $record->attachment;
+                    Log::info("EDIT DEBUG - Setting attachment_old to: '" . $this->attachment_old . "'");
+                } else {
+                    $this->attachment_old = '';
+                    Log::info("EDIT DEBUG - Setting attachment_old to empty string");
+                }
+
+                $this->attachment = null; // Clear any temporary attachment
+
                 $this->commercial = $record->commercial;
                 $this->dataId = $record->id;
                 $this->update = true;
@@ -542,12 +597,18 @@ class RecordOUT extends Component
                         }
                     }
 
-
                     $this->rows[] = $rowData;
                 }
+
+                // Final debug log
+                Log::info("EDIT DEBUG - Final attachment_old value: '" . $this->attachment_old . "'");
+
+                // Force Livewire to re-render
+                $this->emit('refresh');
             }
         } catch (\Exception $ex) {
             $this->emit('flash-error', 'Errore (' . $ex->getMessage() . ')');
+            Log::error("EDIT ERROR: " . $ex->getMessage());
         }
     }
 

+ 1 - 1
resources/views/livewire/records_out.blade.php

@@ -1056,7 +1056,7 @@
                         }
                     },
                 ],
-                order: [[0, 'desc']],
+                order: [[1, 'desc']],
                 thead: {
                 'th': {'background-color': 'blue'}
                 },