RecordINOUT.php 20 KB

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