RecordINOUT.php 23 KB

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