RecordINOUT.php 26 KB

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