Parcourir la source

modifiche calcolo totali reports e gestionale

ferrari il y a 2 mois
Parent
commit
7e8fc6a36b
2 fichiers modifiés avec 167 ajouts et 49 suppressions
  1. 2 2
      app/Http/Livewire/RecordINOUT.php
  2. 165 47
      app/Http/Livewire/Reports.php

+ 2 - 2
app/Http/Livewire/RecordINOUT.php

@@ -193,11 +193,11 @@ class RecordINOUT extends Component
                     $start = Carbon::createFromFormat("Y-m-d", "$year-09-01")->startOfMonth();
                     $start = Carbon::createFromFormat("Y-m-d", "$year-09-01")->startOfMonth();
                     $end = Carbon::createFromFormat("Y-m-d", "$next_year-08-31")->startOfMonth();
                     $end = Carbon::createFromFormat("Y-m-d", "$next_year-08-31")->startOfMonth();
                     foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
                     foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
-                        $pairs[] = (string)$d->month . "-" . (string)$d->year;
+                        $pairs[] = '"' . (string)$d->month . "-" . (string)$d->year . '"';
                     }
                     }
                     $pairs = implode("|", $pairs);
                     $pairs = implode("|", $pairs);
                 } else {
                 } else {
-                    $pairs = $filter;
+                    $pairs = '"' . $filter . '"';
                 }
                 }
 
 
                 $incomeQuery = \App\Models\Record::where('records.type', 'IN')
                 $incomeQuery = \App\Models\Record::where('records.type', 'IN')

+ 165 - 47
app/Http/Livewire/Reports.php

@@ -161,16 +161,19 @@ class Reports extends Component
         Log::info('Date range start: ' . $dateRange['start']);
         Log::info('Date range start: ' . $dateRange['start']);
         Log::info('Date range end: ' . $dateRange['end']);
         Log::info('Date range end: ' . $dateRange['end']);
         $monthOrder = [9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8];
         $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'];
         $monthNames = ['Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago'];
 
 
         $incomeData = array_fill(0, 12, 0);
         $incomeData = array_fill(0, 12, 0);
         $expenseData = array_fill(0, 12, 0);
         $expenseData = array_fill(0, 12, 0);
 
 
         $pairs = [];
         $pairs = [];
+        $targetPeriods = [];
         $start = $dateRange['start']->copy()->startOfMonth();
         $start = $dateRange['start']->copy()->startOfMonth();
         $end = $dateRange['end']->copy()->startOfMonth();
         $end = $dateRange['end']->copy()->startOfMonth();
         foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
         foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
-            $pairs[] = (string)$d->month . "-" . (string)$d->year;
+            $pairs[] = '"' . (string)$d->month . "-" . (string)$d->year . '"';
+            $targetPeriods[$d->month . '-' . $d->year] = true;
         }
         }
         $pairs = implode("|", $pairs);
         $pairs = implode("|", $pairs);
 
 
@@ -199,23 +202,56 @@ class Reports extends Component
         $incomeRecords = $incomeQuery->get();
         $incomeRecords = $incomeQuery->get();
 
 
         foreach ($incomeRecords as $record) {
         foreach ($incomeRecords as $record) {
-            $total_months = count(json_decode($record->when, true));
-            $matches = [];
-            if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
+            $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'];
+                }
 
 
-            $amount = $record->amount;
-            $amount += getVatValue($amount, $record->vat_id);
+                $period = $m . '-' . $y;
 
 
-            foreach ($matches[0] as $match) {
-                $m = explode("-", $match)[0];
+                if (!isset($targetPeriods[$period])) continue;
 
 
-                $monthIndex = array_search($m, $monthOrder);
-                if ($monthIndex !== false) {
-                    $incomeData[$monthIndex] += $amount / $total_months;
-                }
+                if (!isset($monthIndex[$m])) continue;
+
+                $idx = $monthIndex[$m];
+                $incomeData[$idx] += $split;
             }
             }
         }
         }
 
 
