RecordINOUT.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. $f = $filter;
  158. if ($m == 'x')
  159. $f = str_replace("x-", "", $filter);
  160. //$dt = $y . "-0" . $m;
  161. $records = \App\Models\Record::where('type', 'IN')
  162. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  163. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  164. ->where(function ($query) {
  165. $query->where('deleted', false)->orWhere('deleted', null);
  166. })
  167. ->where(function ($query) {
  168. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  169. })
  170. ->whereNotIn('member_id', $exclude_from_records)
  171. /*->where(function ($query) use ($f, $dt) {
  172. $query->where('records.date', 'like', '%' . $dt . '%')->orWhere('records_rows.when', 'like', '%' . $f . '%');
  173. })*/
  174. ->where('records_rows.when', 'like', '%"' . $f . '"%')
  175. ->get();
  176. //$records = $records->orderBy('date', 'DESC')->get();
  177. foreach($records as $record)
  178. {
  179. $amount = $record->amount;
  180. $amount += getVatValue($amount, $record->vat_id);
  181. $when = sizeof(json_decode($record->when));
  182. if ($when > 1)
  183. {
  184. $amount = $amount / $when;
  185. $record->amount = $amount;
  186. }
  187. // Aggiungo/aggiorno i dati
  188. if (isset($this->records_in[$filter][$record->causal_id]))
  189. $this->records_in[$filter][$record->causal_id] += $amount;
  190. else
  191. $this->records_in[$filter][$record->causal_id] = $amount;
  192. // Aggiorno i dati del padre
  193. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  194. }
  195. $records = \App\Models\Record::where('type', 'OUT')
  196. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  197. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  198. ->where(function ($query) {
  199. $query->where('deleted', false)->orWhere('deleted', null);
  200. })
  201. ->where(function ($query) {
  202. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  203. })
  204. ->whereNotIn('member_id', $exclude_from_records)
  205. ->where('records_rows.when', 'like', '%"' . $f . '"%')->get();
  206. //$records = $records->orderBy('date', 'DESC')->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_out[$filter][$record->causal_id]))
  219. $this->records_out[$filter][$record->causal_id] += $amount;
  220. else
  221. $this->records_out[$filter][$record->causal_id] = $amount;
  222. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  223. }
  224. }
  225. }
  226. //$this->showData = true;
  227. $this->emit('load-table');
  228. }
  229. public function updateParent($type, $causal_id, $amount, $filter)
  230. {
  231. if ($type == "IN")
  232. {
  233. foreach($this->rows_in as $r)
  234. {
  235. if ($r["id"] == $causal_id)
  236. {
  237. if (isset($this->records_in[$filter][$r["parent_id"]]))
  238. $this->records_in[$filter][$r["parent_id"]] += $amount;
  239. else
  240. $this->records_in[$filter][$r["parent_id"]] = $amount;
  241. if ($r["parent_id"] > 0)
  242. $this->updateParent("IN", $r["parent_id"], $amount, $filter);
  243. }
  244. }
  245. }
  246. if ($type == "OUT")
  247. {
  248. foreach($this->rows_out as $r)
  249. {
  250. if ($r["id"] == $causal_id)
  251. {
  252. if (isset($this->records_out[$filter][$r["parent_id"]]))
  253. $this->records_out[$filter][$r["parent_id"]] += $amount;
  254. else
  255. $this->records_out[$filter][$r["parent_id"]] = $amount;
  256. if ($r["parent_id"] > 0)
  257. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  258. }
  259. }
  260. }
  261. }
  262. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  263. {
  264. if ($type == "IN")
  265. {
  266. foreach($this->rows_in as $r)
  267. {
  268. if ($r["id"] == $causal_id)
  269. {
  270. if (isset($records_in[$filter][$r["parent_id"]]))
  271. $records_in[$filter][$r["parent_id"]] += $amount;
  272. else
  273. $records_in[$filter][$r["parent_id"]] = $amount;
  274. if ($r["parent_id"] > 0)
  275. $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  276. }
  277. }
  278. }
  279. if ($type == "OUT")
  280. {
  281. foreach($this->rows_out as $r)
  282. {
  283. if ($r["id"] == $causal_id)
  284. {
  285. if (isset($records_out[$filter][$r["parent_id"]]))
  286. $records_out[$filter][$r["parent_id"]] += $amount;
  287. else
  288. $records_out[$filter][$r["parent_id"]] = $amount;
  289. if ($r["parent_id"] > 0)
  290. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  291. }
  292. }
  293. }
  294. }
  295. public function getCausal($causal)
  296. {
  297. $ret = '';
  298. if ($causal > 0)
  299. {
  300. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  301. }
  302. return $ret;
  303. }
  304. public function getMonth($str)
  305. {
  306. $ret = '';
  307. list($m, $y) = explode("-", $str);
  308. switch ($m) {
  309. case 'x':
  310. $ret = '';
  311. break;
  312. case '01':
  313. $ret = 'Gennaio ';
  314. break;
  315. case '02':
  316. $ret = 'Febbraio ';
  317. break;
  318. case '03':
  319. $ret = 'Marzo ';
  320. break;
  321. case '04':
  322. $ret = 'Aprile ';
  323. break;
  324. case '05':
  325. $ret = 'Maggio ';
  326. break;
  327. case '06':
  328. $ret = 'Giugno ';
  329. break;
  330. case '07':
  331. $ret = 'Luglio ';
  332. break;
  333. case '08':
  334. $ret = 'Agosto ';
  335. break;
  336. case '09':
  337. $ret = 'Settembre ';
  338. break;
  339. case '10':
  340. $ret = 'Ottobre ';
  341. break;
  342. case '11':
  343. $ret = 'Novembre ';
  344. break;
  345. case '12':
  346. $ret = 'Dicembre ';
  347. break;
  348. default:
  349. $ret = '';
  350. break;
  351. }
  352. $ret .= $y;
  353. return $ret;
  354. }
  355. public function clear()
  356. {
  357. $this->columns = [];
  358. $this->datas = [];
  359. $this->records_out = [];
  360. $this->records_in = [];
  361. $this->emit('load-select');
  362. $this->emit('reset-collapse');
  363. //$this->showData = false;
  364. }
  365. public function remove($idx)
  366. {
  367. if (sizeof($this->datas) > 1)
  368. array_splice($this->datas, $idx, 1);
  369. //unset($this->datas[$idx]);
  370. else
  371. $this->datas = array();
  372. $this->show('', '');
  373. }
  374. public function export()
  375. {
  376. $rows_in = array();
  377. if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0)
  378. {
  379. foreach($this->rows_in as $r)
  380. {
  381. if (in_array($r["id"], $this->filterCausalsIn) || in_array($r["parent_id"], $this->filterCausalsIn) || in_array($r["first_parent_id"], $this->filterCausalsIn))
  382. {
  383. $rows_in[] = $r;
  384. }
  385. }
  386. }
  387. else
  388. {
  389. $rows_in = $this->rows_in;
  390. }
  391. $path = $this->generateExcel($this->columns, $rows_in, $this->records_in, $this->rows_out, $this->records_out,false);
  392. return response()->download($path)->deleteFileAfterSend();
  393. }
  394. public function exportYear($year)
  395. {
  396. $records_in = [];
  397. $records_out = [];
  398. $datas = [];
  399. if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1)
  400. {
  401. if (date("m") < env('FISCAL_YEAR_MONTH_FROM', 1))
  402. $year -= 1;
  403. for($m=env('FISCAL_YEAR_MONTH_FROM', 1);$m<=12;$m++)
  404. {
  405. $datas[] = $m . "-" . $year;
  406. }
  407. for($m=1;$m<=env('FISCAL_YEAR_MONTH_TO', 12);$m++)
  408. {
  409. $datas[] = $m . "-" . ($year + 1);
  410. }
  411. }
  412. else
  413. {
  414. for($m=1;$m<=12;$m++)
  415. {
  416. $datas[] = $m . "-" . $year;
  417. }
  418. }
  419. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  420. foreach($datas as $filter)
  421. {
  422. $columns[] = $filter;
  423. $records = \App\Models\Record::where('type', 'IN')
  424. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  425. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  426. ->where(function ($query) {
  427. $query->where('deleted', false)->orWhere('deleted', null);
  428. })
  429. ->where(function ($query) {
  430. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  431. })
  432. ->whereNotIn('member_id', $exclude_from_records)
  433. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  434. //$records = $records->orderBy('date', 'DESC')->get();
  435. foreach($records as $record)
  436. {
  437. $amount = $record->amount;
  438. $amount += getVatValue($amount, $record->vat_id);
  439. $when = sizeof(json_decode($record->when));
  440. if ($when > 1)
  441. {
  442. $amount = $amount / $when;
  443. $record->amount = $amount;
  444. }
  445. // Aggiungo/aggiorno i dati
  446. if (isset($records_in[$filter][$record->causal_id]))
  447. $records_in[$filter][$record->causal_id] += $amount;
  448. else
  449. $records_in[$filter][$record->causal_id] = $amount;
  450. // Aggiorno i dati del padre
  451. $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
  452. }
  453. $records = \App\Models\Record::where('type', 'OUT')
  454. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  455. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  456. ->where(function ($query) {
  457. $query->where('deleted', false)->orWhere('deleted', null);
  458. })
  459. ->where(function ($query) {
  460. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  461. })
  462. ->whereNotIn('member_id', $exclude_from_records)
  463. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  464. //$records = $records->orderBy('date', 'DESC')->get();
  465. foreach($records as $record)
  466. {
  467. $amount = $record->amount;
  468. $when = sizeof(json_decode($record->when));
  469. if ($when > 1)
  470. {
  471. $amount = $amount / $when;
  472. $record->amount = $amount;
  473. }
  474. // Aggiungo/aggiorno i dati
  475. if (isset($records_out[$filter][$record->causal_id]))
  476. $records_out[$filter][$record->causal_id] += $amount;
  477. else
  478. $records_out[$filter][$record->causal_id] = $amount;
  479. $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
  480. }
  481. }
  482. $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out, true);
  483. return response()->download($path)->deleteFileAfterSend();
  484. }
  485. public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out, $isYearExport)
  486. {
  487. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
  488. $spreadsheet = new Spreadsheet();
  489. $activeWorksheet = $spreadsheet->getActiveSheet();
  490. $activeWorksheet->setCellValue('A1', 'Entrate');
  491. foreach($columns as $idx => $column)
  492. {
  493. $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
  494. }
  495. $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
  496. $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  497. $count = 2;
  498. $totals = [];
  499. foreach($rows_in as $in)
  500. {
  501. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
  502. foreach($columns as $idx => $column)
  503. {
  504. if(isset($records_in[$column][$in["id"]]))
  505. {
  506. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
  507. if ($in["level"] == 0)
  508. {
  509. if (isset($totals[$idx]))
  510. $totals[$idx] += $records_in[$column][$in["id"]];
  511. else
  512. $totals[$idx] = $records_in[$column][$in["id"]];
  513. }
  514. }
  515. }
  516. if ($in["level"] == 0)
  517. {
  518. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  519. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
  520. }
  521. $count += 1;
  522. }
  523. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  524. foreach($totals as $idx => $total)
  525. {
  526. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  527. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  528. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  529. }
  530. $count += 2;
  531. $activeWorksheet->setCellValue('A' . $count, "Uscite");
  532. foreach($columns as $idx => $column)
  533. {
  534. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
  535. }
  536. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  537. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  538. $count += 1;
  539. $totals = [];
  540. foreach($rows_out as $out)
  541. {
  542. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
  543. foreach($columns as $idx => $column)
  544. {
  545. if(isset($records_out[$column][$out["id"]]))
  546. {
  547. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
  548. if ($out["level"] == 0)
  549. {
  550. if (isset($totals[$idx]))
  551. $totals[$idx] += $records_out[$column][$out["id"]];
  552. else
  553. $totals[$idx] = $records_out[$column][$out["id"]];
  554. }
  555. }
  556. }
  557. if ($out["level"] == 0)
  558. {
  559. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  560. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
  561. }
  562. $count += 1;
  563. }
  564. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  565. foreach($totals as $idx => $total)
  566. {
  567. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  568. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  569. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  570. }
  571. $activeWorksheet->getColumnDimension('A')->setWidth(35);
  572. for($i = 1; $i < count($letters); $i++) {
  573. $activeWorksheet->getColumnDimension($letters[$i])->setWidth(20);
  574. }
  575. $fileSuffix = $isYearExport ? 'AnnoFiscale' : 'Selezione';
  576. $writer = new Xlsx($spreadsheet);
  577. $writer->save($path = storage_path(date("Ymd") .'_Gestionale_' . $fileSuffix . '.xlsx'));
  578. return $path;
  579. }
  580. }