RecordINOUT.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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\Log;
  7. use SimpleXMLElement;
  8. use Illuminate\Support\Facades\Auth;
  9. class RecordINOUT extends Component
  10. {
  11. //public $records_in, $records_out;
  12. public $total_in = 0;
  13. public $total_out = 0;
  14. public $datas = [];
  15. public $month;
  16. public $year;
  17. public $months_1 = [];
  18. public $year_1;
  19. public $months_2 = [];
  20. public $year_2;
  21. public $show_block_2;
  22. //public $borsellino_id = 0;
  23. public $columns = array();
  24. public $rows_in = array();
  25. public $rows_out = array();
  26. public $records_in = array();
  27. public $records_out = array();
  28. public $showData = true;
  29. public $hasFilter = false;
  30. public $total = 0;
  31. public $selectedFilter = '';
  32. public $causalsIn = array();
  33. public $causalsOut = array();
  34. public $payments = array();
  35. public $members = array();
  36. public $filterCausalsIn = null;
  37. public $filterCausalsOut = null;
  38. public $excludeCausals = array();
  39. public function mount()
  40. {
  41. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  42. return redirect()->to('/dashboard');
  43. $borsellino = \App\Models\Causal::where('money', true)->first();
  44. if ($borsellino)
  45. $this->excludeCausals[] = $borsellino->id;
  46. //$this->borsellino_id = $borsellino->id;
  47. // Aggiungo
  48. $excludes = \App\Models\Causal::where('no_records', true)->get();
  49. foreach($excludes as $e)
  50. {
  51. $this->excludeCausals[] = $e->id;
  52. }
  53. $this->month = date("m");
  54. $this->year = date("Y");
  55. $this->year_1 = date("Y");
  56. $this->year_2 = date("Y");
  57. $this->show_block_2 = false;
  58. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get(), 'IN', 0);
  59. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get(), 'OUT', 0);
  60. $this->getCausalsIn(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  61. $this->getCausalsOut(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->get(), 0);
  62. //$this->causalsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get();
  63. //$this->causalsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get();
  64. }
  65. public function getCausalsIn($records, $indentation)
  66. {
  67. foreach($records as $record)
  68. {
  69. $this->causalsIn[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  70. if(count($record->childs))
  71. $this->getCausalsIn($record->childs, $indentation + 1);
  72. }
  73. }
  74. public function getCausalsOut($records, $indentation)
  75. {
  76. foreach($records as $record)
  77. {
  78. $this->causalsOut[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  79. if(count($record->childs))
  80. $this->getCausalsOut($record->childs, $indentation + 1);
  81. }
  82. }
  83. public function getCausale($records, $type, $indentation)
  84. {
  85. foreach($records as $record)
  86. {
  87. $first_parent_id = null;
  88. if ($record->parent_id != null)
  89. {
  90. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  91. }
  92. if ($type == 'IN')
  93. $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));
  94. if ($type == 'OUT')
  95. $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));
  96. if(count($record->childs))
  97. $this->getCausale($record->childs, $type, $indentation + 1);
  98. }
  99. }
  100. public function getAllChild($id)
  101. {
  102. $aChilds = array();
  103. $aChilds[] = $id;
  104. $record = \App\Models\Causal::findOrFail($id);
  105. $aChilds[] = $record->parent_id;
  106. if ($record->parent_id != null)
  107. {
  108. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  109. $aChilds[] = $first_parent_id;
  110. }
  111. $childs = \App\Models\Causal::where('parent_id', $id)->get();
  112. foreach($childs as $child)
  113. {
  114. $aChilds[] = $child->id;
  115. $childs2 = \App\Models\Causal::where('parent_id', $child->id)->get();
  116. foreach($childs2 as $child2)
  117. {
  118. $aChilds[] = $child2->id;
  119. $childs3 = \App\Models\Causal::where('parent_id', $child2->id)->get();
  120. foreach($childs3 as $child3)
  121. {
  122. $aChilds[] = $child3->id;
  123. }
  124. }
  125. }
  126. return $aChilds;
  127. }
  128. public function getCausalName($id)
  129. {
  130. if ($id > 0)
  131. return \App\Models\Causal::findOrFail($id)->name;
  132. else
  133. return "";
  134. }
  135. public function render()
  136. {
  137. return view('livewire.records_in_out');
  138. }
  139. public function hydrate()
  140. {
  141. $this->emit('load-select');
  142. }
  143. public function show($m, $y)
  144. //public function show($dt)
  145. {
  146. //list($m, $y) = explode("_", $dt);
  147. if ($m != "" && $y != "" && !in_array($m . "-" . $y, $this->datas))
  148. $this->datas[] = $m . "-" . $y;
  149. $this->columns = array();
  150. $this->records_in = [];
  151. $this->records_out = [];
  152. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  153. if (sizeof($this->datas) > 0)
  154. {
  155. foreach($this->datas as $filter)
  156. {
  157. // $filter = $m . "-" . $this->year;
  158. $this->columns[] = $filter;
  159. $f = $filter;
  160. if ($m == 'x')
  161. $f = str_replace("x-", "", $filter);
  162. //$dt = $y . "-0" . $m;
  163. $records = \App\Models\Record::where('type', 'IN')
  164. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  165. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  166. ->where(function ($query) {
  167. $query->where('deleted', false)->orWhere('deleted', null);
  168. })
  169. ->where(function ($query) {
  170. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  171. })
  172. ->whereNotIn('member_id', $exclude_from_records)
  173. /*->where(function ($query) use ($f, $dt) {
  174. $query->where('records.date', 'like', '%' . $dt . '%')->orWhere('records_rows.when', 'like', '%' . $f . '%');
  175. })*/
  176. ->where('records_rows.when', 'like', '%"' . $f . '"%')
  177. ->get();
  178. //$records = $records->orderBy('date', 'DESC')->get();
  179. foreach($records as $record)
  180. {
  181. $amount = $record->amount;
  182. $amount += getVatValue($amount, $record->vat_id);
  183. $when = sizeof(json_decode($record->when));
  184. if ($when > 1)
  185. {
  186. $amount = $amount / $when;
  187. $record->amount = $amount;
  188. }
  189. // Aggiungo/aggiorno i dati
  190. if (isset($this->records_in[$filter][$record->causal_id]))
  191. $this->records_in[$filter][$record->causal_id] += $amount;
  192. else
  193. $this->records_in[$filter][$record->causal_id] = $amount;
  194. // Aggiorno i dati del padre
  195. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  196. }
  197. $records = \App\Models\Record::where('type', 'OUT')
  198. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  199. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  200. ->where(function ($query) {
  201. $query->where('deleted', false)->orWhere('deleted', null);
  202. })
  203. ->where(function ($query) {
  204. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  205. })
  206. ->whereNotIn('member_id', $exclude_from_records)
  207. ->where('records_rows.when', 'like', '%"' . $f . '"%')->get();
  208. //$records = $records->orderBy('date', 'DESC')->get();
  209. foreach($records as $record)
  210. {
  211. $amount = $record->amount;
  212. $amount += getVatValue($amount, $record->vat_id);
  213. $when = sizeof(json_decode($record->when));
  214. if ($when > 1)
  215. {
  216. $amount = $amount / $when;
  217. $record->amount = $amount;
  218. }
  219. // Aggiungo/aggiorno i dati
  220. if (isset($this->records_out[$filter][$record->causal_id]))
  221. $this->records_out[$filter][$record->causal_id] += $amount;
  222. else
  223. $this->records_out[$filter][$record->causal_id] = $amount;
  224. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  225. }
  226. }
  227. }
  228. //$this->showData = true;
  229. $this->emit('load-table');
  230. }
  231. public function updateParent($type, $causal_id, $amount, $filter)
  232. {
  233. if ($type == "IN")
  234. {
  235. foreach($this->rows_in as $r)
  236. {
  237. if ($r["id"] == $causal_id)
  238. {
  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. {
  250. foreach($this->rows_out as $r)
  251. {
  252. if ($r["id"] == $causal_id)
  253. {
  254. if (isset($this->records_out[$filter][$r["parent_id"]]))
  255. $this->records_out[$filter][$r["parent_id"]] += $amount;
  256. else
  257. $this->records_out[$filter][$r["parent_id"]] = $amount;
  258. if ($r["parent_id"] > 0)
  259. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  260. }
  261. }
  262. }
  263. }
  264. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  265. {
  266. if ($type == "IN")
  267. {
  268. foreach($this->rows_in as $r)
  269. {
  270. if ($r["id"] == $causal_id)
  271. {
  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. {
  283. foreach($this->rows_out as $r)
  284. {
  285. if ($r["id"] == $causal_id)
  286. {
  287. if (isset($records_out[$filter][$r["parent_id"]]))
  288. $records_out[$filter][$r["parent_id"]] += $amount;
  289. else
  290. $records_out[$filter][$r["parent_id"]] = $amount;
  291. if ($r["parent_id"] > 0)
  292. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  293. }
  294. }
  295. }
  296. }
  297. public function getCausal($causal)
  298. {
  299. $ret = '';
  300. if ($causal > 0)
  301. {
  302. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  303. }
  304. return $ret;
  305. }
  306. public function getMonth($str)
  307. {
  308. $ret = '';
  309. list($m, $y) = explode("-", $str);
  310. switch ($m) {
  311. case 'x':
  312. $ret = '';
  313. break;
  314. case '01':
  315. $ret = 'Gennaio ';
  316. break;
  317. case '02':
  318. $ret = 'Febbraio ';
  319. break;
  320. case '03':
  321. $ret = 'Marzo ';
  322. break;
  323. case '04':
  324. $ret = 'Aprile ';
  325. break;
  326. case '05':
  327. $ret = 'Maggio ';
  328. break;
  329. case '06':
  330. $ret = 'Giugno ';
  331. break;
  332. case '07':
  333. $ret = 'Luglio ';
  334. break;
  335. case '08':
  336. $ret = 'Agosto ';
  337. break;
  338. case '09':
  339. $ret = 'Settembre ';
  340. break;
  341. case '10':
  342. $ret = 'Ottobre ';
  343. break;
  344. case '11':
  345. $ret = 'Novembre ';
  346. break;
  347. case '12':
  348. $ret = 'Dicembre ';
  349. break;
  350. default:
  351. $ret = '';
  352. break;
  353. }
  354. $ret .= $y;
  355. return $ret;
  356. }
  357. public function clear()
  358. {
  359. $this->columns = [];
  360. $this->datas = [];
  361. $this->records_out = [];
  362. $this->records_in = [];
  363. $this->emit('load-select');
  364. $this->emit('reset-collapse');
  365. //$this->showData = false;
  366. }
  367. public function remove($idx)
  368. {
  369. if (sizeof($this->datas) > 1)
  370. array_splice($this->datas, $idx, 1);
  371. //unset($this->datas[$idx]);
  372. else
  373. $this->datas = array();
  374. $this->show('', '');
  375. }
  376. public function export()
  377. {
  378. $rows_in = array();
  379. if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0)
  380. {
  381. foreach($this->rows_in as $r)
  382. {
  383. if (in_array($r["id"], $this->filterCausalsIn) || in_array($r["parent_id"], $this->filterCausalsIn) || in_array($r["first_parent_id"], $this->filterCausalsIn))
  384. {
  385. $rows_in[] = $r;
  386. }
  387. }
  388. }
  389. else
  390. {
  391. $rows_in = $this->rows_in;
  392. }
  393. $path = $this->generateExcel($this->columns, $rows_in, $this->records_in, $this->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. {
  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. public function importReceipts()
  581. {
  582. $this->validate([
  583. 'receiptFile' => 'required|mimes:xml|max:2048',
  584. ]);
  585. try {
  586. $xmlString = file_get_contents($this->receiptFile->getRealPath());
  587. $xml = new SimpleXMLElement($xmlString);
  588. Log::info('XML Data: ' . print_r($xml, true));
  589. // Extract data from XML and create receipts
  590. foreach ($xml->receipt as $receiptData) {
  591. Log::info('Receipt Data: ' . print_r($receiptData, true));
  592. $member = Member::where('fiscal_code', (string)$receiptData->fiscal_code)->first();
  593. if ($member) {
  594. $receipt = new \App\Models\Receipt();
  595. $receipt->member_id = $member->id;
  596. $receipt->date = (string)$receiptData->date;
  597. $receipt->amount = (float)$receiptData->amount;
  598. $receipt->number = (string)$receiptData->number;
  599. $receipt->year = (int)$receiptData->year;
  600. $receipt->save();
  601. }
  602. }
  603. session()->flash('message', 'Ricevute importate con successo.');
  604. } catch (\Exception $e) {
  605. session()->flash('error', 'Errore durante l\'importazione del file XML: ' . $e->getMessage());
  606. }
  607. $this->reset('receiptFile');
  608. $this->emit('load-data-table');
  609. }
  610. }