RecordINOUT.php 18 KB

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