RecordINOUT.php 17 KB

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