RecordINOUT.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 $causals = array();
  30. public $payments = array();
  31. public $members = array();
  32. public function mount()
  33. {
  34. $borsellino = \App\Models\Causal::where('money', true)->first();
  35. if ($borsellino)
  36. $this->borsellino_id = $borsellino->id;
  37. //$this->month = 1;
  38. $this->year = date("Y");
  39. $this->year_1 = date("Y");
  40. $this->year_2 = date("Y");
  41. $this->show_block_2 = false;
  42. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 'IN', 0);
  43. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->get(), 'OUT', 0);
  44. }
  45. public function getCausale($records, $type, $indentation)
  46. {
  47. foreach($records as $record)
  48. {
  49. if ($type == 'IN')
  50. $this->rows_in[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id);
  51. if ($type == 'OUT')
  52. $this->rows_out[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id);
  53. if(count($record->childs))
  54. $this->getCausale($record->childs, $type, $indentation + 1);
  55. }
  56. }
  57. public function render()
  58. {
  59. return view('livewire.records_in_out');
  60. }
  61. public function hydrate()
  62. {
  63. //$this->emit('load-select');
  64. }
  65. public function show($m, $y)
  66. //public function show($dt)
  67. {
  68. //list($m, $y) = explode("_", $dt);
  69. if ($m != "" && $y != "")
  70. $this->datas[] = $m . "-" . $y;
  71. $this->columns = array();
  72. $this->records_in = [];
  73. $this->records_out = [];
  74. if (sizeof($this->datas) > 0)
  75. {
  76. foreach($this->datas as $filter)
  77. {
  78. // $filter = $m . "-" . $this->year;
  79. $this->columns[] = $filter;
  80. $records = \App\Models\Record::where('type', 'IN')
  81. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  82. ->where('records_rows.causal_id', '<>', $this->borsellino_id)
  83. ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
  84. //$records = $records->orderBy('date', 'DESC')->get();
  85. foreach($records as $record)
  86. {
  87. $amount = $record->amount;
  88. $when = sizeof(json_decode($record->when));
  89. if ($when > 1)
  90. {
  91. $amount = $amount / $when;
  92. $record->amount = $amount;
  93. }
  94. // Aggiungo/aggiorno i dati
  95. if (isset($this->records_in[$filter][$record->causal_id]))
  96. $this->records_in[$filter][$record->causal_id] += $amount;
  97. else
  98. $this->records_in[$filter][$record->causal_id] = $amount;
  99. // Aggiorno i dati del padre
  100. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  101. }
  102. $records = \App\Models\Record::where('type', 'OUT')
  103. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  104. ->where('records_rows.causal_id', '<>', $this->borsellino_id)
  105. ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
  106. //$records = $records->orderBy('date', 'DESC')->get();
  107. foreach($records as $record)
  108. {
  109. $amount = $record->amount;
  110. $when = sizeof(json_decode($record->when));
  111. if ($when > 1)
  112. {
  113. $amount = $amount / $when;
  114. $record->amount = $amount;
  115. }
  116. // Aggiungo/aggiorno i dati
  117. if (isset($this->records_out[$filter][$record->causal_id]))
  118. $this->records_out[$filter][$record->causal_id] += $amount;
  119. else
  120. $this->records_out[$filter][$record->causal_id] = $amount;
  121. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  122. }
  123. }
  124. }
  125. //$this->showData = true;
  126. $this->emit('load-table');
  127. }
  128. public function updateParent($type, $causal_id, $amount, $filter)
  129. {
  130. if ($type == "IN")
  131. {
  132. foreach($this->rows_in as $r)
  133. {
  134. if ($r["id"] == $causal_id)
  135. {
  136. if (isset($this->records_in[$filter][$r["parent_id"]]))
  137. $this->records_in[$filter][$r["parent_id"]] += $amount;
  138. else
  139. $this->records_in[$filter][$r["parent_id"]] = $amount;
  140. if ($r["parent_id"] > 0)
  141. $this->updateParent("IN", $r["parent_id"], $amount, $filter);
  142. }
  143. }
  144. }
  145. if ($type == "OUT")
  146. {
  147. foreach($this->rows_out as $r)
  148. {
  149. if ($r["id"] == $causal_id)
  150. {
  151. if (isset($this->records_out[$filter][$r["parent_id"]]))
  152. $this->records_out[$filter][$r["parent_id"]] += $amount;
  153. else
  154. $this->records_out[$filter][$r["parent_id"]] = $amount;
  155. if ($r["parent_id"] > 0)
  156. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  157. }
  158. }
  159. }
  160. }
  161. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  162. {
  163. if ($type == "IN")
  164. {
  165. foreach($this->rows_in as $r)
  166. {
  167. if ($r["id"] == $causal_id)
  168. {
  169. if (isset($records_in[$filter][$r["parent_id"]]))
  170. $records_in[$filter][$r["parent_id"]] += $amount;
  171. else
  172. $records_in[$filter][$r["parent_id"]] = $amount;
  173. if ($r["parent_id"] > 0)
  174. $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  175. }
  176. }
  177. }
  178. if ($type == "OUT")
  179. {
  180. foreach($this->rows_out as $r)
  181. {
  182. if ($r["id"] == $causal_id)
  183. {
  184. if (isset($records_out[$filter][$r["parent_id"]]))
  185. $records_out[$filter][$r["parent_id"]] += $amount;
  186. else
  187. $records_out[$filter][$r["parent_id"]] = $amount;
  188. if ($r["parent_id"] > 0)
  189. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  190. }
  191. }
  192. }
  193. }
  194. public function getCausal($causal)
  195. {
  196. $ret = '';
  197. if ($causal > 0)
  198. {
  199. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  200. }
  201. return $ret;
  202. }
  203. public function getMonth($str)
  204. {
  205. $ret = '';
  206. list($m, $y) = explode("-", $str);
  207. switch ($m) {
  208. case '01':
  209. $ret = 'Gennaio ';
  210. break;
  211. case '02':
  212. $ret = 'Febbraio ';
  213. break;
  214. case '03':
  215. $ret = 'Marzo ';
  216. break;
  217. case '04':
  218. $ret = 'Aprile ';
  219. break;
  220. case '05':
  221. $ret = 'Maggio ';
  222. break;
  223. case '06':
  224. $ret = 'Giugno ';
  225. break;
  226. case '07':
  227. $ret = 'Luglio ';
  228. break;
  229. case '08':
  230. $ret = 'Agosto ';
  231. break;
  232. case '09':
  233. $ret = 'Settembre ';
  234. break;
  235. case '10':
  236. $ret = 'Ottobre ';
  237. break;
  238. case '11':
  239. $ret = 'Novembre ';
  240. break;
  241. case '12':
  242. $ret = 'Dicembre ';
  243. break;
  244. default:
  245. $ret = '';
  246. break;
  247. }
  248. $ret .= $y;
  249. return $ret;
  250. }
  251. public function clear()
  252. {
  253. $this->columns = [];
  254. $this->datas = [];
  255. $this->records_out = [];
  256. $this->records_in = [];
  257. $this->emit('load-select');
  258. //$this->showData = false;
  259. }
  260. public function remove($idx)
  261. {
  262. unset($this->datas[$idx]);
  263. $this->show('', '');
  264. }
  265. public function export()
  266. {
  267. $path = $this->generateExcel($this->columns, $this->rows_in, $this->records_in, $this->rows_out, $this->records_out);
  268. return response()->download($path)->deleteFileAfterSend();
  269. }
  270. public function exportYear()
  271. {
  272. $records_in = [];
  273. $records_out = [];
  274. $datas = [];
  275. for($m=1;$m<=12;$m++)
  276. {
  277. $datas[] = $m . "-" . $this->year;
  278. }
  279. foreach($datas as $filter)
  280. {
  281. $columns[] = $filter;
  282. $records = \App\Models\Record::where('type', 'IN')
  283. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  284. ->where('records_rows.causal_id', '<>', $this->borsellino_id)
  285. ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
  286. //$records = $records->orderBy('date', 'DESC')->get();
  287. foreach($records as $record)
  288. {
  289. $amount = $record->amount;
  290. $when = sizeof(json_decode($record->when));
  291. if ($when > 1)
  292. {
  293. $amount = $amount / $when;
  294. $record->amount = $amount;
  295. }
  296. // Aggiungo/aggiorno i dati
  297. if (isset($records_in[$filter][$record->causal_id]))
  298. $records_in[$filter][$record->causal_id] += $amount;
  299. else
  300. $records_in[$filter][$record->causal_id] = $amount;
  301. // Aggiorno i dati del padre
  302. $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
  303. }
  304. $records = \App\Models\Record::where('type', 'OUT')
  305. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  306. ->where('records_rows.causal_id', '<>', $this->borsellino_id)
  307. ->where('records_rows.when', 'like', '%' . $filter . '%')->get();
  308. //$records = $records->orderBy('date', 'DESC')->get();
  309. foreach($records as $record)
  310. {
  311. $amount = $record->amount;
  312. $when = sizeof(json_decode($record->when));
  313. if ($when > 1)
  314. {
  315. $amount = $amount / $when;
  316. $record->amount = $amount;
  317. }
  318. // Aggiungo/aggiorno i dati
  319. if (isset($records_out[$filter][$record->causal_id]))
  320. $records_out[$filter][$record->causal_id] += $amount;
  321. else
  322. $records_out[$filter][$record->causal_id] = $amount;
  323. $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
  324. }
  325. }
  326. $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out);
  327. return response()->download($path)->deleteFileAfterSend();
  328. }
  329. public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out)
  330. {
  331. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
  332. $spreadsheet = new Spreadsheet();
  333. $activeWorksheet = $spreadsheet->getActiveSheet();
  334. $activeWorksheet->setCellValue('A1', 'Entrate');
  335. foreach($columns as $idx => $column)
  336. {
  337. $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
  338. }
  339. $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
  340. $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  341. $count = 2;
  342. $totals = [];
  343. foreach($rows_in as $in)
  344. {
  345. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
  346. foreach($columns as $idx => $column)
  347. {
  348. if(isset($records_in[$column][$in["id"]]))
  349. {
  350. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
  351. if ($in["level"] == 0)
  352. {
  353. if (isset($totals[$idx]))
  354. $totals[$idx] += $records_in[$column][$in["id"]];
  355. else
  356. $totals[$idx] = $records_in[$column][$in["id"]];
  357. }
  358. }
  359. }
  360. if ($in["level"] == 0)
  361. {
  362. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  363. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
  364. }
  365. $count += 1;
  366. }
  367. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  368. foreach($totals as $idx => $total)
  369. {
  370. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  371. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  372. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  373. }
  374. $count += 2;
  375. $activeWorksheet->setCellValue('A' . $count, "Uscite");
  376. foreach($columns as $idx => $column)
  377. {
  378. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
  379. }
  380. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  381. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ff0000');
  382. $count += 1;
  383. $totals = [];
  384. foreach($rows_out as $out)
  385. {
  386. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
  387. foreach($columns as $idx => $column)
  388. {
  389. if(isset($records_out[$column][$out["id"]]))
  390. {
  391. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
  392. if ($out["level"] == 0)
  393. {
  394. if (isset($totals[$idx]))
  395. $totals[$idx] += $records_out[$column][$out["id"]];
  396. else
  397. $totals[$idx] = $records_out[$column][$out["id"]];
  398. }
  399. }
  400. }
  401. if ($out["level"] == 0)
  402. {
  403. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  404. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
  405. }
  406. $count += 1;
  407. }
  408. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  409. foreach($totals as $idx => $total)
  410. {
  411. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  412. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  413. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ff0000');
  414. }
  415. foreach($letters as $l)
  416. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  417. $writer = new Xlsx($spreadsheet);
  418. $writer->save($path = storage_path('export.xlsx'));
  419. return $path;
  420. }
  421. }