RecordINOUT.php 21 KB

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