+        // 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')
         $expenseQuery = DB::table('records')
             ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
             ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
             ->join('causals', function ($join) {
             ->join('causals', function ($join) {
@@ -237,21 +273,52 @@ class Reports extends Component
             ->where('records.type', 'OUT');
             ->where('records.type', 'OUT');
         $expenseRecords = $expenseQuery->get();
         $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;
+        // 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);
+        //     $amount = $record->amount;
+        //     $amount += getVatValue($amount, $record->vat_id);
+
+        //     foreach ($matches[0] as $match) {
+        //         $m = explode("-", trim($match, '"'))[0];
 
 
-            foreach ($matches[0] as $match) {
-                $m = explode("-", $match)[0];
+        //         $monthIndex = array_search($m, $monthOrder);
+        //         if ($monthIndex !== false) {
+        //             $expenseData[$monthIndex] += $amount / $total_months;
+        //         }
+        //     }
+        // }
 
 
-                $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;
+
+                if (!isset($targetPeriods[$period])) continue;
+
+                if (!isset($monthIndex[$m])) continue;
+
+                $idx = $monthIndex[$m];
+                $expenseData[$idx] += $split;
             }
             }
         }
         }
 
 
@@ -296,10 +363,12 @@ class Reports extends Component
             $next_year = $year + 1;
             $next_year = $year + 1;
 
 
             $pairs = [];
             $pairs = [];
+            $targetPeriods = [];
             $start = Carbon::createFromFormat("Y-m-d", "$year-09-01")->startOfMonth();
             $start = Carbon::createFromFormat("Y-m-d", "$year-09-01")->startOfMonth();
             $end = Carbon::createFromFormat("Y-m-d", "$next_year-08-31")->startOfMonth();
             $end = Carbon::createFromFormat("Y-m-d", "$next_year-08-31")->startOfMonth();
             foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
             foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
-                $pairs[] = (string)$d->month . "-" . (string)$d->year;
+                $pairs[] = '"' . (string)$d->month . "-" . (string)$d->year . '"';
+                $targetPeriods[$d->month . '-' . $d->year] = true;
             }
             }
             $pairs = implode("|", $pairs);
             $pairs = implode("|", $pairs);
 
 
@@ -325,18 +394,43 @@ class Reports extends Component
             $incomeRecords = $incomeQuery->get();
             $incomeRecords = $incomeQuery->get();
 
 
             foreach ($incomeRecords as $record) {
             foreach ($incomeRecords as $record) {
-                $total_months = count(json_decode($record->when, true));
-                $matches = [];
-                if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
-
-                $matching_months = count($matches[0]);
-                $amount = $record->amount;
-                $amount += getVatValue($amount, $record->vat_id);
-                $amount *= ($matching_months / $total_months);
-                
-                $incomeData[$index] += $amount;
+                $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;
+                }
+
+                $amount = (float)$record->amount;
+                $gross  = $amount + (float)getVatValue($amount, $record->vat_id);
+
+                $fraction = $matchingMonths / $totalMonths;
+                $incomeData[$index] += $gross * $fraction;
             }
             }
 
 
+
             $expenseQuery = DB::table('records')
             $expenseQuery = DB::table('records')
                 ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
                 ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
                 ->join('causals', function ($join) {
                 ->join('causals', function ($join) {
@@ -359,16 +453,40 @@ class Reports extends Component
             $expenseRecords = $expenseQuery->get();
             $expenseRecords = $expenseQuery->get();
 
 
             foreach ($expenseRecords as $record) {
             foreach ($expenseRecords as $record) {
-                $total_months = count(json_decode($record->when, true));
-                $matches = [];
-                if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
-
-                $matching_months = count($matches[0]);
-                $amount = $record->amount;
-                $amount += getVatValue($amount, $record->vat_id);
-                $amount *= ($matching_months / $total_months);
-                
-                $expenseData[$index] += $amount;
+                $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;
+                }
+
+                $amount = (float)$record->amount;
+                $gross  = $amount + (float)getVatValue($amount, $record->vat_id);
+
+                $fraction = $matchingMonths / $totalMonths;
+                $expenseData[$index] += $gross * $fraction;
             }
             }
         }
         }
 
 
@@ -401,7 +519,7 @@ class Reports extends Component
         $start = $dateRange['start']->copy()->startOfMonth();
         $start = $dateRange['start']->copy()->startOfMonth();
         $end = $dateRange['end']->copy()->startOfMonth();
         $end = $dateRange['end']->copy()->startOfMonth();
         foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
         foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
-            $pairs[] = (string)$d->month . "-" . (string)$d->year;
+            $pairs[] = '"' . (string)$d->month . "-" . (string)$d->year . '"';
         }
         }
         $pairs = implode("|", $pairs);
         $pairs = implode("|", $pairs);
 
 
@@ -438,7 +556,7 @@ class Reports extends Component
             $amount = $record->amount;
             $amount = $record->amount;
             $amount += getVatValue($amount, $record->vat_id);
             $amount += getVatValue($amount, $record->vat_id);
             $amount *= ($matching_months / $total_months);
             $amount *= ($matching_months / $total_months);
-                
+
             $totalIncome += $amount;
             $totalIncome += $amount;
         }
         }
 
 
@@ -472,7 +590,7 @@ class Reports extends Component
             $amount = $record->amount;
             $amount = $record->amount;
             $amount += getVatValue($amount, $record->vat_id);
             $amount += getVatValue($amount, $record->vat_id);
             $amount *= ($matching_months / $total_months);
             $amount *= ($matching_months / $total_months);
-                
+
             $totalExpenses += $amount;
             $totalExpenses += $amount;
         }
         }