| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- class RecordINOUT extends Component
- {
- //public $records_in, $records_out;
- public $total_in = 0;
- public $total_out = 0;
- public $datas = [];
- public $month;
- public $year;
- public $months_1 = [];
- public $year_1;
- public $months_2 = [];
- public $year_2;
- public $show_block_2;
- public $borsellino_id = 0;
- public $columns = array();
- public $rows_in = array();
- public $rows_out = array();
- public $records_in = array();
- public $records_out = array();
- public $showData = true;
- public $hasFilter = false;
- public $total = 0;
- public $selectedFilter = '';
- public $causals = array();
- public $payments = array();
- public $members = array();
- public function mount()
- {
- $borsellino = \App\Models\Causal::where('money', true)->first();
- if ($borsellino)
- $this->borsellino_id = $borsellino->id;
- $this->month = date("m");
- $this->year = date("Y");
- $this->year_1 = date("Y");
- $this->year_2 = date("Y");
- $this->show_block_2 = false;
- $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 'IN', 0);
- $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->get(), 'OUT', 0);
- }
- public function getCausale($records, $type, $indentation)
- {
- foreach($records as $record)
- {
- if ($type == 'IN')
- $this->rows_in[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id);
- if ($type == 'OUT')
- $this->rows_out[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id);
- if(count($record->childs))
- $this->getCausale($record->childs, $type, $indentation + 1);
- }
- }
- public function render()
- {
- return view('livewire.records_in_out');
- }
- public function hydrate()
- {
- //$this->emit('load-select');
- }
- public function show($m, $y)
- //public function show($dt)
- {
- //list($m, $y) = explode("_", $dt);
- if ($m != "" && $y != "" && !in_array($m . "-" . $y, $this->datas))
- $this->datas[] = $m . "-" . $y;
- $this->columns = array();
- $this->records_in = [];
- $this->records_out = [];
- if (sizeof($this->datas) > 0)
- {
- foreach($this->datas as $filter)
- {
- // $filter = $m . "-" . $this->year;
- $this->columns[] = $filter;
- $records = \App\Models\Record::where('type', 'IN')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->where('records_rows.causal_id', '<>', $this->borsellino_id)
- ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
- //$records = $records->orderBy('date', 'DESC')->get();
- foreach($records as $record)
- {
- $amount = $record->amount;
- $amount += getVatValue($amount, $record->vat_id);
- $when = sizeof(json_decode($record->when));
- if ($when > 1)
- {
- $amount = $amount / $when;
- $record->amount = $amount;
- }
- // Aggiungo/aggiorno i dati
- if (isset($this->records_in[$filter][$record->causal_id]))
- $this->records_in[$filter][$record->causal_id] += $amount;
- else
- $this->records_in[$filter][$record->causal_id] = $amount;
- // Aggiorno i dati del padre
- $this->updateParent("IN", $record->causal_id, $amount, $filter);
- }
- $records = \App\Models\Record::where('type', 'OUT')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->where('records_rows.causal_id', '<>', $this->borsellino_id)
- ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
- //$records = $records->orderBy('date', 'DESC')->get();
- foreach($records as $record)
- {
- $amount = $record->amount;
- $amount += getVatValue($amount, $record->vat_id);
- $when = sizeof(json_decode($record->when));
- if ($when > 1)
- {
- $amount = $amount / $when;
- $record->amount = $amount;
- }
- // Aggiungo/aggiorno i dati
- if (isset($this->records_out[$filter][$record->causal_id]))
- $this->records_out[$filter][$record->causal_id] += $amount;
- else
- $this->records_out[$filter][$record->causal_id] = $amount;
- $this->updateParent("OUT", $record->causal_id, $amount, $filter);
- }
- }
- }
- //$this->showData = true;
- $this->emit('load-table');
- }
- public function updateParent($type, $causal_id, $amount, $filter)
- {
- if ($type == "IN")
- {
- foreach($this->rows_in as $r)
- {
- if ($r["id"] == $causal_id)
- {
- if (isset($this->records_in[$filter][$r["parent_id"]]))
- $this->records_in[$filter][$r["parent_id"]] += $amount;
- else
- $this->records_in[$filter][$r["parent_id"]] = $amount;
- if ($r["parent_id"] > 0)
- $this->updateParent("IN", $r["parent_id"], $amount, $filter);
- }
- }
- }
- if ($type == "OUT")
- {
- foreach($this->rows_out as $r)
- {
- if ($r["id"] == $causal_id)
- {
- if (isset($this->records_out[$filter][$r["parent_id"]]))
- $this->records_out[$filter][$r["parent_id"]] += $amount;
- else
- $this->records_out[$filter][$r["parent_id"]] = $amount;
- if ($r["parent_id"] > 0)
- $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
- }
- }
- }
- }
- public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
- {
- if ($type == "IN")
- {
- foreach($this->rows_in as $r)
- {
- if ($r["id"] == $causal_id)
- {
- if (isset($records_in[$filter][$r["parent_id"]]))
- $records_in[$filter][$r["parent_id"]] += $amount;
- else
- $records_in[$filter][$r["parent_id"]] = $amount;
- if ($r["parent_id"] > 0)
- $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
- }
- }
- }
- if ($type == "OUT")
- {
- foreach($this->rows_out as $r)
- {
- if ($r["id"] == $causal_id)
- {
- if (isset($records_out[$filter][$r["parent_id"]]))
- $records_out[$filter][$r["parent_id"]] += $amount;
- else
- $records_out[$filter][$r["parent_id"]] = $amount;
- if ($r["parent_id"] > 0)
- $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
- }
- }
- }
- }
- public function getCausal($causal)
- {
- $ret = '';
- if ($causal > 0)
- {
- $ret = \App\Models\Causal::findOrFail($causal)->getTree();
- }
- return $ret;
- }
- public function getMonth($str)
- {
- $ret = '';
- list($m, $y) = explode("-", $str);
- switch ($m) {
- case '01':
- $ret = 'Gennaio ';
- break;
- case '02':
- $ret = 'Febbraio ';
- break;
- case '03':
- $ret = 'Marzo ';
- break;
- case '04':
- $ret = 'Aprile ';
- break;
- case '05':
- $ret = 'Maggio ';
- break;
- case '06':
- $ret = 'Giugno ';
- break;
- case '07':
- $ret = 'Luglio ';
- break;
- case '08':
- $ret = 'Agosto ';
- break;
- case '09':
- $ret = 'Settembre ';
- break;
- case '10':
- $ret = 'Ottobre ';
- break;
- case '11':
- $ret = 'Novembre ';
- break;
- case '12':
- $ret = 'Dicembre ';
- break;
- default:
- $ret = '';
- break;
- }
- $ret .= $y;
- return $ret;
- }
- public function clear()
- {
- $this->columns = [];
- $this->datas = [];
- $this->records_out = [];
- $this->records_in = [];
- $this->emit('load-select');
- //$this->showData = false;
- }
- public function remove($idx)
- {
- unset($this->datas[$idx]);
- $this->show('', '');
- }
- public function export()
- {
- $path = $this->generateExcel($this->columns, $this->rows_in, $this->records_in, $this->rows_out, $this->records_out);
- return response()->download($path)->deleteFileAfterSend();
- }
- public function exportYear($year)
- {
- $records_in = [];
- $records_out = [];
- $datas = [];
- for($m=1;$m<=12;$m++)
- {
- $datas[] = $m . "-" . $year;
- }
- foreach($datas as $filter)
- {
- $columns[] = $filter;
- $records = \App\Models\Record::where('type', 'IN')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->where('records_rows.causal_id', '<>', $this->borsellino_id)
- ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
- //$records = $records->orderBy('date', 'DESC')->get();
- foreach($records as $record)
- {
- $amount = $record->amount;
- $amount += getVatValue($amount, $record->vat_id);
- $when = sizeof(json_decode($record->when));
- if ($when > 1)
- {
- $amount = $amount / $when;
- $record->amount = $amount;
- }
- // Aggiungo/aggiorno i dati
- if (isset($records_in[$filter][$record->causal_id]))
- $records_in[$filter][$record->causal_id] += $amount;
- else
- $records_in[$filter][$record->causal_id] = $amount;
- // Aggiorno i dati del padre
- $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
- }
- $records = \App\Models\Record::where('type', 'OUT')
- ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
- ->where('records_rows.causal_id', '<>', $this->borsellino_id)
- ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
- //$records = $records->orderBy('date', 'DESC')->get();
- foreach($records as $record)
- {
- $amount = $record->amount;
- $when = sizeof(json_decode($record->when));
- if ($when > 1)
- {
- $amount = $amount / $when;
- $record->amount = $amount;
- }
- // Aggiungo/aggiorno i dati
- if (isset($records_out[$filter][$record->causal_id]))
- $records_out[$filter][$record->causal_id] += $amount;
- else
- $records_out[$filter][$record->causal_id] = $amount;
- $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
- }
- }
- $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out);
- return response()->download($path)->deleteFileAfterSend();
- }
- public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out)
- {
- $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
- $spreadsheet = new Spreadsheet();
- $activeWorksheet = $spreadsheet->getActiveSheet();
- $activeWorksheet->setCellValue('A1', 'Entrate');
- foreach($columns as $idx => $column)
- {
- $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
- }
- $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
- $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
- $count = 2;
- $totals = [];
- foreach($rows_in as $in)
- {
- $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
- foreach($columns as $idx => $column)
- {
- if(isset($records_in[$column][$in["id"]]))
- {
- $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
- if ($in["level"] == 0)
- {
- if (isset($totals[$idx]))
- $totals[$idx] += $records_in[$column][$in["id"]];
- else
- $totals[$idx] = $records_in[$column][$in["id"]];
- }
- }
- }
- if ($in["level"] == 0)
- {
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
- }
- $count += 1;
- }
- $activeWorksheet->setCellValue('A' . $count, 'Totale');
- foreach($totals as $idx => $total)
- {
- $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
- }
- $count += 2;
- $activeWorksheet->setCellValue('A' . $count, "Uscite");
- foreach($columns as $idx => $column)
- {
- $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
- }
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ff0000');
- $count += 1;
- $totals = [];
- foreach($rows_out as $out)
- {
- $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
- foreach($columns as $idx => $column)
- {
- if(isset($records_out[$column][$out["id"]]))
- {
- $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
- if ($out["level"] == 0)
- {
- if (isset($totals[$idx]))
- $totals[$idx] += $records_out[$column][$out["id"]];
- else
- $totals[$idx] = $records_out[$column][$out["id"]];
- }
- }
- }
- if ($out["level"] == 0)
- {
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
- }
- $count += 1;
- }
- $activeWorksheet->setCellValue('A' . $count, 'Totale');
- foreach($totals as $idx => $total)
- {
- $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
- $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ff0000');
- }
- foreach($letters as $l)
- $activeWorksheet->getColumnDimension($l)->setWidth(20);
- $writer = new Xlsx($spreadsheet);
- $writer->save($path = storage_path('export.xlsx'));
- return $path;
- }
- }
|