| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- 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()
- {
- }
- public function render()
- {
- $fromDate = date("Y-m-01");
- $toDate = date("Y-m-t 23:59:59");
- if ($this->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;
- }
- }
|