Luca Parisio 2 éve
szülő
commit
56a2796e77

+ 109 - 0
app/Http/Livewire/Dashboard.php

@@ -13,6 +13,13 @@ class Dashboard extends Component
     public $totTodayIn = 0;
     public $totTodayOut = 0;
     public $dayName;
+    public $in;
+    public $out;
+    public $members;
+
+    public array $membersDatas = [];
+    public array $recordDatas = [];
+    public array $labels = [];
 
     public function render()
     {
@@ -29,6 +36,108 @@ class Dashboard extends Component
         $this->totTodayIn = \App\Models\Record::where('type', 'IN')->where('date', date("Y-m-d"))->sum('amount');
         $this->totTodayOut = \App\Models\Record::where('type', 'OUT')->where('date', date("Y-m-d"))->sum('amount');
 
+        $this->members = \App\Models\Member::whereBetween('created_at', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("COUNT(*) as total, created_at"))->groupBy('created_at')->get();
+
+        $this->in = \App\Models\Record::where('type', 'IN')->whereBetween('date', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
+        $this->out = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
+
+        $this->labels = $this->getLabels();
+
+        $this->memberDatas = [
+            [
+                'label' => 'Utenti',
+                'backgroundColor' => 'blue',
+                'borderColor' => 'blue',
+                // 'data' => $this->getRandomData(),
+                'data' => $this->getMemberData(),
+            ]
+        ];
+
+        $this->recordDatas = [
+            [
+                'label' => 'Entrate',
+                'backgroundColor' => 'green',
+                'borderColor' => 'green',
+                // 'data' => $this->getRandomData(),
+                'data' => $this->getRecordData('IN'),
+            ],
+            [
+                'label' => 'Uscite',
+                'backgroundColor' => 'red',
+                'borderColor' => 'red',
+                'data' => $this->getRecordData('OUT'),
+            ]
+        ];
+    }
+
+    private function getLabels()
+    {
+        $labels = array();
+        for($i=0; $i<=7; $i++)
+        {
+            $labels[] = date("d/M", strtotime('-' . $i . ' days'));
+        }
+        return array_reverse($labels);
+    }
+
+    private function getRecordData($type)
+    {
+        $data = [];
+        for($i=0; $i<=7; $i++)
+        {
+            if ($type == 'IN')
+            {
+                $found = false;
+                foreach($this->in as $in)
+                {
+                    if (date("Y-m-d", strtotime($in->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
+                    {
+                        $data[] = $in->total;
+                        $found = true;
+                    }
+                }
+                if (!$found)
+                    $data[] = 0;
+            }
+            if ($type == 'OUT')
+            {
+                $found = false;
+                foreach($this->out as $out)
+                {
+                    if (date("Y-m-d", strtotime($out->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
+                    {
+                        $data[] = $out->total;
+                        $found = true;
+                    }
+                }
+                if (!$found)
+                    $data[] = 0;
+            }
+        }
+
+        return array_reverse($data);
+    }
+
+    private function getMemberData()
+    {
+        $data = [];
+        for($i=0; $i<=7; $i++)
+        {
+            $found = false;
+            foreach($this->members as $member)
+            {
+                if (date("Y-m-d", strtotime($member->created_at)) == date("Y-m-d", strtotime('-' . $i . ' days')))
+                {
+                    $data[] = $member->total;
+                    $found = true;
+                }
+            }
+            if (!$found)
+                $data[] = 0;
+
+        }
+
+        return array_reverse($data);
     }
 
 }

+ 94 - 3
app/Http/Livewire/Record.php

@@ -2,11 +2,20 @@
 
 namespace App\Http\Livewire;
 use Livewire\Component;
+use DateInterval;
+use DatePeriod;
+use DateTime;
 
 class Record extends Component
 {
     public $records, $dataId;
 
+    public $in;
+    public $out;
+
+    public array $recordDatas = [];
+    public array $labels = [];
+
     public $selectedFilter = 0;
 
     public function mount()
@@ -16,19 +25,101 @@ class Record extends Component
     public function render()
     {
         $fromDate = date("Y-m-01");
-        $toDate = date("Y-m-t");
+        $toDate = date("Y-m-t 23:59:59");
         if ($this->selectedFilter == 1)
         {
             $fromDate = date("Y-01-01");
-            $toDate = date("Y-12-31");
+            $toDate = date("Y-12-31 23:59:59");
         }
         if ($this->selectedFilter == 2)
         {
             $fromDate = date("Y-01-01");
-            $toDate = date("Y-12-31");
+            $toDate = date("Y-12-31 23:59:59");
         }
         $this->records = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
+
+        $this->in = \App\Models\Record::where('type', 'IN')->whereBetween('date', [$fromDate, $toDate])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
+        $this->out = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
+
+        $this->labels = $this->getLabels($fromDate, $toDate);
+
+        $this->recordDatas = [
+            [
+                'label' => 'Entrate',
+                'backgroundColor' => 'green',
+                'borderColor' => 'green',
+                // 'data' => $this->getRandomData(),
+                'data' => $this->getRecordData('IN', $fromDate, $toDate),
+            ],
+            [
+                'label' => 'Uscite',
+                'backgroundColor' => 'red',
+                'borderColor' => 'red',
+                'data' => $this->getRecordData('OUT', $fromDate, $toDate),
+            ]
+        ];
+
         return view('livewire.records');
     }
 
+    private function getLabels($fromDate, $toDate)
+    {
+
+        $begin = new DateTime($fromDate);
+        $end = new DateTime($toDate);
+
+        $interval = DateInterval::createFromDateString('1 day');
+        $date_range = new DatePeriod($begin, $interval, $end);
+        foreach ($date_range as $date)
+        {
+            $labels[] = $date->format('d/M');
+        }
+
+        return $labels;
+    }
+
+    private function getRecordData($type, $fromDate, $toDate)
+    {
+        $data = [];
+        $begin = new DateTime($fromDate);
+        $end = new DateTime($toDate);
+
+        $interval = DateInterval::createFromDateString('1 day');
+        $date_range = new DatePeriod($begin, $interval, $end);
+
+        foreach ($date_range as $date)
+        {
+            if ($type == 'IN')
+            {
+                $found = false;
+                foreach($this->in as $in)
+                {
+                    if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
+                    {
+                        $data[] = number_format($in->total, 0, "", "");
+                        $found = true;
+                    }
+                }
+                if (!$found)
+                    $data[] = 0;
+            }
+            if ($type == 'OUT')
+            {
+                $found = false;
+                foreach($this->out as $out)
+                {
+                    if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
+                    {
+                        $data[] = number_format($out->total, 0, "", "");
+                        $found = true;
+                    }
+                }
+                if (!$found)
+                    $data[] = 0;
+            }
+        }
+
+        return $data;
+    }
+
 }

+ 2 - 0
resources/views/layouts/app.blade.php

@@ -348,6 +348,8 @@
 
     @livewireScripts
 
+    @stack('scripts')
+
 </body>
 
 </html>

+ 62 - 4
resources/views/livewire/dashboard.blade.php

@@ -43,8 +43,8 @@
             </div>
         </div>
         <div class="anagrafica_chart">
-            <h1>Utenti</h1>
-
+            <h1>Utenti iscritti ultimi 7 giorni</h1>
+            <canvas id="userChart"></canvas>
         </div>
         </section>
         <section id="dash_contabilita">
@@ -78,11 +78,69 @@
             </div>
         </div>
         <div class="contabilita--chart">
-            <h1>Entrate/Uscite</h1>
-
+            <h1>Entrate/Uscite ultimi 7 giorni</h1>
+            <canvas id="recordChart"></canvas>
         </div>
         </section>
 
     </main>
 
 </div>
+
+@push('scripts')
+    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
+@endpush
+
+
+@push('scripts')
+    <script>
+        const chart = new Chart(
+            document.getElementById('recordChart'), {
+                type: 'line',
+                data: {
+                    labels: @json($labels),
+                    datasets: @json($recordDatas)
+                },
+                options: {
+                    plugins: {
+                        legend: {
+                            position: 'bottom'
+                        }
+                    },
+                    responsive: true,
+                    elements: {
+                        line: {
+                            tension : 0.4  // smooth lines
+                        },
+                    },
+                }
+            }
+        );
+        const chartX = new Chart(
+            document.getElementById('userChart'), {
+                type: 'line',
+                data: {
+                    labels: @json($labels),
+                    datasets: @json($memberDatas)
+                },
+                options: {
+                    plugins: {
+                        legend: {
+                            position: 'bottom'
+                        }
+                    },
+                    responsive: true,
+                    elements: {
+                        line: {
+                            tension : 0.4  // smooth lines
+                        },
+                    },
+                }
+            }
+        );
+        Livewire.on('updateChart', data => {
+            chart.data = data;
+            chart.update();
+        });
+    </script>
+@endpush

+ 44 - 1
resources/views/livewire/records.blade.php

@@ -31,7 +31,11 @@
     </section>
 
     <section id="resume-table">
-        <div class="compare--chart_wrapper d-none"></div>
+
+
+            <canvas id="recordChart"></canvas>
+
+
 
         <table class="table tablesaw tablesaw-stack" data-tablesaw="" id="tablesaw-350">
             <thead>
@@ -86,3 +90,42 @@
 
 
 </div>
+
+@push('scripts')
+    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
+@endpush
+
+
+@push('scripts')
+    <script>
+        const chart = new Chart(
+            document.getElementById('recordChart'), {
+                type: 'line',
+                data: {
+                    labels: @json($labels),
+                    datasets: @json($recordDatas)
+                },
+                options: {
+                    height:200,
+                    plugins: {
+                        legend: {
+                            position: 'bottom'
+                        }
+                    },
+                    responsive: true,
+                    elements: {
+                        line: {
+                            tension : 0.4  // smooth lines
+                        },
+                    },
+
+                }
+            }
+        );
+
+        Livewire.on('updateChart', data => {
+            chart.data = data;
+            chart.update();
+        });
+    </script>
+@endpush