RecordINOUT.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. $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. $this->causalsIn[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  67. if (count($record->childs))
  68. $this->getCausalsIn($record->childs, $indentation + 1);
  69. }
  70. }
  71. public function getCausalsOut($records, $indentation)
  72. {
  73. foreach ($records as $record) {
  74. $this->causalsOut[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  75. if (count($record->childs))
  76. $this->getCausalsOut($record->childs, $indentation + 1);
  77. }
  78. }
  79. public function getCausale($records, $type, $indentation)
  80. {
  81. foreach ($records as $record) {
  82. $first_parent_id = null;
  83. if ($record->parent_id != null) {
  84. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  85. }
  86. if ($type == 'IN')
  87. $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));
  88. if ($type == 'OUT')
  89. $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));
  90. if (count($record->childs))
  91. $this->getCausale($record->childs, $type, $indentation + 1);
  92. }
  93. }
  94. public function getAllChild($id)
  95. {
  96. $aChilds = array();
  97. $aChilds[] = $id;
  98. $record = \App\Models\Causal::findOrFail($id);
  99. $aChilds[] = $record->parent_id;
  100. if ($record->parent_id != null) {
  101. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  102. $aChilds[] = $first_parent_id;
  103. }
  104. $childs = \App\Models\Causal::where('parent_id', $id)->get();
  105. foreach ($childs as $child) {
  106. $aChilds[] = $child->id;
  107. $childs2 = \App\Models\Causal::where('parent_id', $child->id)->get();
  108. foreach ($childs2 as $child2) {
  109. $aChilds[] = $child2->id;
  110. $childs3 = \App\Models\Causal::where('parent_id', $child2->id)->get();
  111. foreach ($childs3 as $child3) {
  112. $aChilds[] = $child3->id;
  113. }
  114. }
  115. }
  116. return $aChilds;
  117. }
  118. public function getCausalName($id)
  119. {
  120. if ($id > 0)
  121. return \App\Models\Causal::findOrFail($id)->name;
  122. else
  123. return "";
  124. }
  125. public function render()
  126. {
  127. return view('livewire.records_in_out');
  128. }
  129. public function hydrate()
  130. {
  131. $this->emit('load-select');
  132. }
  133. public function show($m, $y)
  134. {
  135. if ($m != "" && $y != "" && !in_array($m . "-" . $y, $this->datas))
  136. $this->datas[] = $m . "-" . $y;
  137. $this->columns = array();
  138. $this->records_in = [];
  139. $this->records_out = [];
  140. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  141. if (sizeof($this->datas) > 0) {
  142. foreach ($this->datas as $filter) {
  143. $this->columns[] = $filter;
  144. list($m, $y) = explode("-", $filter);
  145. $pairs = [];
  146. if ($m == 'x') {
  147. $year = $y;
  148. $next_year = $y + 1;
  149. $start = Carbon::createFromFormat("Y-m-d", "$year-09-01")->startOfMonth();
  150. $end = Carbon::createFromFormat("Y-m-d", "$next_year-08-31")->startOfMonth();
  151. foreach (\Carbon\CarbonPeriod::create($start, '1 month', $end) as $d) {
  152. $pairs[] = (string)$d->month . "-" . (string)$d->year;
  153. }
  154. $pairs = implode("|", $pairs);
  155. } else {
  156. $pairs = $filter;
  157. }
  158. $incomeQuery = \App\Models\Record::where('records.type', 'IN')
  159. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  160. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  161. ->where(function ($query) {
  162. $query->where('deleted', false)->orWhere('deleted', null);
  163. })
  164. ->where(function ($query) {
  165. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  166. })
  167. ->whereNotIn('member_id', $exclude_from_records)
  168. ->whereRaw('records_rows.when REGEXP ?', [$pairs]);
  169. $incomeRecords = $incomeQuery->get();
  170. foreach ($incomeRecords as $record) {
  171. $total_months = count(json_decode($record->when, true));
  172. $matching_months = 0;
  173. $matches = [];
  174. if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
  175. $matching_months = count($matches[0]);
  176. $amount = $record->amount;
  177. $amount += getVatValue($amount, $record->vat_id);
  178. $amount *= ($matching_months / $total_months);
  179. // Aggiungo/aggiorno i dati
  180. if (isset($this->records_in[$filter][$record->causal_id])) {
  181. $this->records_in[$filter][$record->causal_id] += $amount;
  182. } else {
  183. $this->records_in[$filter][$record->causal_id] = $amount;
  184. }
  185. // Aggiorno i dati del padre
  186. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  187. }
  188. $expenseQuery = \App\Models\Record::where('records.type', 'OUT')
  189. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  190. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  191. ->where(function ($query) {
  192. $query->where('deleted', false)->orWhere('deleted', null);
  193. })
  194. ->where(function ($query) {
  195. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  196. })
  197. ->whereNotIn('member_id', $exclude_from_records)
  198. ->whereRaw('records_rows.when REGEXP ?', [$pairs]);
  199. $expenseRecords = $expenseQuery->get();
  200. foreach ($expenseRecords as $record) {
  201. $when_array = json_decode($record->when, true);
  202. if (!is_array($when_array) || empty($when_array) || count($when_array) <= 0) continue;
  203. $total_months = count($when_array);
  204. $matching_months = 0;
  205. $matches = [];
  206. if (!preg_match_all("/$pairs/", $record->when, $matches)) continue;
  207. $matching_months = count($matches[0]);
  208. $amount = $record->amount;
  209. $amount += getVatValue($amount, $record->vat_id);
  210. $amount *= ($matching_months / $total_months);
  211. // Aggiungo/aggiorno i dati
  212. if (isset($this->records_out[$filter][$record->causal_id])) {
  213. $this->records_out[$filter][$record->causal_id] += $amount;
  214. } else {
  215. $this->records_out[$filter][$record->causal_id] = $amount;
  216. }
  217. // Aggiorno i dati del padre
  218. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  219. }
  220. }
  221. }
  222. //print $ccc."-";
  223. //print $ids;
  224. //$this->showData = true;
  225. $this->emit('load-table');
  226. }
  227. public function updateParent($type, $causal_id, $amount, $filter)
  228. {
  229. if ($type == "IN") {
  230. foreach ($this->rows_in as $r) {
  231. if ($r["id"] == $causal_id) {
  232. if (isset($this->records_in[$filter][$r["parent_id"]]))
  233. $this->records_in[$filter][$r["parent_id"]] += $amount;
  234. else
  235. $this->records_in[$filter][$r["parent_id"]] = $amount;
  236. if ($r["parent_id"] > 0)
  237. $this->updateParent("IN", $r["parent_id"], $amount, $filter);
  238. }
  239. }
  240. }
  241. if ($type == "OUT") {
  242. foreach ($this->rows_out as $r) {
  243. if ($r["id"] == $causal_id) {
  244. if (isset($this->records_out[$filter][$r["parent_id"]]))
  245. $this->records_out[$filter][$r["parent_id"]] += $amount;
  246. else
  247. $this->records_out[$filter][$r["parent_id"]] = $amount;
  248. if ($r["parent_id"] > 0)
  249. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  250. }
  251. }
  252. }
  253. }
  254. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  255. {
  256. if ($type == "IN") {
  257. foreach ($this->rows_in as $r) {
  258. if ($r["id"] == $causal_id) {
  259. if (isset($records_in[$filter][$r["parent_id"]]))
  260. $records_in[$filter][$r["parent_id"]] += $amount;
  261. else
  262. $records_in[$filter][$r["parent_id"]] = $amount;
  263. if ($r["parent_id"] > 0)
  264. $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  265. }
  266. }
  267. }
  268. if ($type == "OUT") {
  269. foreach ($this->rows_out as $r) {
  270. if ($r["id"] == $causal_id) {
  271. if (isset($records_out[$filter][$r["parent_id"]]))
  272. $records_out[$filter][$r["parent_id"]] += $amount;
  273. else
  274. $records_out[$filter][$r["parent_id"]] = $amount;
  275. if ($r["parent_id"] > 0)
  276. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  277. }
  278. }
  279. }
  280. }
  281. public function getCausal($causal)
  282. {
  283. $ret = '';
  284. if ($causal > 0) {
  285. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  286. }
  287. return $ret;
  288. }
  289. public function getMonth($str)
  290. {
  291. $ret = '';
  292. list($m, $y) = explode("-", $str);
  293. switch ($m) {
  294. case 'x':
  295. $ret = '';
  296. break;
  297. case '01':
  298. $ret = 'Gennaio ';
  299. break;
  300. case '02':
  301. $ret = 'Febbraio ';
  302. break;
  303. case '03':
  304. $ret = 'Marzo ';
  305. break;
  306. case '04':
  307. $ret = 'Aprile ';
  308. break;
  309. case '05':
  310. $ret = 'Maggio ';
  311. break;
  312. case '06':
  313. $ret = 'Giugno ';
  314. break;
  315. case '07':
  316. $ret = 'Luglio ';
  317. break;
  318. case '08':
  319. $ret = 'Agosto ';
  320. break;
  321. case '09':
  322. $ret = 'Settembre ';
  323. break;
  324. case '10':
  325. $ret = 'Ottobre ';
  326. break;
  327. case '11':
  328. $ret = 'Novembre ';
  329. break;
  330. case '12':
  331. $ret = 'Dicembre ';
  332. break;
  333. default:
  334. $ret = '';
  335. break;
  336. }
  337. if ($m == 'x') {
  338. $nextY = $y + 1;
  339. $ret = "{$y}/{$nextY}";
  340. } else {
  341. $ret .= $y;
  342. }
  343. return $ret;
  344. }
  345. public function clear()
  346. {
  347. $this->columns = [];
  348. $this->datas = [];
  349. $this->records_out = [];
  350. $this->records_in = [];
  351. $this->emit('load-select');
  352. $this->emit('reset-collapse');
  353. //$this->showData = false;
  354. }
  355. public function remove($idx)
  356. {
  357. if (sizeof($this->datas) > 1)
  358. array_splice($this->datas, $idx, 1);
  359. //unset($this->datas[$idx]);
  360. else
  361. $this->datas = array();
  362. $this->show('', '');
  363. }
  364. public function export()
  365. {
  366. $rows_in = array();
  367. $rows_out = array();
  368. if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0) {
  369. foreach ($this->rows_in as $r) {
  370. if (in_array($r["id"], $this->filterCausalsIn) || in_array($r["parent_id"], $this->filterCausalsIn) || in_array($r["first_parent_id"], $this->filterCausalsIn)) {
  371. $rows_in[] = $r;
  372. }
  373. }
  374. } else {
  375. $rows_in = $this->rows_in;
  376. }
  377. if ($this->filterCausalsOut != null && sizeof($this->filterCausalsOut) > 0) {
  378. foreach ($this->rows_out as $r) {
  379. if (in_array($r["id"], $this->filterCausalsOut) || in_array($r["parent_id"], $this->filterCausalsOut) || in_array($r["first_parent_id"], $this->filterCausalsOut)) {
  380. $rows_out[] = $r;
  381. }
  382. }
  383. } else {
  384. $rows_out = $this->rows_out;
  385. }
  386. $path = $this->generateExcel($this->columns, $rows_in, $this->records_in, $rows_out, $this->records_out, false);
  387. return response()->download($path)->deleteFileAfterSend();
  388. }
  389. public function exportYear($year)
  390. {
  391. $records_in = [];
  392. $records_out = [];
  393. $datas = [];
  394. if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1) {
  395. if (date("m") < env('FISCAL_YEAR_MONTH_FROM', 1))
  396. $year -= 1;
  397. for ($m = env('FISCAL_YEAR_MONTH_FROM', 1); $m <= 12; $m++) {
  398. $datas[] = $m . "-" . $year;
  399. }
  400. for ($m = 1; $m <= env('FISCAL_YEAR_MONTH_TO', 12); $m++) {
  401. $datas[] = $m . "-" . ($year + 1);
  402. }
  403. } else {
  404. for ($m = 1; $m <= 12; $m++) {
  405. $datas[] = $m . "-" . $year;
  406. }
  407. }
  408. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  409. foreach ($datas as $filter) {
  410. $columns[] = $filter;
  411. $records = \App\Models\Record::where('type', 'IN')
  412. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  413. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  414. ->where(function ($query) {
  415. $query->where('deleted', false)->orWhere('deleted', null);
  416. })
  417. ->where(function ($query) {
  418. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  419. })
  420. ->whereNotIn('member_id', $exclude_from_records)
  421. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  422. //$records = $records->orderBy('date', 'DESC')->get();
  423. foreach ($records as $record) {
  424. $amount = $record->amount;
  425. $amount += getVatValue($amount, $record->vat_id);
  426. $when = sizeof(json_decode($record->when));
  427. if ($when > 1) {
  428. $amount = $amount / $when;
  429. //$record->amount = $amount;
  430. }
  431. // Aggiungo/aggiorno i dati
  432. if (isset($records_in[$filter][$record->causal_id]))
  433. $records_in[$filter][$record->causal_id] += $amount;
  434. else
  435. $records_in[$filter][$record->causal_id] = $amount;
  436. // Aggiorno i dati del padre
  437. $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
  438. }
  439. $records = \App\Models\Record::where('type', 'OUT')
  440. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  441. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  442. ->where(function ($query) {
  443. $query->where('deleted', false)->orWhere('deleted', null);
  444. })
  445. ->where(function ($query) {
  446. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  447. })
  448. ->whereNotIn('member_id', $exclude_from_records)
  449. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  450. //$records = $records->orderBy('date', 'DESC')->get();
  451. foreach ($records as $record) {
  452. $amount = $record->amount;
  453. $when = sizeof(json_decode($record->when));
  454. if ($when > 1) {
  455. $amount = $amount / $when;
  456. $record->amount = $amount;
  457. }
  458. // Aggiungo/aggiorno i dati
  459. if (isset($records_out[$filter][$record->causal_id]))
  460. $records_out[$filter][$record->causal_id] += $amount;
  461. else
  462. $records_out[$filter][$record->causal_id] = $amount;
  463. $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
  464. }
  465. }
  466. $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out, true);
  467. return response()->download($path)->deleteFileAfterSend();
  468. }
  469. public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out, $isYearExport)
  470. {
  471. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
  472. $spreadsheet = new Spreadsheet();
  473. $activeWorksheet = $spreadsheet->getActiveSheet();
  474. $activeWorksheet->setCellValue('A1', 'Entrate');
  475. foreach ($columns as $idx => $column) {
  476. $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
  477. }
  478. $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
  479. $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  480. $count = 2;
  481. $totals = [];
  482. foreach ($rows_in as $in) {
  483. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
  484. foreach ($columns as $idx => $column) {
  485. if (isset($records_in[$column][$in["id"]])) {
  486. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
  487. if ($in["level"] == 0) {
  488. if (isset($totals[$idx]))
  489. $totals[$idx] += $records_in[$column][$in["id"]];
  490. else
  491. $totals[$idx] = $records_in[$column][$in["id"]];
  492. }
  493. }
  494. }
  495. if ($in["level"] == 0) {
  496. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  497. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
  498. }
  499. $count += 1;
  500. }
  501. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  502. foreach ($totals as $idx => $total) {
  503. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  504. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  505. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  506. }
  507. $count += 2;
  508. $activeWorksheet->setCellValue('A' . $count, "Uscite");
  509. foreach ($columns as $idx => $column) {
  510. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
  511. }
  512. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  513. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  514. $count += 1;
  515. $totals = [];
  516. foreach ($rows_out as $out) {
  517. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
  518. foreach ($columns as $idx => $column) {
  519. if (isset($records_out[$column][$out["id"]])) {
  520. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
  521. if ($out["level"] == 0) {
  522. if (isset($totals[$idx]))
  523. $totals[$idx] += $records_out[$column][$out["id"]];
  524. else
  525. $totals[$idx] = $records_out[$column][$out["id"]];
  526. }
  527. }
  528. }
  529. if ($out["level"] == 0) {
  530. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  531. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
  532. }
  533. $count += 1;
  534. }
  535. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  536. foreach ($totals as $idx => $total) {
  537. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  538. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  539. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  540. }
  541. $activeWorksheet->getColumnDimension('A')->setWidth(35);
  542. for ($i = 1; $i < count($letters); $i++) {
  543. $activeWorksheet->getColumnDimension($letters[$i])->setWidth(20);
  544. }
  545. $fileSuffix = $isYearExport ? 'AnnoFiscale' : 'Selezione';
  546. $writer = new Xlsx($spreadsheet);
  547. $writer->save($path = storage_path(date("Ymd") . '_Gestionale_' . $fileSuffix . '.xlsx'));
  548. return $path;
  549. }
  550. }