RecordINOUT.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. class RecordINOUT extends Component
  7. {
  8. //public $records_in, $records_out;
  9. public $total_in = 0;
  10. public $total_out = 0;
  11. public $datas = [];
  12. public $month;
  13. public $year;
  14. public $months_1 = [];
  15. public $year_1;
  16. public $months_2 = [];
  17. public $year_2;
  18. public $show_block_2;
  19. //public $borsellino_id = 0;
  20. public $columns = array();
  21. public $rows_in = array();
  22. public $rows_out = array();
  23. public $records_in = array();
  24. public $records_out = array();
  25. public $showData = true;
  26. public $hasFilter = false;
  27. public $total = 0;
  28. public $selectedFilter = '';
  29. public $causalsIn = array();
  30. public $causalsOut = array();
  31. public $payments = array();
  32. public $members = array();
  33. public $filterCausalsIn = null;
  34. public $filterCausalsOut = null;
  35. public $excludeCausals = array();
  36. public function mount()
  37. {
  38. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  39. return redirect()->to('/dashboard');
  40. $borsellino = \App\Models\Causal::where('money', true)->first();
  41. if ($borsellino)
  42. $this->excludeCausals[] = $borsellino->id;
  43. //$this->borsellino_id = $borsellino->id;
  44. // Aggiungo
  45. $excludes = \App\Models\Causal::where('no_records', true)->get();
  46. foreach($excludes as $e)
  47. {
  48. $this->excludeCausals[] = $e->id;
  49. }
  50. $this->month = date("m");
  51. $this->year = date("Y");
  52. $this->year_1 = date("Y");
  53. $this->year_2 = date("Y");
  54. $this->show_block_2 = false;
  55. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get(), 'IN', 0);
  56. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get(), 'OUT', 0);
  57. $this->getCausalsIn(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  58. $this->getCausalsOut(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->get(), 0);
  59. //$this->causalsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get();
  60. //$this->causalsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get();
  61. }
  62. public function getCausalsIn($records, $indentation)
  63. {
  64. foreach($records as $record)
  65. {
  66. $this->causalsIn[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  67. if(count($record->childs))
  68. $this->getCausalsIn($record->childs, $indentation + 1);
  69. }
  70. }
  71. public function getCausalsOut($records, $indentation)
  72. {
  73. foreach($records as $record)
  74. {
  75. $this->causalsOut[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  76. if(count($record->childs))
  77. $this->getCausalsOut($record->childs, $indentation + 1);
  78. }
  79. }
  80. public function getCausale($records, $type, $indentation)
  81. {
  82. foreach($records as $record)
  83. {
  84. $first_parent_id = null;
  85. if ($record->parent_id != null)
  86. {
  87. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  88. }
  89. if ($type == 'IN')
  90. $this->rows_in[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id, 'parent_name' => $this->getCausalName($record->parent_id), 'first_parent_id' => $first_parent_id, 'first_parent_name' => $this->getCausalName($first_parent_id));
  91. if ($type == 'OUT')
  92. $this->rows_out[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id, 'parent_name' => $this->getCausalName($record->parent_id), 'first_parent_id' => $first_parent_id, 'first_parent_name' => $this->getCausalName($first_parent_id));
  93. if(count($record->childs))
  94. $this->getCausale($record->childs, $type, $indentation + 1);
  95. }
  96. }
  97. public function getCausalName($id)
  98. {
  99. if ($id > 0)
  100. return \App\Models\Causal::findOrFail($id)->name;
  101. else
  102. return "";
  103. }
  104. public function render()
  105. {
  106. return view('livewire.records_in_out');
  107. }
  108. public function hydrate()
  109. {
  110. $this->emit('load-select');
  111. }
  112. public function show($m, $y)
  113. //public function show($dt)
  114. {
  115. //list($m, $y) = explode("_", $dt);
  116. if ($m != "" && $y != "" && !in_array($m . "-" . $y, $this->datas))
  117. $this->datas[] = $m . "-" . $y;
  118. $this->columns = array();
  119. $this->records_in = [];
  120. $this->records_out = [];
  121. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  122. if (sizeof($this->datas) > 0)
  123. {
  124. foreach($this->datas as $filter)
  125. {
  126. // $filter = $m . "-" . $this->year;
  127. $this->columns[] = $filter;
  128. $f = $filter;
  129. if ($m == 'x')
  130. $f = str_replace("x-", "", $filter);
  131. //$dt = $y . "-0" . $m;
  132. $records = \App\Models\Record::where('type', 'IN')
  133. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  134. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  135. ->where(function ($query) {
  136. $query->where('deleted', false)->orWhere('deleted', null);
  137. })
  138. ->where(function ($query) {
  139. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  140. })
  141. ->whereNotIn('member_id', $exclude_from_records)
  142. /*->where(function ($query) use ($f, $dt) {
  143. $query->where('records.date', 'like', '%' . $dt . '%')->orWhere('records_rows.when', 'like', '%' . $f . '%');
  144. })*/
  145. ->where('records_rows.when', 'like', '%' . $f . '%')
  146. ->get();
  147. //$records = $records->orderBy('date', 'DESC')->get();
  148. foreach($records as $record)
  149. {
  150. $amount = $record->amount;
  151. $amount += getVatValue($amount, $record->vat_id);
  152. $when = sizeof(json_decode($record->when));
  153. if ($when > 1)
  154. {
  155. $amount = $amount / $when;
  156. $record->amount = $amount;
  157. }
  158. // Aggiungo/aggiorno i dati
  159. if (isset($this->records_in[$filter][$record->causal_id]))
  160. $this->records_in[$filter][$record->causal_id] += $amount;
  161. else
  162. $this->records_in[$filter][$record->causal_id] = $amount;
  163. // Aggiorno i dati del padre
  164. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  165. }
  166. $records = \App\Models\Record::where('type', 'OUT')
  167. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  168. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  169. ->where(function ($query) {
  170. $query->where('deleted', false)->orWhere('deleted', null);
  171. })
  172. ->where(function ($query) {
  173. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  174. })
  175. ->whereNotIn('member_id', $exclude_from_records)
  176. ->where('records_rows.when', 'like', '%' . $f . '%')->get();
  177. //$records = $records->orderBy('date', 'DESC')->get();
  178. foreach($records as $record)
  179. {
  180. $amount = $record->amount;
  181. $amount += getVatValue($amount, $record->vat_id);
  182. $when = sizeof(json_decode($record->when));
  183. if ($when > 1)
  184. {
  185. $amount = $amount / $when;
  186. $record->amount = $amount;
  187. }
  188. // Aggiungo/aggiorno i dati
  189. if (isset($this->records_out[$filter][$record->causal_id]))
  190. $this->records_out[$filter][$record->causal_id] += $amount;
  191. else
  192. $this->records_out[$filter][$record->causal_id] = $amount;
  193. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  194. }
  195. }
  196. }
  197. //$this->showData = true;
  198. $this->emit('load-table');
  199. }
  200. public function updateParent($type, $causal_id, $amount, $filter)
  201. {
  202. if ($type == "IN")
  203. {
  204. foreach($this->rows_in as $r)
  205. {
  206. if ($r["id"] == $causal_id)
  207. {
  208. if (isset($this->records_in[$filter][$r["parent_id"]]))
  209. $this->records_in[$filter][$r["parent_id"]] += $amount;
  210. else
  211. $this->records_in[$filter][$r["parent_id"]] = $amount;
  212. if ($r["parent_id"] > 0)
  213. $this->updateParent("IN", $r["parent_id"], $amount, $filter);
  214. }
  215. }
  216. }
  217. if ($type == "OUT")
  218. {
  219. foreach($this->rows_out as $r)
  220. {
  221. if ($r["id"] == $causal_id)
  222. {
  223. if (isset($this->records_out[$filter][$r["parent_id"]]))
  224. $this->records_out[$filter][$r["parent_id"]] += $amount;
  225. else
  226. $this->records_out[$filter][$r["parent_id"]] = $amount;
  227. if ($r["parent_id"] > 0)
  228. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  229. }
  230. }
  231. }
  232. }
  233. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  234. {
  235. if ($type == "IN")
  236. {
  237. foreach($this->rows_in as $r)
  238. {
  239. if ($r["id"] == $causal_id)
  240. {
  241. if (isset($records_in[$filter][$r["parent_id"]]))
  242. $records_in[$filter][$r["parent_id"]] += $amount;
  243. else
  244. $records_in[$filter][$r["parent_id"]] = $amount;
  245. if ($r["parent_id"] > 0)
  246. $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  247. }
  248. }
  249. }
  250. if ($type == "OUT")
  251. {
  252. foreach($this->rows_out as $r)
  253. {
  254. if ($r["id"] == $causal_id)
  255. {
  256. if (isset($records_out[$filter][$r["parent_id"]]))
  257. $records_out[$filter][$r["parent_id"]] += $amount;
  258. else
  259. $records_out[$filter][$r["parent_id"]] = $amount;
  260. if ($r["parent_id"] > 0)
  261. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  262. }
  263. }
  264. }
  265. }
  266. public function getCausal($causal)
  267. {
  268. $ret = '';
  269. if ($causal > 0)
  270. {
  271. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  272. }
  273. return $ret;
  274. }
  275. public function getMonth($str)
  276. {
  277. $ret = '';
  278. list($m, $y) = explode("-", $str);
  279. switch ($m) {
  280. case 'x':
  281. $ret = '';
  282. break;
  283. case '01':
  284. $ret = 'Gennaio ';
  285. break;
  286. case '02':
  287. $ret = 'Febbraio ';
  288. break;
  289. case '03':
  290. $ret = 'Marzo ';
  291. break;
  292. case '04':
  293. $ret = 'Aprile ';
  294. break;
  295. case '05':
  296. $ret = 'Maggio ';
  297. break;
  298. case '06':
  299. $ret = 'Giugno ';
  300. break;
  301. case '07':
  302. $ret = 'Luglio ';
  303. break;
  304. case '08':
  305. $ret = 'Agosto ';
  306. break;
  307. case '09':
  308. $ret = 'Settembre ';
  309. break;
  310. case '10':
  311. $ret = 'Ottobre ';
  312. break;
  313. case '11':
  314. $ret = 'Novembre ';
  315. break;
  316. case '12':
  317. $ret = 'Dicembre ';
  318. break;
  319. default:
  320. $ret = '';
  321. break;
  322. }
  323. $ret .= $y;
  324. return $ret;
  325. }
  326. public function clear()
  327. {
  328. $this->columns = [];
  329. $this->datas = [];
  330. $this->records_out = [];
  331. $this->records_in = [];
  332. $this->emit('load-select');
  333. $this->emit('reset-collapse');
  334. //$this->showData = false;
  335. }
  336. public function remove($idx)
  337. {
  338. unset($this->datas[$idx]);
  339. $this->show('', '');
  340. }
  341. public function export()
  342. {
  343. $rows_in = array();
  344. if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0)
  345. {
  346. foreach($this->rows_in as $r)
  347. {
  348. if (in_array($r["id"], $this->filterCausalsIn) || in_array($r["parent_id"], $this->filterCausalsIn) || in_array($r["first_parent_id"], $this->filterCausalsIn))
  349. {
  350. $rows_in[] = $r;
  351. }
  352. }
  353. }
  354. else
  355. {
  356. $rows_in = $this->rows_in;
  357. }
  358. $path = $this->generateExcel($this->columns, $rows_in, $this->records_in, $this->rows_out, $this->records_out);
  359. return response()->download($path)->deleteFileAfterSend();
  360. }
  361. public function exportYear($year)
  362. {
  363. $records_in = [];
  364. $records_out = [];
  365. $datas = [];
  366. if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1)
  367. {
  368. for($m=env('FISCAL_YEAR_MONTH_FROM', 1);$m<=12;$m++)
  369. {
  370. $datas[] = $m . "-" . $year;
  371. }
  372. for($m=1;$m<=env('FISCAL_YEAR_MONTH_TO', 12);$m++)
  373. {
  374. $datas[] = $m . "-" . ($year + 1);
  375. }
  376. }
  377. else
  378. {
  379. for($m=1;$m<=12;$m++)
  380. {
  381. $datas[] = $m . "-" . $year;
  382. }
  383. }
  384. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  385. foreach($datas as $filter)
  386. {
  387. $columns[] = $filter;
  388. $records = \App\Models\Record::where('type', 'IN')
  389. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  390. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  391. ->where(function ($query) {
  392. $query->where('deleted', false)->orWhere('deleted', null);
  393. })
  394. ->where(function ($query) {
  395. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  396. })
  397. ->whereNotIn('member_id', $exclude_from_records)
  398. ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
  399. //$records = $records->orderBy('date', 'DESC')->get();
  400. foreach($records as $record)
  401. {
  402. $amount = $record->amount;
  403. $amount += getVatValue($amount, $record->vat_id);
  404. $when = sizeof(json_decode($record->when));
  405. if ($when > 1)
  406. {
  407. $amount = $amount / $when;
  408. $record->amount = $amount;
  409. }
  410. // Aggiungo/aggiorno i dati
  411. if (isset($records_in[$filter][$record->causal_id]))
  412. $records_in[$filter][$record->causal_id] += $amount;
  413. else
  414. $records_in[$filter][$record->causal_id] = $amount;
  415. // Aggiorno i dati del padre
  416. $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
  417. }
  418. $records = \App\Models\Record::where('type', 'OUT')
  419. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  420. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  421. ->where(function ($query) {
  422. $query->where('deleted', false)->orWhere('deleted', null);
  423. })
  424. ->where(function ($query) {
  425. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  426. })
  427. ->whereNotIn('member_id', $exclude_from_records)
  428. ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
  429. //$records = $records->orderBy('date', 'DESC')->get();
  430. foreach($records as $record)
  431. {
  432. $amount = $record->amount;
  433. $when = sizeof(json_decode($record->when));
  434. if ($when > 1)
  435. {
  436. $amount = $amount / $when;
  437. $record->amount = $amount;
  438. }
  439. // Aggiungo/aggiorno i dati
  440. if (isset($records_out[$filter][$record->causal_id]))
  441. $records_out[$filter][$record->causal_id] += $amount;
  442. else
  443. $records_out[$filter][$record->causal_id] = $amount;
  444. $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
  445. }
  446. }
  447. $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out);
  448. return response()->download($path)->deleteFileAfterSend();
  449. }
  450. public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out)
  451. {
  452. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
  453. $spreadsheet = new Spreadsheet();
  454. $activeWorksheet = $spreadsheet->getActiveSheet();
  455. $activeWorksheet->setCellValue('A1', 'Entrate');
  456. foreach($columns as $idx => $column)
  457. {
  458. $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
  459. }
  460. $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
  461. $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  462. $count = 2;
  463. $totals = [];
  464. foreach($rows_in as $in)
  465. {
  466. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
  467. foreach($columns as $idx => $column)
  468. {
  469. if(isset($records_in[$column][$in["id"]]))
  470. {
  471. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
  472. if ($in["level"] == 0)
  473. {
  474. if (isset($totals[$idx]))
  475. $totals[$idx] += $records_in[$column][$in["id"]];
  476. else
  477. $totals[$idx] = $records_in[$column][$in["id"]];
  478. }
  479. }
  480. }
  481. if ($in["level"] == 0)
  482. {
  483. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  484. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
  485. }
  486. $count += 1;
  487. }
  488. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  489. foreach($totals as $idx => $total)
  490. {
  491. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  492. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  493. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  494. }
  495. $count += 2;
  496. $activeWorksheet->setCellValue('A' . $count, "Uscite");
  497. foreach($columns as $idx => $column)
  498. {
  499. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
  500. }
  501. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  502. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ff0000');
  503. $count += 1;
  504. $totals = [];
  505. foreach($rows_out as $out)
  506. {
  507. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
  508. foreach($columns as $idx => $column)
  509. {
  510. if(isset($records_out[$column][$out["id"]]))
  511. {
  512. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
  513. if ($out["level"] == 0)
  514. {
  515. if (isset($totals[$idx]))
  516. $totals[$idx] += $records_out[$column][$out["id"]];
  517. else
  518. $totals[$idx] = $records_out[$column][$out["id"]];
  519. }
  520. }
  521. }
  522. if ($out["level"] == 0)
  523. {
  524. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  525. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
  526. }
  527. $count += 1;
  528. }
  529. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  530. foreach($totals as $idx => $total)
  531. {
  532. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  533. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  534. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ff0000');
  535. }
  536. foreach($letters as $l)
  537. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  538. $writer = new Xlsx($spreadsheet);
  539. $writer->save($path = storage_path('entrate_uscite_' . date("YmdHis") . '.xlsx'));
  540. return $path;
  541. }
  542. }