RecordINOUT.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. use Illuminate\Support\Facades\Auth;
  7. class RecordINOUT extends Component
  8. {
  9. //public $records_in, $records_out;
  10. public $total_in = 0;
  11. public $total_out = 0;
  12. public $datas = [];
  13. public $month;
  14. public $year;
  15. public $months_1 = [];
  16. public $year_1;
  17. public $months_2 = [];
  18. public $year_2;
  19. public $show_block_2;
  20. //public $borsellino_id = 0;
  21. public $columns = array();
  22. public $rows_in = array();
  23. public $rows_out = array();
  24. public $records_in = array();
  25. public $records_out = array();
  26. public $showData = true;
  27. public $hasFilter = false;
  28. public $total = 0;
  29. public $selectedFilter = '';
  30. public $causalsIn = array();
  31. public $causalsOut = array();
  32. public $payments = array();
  33. public $members = array();
  34. public $filterCausalsIn = null;
  35. public $filterCausalsOut = null;
  36. public $excludeCausals = array();
  37. public function mount()
  38. {
  39. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  40. return redirect()->to('/dashboard');
  41. $borsellino = \App\Models\Causal::where('money', true)->first();
  42. if ($borsellino)
  43. $this->excludeCausals[] = $borsellino->id;
  44. //$this->borsellino_id = $borsellino->id;
  45. // Aggiungo
  46. $excludes = \App\Models\Causal::where('no_records', true)->get();
  47. foreach($excludes as $e)
  48. {
  49. $this->excludeCausals[] = $e->id;
  50. }
  51. $this->month = date("m");
  52. $this->year = date("Y");
  53. $this->year_1 = date("Y");
  54. $this->year_2 = date("Y");
  55. $this->show_block_2 = false;
  56. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get(), 'IN', 0);
  57. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get(), 'OUT', 0);
  58. $this->getCausalsIn(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  59. $this->getCausalsOut(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->get(), 0);
  60. //$this->causalsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get();
  61. //$this->causalsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get();
  62. }
  63. public function getCausalsIn($records, $indentation)
  64. {
  65. foreach($records as $record)
  66. {
  67. $this->causalsIn[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  68. if(count($record->childs))
  69. $this->getCausalsIn($record->childs, $indentation + 1);
  70. }
  71. }
  72. public function getCausalsOut($records, $indentation)
  73. {
  74. foreach($records as $record)
  75. {
  76. $this->causalsOut[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  77. if(count($record->childs))
  78. $this->getCausalsOut($record->childs, $indentation + 1);
  79. }
  80. }
  81. public function getCausale($records, $type, $indentation)
  82. {
  83. foreach($records as $record)
  84. {
  85. $first_parent_id = null;
  86. if ($record->parent_id != null)
  87. {
  88. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  89. }
  90. if ($type == 'IN')
  91. $this->rows_in[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id, 'parent_name' => $this->getCausalName($record->parent_id), 'first_parent_id' => $first_parent_id, 'first_parent_name' => $this->getCausalName($first_parent_id), 'all_childs' => $this->getAllChild($record->id));
  92. if ($type == 'OUT')
  93. $this->rows_out[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id, 'parent_name' => $this->getCausalName($record->parent_id), 'first_parent_id' => $first_parent_id, 'first_parent_name' => $this->getCausalName($first_parent_id), 'all_childs' => $this->getAllChild($record->id));
  94. if(count($record->childs))
  95. $this->getCausale($record->childs, $type, $indentation + 1);
  96. }
  97. }
  98. public function getAllChild($id)
  99. {
  100. $aChilds = array();
  101. $aChilds[] = $id;
  102. $record = \App\Models\Causal::findOrFail($id);
  103. $aChilds[] = $record->parent_id;
  104. if ($record->parent_id != null)
  105. {
  106. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  107. $aChilds[] = $first_parent_id;
  108. }
  109. $childs = \App\Models\Causal::where('parent_id', $id)->get();
  110. foreach($childs as $child)
  111. {
  112. $aChilds[] = $child->id;
  113. $childs2 = \App\Models\Causal::where('parent_id', $child->id)->get();
  114. foreach($childs2 as $child2)
  115. {
  116. $aChilds[] = $child2->id;
  117. $childs3 = \App\Models\Causal::where('parent_id', $child2->id)->get();
  118. foreach($childs3 as $child3)
  119. {
  120. $aChilds[] = $child3->id;
  121. }
  122. }
  123. }
  124. return $aChilds;
  125. }
  126. public function getCausalName($id)
  127. {
  128. if ($id > 0)
  129. return \App\Models\Causal::findOrFail($id)->name;
  130. else
  131. return "";
  132. }
  133. public function render()
  134. {
  135. return view('livewire.records_in_out');
  136. }
  137. public function hydrate()
  138. {
  139. $this->emit('load-select');
  140. }
  141. public function show($m, $y)
  142. //public function show($dt)
  143. {
  144. //list($m, $y) = explode("_", $dt);
  145. if ($m != "" && $y != "" && !in_array($m . "-" . $y, $this->datas))
  146. $this->datas[] = $m . "-" . $y;
  147. $this->columns = array();
  148. $this->records_in = [];
  149. $this->records_out = [];
  150. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  151. if (sizeof($this->datas) > 0)
  152. {
  153. foreach($this->datas as $filter)
  154. {
  155. // $filter = $m . "-" . $this->year;
  156. $this->columns[] = $filter;
  157. list($m, $y) = explode("-", $filter);
  158. // $f = $filter;
  159. // if ($m == 'x')
  160. // $f = str_replace("x-", "", $filter);
  161. //$dt = $y . "-0" . $m;
  162. // $records = \App\Models\Record::where('type', 'IN')
  163. // ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  164. // ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  165. // ->where(function ($query) {
  166. // $query->where('deleted', false)->orWhere('deleted', null);
  167. // })
  168. // ->where(function ($query) {
  169. // $query->where('financial_movement', false)->orWhere('financial_movement', null);
  170. // })
  171. // ->whereNotIn('member_id', $exclude_from_records)
  172. // /*->where(function ($query) use ($f, $dt) {
  173. // $query->where('records.date', 'like', '%' . $dt . '%')->orWhere('records_rows.when', 'like', '%' . $f . '%');
  174. // })*/
  175. // ->where('records_rows.when', 'like', '%"' . $f . '"%')
  176. // ->get();
  177. // //$records = $records->orderBy('date', 'DESC')->get();
  178. $recordsQuery = \App\Models\Record::where('type', 'IN')
  179. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  180. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  181. ->where(function ($query) {
  182. $query->where('deleted', false)->orWhere('deleted', null);
  183. })
  184. ->where(function ($query) {
  185. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  186. })
  187. ->whereNotIn('member_id', $exclude_from_records);
  188. if ($m === 'x') {
  189. // Anno fiscale da settembre $y a agosto $y+1
  190. $months = array_merge(range(9, 12), range(1, 8));
  191. $years = [
  192. 9 => $y, 10 => $y, 11 => $y, 12 => $y,
  193. 1 => $y+1, 2 => $y+1, 3 => $y+1, 4 => $y+1,
  194. 5 => $y+1, 6 => $y+1, 7 => $y+1, 8 => $y+1
  195. ];
  196. $recordsQuery->where(function ($q) use ($months, $years) {
  197. foreach ($months as $month) {
  198. $year = $years[$month];
  199. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$month}\",\"year\":\"{$year}\"}')");
  200. }
  201. });
  202. } else {
  203. $recordsQuery->whereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$m}\",\"year\":\"{$y}\"}')");
  204. }
  205. $records = $recordsQuery->get();
  206. foreach($records as $record)
  207. {
  208. $amount = $record->amount;
  209. $amount += getVatValue($amount, $record->vat_id);
  210. $when = sizeof(json_decode($record->when));
  211. if ($when > 1)
  212. {
  213. $amount = $amount / $when;
  214. $record->amount = $amount;
  215. }
  216. // Aggiungo/aggiorno i dati
  217. if (isset($this->records_in[$filter][$record->causal_id]))
  218. $this->records_in[$filter][$record->causal_id] += $amount;
  219. else
  220. $this->records_in[$filter][$record->causal_id] = $amount;
  221. // Aggiorno i dati del padre
  222. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  223. }
  224. // $records = \App\Models\Record::where('type', 'OUT')
  225. // ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  226. // ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  227. // ->where(function ($query) {
  228. // $query->where('deleted', false)->orWhere('deleted', null);
  229. // })
  230. // ->where(function ($query) {
  231. // $query->where('financial_movement', false)->orWhere('financial_movement', null);
  232. // })
  233. // ->whereNotIn('member_id', $exclude_from_records)
  234. // ->where('records_rows.when', 'like', '%"' . $f . '"%')->get();
  235. //$records = $records->orderBy('date', 'DESC')->get();
  236. $recordsQuery = \App\Models\Record::where('type', 'OUT')
  237. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  238. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  239. ->where(function ($query) {
  240. $query->where('deleted', false)->orWhere('deleted', null);
  241. })
  242. ->where(function ($query) {
  243. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  244. })
  245. ->whereNotIn('member_id', $exclude_from_records);
  246. if ($m === 'x') {
  247. // Anno fiscale da settembre $y a agosto $y+1
  248. $months = array_merge(range(9, 12), range(1, 8));
  249. $years = [
  250. 9 => $y, 10 => $y, 11 => $y, 12 => $y,
  251. 1 => $y+1, 2 => $y+1, 3 => $y+1, 4 => $y+1,
  252. 5 => $y+1, 6 => $y+1, 7 => $y+1, 8 => $y+1
  253. ];
  254. $recordsQuery->where(function ($q) use ($months, $years) {
  255. foreach ($months as $month) {
  256. $year = $years[$month];
  257. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$month}\",\"year\":\"{$year}\"}')");
  258. }
  259. });
  260. } else {
  261. $recordsQuery->whereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$m}\",\"year\":\"{$y}\"}')");
  262. }
  263. $records = $recordsQuery->get();
  264. foreach($records as $record)
  265. {
  266. $amount = $record->amount;
  267. $amount += getVatValue($amount, $record->vat_id);
  268. $when = sizeof(json_decode($record->when));
  269. if ($when > 1)
  270. {
  271. $amount = $amount / $when;
  272. $record->amount = $amount;
  273. }
  274. // Aggiungo/aggiorno i dati
  275. if (isset($this->records_out[$filter][$record->causal_id]))
  276. $this->records_out[$filter][$record->causal_id] += $amount;
  277. else
  278. $this->records_out[$filter][$record->causal_id] = $amount;
  279. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  280. }
  281. }
  282. }
  283. //$this->showData = true;
  284. $this->emit('load-table');
  285. }
  286. public function updateParent($type, $causal_id, $amount, $filter)
  287. {
  288. if ($type == "IN")
  289. {
  290. foreach($this->rows_in as $r)
  291. {
  292. if ($r["id"] == $causal_id)
  293. {
  294. if (isset($this->records_in[$filter][$r["parent_id"]]))
  295. $this->records_in[$filter][$r["parent_id"]] += $amount;
  296. else
  297. $this->records_in[$filter][$r["parent_id"]] = $amount;
  298. if ($r["parent_id"] > 0)
  299. $this->updateParent("IN", $r["parent_id"], $amount, $filter);
  300. }
  301. }
  302. }
  303. if ($type == "OUT")
  304. {
  305. foreach($this->rows_out as $r)
  306. {
  307. if ($r["id"] == $causal_id)
  308. {
  309. if (isset($this->records_out[$filter][$r["parent_id"]]))
  310. $this->records_out[$filter][$r["parent_id"]] += $amount;
  311. else
  312. $this->records_out[$filter][$r["parent_id"]] = $amount;
  313. if ($r["parent_id"] > 0)
  314. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  315. }
  316. }
  317. }
  318. }
  319. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  320. {
  321. if ($type == "IN")
  322. {
  323. foreach($this->rows_in as $r)
  324. {
  325. if ($r["id"] == $causal_id)
  326. {
  327. if (isset($records_in[$filter][$r["parent_id"]]))
  328. $records_in[$filter][$r["parent_id"]] += $amount;
  329. else
  330. $records_in[$filter][$r["parent_id"]] = $amount;
  331. if ($r["parent_id"] > 0)
  332. $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  333. }
  334. }
  335. }
  336. if ($type == "OUT")
  337. {
  338. foreach($this->rows_out as $r)
  339. {
  340. if ($r["id"] == $causal_id)
  341. {
  342. if (isset($records_out[$filter][$r["parent_id"]]))
  343. $records_out[$filter][$r["parent_id"]] += $amount;
  344. else
  345. $records_out[$filter][$r["parent_id"]] = $amount;
  346. if ($r["parent_id"] > 0)
  347. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  348. }
  349. }
  350. }
  351. }
  352. public function getCausal($causal)
  353. {
  354. $ret = '';
  355. if ($causal > 0)
  356. {
  357. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  358. }
  359. return $ret;
  360. }
  361. public function getMonth($str)
  362. {
  363. $ret = '';
  364. list($m, $y) = explode("-", $str);
  365. switch ($m) {
  366. case 'x':
  367. $ret = '';
  368. break;
  369. case '01':
  370. $ret = 'Gennaio ';
  371. break;
  372. case '02':
  373. $ret = 'Febbraio ';
  374. break;
  375. case '03':
  376. $ret = 'Marzo ';
  377. break;
  378. case '04':
  379. $ret = 'Aprile ';
  380. break;
  381. case '05':
  382. $ret = 'Maggio ';
  383. break;
  384. case '06':
  385. $ret = 'Giugno ';
  386. break;
  387. case '07':
  388. $ret = 'Luglio ';
  389. break;
  390. case '08':
  391. $ret = 'Agosto ';
  392. break;
  393. case '09':
  394. $ret = 'Settembre ';
  395. break;
  396. case '10':
  397. $ret = 'Ottobre ';
  398. break;
  399. case '11':
  400. $ret = 'Novembre ';
  401. break;
  402. case '12':
  403. $ret = 'Dicembre ';
  404. break;
  405. default:
  406. $ret = '';
  407. break;
  408. }
  409. if ($m == 'x') {
  410. $nextY = $y+1;
  411. $ret = "{$y}/{$nextY}";
  412. } else {
  413. $ret .= $y;
  414. }
  415. return $ret;
  416. }
  417. public function clear()
  418. {
  419. $this->columns = [];
  420. $this->datas = [];
  421. $this->records_out = [];
  422. $this->records_in = [];
  423. $this->emit('load-select');
  424. $this->emit('reset-collapse');
  425. //$this->showData = false;
  426. }
  427. public function remove($idx)
  428. {
  429. if (sizeof($this->datas) > 1)
  430. array_splice($this->datas, $idx, 1);
  431. //unset($this->datas[$idx]);
  432. else
  433. $this->datas = array();
  434. $this->show('', '');
  435. }
  436. public function export()
  437. {
  438. $rows_in = array();
  439. $rows_out = array();
  440. if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0)
  441. {
  442. foreach($this->rows_in as $r)
  443. {
  444. if (in_array($r["id"], $this->filterCausalsIn) || in_array($r["parent_id"], $this->filterCausalsIn) || in_array($r["first_parent_id"], $this->filterCausalsIn))
  445. {
  446. $rows_in[] = $r;
  447. }
  448. }
  449. }
  450. else
  451. {
  452. $rows_in = $this->rows_in;
  453. }
  454. if ($this->filterCausalsOut != null && sizeof($this->filterCausalsOut) > 0)
  455. {
  456. foreach($this->rows_out as $r)
  457. {
  458. if (in_array($r["id"], $this->filterCausalsOut) || in_array($r["parent_id"], $this->filterCausalsOut) || in_array($r["first_parent_id"], $this->filterCausalsOut))
  459. {
  460. $rows_out[] = $r;
  461. }
  462. }
  463. }
  464. else
  465. {
  466. $rows_out = $this->rows_out;
  467. }
  468. $path = $this->generateExcel($this->columns, $rows_in, $this->records_in, $rows_out, $this->records_out,false);
  469. return response()->download($path)->deleteFileAfterSend();
  470. }
  471. public function exportYear($year)
  472. {
  473. $records_in = [];
  474. $records_out = [];
  475. $datas = [];
  476. if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1)
  477. {
  478. if (date("m") < env('FISCAL_YEAR_MONTH_FROM', 1))
  479. $year -= 1;
  480. for($m=env('FISCAL_YEAR_MONTH_FROM', 1);$m<=12;$m++)
  481. {
  482. $datas[] = $m . "-" . $year;
  483. }
  484. for($m=1;$m<=env('FISCAL_YEAR_MONTH_TO', 12);$m++)
  485. {
  486. $datas[] = $m . "-" . ($year + 1);
  487. }
  488. }
  489. else
  490. {
  491. for($m=1;$m<=12;$m++)
  492. {
  493. $datas[] = $m . "-" . $year;
  494. }
  495. }
  496. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  497. foreach($datas as $filter)
  498. {
  499. $columns[] = $filter;
  500. $records = \App\Models\Record::where('type', 'IN')
  501. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  502. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  503. ->where(function ($query) {
  504. $query->where('deleted', false)->orWhere('deleted', null);
  505. })
  506. ->where(function ($query) {
  507. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  508. })
  509. ->whereNotIn('member_id', $exclude_from_records)
  510. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  511. //$records = $records->orderBy('date', 'DESC')->get();
  512. foreach($records as $record)
  513. {
  514. $amount = $record->amount;
  515. $amount += getVatValue($amount, $record->vat_id);
  516. $when = sizeof(json_decode($record->when));
  517. if ($when > 1)
  518. {
  519. $amount = $amount / $when;
  520. $record->amount = $amount;
  521. }
  522. // Aggiungo/aggiorno i dati
  523. if (isset($records_in[$filter][$record->causal_id]))
  524. $records_in[$filter][$record->causal_id] += $amount;
  525. else
  526. $records_in[$filter][$record->causal_id] = $amount;
  527. // Aggiorno i dati del padre
  528. $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
  529. }
  530. $records = \App\Models\Record::where('type', 'OUT')
  531. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  532. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  533. ->where(function ($query) {
  534. $query->where('deleted', false)->orWhere('deleted', null);
  535. })
  536. ->where(function ($query) {
  537. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  538. })
  539. ->whereNotIn('member_id', $exclude_from_records)
  540. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  541. //$records = $records->orderBy('date', 'DESC')->get();
  542. foreach($records as $record)
  543. {
  544. $amount = $record->amount;
  545. $when = sizeof(json_decode($record->when));
  546. if ($when > 1)
  547. {
  548. $amount = $amount / $when;
  549. $record->amount = $amount;
  550. }
  551. // Aggiungo/aggiorno i dati
  552. if (isset($records_out[$filter][$record->causal_id]))
  553. $records_out[$filter][$record->causal_id] += $amount;
  554. else
  555. $records_out[$filter][$record->causal_id] = $amount;
  556. $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
  557. }
  558. }
  559. $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out, true);
  560. return response()->download($path)->deleteFileAfterSend();
  561. }
  562. public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out, $isYearExport)
  563. {
  564. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
  565. $spreadsheet = new Spreadsheet();
  566. $activeWorksheet = $spreadsheet->getActiveSheet();
  567. $activeWorksheet->setCellValue('A1', 'Entrate');
  568. foreach($columns as $idx => $column)
  569. {
  570. $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
  571. }
  572. $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
  573. $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  574. $count = 2;
  575. $totals = [];
  576. foreach($rows_in as $in)
  577. {
  578. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
  579. foreach($columns as $idx => $column)
  580. {
  581. if(isset($records_in[$column][$in["id"]]))
  582. {
  583. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
  584. if ($in["level"] == 0)
  585. {
  586. if (isset($totals[$idx]))
  587. $totals[$idx] += $records_in[$column][$in["id"]];
  588. else
  589. $totals[$idx] = $records_in[$column][$in["id"]];
  590. }
  591. }
  592. }
  593. if ($in["level"] == 0)
  594. {
  595. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  596. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
  597. }
  598. $count += 1;
  599. }
  600. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  601. foreach($totals as $idx => $total)
  602. {
  603. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  604. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  605. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  606. }
  607. $count += 2;
  608. $activeWorksheet->setCellValue('A' . $count, "Uscite");
  609. foreach($columns as $idx => $column)
  610. {
  611. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
  612. }
  613. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  614. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  615. $count += 1;
  616. $totals = [];
  617. foreach($rows_out as $out)
  618. {
  619. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
  620. foreach($columns as $idx => $column)
  621. {
  622. if(isset($records_out[$column][$out["id"]]))
  623. {
  624. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
  625. if ($out["level"] == 0)
  626. {
  627. if (isset($totals[$idx]))
  628. $totals[$idx] += $records_out[$column][$out["id"]];
  629. else
  630. $totals[$idx] = $records_out[$column][$out["id"]];
  631. }
  632. }
  633. }
  634. if ($out["level"] == 0)
  635. {
  636. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  637. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
  638. }
  639. $count += 1;
  640. }
  641. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  642. foreach($totals as $idx => $total)
  643. {
  644. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  645. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  646. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  647. }
  648. $activeWorksheet->getColumnDimension('A')->setWidth(35);
  649. for($i = 1; $i < count($letters); $i++) {
  650. $activeWorksheet->getColumnDimension($letters[$i])->setWidth(20);
  651. }
  652. $fileSuffix = $isYearExport ? 'AnnoFiscale' : 'Selezione';
  653. $writer = new Xlsx($spreadsheet);
  654. $writer->save($path = storage_path(date("Ymd") .'_Gestionale_' . $fileSuffix . '.xlsx'));
  655. return $path;
  656. }
  657. }