RecordINOUT.php 26 KB

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