| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use Carbon\Carbon;
- class Dashboard extends Component
- {
- public $totMembers = 0;
- public $totSuppliers = 0;
- 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()
- {
- return view('livewire.dashboard');
- }
- public function mount()
- {
- $this->dayName = Carbon::now()->locale('it_IT')->dayName;
- $this->totMembers = \App\Models\Member::count();
- $this->totSuppliers = \App\Models\Supplier::count();
- $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);
- }
- }
|