selectedFilter == 1) { $fromDate = date("Y-01-01"); $toDate = date("Y-12-31 23:59:59"); } if ($this->selectedFilter == 2) { $fromDate = date("Y-01-01"); $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; } }