RecordINOUT.php 20 KB

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