period[] = array('value' => $month . "-" . $year, 'text' => $this->getMonth($month) . " " . $year); } } $this->payments = \App\Models\PaymentMethod::select('id', 'name')->get(); } public function getMonth($m) { $ret = ''; switch ($m) { case 1: $ret = 'Gennaio'; break; case 2: $ret = 'Febbraio'; break; case 3: $ret = 'Marzo'; break; case 4: $ret = 'Aprile'; break; case 5: $ret = 'Maggio'; break; case 6: $ret = 'Giugno'; break; case 7: $ret = 'Luglio'; break; case 8: $ret = 'Agosto'; break; case 9: $ret = 'Settembre'; break; case 10: $ret = 'Ottobre'; break; case 11: $ret = 'Novembre'; break; case 12: $ret = 'Dicembre'; break; default: $ret = ''; break; } return $ret; } public function render() { $month = 0; $year = 0; if ($this->selectedFilter == '') { $month = date("m"); $year = date("Y"); } else { list($month, $year) = explode("-", $this->selectedFilter); } /* $fromDate = date("Y-m-01"); $toDate = date("Y-m-t 23:59:59"); if ($this->selectedFilter == 1) { $month = date("m"); $year = date("m"); $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 = array(); $this->totals = array(); // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get(); $datas = \App\Models\Record::where('month', $month)->where('year', $year)->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get(); foreach($datas as $idx => $data) { if ($data->causal->no_first == null || !$data->causal->no_first) { $amount = $data->amount; $prefix = ''; if (!$data->commercial) $prefix = $idx . "$"; $causal = $prefix . $data->date . "ยง" . $data->causal->getTree(); if (isset($this->records[$causal])) { if (isset($this->records[$causal][$data->payment_method->name])) { if ($data->commercial) { if ($this->records[$causal][$data->payment_method->name][$data->type]) $amount += $this->records[$causal][$data->payment_method->name][$data->type]; } } } if (!isset($this->totals[$data->payment_method->name])) { $this->totals[$data->payment_method->name]["IN"] = 0; $this->totals[$data->payment_method->name]["OUT"] = 0; } $this->records[$causal][$data->payment_method->name][$data->type] = $amount; $this->totals[$data->payment_method->name][$data->type] += $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type]; } } // asort($this->records); /* $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; } }