Parcourir la source

dashboard - modificato calcolo blocchi costi

ferrari il y a 2 mois
Parent
commit
be5cdeae49
2 fichiers modifiés avec 116 ajouts et 14 suppressions
  1. 114 13
      app/Http/Livewire/Dashboard.php
  2. 2 1
      resources/views/livewire/dashboard.blade.php

+ 114 - 13
app/Http/Livewire/Dashboard.php

@@ -203,31 +203,132 @@ class Dashboard extends Component
             $currentDate = now()->format('Y-m-d');
             Log::info('Calculating financial stats for month', ['month' => $currentMonth]);
 
-            $this->received = \App\Models\Record::where('type', 'IN')
+            // excluded members
+            $excluded_members = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
+            // excluded payment method
+            $excluded_payment_methods = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
+            // excluded causals
+            $excluded_causals = \App\Models\Causal::orWhereIn('name', [
+                'Corrispettivo fiscale giornaliero',
+                'CFG',
+            ])->orWhere('hidden', 1)->orWhere('enabled', 0)->pluck('id')->toArray();
+
+
+            // Incassato
+            $this->received = 0;
+            $records_in = \App\Models\Record::with('rows')->where('type', 'IN')
                 ->whereRaw('DATE_FORMAT(date, "%Y-%m") = ?', [$currentMonth])
                 ->where(function ($query) {
                     $query->where('deleted', false)->orWhere('deleted', null);
-                })
-                ->sum('amount') ?? 0;
+                })->get();
+            foreach ($records_in as $record) {
+                if (in_array($record->payment_method_id, $excluded_payment_methods)) {
+                    continue;
+                }
 
-            $this->paid = \App\Models\Record::where('type', 'OUT')
-                ->whereRaw('DATE_FORMAT(date, "%Y-%m") = ?', [$currentMonth])
-                ->where('data_pagamento', '<=', $currentDate)
+                if (in_array($record->member_id, $excluded_members)) {
+                    continue;
+                }
+
+                foreach ($record->rows as $row) {
+                    if ($row->causal_id) {
+                        $causal = \App\Models\Causal::find($row->causal_id);
+
+                        if (!$causal || (isset($causal->hidden) && $causal->hidden)) {
+                            continue;
+                        }
+
+                        if (in_array($row->causal_id, $excluded_causals)) {
+                            continue;
+                        }
+
+                        $this->received += $row->amount;
+                        if ($row->vat_id > 0) {
+                            $this->received += getVatValue($row->amount, $row->vat_id);
+                        }
+                    }
+                }
+            }
+            // END - Incassato
+
+
+            // Pagato
+            $this->paid = 0;
+            $records_out = \App\Models\Record::with('rows')->where('type', 'OUT')
+                ->whereRaw('DATE_FORMAT(data_pagamento, "%Y-%m") = ?', [$currentMonth])
                 ->where(function ($query) {
                     $query->where('deleted', false)->orWhere('deleted', null);
-                })
-                ->sum('amount') ?? 0;
+                })->get();
+            foreach ($records_out as $record) {
+                if (in_array($record->payment_method_id, $excluded_payment_methods)) {
+                    continue;
+                }
 
-            $this->toPay = \App\Models\Record::where('type', 'OUT')
-                ->whereRaw('DATE_FORMAT(date, "%Y-%m") = ?', [$currentMonth])
-                ->where(function ($query) use($currentDate) {
+                if (in_array($record->member_id, $excluded_members)) {
+                    continue;
+                }
+
+                foreach ($record->rows as $row) {
+                    if ($row->causal_id) {
+                        $causal = \App\Models\Causal::find($row->causal_id);
+
+                        if (!$causal || (isset($causal->hidden) && $causal->hidden)) {
+                            continue;
+                        }
+
+                        if (in_array($row->causal_id, $excluded_causals)) {
+                            continue;
+                        }
+
+                        $this->paid += $row->amount;
+                        if ($row->vat_id > 0) {
+                            $this->paid += getVatValue($row->amount, $row->vat_id);
+                        }
+                    }
+                }
+            }
+            // END - Pagato
+
+
+            // Da pagare
+            $this->toPay = 0;
+            $records_toPay = \App\Models\Record::with('rows')->where('type', 'OUT')
+                ->where(function ($query) use ($currentDate) {
                     $query->where('data_pagamento', '>', $currentDate)
                         ->orWhereNull('data_pagamento');
                 })
                 ->where(function ($query) {
                     $query->where('deleted', false)->orWhere('deleted', null);
-                })
-                ->sum('amount') ?? 0;
+                })->get();
+            foreach ($records_toPay as $record) {
+                if (in_array($record->payment_method_id, $excluded_payment_methods)) {
+                    continue;
+                }
+
+                if (in_array($record->member_id, $excluded_members)) {
+                    continue;
+                }
+
+                foreach ($record->rows as $row) {
+                    if ($row->causal_id) {
+                        $causal = \App\Models\Causal::find($row->causal_id);
+
+                        if (!$causal || (isset($causal->hidden) && $causal->hidden)) {
+                            continue;
+                        }
+
+                        if (in_array($row->causal_id, $excluded_causals)) {
+                            continue;
+                        }
+
+                        $this->toPay += $row->amount;
+                        if ($row->vat_id > 0) {
+                            $this->toPay += getVatValue($row->amount, $row->vat_id);
+                        }
+                    }
+                }
+            }
+            // END - Da pagare
 
             $endTime = microtime(true);
             Log::info('Financial stats loaded successfully', [

+ 2 - 1
resources/views/livewire/dashboard.blade.php

@@ -244,7 +244,8 @@
 
             <div class="dashboard-card dashboard-stat card-inverted card-topay">
                 <div class="dashboard-card-header">
-                    <div class="dashboard-card-title">Da pagare<br/><small><small><i>{{$current_month}}</i></small></small></div>
+                    {{-- <div class="dashboard-card-title">Da pagare<br/><small><small><i>{{$current_month}}</i></small></small></div> --}}
+                    <div class="dashboard-card-title">Da pagare<br/><small><small><i>&nbsp;</i></small></small></div>
                     <i class="dashboard-card-icon fa-solid fa-money-bill-transfer"></i>
                 </div>
                 <div class="dashboard-card-value">{{number_format($toPay, 2, ",", ".")}}</div>