| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?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 $months = [];
- public $years = [];
- public array $recordDatas = [];
- public array $labels = [];
- public $selectedFilterFromMonth;
- public $selectedFilterFromYear;
- public $selectedFilterToMonth;
- public $selectedFilterToYear;
- public function mount()
- {
- $this->selectedFilterFromMonth = date("n");
- $this->selectedFilterToMonth = date("n");
- $this->selectedFilterFromYear = date("Y");
- $this->selectedFilterToYear = date("Y");
- for($month = 1; $month <= 12; $month++)
- {
- $this->months[] = array('value' => $month, 'text' => $this->getMonth($month));
- }
- for($year = date("Y"); $year >= 2020; $year--)
- {
- $this->years[] = array('value' => $year, 'text' => $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;
- $this->records = array();
- $this->totals = array();
- $fromDate = '';
- $toDate = '';
- if ($this->selectedFilterFromMonth != '' && $this->selectedFilterFromYear != '')
- {
- $fromDate = date($this->selectedFilterFromYear . "-" . $this->selectedFilterFromMonth . "-01");
- }
- if ($this->selectedFilterToMonth != '' && $this->selectedFilterToYear != '')
- {
- $toDate = date($this->selectedFilterToYear . "-" . $this->selectedFilterToMonth . "-t 23:59:59");
- }
- $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
- ->whereBetween('date', [$fromDate, $toDate])
- ->get();
- /*
- if ($this->selectedFilter == '')
- {
- $month = date("m");
- $year = date("Y");
- $fromDate = date("Y-m-01");
- $toDate = date("Y-m-t 23:59:59");
- // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
- $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
- ->whereBetween('date', [$fromDate, $toDate])
- ->get();
- }
- else
- {
- if ($this->selectedFilter == '0')
- {
- $fromDate = date("Y-m-01");
- $toDate = date("Y-m-t 23:59:59");
- $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->whereBetween('date', [$fromDate, $toDate])
- //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
- ->get();
- // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
- }
- else if ($this->selectedFilter == '1')
- {
- $fromDate = date("Y-m-01", strtotime("-1 month"));
- $toDate = date("Y-m-t 23:59:59", strtotime("-1 month"));
- $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->whereBetween('date', [$fromDate, $toDate])
- //->where('records_rows.when', 'like', '%' . date("m-Y", strtotime("-1 month")) . '%')
- ->get();
- // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
- }
- else if ($this->selectedFilter == '2')
- {
- $fromDate = date("Y-01-01");
- $toDate = date("Y-12-31 23:59:59");
- // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
- $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->whereBetween('date', [$fromDate, $toDate])
- //->where('records_rows.when', 'like', '%' . date("m-Y", strtotime("-1 month")) . '%')
- ->get();
- }
- else
- {
- list($month, $year) = explode("-", $this->selectedFilter);
- $fromDate = date($year . "-" . $month . "-01");
- $toDate = date($year . "-" . $month . "-t 23:59:59");
- $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->whereBetween('date', [$fromDate, $toDate])
- //->where('records_rows.when', 'like', '%' . $this->selectedFilter . '%')
- ->get();
- //$datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
- }
- }
- */
- /*
- $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");
- }
- */
- // $
- foreach($datas as $idx => $data)
- {
- $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
- $paymentCheck = $data->payment_method->money;
- if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first))
- {
- $amount = $data->amount;
- /*$when = sizeof(json_decode($data->when));
- if ($when > 1)
- {
- $amount = $amount / $when;
- }*/
- $prefix = '';
- if (!$data->commercial)
- $prefix = $idx . "$";
- $causal = $prefix . $data->date . "§" . $causalCheck->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] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
- }
- /*
- 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];
- }
- */
- }
- 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;
- }
- }
|