| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use DateInterval;
- use DatePeriod;
- use DateTime;
- class Record extends Component
- {
- public $records, $dataId, $totals;
- public $in;
- public $out;
- public $payments = [];
- public $period = [];
- public array $recordDatas = [];
- public array $labels = [];
- public $selectedFilter ;
- public function mount()
- {
- foreach(range(date("Y"), date("Y") - 1) as $year)
- {
- foreach(range(date("m"), 1) as $month)
- {
- $this->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;
- }
- }
|