Просмотр исходного кода

reports - ottimizzazione aggiornamento grafici

ferrari 2 месяцев назад
Родитель
Сommit
3761b93fbb
1 измененных файлов с 54 добавлено и 12 удалено
  1. 54 12
      app/Http/Livewire/Reports.php

+ 54 - 12
app/Http/Livewire/Reports.php

@@ -161,8 +161,11 @@ class Reports extends Component
         Log::info('Date range start: ' . $dateRange['start']);
         Log::info('Date range end: ' . $dateRange['end']);
         $monthOrder = [9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8];
+        $monthIndex = [9 => 0, 10 => 1, 11 => 2, 12 => 3, 1 => 4, 2 => 5, 3 => 6, 4 => 7, 5 => 8, 6 => 9, 7 => 10, 8 => 11];
         $monthNames = ['Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago'];
 
+        $vats = $this->getVatMap();
+
         $incomeData = array_fill(0, 12, 0);
         $expenseData = array_fill(0, 12, 0);
 
@@ -204,14 +207,17 @@ class Reports extends Component
             if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
 
             $amount = $record->amount;
-            $amount += getVatValue($amount, $record->vat_id);
+            if (isset($vats[$record->vat_id]) && $vats[$record->vat_id] > 0) {
+                $vat = $vats[$record->vat_id];
+                $amount += $amount / 100 * $vat;
+            }
 
             foreach ($matches[0] as $match) {
                 $m = explode("-", trim($match, '"'))[0];
 
-                $monthIndex = array_search($m, $monthOrder);
-                if ($monthIndex !== false) {
-                    $incomeData[$monthIndex] += $amount / $total_months;
+                // $monthIndex = array_search($m, $monthOrder);
+                if (isset($monthIndex[$m])) {
+                    $incomeData[$monthIndex[$m]] += $amount / $total_months;
                 }
             }
         }
@@ -243,14 +249,17 @@ class Reports extends Component
             if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
 
             $amount = $record->amount;
-            $amount += getVatValue($amount, $record->vat_id);
+            if (isset($vats[$record->vat_id]) && $vats[$record->vat_id] > 0) {
+                $vat = $vats[$record->vat_id];
+                $amount += $amount / 100 * $vat;
+            }
 
             foreach ($matches[0] as $match) {
                 $m = explode("-", trim($match, '"'))[0];
 
-                $monthIndex = array_search($m, $monthOrder);
-                if ($monthIndex !== false) {
-                    $expenseData[$monthIndex] += $amount / $total_months;
+                // $monthIndex = array_search($m, $monthOrder);
+                if (isset($monthIndex[$m])) {
+                    $expenseData[$monthIndex[$m]] += $amount / $total_months;
                 }
             }
         }
@@ -282,6 +291,8 @@ class Reports extends Component
         $excluded_causals = \App\Models\Causal::where('no_records', true)->orWhere('money', true)->pluck('id')->toArray();
         $excluded_members = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
 
+        $vats = $this->getVatMap();
+
         $incomeData = [];
         $expenseData = [];
 
@@ -331,7 +342,10 @@ class Reports extends Component
 
                 $matching_months = count($matches[0]);
                 $amount = $record->amount;
-                $amount += getVatValue($amount, $record->vat_id);
+                if (isset($vats[$record->vat_id]) && $vats[$record->vat_id] > 0) {
+                    $vat = $vats[$record->vat_id];
+                    $amount += $amount / 100 * $vat;
+                }
                 $amount *= ($matching_months / $total_months);
 
                 $incomeData[$index] += $amount;
@@ -365,7 +379,10 @@ class Reports extends Component
 
                 $matching_months = count($matches[0]);
                 $amount = $record->amount;
-                $amount += getVatValue($amount, $record->vat_id);
+                if (isset($vats[$record->vat_id]) && $vats[$record->vat_id] > 0) {
+                    $vat = $vats[$record->vat_id];
+                    $amount += $amount / 100 * $vat;
+                }
                 $amount *= ($matching_months / $total_months);
 
                 $expenseData[$index] += $amount;
@@ -394,6 +411,8 @@ class Reports extends Component
         $dateRange = $this->getSeasonDateRange($this->seasonFilter);
         Log::info('=== getYearlySummary called ===');
 
+        $vats = $this->getVatMap();
+
         $excluded_causals = \App\Models\Causal::where('no_records', true)->orWhere('money', true)->pluck('id')->toArray();
         $excluded_members = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
 
@@ -436,7 +455,10 @@ class Reports extends Component
 
             $matching_months = count($matches[0]);
             $amount = $record->amount;
-            $amount += getVatValue($amount, $record->vat_id);
+            if (isset($vats[$record->vat_id]) && $vats[$record->vat_id] > 0) {
+                $vat = $vats[$record->vat_id];
+                $amount += $amount / 100 * $vat;
+            }
             $amount *= ($matching_months / $total_months);
 
             $totalIncome += $amount;
@@ -470,7 +492,10 @@ class Reports extends Component
 
             $matching_months = count($matches[0]);
             $amount = $record->amount;
-            $amount += getVatValue($amount, $record->vat_id);
+            if (isset($vats[$record->vat_id]) && $vats[$record->vat_id] > 0) {
+                $vat = $vats[$record->vat_id];
+                $amount += $amount / 100 * $vat;
+            }
             $amount *= ($matching_months / $total_months);
 
             $totalExpenses += $amount;
@@ -979,4 +1004,21 @@ class Reports extends Component
             'datasets' => $datasets
         ];
     }
+
+    function getVatMap()
+    {
+        static $map = null;
+
+        if ($map === null) {
+            $map = [];
+
+            $vats = \App\Models\Vat::select('id', 'value')->get();
+            foreach ($vats as $vat) {
+                $rate = (float)$vat->value;
+                $map[$vat->id] = $rate > 0 ? 1.0 + ($rate / 100.0) : 1.0;
+            }
+        }
+
+        return $map;
+    }
 }