|
@@ -372,8 +372,12 @@ class RecordOUT extends Component
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
public function store()
|
|
public function store()
|
|
|
{
|
|
{
|
|
|
|
|
+ // Start loading state
|
|
|
|
|
+ $this->emit('start-loading', 'Salvataggio in corso...');
|
|
|
|
|
+
|
|
|
$this->emit('refresh');
|
|
$this->emit('refresh');
|
|
|
|
|
|
|
|
if ($this->numero_fattura == null || $this->numero_fattura == '') {
|
|
if ($this->numero_fattura == null || $this->numero_fattura == '') {
|
|
@@ -382,30 +386,27 @@ class RecordOUT extends Component
|
|
|
|
|
|
|
|
if (empty($this->data_pagamento) || $this->data_pagamento == '1970-01-01') {
|
|
if (empty($this->data_pagamento) || $this->data_pagamento == '1970-01-01') {
|
|
|
$this->data_pagamento = null;
|
|
$this->data_pagamento = null;
|
|
|
- Log::info("Setting data_pagamento to NULL in store");
|
|
|
|
|
$is_paid = false;
|
|
$is_paid = false;
|
|
|
} else {
|
|
} else {
|
|
|
- Log::info("Using data_pagamento: " . $this->data_pagamento);
|
|
|
|
|
$is_paid = true;
|
|
$is_paid = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$this->validate();
|
|
$this->validate();
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // Optimize: Use DB transaction for better performance
|
|
|
|
|
+ \DB::beginTransaction();
|
|
|
|
|
|
|
|
- if ($this->attachment) {
|
|
|
|
|
- Log::info("STORE DEBUG - Attachment original name: " . $this->attachment->getClientOriginalName());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // Create record first
|
|
|
$record = \App\Models\Record::create([
|
|
$record = \App\Models\Record::create([
|
|
|
'member_id' => $this->member_id,
|
|
'member_id' => $this->member_id,
|
|
|
'supplier_id' => $this->supplier_id,
|
|
'supplier_id' => $this->supplier_id,
|
|
|
'payment_method_id' => $this->payment_method_id,
|
|
'payment_method_id' => $this->payment_method_id,
|
|
|
'date' => $this->date,
|
|
'date' => $this->date,
|
|
|
'data_pagamento' => $this->data_pagamento,
|
|
'data_pagamento' => $this->data_pagamento,
|
|
|
- 'attachment' => '',
|
|
|
|
|
|
|
+ 'attachment' => '', // Will be updated later if needed
|
|
|
'type' => $this->type,
|
|
'type' => $this->type,
|
|
|
- 'amount' => $this->currencyToDouble($this->amount),
|
|
|
|
|
|
|
+ 'amount' => 0, // Will be calculated after rows
|
|
|
'commercial' => $this->commercial,
|
|
'commercial' => $this->commercial,
|
|
|
'numero_fattura' => $this->numero_fattura,
|
|
'numero_fattura' => $this->numero_fattura,
|
|
|
'is_paid' => $is_paid,
|
|
'is_paid' => $is_paid,
|
|
@@ -413,63 +414,83 @@ class RecordOUT extends Component
|
|
|
|
|
|
|
|
$this->dataId = $record->id;
|
|
$this->dataId = $record->id;
|
|
|
|
|
|
|
|
- $this->recordFileService->createRecordFolders($record->id, 'OUT');
|
|
|
|
|
-
|
|
|
|
|
- if ($this->attachment) {
|
|
|
|
|
- try {
|
|
|
|
|
- $attachmentPath = $this->recordFileService->uploadAttachment($this->attachment, $record->id, 'OUT');
|
|
|
|
|
-
|
|
|
|
|
- $updateResult = \DB::table('records')
|
|
|
|
|
- ->where('id', $record->id)
|
|
|
|
|
- ->update(['attachment' => $attachmentPath]);
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- $updatedRecord = \App\Models\Record::where('id', $record->id)->first();
|
|
|
|
|
|
|
+ // Update loading message
|
|
|
|
|
+ $this->emit('update-loading', 'Creazione cartelle...');
|
|
|
|
|
|
|
|
- $record->refresh();
|
|
|
|
|
-
|
|
|
|
|
- } catch (\Exception $ex) {
|
|
|
|
|
- session()->flash('warning', 'Record created but attachment upload failed: ' . $ex->getMessage());
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- Log::info("STORE DEBUG - No attachment to upload");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Create folders (this is usually fast)
|
|
|
|
|
+ $this->recordFileService->createRecordFolders($record->id, 'OUT');
|
|
|
|
|
|
|
|
|
|
+ // Create record rows first (faster than file operations)
|
|
|
|
|
+ $this->emit('update-loading', 'Salvataggio dati...');
|
|
|
$tot = 0;
|
|
$tot = 0;
|
|
|
|
|
+ $rowsData = [];
|
|
|
|
|
+
|
|
|
foreach ($this->rows as $row) {
|
|
foreach ($this->rows as $row) {
|
|
|
foreach ($row["when"] as $x => $y) {
|
|
foreach ($row["when"] as $x => $y) {
|
|
|
$row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
|
|
$row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
$imponibile = isset($row["imponibile"]) ? $this->currencyToDouble($row["imponibile"]) : null;
|
|
$imponibile = isset($row["imponibile"]) ? $this->currencyToDouble($row["imponibile"]) : null;
|
|
|
$aliquota_iva = isset($row["aliquota_iva"]) ? floatval(str_replace('%', '', $row["aliquota_iva"])) : null;
|
|
$aliquota_iva = isset($row["aliquota_iva"]) ? floatval(str_replace('%', '', $row["aliquota_iva"])) : null;
|
|
|
|
|
+ $amount = $this->currencyToDouble($row["amount"]);
|
|
|
|
|
|
|
|
- \App\Models\RecordRow::create([
|
|
|
|
|
|
|
+ $rowsData[] = [
|
|
|
'record_id' => $this->dataId,
|
|
'record_id' => $this->dataId,
|
|
|
'causal_id' => $row["causal_id"],
|
|
'causal_id' => $row["causal_id"],
|
|
|
'note' => $row["note"],
|
|
'note' => $row["note"],
|
|
|
- 'amount' => $this->currencyToDouble($row["amount"]),
|
|
|
|
|
|
|
+ 'amount' => $amount,
|
|
|
'imponibile' => $imponibile,
|
|
'imponibile' => $imponibile,
|
|
|
'aliquota_iva' => $aliquota_iva,
|
|
'aliquota_iva' => $aliquota_iva,
|
|
|
'commercial' => $row["commercial"],
|
|
'commercial' => $row["commercial"],
|
|
|
- 'when' => json_encode($row["when"])
|
|
|
|
|
- ]);
|
|
|
|
|
- $tot += $this->currencyToDouble($row["amount"]);
|
|
|
|
|
|
|
+ 'when' => json_encode($row["when"]),
|
|
|
|
|
+ 'created_at' => now(),
|
|
|
|
|
+ 'updated_at' => now()
|
|
|
|
|
+ ];
|
|
|
|
|
+ $tot += $amount;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Bulk insert for better performance
|
|
|
|
|
+ \App\Models\RecordRow::insert($rowsData);
|
|
|
|
|
+
|
|
|
|
|
+ // Update total amount
|
|
|
$record->amount = $tot;
|
|
$record->amount = $tot;
|
|
|
$record->save();
|
|
$record->save();
|
|
|
|
|
|
|
|
- session()->flash('success', 'Movimento creato');
|
|
|
|
|
|
|
+ // Handle attachment upload last (slowest operation)
|
|
|
|
|
+ if ($this->attachment) {
|
|
|
|
|
+ $this->emit('update-loading', 'Caricamento allegato...');
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // Use a more optimized approach for file upload
|
|
|
|
|
+ $attachmentPath = $this->recordFileService->uploadAttachment($this->attachment, $record->id, 'OUT');
|
|
|
|
|
+
|
|
|
|
|
+ // Update only the attachment field to avoid unnecessary queries
|
|
|
|
|
+ \DB::table('records')
|
|
|
|
|
+ ->where('id', $record->id)
|
|
|
|
|
+ ->update(['attachment' => $attachmentPath]);
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+ // Don't fail the entire operation for attachment issues
|
|
|
|
|
+ Log::error("Attachment upload failed for record {$record->id}: " . $ex->getMessage());
|
|
|
|
|
+ session()->flash('warning', 'Record creato ma caricamento allegato fallito: ' . $ex->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ \DB::commit();
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('stop-loading');
|
|
|
|
|
+ session()->flash('success', 'Movimento creato con successo');
|
|
|
|
|
+
|
|
|
$this->resetFields();
|
|
$this->resetFields();
|
|
|
$this->add = false;
|
|
$this->add = false;
|
|
|
$this->emit('setEdit', false);
|
|
$this->emit('setEdit', false);
|
|
|
} catch (\Exception $ex) {
|
|
} 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() . ')');
|
|
|
|
|
|
|
+ \DB::rollback();
|
|
|
|
|
+ $this->emit('stop-loading');
|
|
|
|
|
+
|
|
|
|
|
+ Log::error("Store error: " . $ex->getMessage());
|
|
|
|
|
+ $this->emit('flash-error', 'Errore durante il salvataggio: ' . $ex->getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
public function setDataPagamentoAttribute($value)
|
|
public function setDataPagamentoAttribute($value)
|
|
|
{
|
|
{
|
|
|
if (empty($value) || $value == '1970-01-01' || $value == '0000-00-00') {
|
|
if (empty($value) || $value == '1970-01-01' || $value == '0000-00-00') {
|
|
@@ -584,33 +605,45 @@ class RecordOUT extends Component
|
|
|
|
|
|
|
|
public function update()
|
|
public function update()
|
|
|
{
|
|
{
|
|
|
|
|
+ // Start loading state
|
|
|
|
|
+ $this->emit('start-loading', 'Aggiornamento in corso...');
|
|
|
|
|
+
|
|
|
$this->emit('refresh');
|
|
$this->emit('refresh');
|
|
|
$this->validate();
|
|
$this->validate();
|
|
|
|
|
+
|
|
|
if (empty($this->data_pagamento) || $this->data_pagamento == '') {
|
|
if (empty($this->data_pagamento) || $this->data_pagamento == '') {
|
|
|
$this->data_pagamento = null;
|
|
$this->data_pagamento = null;
|
|
|
- Log::info("Data pagamento vuota, imposto a NULL in update");
|
|
|
|
|
$is_paid = false;
|
|
$is_paid = false;
|
|
|
} else {
|
|
} else {
|
|
|
- Log::info("Data pagamento in update: " . $this->data_pagamento);
|
|
|
|
|
$is_paid = true;
|
|
$is_paid = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ \DB::beginTransaction();
|
|
|
|
|
|
|
|
$attachmentPath = $this->attachment_old;
|
|
$attachmentPath = $this->attachment_old;
|
|
|
|
|
|
|
|
|
|
+ // Handle attachment first if needed
|
|
|
if ($this->attachment) {
|
|
if ($this->attachment) {
|
|
|
|
|
+ $this->emit('update-loading', 'Aggiornamento allegato...');
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // Delete old attachment if exists
|
|
|
if ($this->attachment_old) {
|
|
if ($this->attachment_old) {
|
|
|
$this->recordFileService->deleteAttachment($this->attachment_old);
|
|
$this->recordFileService->deleteAttachment($this->attachment_old);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$attachmentPath = $this->recordFileService->uploadAttachment($this->attachment, $this->dataId, 'OUT');
|
|
$attachmentPath = $this->recordFileService->uploadAttachment($this->attachment, $this->dataId, 'OUT');
|
|
|
} catch (\Exception $ex) {
|
|
} catch (\Exception $ex) {
|
|
|
|
|
+ $this->emit('stop-loading');
|
|
|
session()->flash('error', 'Errore caricamento allegato: ' . $ex->getMessage());
|
|
session()->flash('error', 'Errore caricamento allegato: ' . $ex->getMessage());
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('update-loading', 'Aggiornamento record...');
|
|
|
|
|
+
|
|
|
|
|
+ // Update main record
|
|
|
\App\Models\Record::whereId($this->dataId)->update([
|
|
\App\Models\Record::whereId($this->dataId)->update([
|
|
|
'member_id' => $this->member_id,
|
|
'member_id' => $this->member_id,
|
|
|
'supplier_id' => $this->supplier_id,
|
|
'supplier_id' => $this->supplier_id,
|
|
@@ -624,7 +657,7 @@ class RecordOUT extends Component
|
|
|
'attachment' => $attachmentPath,
|
|
'attachment' => $attachmentPath,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- $tot = 0;
|
|
|
|
|
|
|
+ $this->emit('update-loading', 'Aggiornamento righe...');
|
|
|
|
|
|
|
|
$existingRows = \App\Models\RecordRow::where('record_id', $this->dataId)
|
|
$existingRows = \App\Models\RecordRow::where('record_id', $this->dataId)
|
|
|
->select('id', 'quantita', 'numero_linea')
|
|
->select('id', 'quantita', 'numero_linea')
|
|
@@ -634,6 +667,9 @@ class RecordOUT extends Component
|
|
|
|
|
|
|
|
\App\Models\RecordRow::where('record_id', $this->dataId)->delete();
|
|
\App\Models\RecordRow::where('record_id', $this->dataId)->delete();
|
|
|
|
|
|
|
|
|
|
+ $tot = 0;
|
|
|
|
|
+ $newRowsData = [];
|
|
|
|
|
+
|
|
|
foreach ($this->rows as $row) {
|
|
foreach ($this->rows as $row) {
|
|
|
foreach ($row["when"] as $x => $y) {
|
|
foreach ($row["when"] as $x => $y) {
|
|
|
$row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
|
|
$row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
|
|
@@ -642,57 +678,62 @@ class RecordOUT extends Component
|
|
|
$imponibile = null;
|
|
$imponibile = null;
|
|
|
if (isset($row["imponibile"]) && $row["imponibile"] !== null && $row["imponibile"] !== '') {
|
|
if (isset($row["imponibile"]) && $row["imponibile"] !== null && $row["imponibile"] !== '') {
|
|
|
$imponibile = $this->currencyToDouble($row["imponibile"]);
|
|
$imponibile = $this->currencyToDouble($row["imponibile"]);
|
|
|
- Log::info("Imponibile: " . $imponibile);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$aliquota_iva = null;
|
|
$aliquota_iva = null;
|
|
|
if (isset($row["aliquota_iva"]) && $row["aliquota_iva"] !== null && $row["aliquota_iva"] !== '') {
|
|
if (isset($row["aliquota_iva"]) && $row["aliquota_iva"] !== null && $row["aliquota_iva"] !== '') {
|
|
|
$aliquota_iva = floatval(str_replace('%', '', $row["aliquota_iva"]));
|
|
$aliquota_iva = floatval(str_replace('%', '', $row["aliquota_iva"]));
|
|
|
- Log::info("Aliquota IVA: " . $aliquota_iva);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$amount = $this->currencyToDouble($row["amount"]);
|
|
$amount = $this->currencyToDouble($row["amount"]);
|
|
|
-
|
|
|
|
|
- $imposta = null;
|
|
|
|
|
- if ($imponibile !== null) {
|
|
|
|
|
- $imposta = $amount - $imponibile;
|
|
|
|
|
- Log::info("Imposta calculated: " . $imposta);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $imposta = $imponibile !== null ? $amount - $imponibile : null;
|
|
|
|
|
|
|
|
$recordRowData = [
|
|
$recordRowData = [
|
|
|
'record_id' => $this->dataId,
|
|
'record_id' => $this->dataId,
|
|
|
'causal_id' => $row["causal_id"],
|
|
'causal_id' => $row["causal_id"],
|
|
|
'note' => $row["note"],
|
|
'note' => $row["note"],
|
|
|
- 'amount' => $this->currencyToDouble($row["amount"]),
|
|
|
|
|
|
|
+ 'amount' => $amount,
|
|
|
'commercial' => $row["commercial"],
|
|
'commercial' => $row["commercial"],
|
|
|
'when' => json_encode($row["when"]),
|
|
'when' => json_encode($row["when"]),
|
|
|
'imponibile' => $imponibile,
|
|
'imponibile' => $imponibile,
|
|
|
'aliquota_iva' => $aliquota_iva,
|
|
'aliquota_iva' => $aliquota_iva,
|
|
|
'imposta' => $imposta,
|
|
'imposta' => $imposta,
|
|
|
'divisa' => 'EUR',
|
|
'divisa' => 'EUR',
|
|
|
|
|
+ 'created_at' => now(),
|
|
|
|
|
+ 'updated_at' => now()
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
+ // Preserve existing metadata if available
|
|
|
if (isset($row["id"]) && isset($existingRows[$row["id"]])) {
|
|
if (isset($row["id"]) && isset($existingRows[$row["id"]])) {
|
|
|
$existingRow = $existingRows[$row["id"]];
|
|
$existingRow = $existingRows[$row["id"]];
|
|
|
$recordRowData['quantita'] = $existingRow['quantita'];
|
|
$recordRowData['quantita'] = $existingRow['quantita'];
|
|
|
$recordRowData['numero_linea'] = $existingRow['numero_linea'];
|
|
$recordRowData['numero_linea'] = $existingRow['numero_linea'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- Log::info("RecordRowData: " . json_encode($recordRowData));
|
|
|
|
|
- \App\Models\RecordRow::create($recordRowData);
|
|
|
|
|
- $tot += $this->currencyToDouble($row["amount"]);
|
|
|
|
|
|
|
+ $newRowsData[] = $recordRowData;
|
|
|
|
|
+ $tot += $amount;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $rec = \App\Models\Record::findOrFail($this->dataId);
|
|
|
|
|
- $rec->amount = $tot;
|
|
|
|
|
- $rec->save();
|
|
|
|
|
|
|
+ \App\Models\RecordRow::insert($newRowsData);
|
|
|
|
|
+
|
|
|
|
|
+ \DB::table('records')
|
|
|
|
|
+ ->where('id', $this->dataId)
|
|
|
|
|
+ ->update(['amount' => $tot]);
|
|
|
|
|
+
|
|
|
|
|
+ \DB::commit();
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('stop-loading');
|
|
|
|
|
+ session()->flash('success', 'Movimento aggiornato con successo');
|
|
|
|
|
|
|
|
- session()->flash('success', 'Movimento aggiornato');
|
|
|
|
|
$this->resetFields();
|
|
$this->resetFields();
|
|
|
$this->update = false;
|
|
$this->update = false;
|
|
|
$this->emit('setEdit', false);
|
|
$this->emit('setEdit', false);
|
|
|
} catch (\Exception $ex) {
|
|
} catch (\Exception $ex) {
|
|
|
- $this->emit('flash-error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
|
|
+ \DB::rollback();
|
|
|
|
|
+ $this->emit('stop-loading');
|
|
|
|
|
+
|
|
|
|
|
+ Log::error("Update error: " . $ex->getMessage());
|
|
|
|
|
+ $this->emit('flash-error', 'Errore durante l\'aggiornamento: ' . $ex->getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|