Browse Source

final fixes import fatture

FabioFratini 9 months ago
parent
commit
3ce65b8c5f

+ 99 - 17
app/Http/Livewire/RecordOUT.php

@@ -126,7 +126,15 @@ class RecordOUT extends Component
         //$this->amount = null;
         $this->commercial = 1;
         $this->rows = array();
-        $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),  'amount' => null, 'note' => '', 'commercial' => 0);
+        $this->rows[] = array(
+            'causal_id' => null,
+            'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),
+            'amount' => null,
+            'imponibile' => null,
+            'aliquota_iva' => null,
+            'note' => '',
+            'commercial' => 0
+        );
         $this->emit('load-data-table');
     }
 
@@ -363,11 +371,17 @@ class RecordOUT extends Component
                 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"],
                     'note' => $row["note"],
                     'amount' => $this->currencyToDouble($row["amount"]),
+                    'imponibile' => $imponibile,
+                    'aliquota_iva' => $aliquota_iva,
                     'commercial' => $row["commercial"],
                     'when' => json_encode($row["when"])
                 ]);
@@ -414,10 +428,30 @@ class RecordOUT extends Component
                 $this->update = true;
                 $this->add = false;
 
-                $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount')->get()->toArray();
-                foreach ($this->rows as $i => $r) {
-                    $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
-                    $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
+                $this->rows = [];
+
+                $recordRows = \App\Models\RecordRow::where('record_id', $this->dataId)->get();
+
+                foreach ($recordRows as $recordRow) {
+                    $rowData = [
+                        'causal_id' => $recordRow->causal_id,
+                        'note' => $recordRow->note,
+                        'commercial' => $recordRow->commercial,
+                        'when' => json_decode($recordRow->when),
+                        'amount' => formatPrice($recordRow->amount)
+                    ];
+
+                    // Add imponibile field if available in the database
+                    if (isset($recordRow->imponibile)) {
+                        $rowData['imponibile'] = formatPrice($recordRow->imponibile);
+                    }
+
+                    // Add aliquota_iva field if available in the database
+                    if (isset($recordRow->aliquota_iva)) {
+                        $rowData['aliquota_iva'] = $recordRow->aliquota_iva . '%';
+                    }
+
+                    $this->rows[] = $rowData;
                 }
             }
         } catch (\Exception $ex) {
@@ -429,39 +463,79 @@ class RecordOUT extends Component
     {
         $this->emit('refresh');
         $this->validate();
+        Log::info("Rows: pipo" );
+
         try {
             \App\Models\Record::whereId($this->dataId)->update([
                 'member_id' => $this->member_id,
                 'supplier_id' => $this->supplier_id,
-                //'causal_id' => $this->causal_id,
                 'payment_method_id' => $this->payment_method_id,
                 'date' => $this->date,
                 'data_pagamento' => $this->data_pagamento,
-                //'month' => $this->month,
-                //'year' => $this->year,
-                //'note' => $this->note,
                 'type' => $this->type,
-                //'amount' => $this->currencyToDouble($this->amount),
                 'commercial' => $this->commercial,
             ]);
 
             $tot = 0;
 
-            // Elimino le righe
+            $existingRows = \App\Models\RecordRow::where('record_id', $this->dataId)
+            ->select('id', 'quantita', 'numero_linea')
+            ->get()
+            ->keyBy('id')
+            ->toArray();
+
+            // Delete existing rows
             \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
-            // Inserisco le righe
+
+            // Insert updated rows
             foreach ($this->rows as $row) {
                 foreach ($row["when"] as $x => $y) {
                     $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
                 }
-                \App\Models\RecordRow::create([
+
+                // Get imponibile and aliquota_iva values if they exist
+                $imponibile = null;
+                if (isset($row["imponibile"]) && $row["imponibile"] !== null && $row["imponibile"] !== '') {
+                    $imponibile = $this->currencyToDouble($row["imponibile"]);
+                    Log::info("Imponibile: " . $imponibile);
+                }
+
+                $aliquota_iva = null;
+                if (isset($row["aliquota_iva"]) && $row["aliquota_iva"] !== null && $row["aliquota_iva"] !== '') {
+                    $aliquota_iva = floatval(str_replace('%', '', $row["aliquota_iva"]));
+                    Log::info("Aliquota IVA: " . $aliquota_iva);
+                }
+
+                $amount = $this->currencyToDouble($row["amount"]);
+
+                // Calculate imposta (amount - imponibile) if imponibile exists
+                $imposta = null;
+                if ($imponibile !== null) {
+                    $imposta = $amount - $imponibile;
+                    Log::info("Imposta calculated: " . $imposta);
+                }
+
+                $recordRowData = [
                     'record_id' => $this->dataId,
                     'causal_id' => $row["causal_id"],
                     'note' => $row["note"],
                     'amount' => $this->currencyToDouble($row["amount"]),
                     'commercial' => $row["commercial"],
-                    'when' => json_encode($row["when"])
-                ]);
+                    'when' => json_encode($row["when"]),
+                    'imponibile' => $imponibile,
+                    'aliquota_iva' => $aliquota_iva,
+                    'imposta' => $imposta,
+                    'divisa' => 'EUR', // Assuming the currency is always EUR
+                ];
+
+                if (isset($row["id"]) && isset($existingRows[$row["id"]])) {
+                    $existingRow = $existingRows[$row["id"]];
+                    $recordRowData['quantita'] = $existingRow['quantita'];
+                    $recordRowData['numero_linea'] = $existingRow['numero_linea'];
+                }
+
+                Log::info("RecordRowData: " . json_encode($recordRowData));
+                \App\Models\RecordRow::create($recordRowData);
                 $tot += $this->currencyToDouble($row["amount"]);
             }
 
@@ -577,7 +651,15 @@ class RecordOUT extends Component
 
     public function addRow()
     {
-        $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),  'amount' => null, 'note' => '', 'commercial' => 0);
+        $this->rows[] = array(
+            'causal_id' => null,
+            'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),
+            'amount' => null,
+            'imponibile' => null,
+            'aliquota_iva' => null,
+            'note' => '',
+            'commercial' => 0
+        );
         $this->emit('load-select');
     }
 
@@ -685,7 +767,7 @@ class RecordOUT extends Component
                     //$receipt = $this->createReceipt($record->id, $supplier->id, $paymentMethodId, $fatturaData);
 
                     // Crea le righe della ricevuta
-                   // $this->createReceiptRows($receipt->id, $fatturaData);
+                    // $this->createReceiptRows($receipt->id, $fatturaData);
 
                     $importCount++;
                     Log::info("Fattura importata con successo: {$fatturaData['numeroFattura']}, Fornitore: {$fatturaData['denominazione']}");

+ 6 - 0
app/Models/RecordRow.php

@@ -19,6 +19,12 @@ class RecordRow extends Model
         'vat_id',
         'note',
         'commercial',
+        'aliquota_iva',
+        'imponibile',
+        'imposta',
+        'divisa',
+        'quantita',
+        'numero_linea'
     ];
 
     public function causal()

