Parcourir la source

sconto ricevute e valore

FabioFratini il y a 8 mois
Parent
commit
4360dfe297

+ 23 - 9
app/Http/Livewire/RecordIN.php

@@ -607,8 +607,7 @@ class RecordIN extends Component
                     $no_receipt_causal = true;
 
                 if ($cau->money) {
-                    if ($rowNet >= env('MIN_MONEY', 100))
-                    {
+                    if ($rowNet >= env('MIN_MONEY', 100)) {
                         $money = new \App\Models\Money();
                         $money->member_id = $this->member_id;
                         $money->record_id = $this->dataId;
@@ -1000,6 +999,18 @@ class RecordIN extends Component
                 if ($exist != null)
                     $number = $exist->number + 1;
 
+                // Calculate totals for the receipt
+                $totalGross = 0;
+                $totalNet = 0;
+                foreach ($this->rows as $row) {
+                    $rowAmount = $this->currencyToDouble($row["amount"]);
+                    $rowSconto = isset($row['sconto']) ? $this->currencyToDouble($row['sconto']) : 0;
+                    $rowNet = max(0, $rowAmount - $rowSconto);
+
+                    $totalGross += $rowAmount;
+                    $totalNet += $rowNet;
+                }
+
                 $receipt = \App\Models\Receipt::create([
                     'record_id' => $this->dataId,
                     'member_id' => $this->member_id,
@@ -1012,14 +1023,22 @@ class RecordIN extends Component
                     'parent' => $this->parent,
                     'status' => 1,
                 ]);
+
                 foreach ($this->rows as $row) {
-                    Log::info("Row - Gross: " . $row["amount"] . ", Sconto: " . $row["sconto"] . ", Net: " . $row["amount"]);
+                    $rowAmount = $this->currencyToDouble($row["amount"]);
+                    $rowSconto = isset($row['sconto']) ? $this->currencyToDouble($row['sconto']) : 0;
+                    $rowNet = max(0, $rowAmount - $rowSconto);
+
+                    Log::info("Receipt Row - Gross: " . $rowAmount . ", Sconto: " . $rowSconto . ", Net: " . $rowNet);
+
                     \App\Models\ReceiptRow::create([
                         'receip_id' => $receipt->id,
                         'causal_id' => $row["causal_id"],
                         'note' => $row["note"],
                         'vat_id' => $row["vat_id"],
-                        'amount' => $this->currencyToDouble($row["amount"]) - $this->currencyToDouble($row["sconto"]),
+                        'amount' => $rowNet,
+                        'prediscount_amount' => $rowAmount,
+                        'sconto' => $rowSconto,
                         'commercial' => $row["commercial"],
                         'when' => json_encode($row["when"])
                     ]);
@@ -1029,10 +1048,8 @@ class RecordIN extends Component
 
                 sendReceiptEmail($receipt);
 
-
                 session()->flash('receipt', "Ricevuta " . $number . "/" . date("Y") . " creata correttamente");
 
-                // Apro la ricevuta
                 $this->emit('showReceipt', $this->currentReceip->id);
             }
         }
@@ -1087,15 +1104,12 @@ class RecordIN extends Component
 
         foreach ($this->rows as $r) {
             if ($r["amount"] != null && $r["amount"] != "") {
-                // Get the amount after discount
                 $rowAmount = $this->currencyToDouble($r["amount"]);
                 $rowSconto = isset($r['sconto']) ? $this->currencyToDouble($r['sconto']) : 0;
                 $netAmount = max(0, $rowAmount - $rowSconto);
 
-                // Add net amount to total
                 $total += $netAmount;
 
-                // Add VAT if applicable (calculated on net amount)
                 if ($r["vat_id"] > 0)
                     $total += getVatValue($netAmount, $r["vat_id"]);
             }

+ 3 - 1
app/Models/ReceiptRow.php

@@ -24,7 +24,9 @@ class ReceiptRow extends Model
         'divisa',
         'numero_linea',
         'prezzo_unitario',
-        'quantita'
+        'quantita',
+        'prediscount_amount',
+        'sconto',
     ];
 
     protected $casts = [

+ 42 - 0
database/migrations/2025_05_23_000000_add_field_sconti_to_receipts_rows_table.php

@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+
+        Schema::table('receipts_rows', function (Blueprint $table) {
+            if (!Schema::hasColumn('receipts_rows', 'prediscount_amount')) {
+                $table->decimal('prediscount_amount', 10, 2)->nullable()->after('amount');
+            }
+            if (!Schema::hasColumn('receipts_rows', 'sconto')) {
+                $table->decimal('sconto', 10, 2)->default(0)->after('prediscount_amount');
+            }
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('receipts', function (Blueprint $table) {
+            $table->dropColumn('prediscount_amount');
+        });
+
+        Schema::table('receipt_rows', function (Blueprint $table) {
+            $table->dropColumn(['prediscount_amount', 'sconto']);
+        });
+    }
+};

+ 4 - 7
resources/views/livewire/records_in.blade.php

@@ -498,17 +498,14 @@
                                                             @php
                                                                 $rowAmount = $this->currencyToDouble($rows[$idx]["amount"]);
                                                                 $rowSconto = isset($rows[$idx]["sconto"]) ? $this->currencyToDouble($rows[$idx]["sconto"]) : 0;
-                                                                $totalValue = $rowAmount; // Use original amount for display
 
-                                                                // Add VAT if applicable
+                                                                $totalValue = max(0, $rowAmount - $rowSconto);
+
                                                                 if ($rows[$idx]["vat_id"] > 0) {
-                                                                    // Use amount after sconto for VAT calculation
-                                                                    $netAmount = max(0, $rowAmount - $rowSconto);
-                                                                    $vatAmount = getVatValue($netAmount, $rows[$idx]["vat_id"]);
-                                                                    $totalValue = $rowAmount + $vatAmount;
+                                                                    $vatAmount = getVatValue($totalValue, $rows[$idx]["vat_id"]);
+                                                                    $totalValue = $totalValue + $vatAmount;
                                                                 }
 
-                                                                // Calculate per period
                                                                 $valuePerPeriod = sizeof($rows[$idx]["when"]) > 0 ? $totalValue / sizeof($rows[$idx]["when"]) : 0;
                                                             @endphp
                                                             {{ $rowAmount > 0 ? formatPrice($valuePerPeriod) : "" }}

+ 36 - 14
resources/views/receipt.blade.php

@@ -72,22 +72,44 @@
     @endif
     <hr><br>
 
-    @php
+   @php
     $total = 0;
-    @endphp
-    @foreach($receipt->rows as $row)
-        <b>Causale</b>: {{@$row->causal->getTree()}}<br><br>
-        <b>Dettaglio causale</b>: {{$row->note != '' ? $row->note : ''}}<br><br>
-        <b>Importo</b>: {{formatPrice($row->amount)}}<br><br>
-        @php
+    $totalSconto = 0;
+    $totalPrediscount = 0;
+    $hasDiscount = false;
+@endphp
+
+@foreach($receipt->rows as $row)
+    <b>Causale</b>: {{@$row->causal->getTree()}}<br><br>
+    <b>Dettaglio causale</b>: {{$row->note != '' ? $row->note : ''}}<br><br>
+
+    @if($row->sconto > 0)
+        @php $hasDiscount = true; @endphp
+        <b>Importo</b>: {{formatPrice($row->prediscount_amount)}}<br><br>
+        <b>Sconto</b>: {{formatPrice($row->sconto)}}<br><br>
+    @endif
+
+    <b>{{$row->sconto > 0 ? 'Importo finale' : 'Importo'}}</b>: {{formatPrice($row->amount)}}<br><br>
+    <hr><br>
+    @php
+        $totalSconto += $row->sconto;
+        $totalPrediscount += $row->prediscount_amount;
         $total += $row->amount;
-        @endphp
-    @endforeach
-    <br><br>
-    <b>Totale</b>: {{formatPrice($total)}}
-    <footer>
-        <small>{{env('LOCALITA', '')}} ({{env('PROVINCIA', '')}}) li {{date("d/m/Y", strtotime($receipt->created_at))}}</small>
-    </footer>
+    @endphp
+@endforeach
+
+<br><br>
+
+@if($hasDiscount)
+    <b>Totale prescontato</b>: {{formatPrice($totalPrediscount)}}<br><br>
+    <b>Sconto totale</b>: {{formatPrice($totalSconto)}}<br><br>
+@endif
+
+<b>Totale</b>: {{formatPrice($total)}}<br><br>
+
+<footer>
+    <small>{{env('LOCALITA', '')}} ({{env('PROVINCIA', '')}}) li {{date("d/m/Y", strtotime($receipt->created_at))}}</small>
+</footer>
 
 </body>
 </html>