소스 검색

modifiche calcolo totali reports e gestionale

ferrari 2 달 전
부모
커밋
06a960f2a9
1개의 변경된 파일39개의 추가작업 그리고 157개의 파일을 삭제
  1. 39 157
      app/Http/Livewire/Reports.php

+ 39 - 157
app/Http/Livewire/Reports.php

@@ -161,19 +161,16 @@ 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'];
 
         $incomeData = array_fill(0, 12, 0);
         $expenseData = array_fill(0, 12, 0);
 
         $pairs = [];
-        $targetPeriods = [];
         $start = $dateRange['start']->copy()->startOfMonth();
         $end = $dateRange['end']->copy()->startOfMonth();
         foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
             $pairs[] = '"' . (string)$d->month . "-" . (string)$d->year . '"';
-            $targetPeriods[$d->month . '-' . $d->year] = true;
         }
         $pairs = implode("|", $pairs);
 
@@ -202,56 +199,23 @@ class Reports extends Component
         $incomeRecords = $incomeQuery->get();
 
         foreach ($incomeRecords as $record) {
-            $when = json_decode($record->when, true);
-            if (empty($when) || !is_array($when)) continue;
-
-            $amount = (float)$record->amount;
-            $totalWithVat = $amount + (float)getVatValue($amount, $record->vat_id);
-
-            $totalMonths = count($when);
-            $split = $totalWithVat / max(1, $totalMonths);
-
-            foreach ($when as $w) {
-                if (isset($w['period'])) {
-                    [$mStr, $yStr] = explode('-', (string)$w['period']);
-                    $m = (int)$mStr;
-                    $y = (int)$yStr;
-                } else {
-                    $m = (int)$w['month'];
-                    $y = (int)$w['year'];
-                }
-
-                $period = $m . '-' . $y;
+            $total_months = count(json_decode($record->when, true));
+            $matches = [];
+            if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
 
-                if (!isset($targetPeriods[$period])) continue;
+            $amount = $record->amount;
+            $amount += getVatValue($amount, $record->vat_id);
 
-                if (!isset($monthIndex[$m])) continue;
+            foreach ($matches[0] as $match) {
+                $m = explode("-", trim($match, '"'))[0];
 
-                $idx = $monthIndex[$m];
-                $incomeData[$idx] += $split;
+                $monthIndex = array_search($m, $monthOrder);
+                if ($monthIndex !== false) {
+                    $incomeData[$monthIndex] += $amount / $total_months;
+                }
             }
         }
 
-        // foreach ($incomeRecords as $record) {
-        //     $total_months = count(json_decode($record->when, true));
-        //     dd($record->when);
-        //     $matches = [];
-        //     if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
-
-        //     $amount = $record->amount;
-        //     $amount += getVatValue($amount, $record->vat_id);
-
-        //     foreach ($matches[0] as $match) {
-        //         $m = explode("-", trim($match, '"'))[0];
-
-        //         $monthOrder = [9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8];
-        //         $monthIndex = array_search($m, $monthOrder);
-        //         if ($monthIndex !== false) {
-        //             $incomeData[$monthIndex] += $amount / $total_months;
-        //         }
-        //     }
-        // }
-
         $expenseQuery = DB::table('records')
             ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
             ->join('causals', function ($join) {
@@ -273,52 +237,21 @@ class Reports extends Component
             ->where('records.type', 'OUT');
         $expenseRecords = $expenseQuery->get();
 
-        // foreach ($expenseRecords as $record) {
-        //     $total_months = count(json_decode($record->when, true));
-        //     $matches = [];
-        //     if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
-
-        //     $amount = $record->amount;
-        //     $amount += getVatValue($amount, $record->vat_id);
-
-        //     foreach ($matches[0] as $match) {
-        //         $m = explode("-", trim($match, '"'))[0];
-
-        //         $monthIndex = array_search($m, $monthOrder);
-        //         if ($monthIndex !== false) {
-        //             $expenseData[$monthIndex] += $amount / $total_months;
-        //         }
-        //     }
-        // }
-
         foreach ($expenseRecords as $record) {
-            $when = json_decode($record->when, true);
-            if (empty($when) || !is_array($when)) continue;
-
-            $amount = (float)$record->amount;
-            $totalWithVat = $amount + (float)getVatValue($amount, $record->vat_id);
-
-            $totalMonths = count($when);
-            $split = $totalWithVat / max(1, $totalMonths);
-
-            foreach ($when as $w) {
-                if (isset($w['period'])) {
-                    [$mStr, $yStr] = explode('-', (string)$w['period']);
-                    $m = (int)$mStr;
-                    $y = (int)$yStr;
-                } else {
-                    $m = (int)$w['month'];
-                    $y = (int)$w['year'];
-                }
-
-                $period = $m . '-' . $y;
+            $total_months = count(json_decode($record->when, true));
+            $matches = [];
+            if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
 
-                if (!isset($targetPeriods[$period])) continue;
+            $amount = $record->amount;
+            $amount += getVatValue($amount, $record->vat_id);
 
-                if (!isset($monthIndex[$m])) continue;
+            foreach ($matches[0] as $match) {
+                $m = explode("-", trim($match, '"'))[0];
 
-                $idx = $monthIndex[$m];
-                $expenseData[$idx] += $split;
+                $monthIndex = array_search($m, $monthOrder);
+                if ($monthIndex !== false) {
+                    $expenseData[$monthIndex] += $amount / $total_months;
+                }
             }
         }
 
@@ -363,12 +296,10 @@ class Reports extends Component
             $next_year = $year + 1;
 
             $pairs = [];
-            $targetPeriods = [];
             $start = Carbon::createFromFormat("Y-m-d", "$year-09-01")->startOfMonth();
             $end = Carbon::createFromFormat("Y-m-d", "$next_year-08-31")->startOfMonth();
             foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
                 $pairs[] = '"' . (string)$d->month . "-" . (string)$d->year . '"';
-                $targetPeriods[$d->month . '-' . $d->year] = true;
             }
             $pairs = implode("|", $pairs);
 
@@ -394,43 +325,18 @@ class Reports extends Component
             $incomeRecords = $incomeQuery->get();
 
             foreach ($incomeRecords as $record) {
-                $when = json_decode($record->when, true);
-                if (empty($when) || !is_array($when)) {
-                    continue;
-                }
-
-                $totalMonths = count($when);
-                if ($totalMonths === 0) {
-                    continue;
-                }
-
-                $matchingMonths = 0;
-                foreach ($when as $w) {
-                    if (isset($w['period'])) {
-                        [$mStr, $yStr] = explode('-', (string)$w['period']);
-                        $m = (int)$mStr;
-                        $y = (int)$yStr;
-                    } else {
-                        $m = (int)$w['month'];
-                        $y = (int)$w['year'];
-                    }
-                    if (isset($targetPeriods[$m . '-' . $y])) {
-                        ++$matchingMonths;
-                    }
-                }
-
-                if ($matchingMonths === 0) {
-                    continue;
-                }
+                $total_months = count(json_decode($record->when, true));
+                $matches = [];
+                if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
 
-                $amount = (float)$record->amount;
-                $gross  = $amount + (float)getVatValue($amount, $record->vat_id);
+                $matching_months = count($matches[0]);
+                $amount = $record->amount;
+                $amount += getVatValue($amount, $record->vat_id);
+                $amount *= ($matching_months / $total_months);
 
-                $fraction = $matchingMonths / $totalMonths;
-                $incomeData[$index] += $gross * $fraction;
+                $incomeData[$index] += $amount;
             }
 
-
             $expenseQuery = DB::table('records')
                 ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
                 ->join('causals', function ($join) {
@@ -453,40 +359,16 @@ class Reports extends Component
             $expenseRecords = $expenseQuery->get();
 
             foreach ($expenseRecords as $record) {
-                $when = json_decode($record->when, true);
-                if (empty($when) || !is_array($when)) {
-                    continue;
-                }
+                $total_months = count(json_decode($record->when, true));
+                $matches = [];
+                if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
 
-                $totalMonths = count($when);
-                if ($totalMonths === 0) {
-                    continue;
-                }
+                $matching_months = count($matches[0]);
+                $amount = $record->amount;
+                $amount += getVatValue($amount, $record->vat_id);
+                $amount *= ($matching_months / $total_months);
 
-                $matchingMonths = 0;
-                foreach ($when as $w) {
-                    if (isset($w['period'])) {
-                        [$mStr, $yStr] = explode('-', (string)$w['period']);
-                        $m = (int)$mStr;
-                        $y = (int)$yStr;
-                    } else {
-                        $m = (int)$w['month'];
-                        $y = (int)$w['year'];
-                    }
-                    if (isset($targetPeriods[$m . '-' . $y])) {
-                        ++$matchingMonths;
-                    }
-                }
-
-                if ($matchingMonths === 0) {
-                    continue;
-                }
-
-                $amount = (float)$record->amount;
-                $gross  = $amount + (float)getVatValue($amount, $record->vat_id);
-
-                $fraction = $matchingMonths / $totalMonths;
-                $expenseData[$index] += $gross * $fraction;
+                $expenseData[$index] += $amount;
             }
         }
 
@@ -510,7 +392,7 @@ class Reports extends Component
     public function getYearlySummary()
     {
         $dateRange = $this->getSeasonDateRange($this->seasonFilter);
-        Log::info('=== getyearlyTotals called ===');
+        Log::info('=== getYearlySummary called ===');
 
         $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();