+ 52 - 0
resources/views/livewire/records_out.blade.php

@@ -339,6 +339,48 @@
                                 </div>
                             </div>
 
+                            <div class="row gx-2 mt-5 align-items-center">
+                                <div class="col-md-6">
+                                    <span class="total primary">Imponibile</span>
+                                </div>
+                                <div class="col-md-6">
+                                    @if($add)
+                                        <input type="text" class="form-control totalInput text-end @error('rows.{{$idx}}.imponibile') is-invalid @enderror"
+                                            id="rows.{{$idx}}.imponibile" wire:model="rows.{{$idx}}.imponibile"
+                                            wire:keydown.enter="store(false)" onkeyup="onlyNumberAmount(this)" placeholder="€ 0,00">
+                                    @endif
+                                    @if($update)
+                                        <input type="text" class="form-control totalInput text-end @error('rows.{{$idx}}.imponibile') is-invalid @enderror"
+                                            id="rows.{{$idx}}.imponibile" placeholder="€ 0,00" wire:model="rows.{{$idx}}.imponibile"
+                                            onkeyup="onlyNumberAmount(this)" wire:keydown.enter="update(false)">
+                                    @endif
+                                    @error('rows.'. $idx . '.imponibile')
+                                        <span style="margin-top: 0.25rem; font-size: 0.875em; color: var(--bs-form-invalid-color);">{{ $message }}</span>
+                                    @enderror
+                                </div>
+                            </div>
+
+                            <div class="row gx-2 mt-5 align-items-center">
+                                <div class="col-md-6">
+                                    <span class="total primary">Aliquota IVA (%)</span>
+                                </div>
+                                <div class="col-md-6">
+                                    @if($add)
+                                        <input type="text" class="form-control text-end @error('rows.{{$idx}}.aliquota_iva') is-invalid @enderror"
+                                            id="rows.{{$idx}}.aliquota_iva" wire:model="rows.{{$idx}}.aliquota_iva"
+                                            wire:keydown.enter="store(false)" onkeyup="onlyNumberPercent(this)" placeholder="0%">
+                                    @endif
+                                    @if($update)
+                                        <input type="text" class="form-control text-end @error('rows.{{$idx}}.aliquota_iva') is-invalid @enderror"
+                                            id="rows.{{$idx}}.aliquota_iva" placeholder="0%" wire:model="rows.{{$idx}}.aliquota_iva"
+                                            onkeyup="onlyNumberPercent(this)" wire:keydown.enter="update(false)">
+                                    @endif
+                                    @error('rows.'. $idx . '.aliquota_iva')
+                                        <span style="margin-top: 0.25rem; font-size: 0.875em; color: var(--bs-form-invalid-color);">{{ $message }}</span>
+                                    @enderror
+                                </div>
+                            </div>
+
 
                             <div class="row gx-2 mt-5 align-items-center">
                                 <div class="col-md-6">
@@ -1094,6 +1136,16 @@
 
     });
 
+    function onlyNumberPercent(input) {
+        let v = input.value.replace(/[^\d]/g, '');
+        if (v.length > 3) v = v.slice(0, 3);
+
+        // Ensure percentage doesn't exceed 100
+        if (parseInt(v) > 100) v = '100';
+
+        input.value = v + "%";
+    }
+
     </script>
 
 @endpush