RecordINOUT.php 22 KB

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