RecordINOUT.php 29 KB

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