RecordINOUT.php 23 KB

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