RecordINOUT.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. use Illuminate\Support\Facades\Auth;
  7. class RecordINOUT extends Component
  8. {
  9. //public $records_in, $records_out;
  10. public $total_in = 0;
  11. public $total_out = 0;
  12. public $datas = [];
  13. public $month;
  14. public $year;
  15. public $months_1 = [];
  16. public $year_1;
  17. public $months_2 = [];
  18. public $year_2;
  19. public $show_block_2;
  20. //public $borsellino_id = 0;
  21. public $columns = array();
  22. public $rows_in = array();
  23. public $rows_out = array();
  24. public $records_in = array();
  25. public $records_out = array();
  26. public $showData = true;
  27. public $hasFilter = false;
  28. public $total = 0;
  29. public $selectedFilter = '';
  30. public $causalsIn = array();
  31. public $causalsOut = array();
  32. public $payments = array();
  33. public $members = array();
  34. public $filterCausalsIn = null;
  35. public $filterCausalsOut = null;
  36. public $excludeCausals = array();
  37. public function mount()
  38. {
  39. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  40. return redirect()->to('/dashboard');
  41. $borsellino = \App\Models\Causal::where('money', true)->first();
  42. if ($borsellino)
  43. $this->excludeCausals[] = $borsellino->id;
  44. //$this->borsellino_id = $borsellino->id;
  45. // Aggiungo
  46. $excludes = \App\Models\Causal::where('no_records', true)->get();
  47. foreach($excludes as $e)
  48. {
  49. $this->excludeCausals[] = $e->id;
  50. }
  51. $this->month = date("m");
  52. $this->year = date("Y");
  53. $this->year_1 = date("Y");
  54. $this->year_2 = date("Y");
  55. $this->show_block_2 = false;
  56. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get(), 'IN', 0);
  57. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get(), 'OUT', 0);
  58. $this->getCausalsIn(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  59. $this->getCausalsOut(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->get(), 0);
  60. //$this->causalsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->whereNotIn('id', $this->excludeCausals)->get();
  61. //$this->causalsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->whereNotIn('id', $this->excludeCausals)->get();
  62. }
  63. public function getCausalsIn($records, $indentation)
  64. {
  65. foreach($records as $record)
  66. {
  67. $this->causalsIn[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  68. if(count($record->childs))
  69. $this->getCausalsIn($record->childs, $indentation + 1);
  70. }
  71. }
  72. public function getCausalsOut($records, $indentation)
  73. {
  74. foreach($records as $record)
  75. {
  76. $this->causalsOut[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  77. if(count($record->childs))
  78. $this->getCausalsOut($record->childs, $indentation + 1);
  79. }
  80. }
  81. public function getCausale($records, $type, $indentation)
  82. {
  83. foreach($records as $record)
  84. {
  85. $first_parent_id = null;
  86. if ($record->parent_id != null)
  87. {
  88. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  89. }
  90. if ($type == 'IN')
  91. $this->rows_in[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id, 'parent_name' => $this->getCausalName($record->parent_id), 'first_parent_id' => $first_parent_id, 'first_parent_name' => $this->getCausalName($first_parent_id), 'all_childs' => $this->getAllChild($record->id));
  92. if ($type == 'OUT')
  93. $this->rows_out[] = array('id' => $record->id, 'name' => $record->name, 'level' => $indentation, 'parent_id' => $record->parent_id, 'parent_name' => $this->getCausalName($record->parent_id), 'first_parent_id' => $first_parent_id, 'first_parent_name' => $this->getCausalName($first_parent_id), 'all_childs' => $this->getAllChild($record->id));
  94. if(count($record->childs))
  95. $this->getCausale($record->childs, $type, $indentation + 1);
  96. }
  97. }
  98. public function getAllChild($id)
  99. {
  100. $aChilds = array();
  101. $aChilds[] = $id;
  102. $record = \App\Models\Causal::findOrFail($id);
  103. $aChilds[] = $record->parent_id;
  104. if ($record->parent_id != null)
  105. {
  106. $first_parent_id = \App\Models\Causal::where('id', $record->parent_id)->first()->parent_id;
  107. $aChilds[] = $first_parent_id;
  108. }
  109. $childs = \App\Models\Causal::where('parent_id', $id)->get();
  110. foreach($childs as $child)
  111. {
  112. $aChilds[] = $child->id;
  113. $childs2 = \App\Models\Causal::where('parent_id', $child->id)->get();
  114. foreach($childs2 as $child2)
  115. {
  116. $aChilds[] = $child2->id;
  117. $childs3 = \App\Models\Causal::where('parent_id', $child2->id)->get();
  118. foreach($childs3 as $child3)
  119. {
  120. $aChilds[] = $child3->id;
  121. }
  122. }
  123. }
  124. return $aChilds;
  125. }
  126. public function getCausalName($id)
  127. {
  128. if ($id > 0)
  129. return \App\Models\Causal::findOrFail($id)->name;
  130. else
  131. return "";
  132. }
  133. public function render()
  134. {
  135. return view('livewire.records_in_out');
  136. }
  137. public function hydrate()
  138. {
  139. $this->emit('load-select');
  140. }
  141. public function show($m, $y)
  142. //public function show($dt)
  143. {
  144. //list($m, $y) = explode("_", $dt);
  145. if ($m != "" && $y != "" && !in_array($m . "-" . $y, $this->datas))
  146. $this->datas[] = $m . "-" . $y;
  147. $this->columns = array();
  148. $this->records_in = [];
  149. $this->records_out = [];
  150. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  151. if (sizeof($this->datas) > 0)
  152. {
  153. foreach($this->datas as $filter)
  154. {
  155. // $filter = $m . "-" . $this->year;
  156. $this->columns[] = $filter;
  157. list($m, $y) = explode("-", $filter);
  158. // $f = $filter;
  159. // if ($m == 'x')
  160. // $f = str_replace("x-", "", $filter);
  161. //$dt = $y . "-0" . $m;
  162. // $records = \App\Models\Record::where('type', 'IN')
  163. // ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  164. // ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  165. // ->where(function ($query) {
  166. // $query->where('deleted', false)->orWhere('deleted', null);
  167. // })
  168. // ->where(function ($query) {
  169. // $query->where('financial_movement', false)->orWhere('financial_movement', null);
  170. // })
  171. // ->whereNotIn('member_id', $exclude_from_records)
  172. // /*->where(function ($query) use ($f, $dt) {
  173. // $query->where('records.date', 'like', '%' . $dt . '%')->orWhere('records_rows.when', 'like', '%' . $f . '%');
  174. // })*/
  175. // ->where('records_rows.when', 'like', '%"' . $f . '"%')
  176. // ->get();
  177. // //$records = $records->orderBy('date', 'DESC')->get();
  178. $recordsQuery = \App\Models\Record::where('records.type', 'IN')
  179. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  180. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  181. ->where(function ($query) {
  182. $query->where('deleted', false)->orWhere('deleted', null);
  183. })
  184. ->where(function ($query) {
  185. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  186. })
  187. ->whereNotIn('member_id', $exclude_from_records);
  188. if ($m === 'x') {
  189. // Anno fiscale da settembre $y a agosto $y+1
  190. $months = array_merge(range(9, 12), range(1, 8));
  191. $years = [
  192. 9 => $y, 10 => $y, 11 => $y, 12 => $y,
  193. 1 => $y+1, 2 => $y+1, 3 => $y+1, 4 => $y+1,
  194. 5 => $y+1, 6 => $y+1, 7 => $y+1, 8 => $y+1
  195. ];
  196. $recordsQuery->where(function ($q) use ($months, $years) {
  197. foreach ($months as $month) {
  198. $year = $years[$month];
  199. // variante entrambi numeri stringhe
  200. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$month}\",\"year\":\"{$year}\"}')");
  201. // variante month stringa e year int
  202. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$month}\",\"year\":{$year}}')");
  203. // variante month int e year stringa
  204. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$month},\"year\":\"{$year}\"}')");
  205. // variante entrambi numeri int
  206. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$month},\"year\":{$year}}')");
  207. }
  208. });
  209. } else {
  210. $recordsQuery->where(function ($q) use ($m, $y) {
  211. // variante entrambi numeri stringhe
  212. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$m}\",\"year\":\"{$y}\"}')");
  213. // variante month stringa e year int
  214. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$m}\",\"year\":{$y}}')");
  215. // variante month int e year stringa
  216. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$m},\"year\":\"{$y}\"}')");
  217. // variante entrambi numeri int
  218. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$m},\"year\":{$y}}')");
  219. });
  220. }
  221. $records = $recordsQuery->get();
  222. $ccc = 0;
  223. $ids = '';
  224. foreach($records as $record)
  225. {
  226. $amount = $record->amount;
  227. $amount += getVatValue($amount, $record->vat_id);
  228. $when = sizeof(json_decode($record->when));
  229. if ($when > 1)
  230. {
  231. $amount = $amount / $when;
  232. $record->amount = $amount;
  233. }
  234. // Aggiungo/aggiorno i dati
  235. if (isset($this->records_in[$filter][$record->causal_id]))
  236. $this->records_in[$filter][$record->causal_id] += $amount;
  237. else
  238. $this->records_in[$filter][$record->causal_id] = $amount;
  239. //if ($record->causal_id == 158)
  240. // print "ID = " . $record->id . "<br>";
  241. // Aggiorno i dati del padre
  242. $this->updateParent("IN", $record->causal_id, $amount, $filter);
  243. if ($record->causal_id == 159 && $filter == '11-2025')
  244. {
  245. $ccc += 1;
  246. $ids .= $record->id . ",";
  247. }
  248. }
  249. //die;
  250. // $records = \App\Models\Record::where('type', 'OUT')
  251. // ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  252. // ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  253. // ->where(function ($query) {
  254. // $query->where('deleted', false)->orWhere('deleted', null);
  255. // })
  256. // ->where(function ($query) {
  257. // $query->where('financial_movement', false)->orWhere('financial_movement', null);
  258. // })
  259. // ->whereNotIn('member_id', $exclude_from_records)
  260. // ->where('records_rows.when', 'like', '%"' . $f . '"%')->get();
  261. //$records = $records->orderBy('date', 'DESC')->get();
  262. $recordsQuery = \App\Models\Record::where('records.type', 'OUT')
  263. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  264. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  265. ->where(function ($query) {
  266. $query->where('deleted', false)->orWhere('deleted', null);
  267. })
  268. ->where(function ($query) {
  269. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  270. })
  271. ->whereNotIn('member_id', $exclude_from_records);
  272. if ($m === 'x') {
  273. // Anno fiscale da settembre $y a agosto $y+1
  274. $months = array_merge(range(9, 12), range(1, 8));
  275. $years = [
  276. 9 => $y, 10 => $y, 11 => $y, 12 => $y,
  277. 1 => $y+1, 2 => $y+1, 3 => $y+1, 4 => $y+1,
  278. 5 => $y+1, 6 => $y+1, 7 => $y+1, 8 => $y+1
  279. ];
  280. $recordsQuery->where(function ($q) use ($months, $years) {
  281. foreach ($months as $month) {
  282. $year = $years[$month];
  283. // variante entrambi numeri stringhe
  284. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$month}\",\"year\":\"{$year}\"}')");
  285. // variante month stringa e year int
  286. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$month}\",\"year\":{$year}}')");
  287. // variante month int e year stringa
  288. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$month},\"year\":\"{$year}\"}')");
  289. // variante entrambi numeri int
  290. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$month},\"year\":{$year}}')");
  291. }
  292. });
  293. } else {
  294. $recordsQuery->where(function ($q) use ($m, $y) {
  295. // variante entrambi numeri stringhe
  296. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$m}\",\"year\":\"{$y}\"}')");
  297. // variante month stringa e year int
  298. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":\"{$m}\",\"year\":{$y}}')");
  299. // variante month int e year stringa
  300. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$m},\"year\":\"{$y}\"}')");
  301. // variante entrambi numeri int
  302. $q->orWhereRaw("JSON_CONTAINS(`records_rows`.`when`, '{\"month\":{$m},\"year\":{$y}}')");
  303. });
  304. }
  305. $records = $recordsQuery->get();
  306. foreach($records as $record)
  307. {
  308. $amount = $record->amount;
  309. $amount += getVatValue($amount, $record->vat_id);
  310. $when = sizeof(json_decode($record->when));
  311. if ($when > 1)
  312. {
  313. $amount = $amount / $when;
  314. $record->amount = $amount;
  315. }
  316. // Aggiungo/aggiorno i dati
  317. if (isset($this->records_out[$filter][$record->causal_id]))
  318. $this->records_out[$filter][$record->causal_id] += $amount;
  319. else
  320. $this->records_out[$filter][$record->causal_id] = $amount;
  321. $this->updateParent("OUT", $record->causal_id, $amount, $filter);
  322. }
  323. }
  324. }
  325. //print $ccc."-";
  326. //print $ids;
  327. //$this->showData = true;
  328. $this->emit('load-table');
  329. }
  330. public function updateParent($type, $causal_id, $amount, $filter)
  331. {
  332. if ($type == "IN")
  333. {
  334. foreach($this->rows_in as $r)
  335. {
  336. if ($r["id"] == $causal_id)
  337. {
  338. if (isset($this->records_in[$filter][$r["parent_id"]]))
  339. $this->records_in[$filter][$r["parent_id"]] += $amount;
  340. else
  341. $this->records_in[$filter][$r["parent_id"]] = $amount;
  342. if ($r["parent_id"] > 0)
  343. $this->updateParent("IN", $r["parent_id"], $amount, $filter);
  344. }
  345. }
  346. }
  347. if ($type == "OUT")
  348. {
  349. foreach($this->rows_out as $r)
  350. {
  351. if ($r["id"] == $causal_id)
  352. {
  353. if (isset($this->records_out[$filter][$r["parent_id"]]))
  354. $this->records_out[$filter][$r["parent_id"]] += $amount;
  355. else
  356. $this->records_out[$filter][$r["parent_id"]] = $amount;
  357. if ($r["parent_id"] > 0)
  358. $this->updateParent("OUT", $r["parent_id"], $amount, $filter);
  359. }
  360. }
  361. }
  362. }
  363. public function updateParentYear($type, $causal_id, $amount, $filter, &$records_in, &$records_out)
  364. {
  365. if ($type == "IN")
  366. {
  367. foreach($this->rows_in as $r)
  368. {
  369. if ($r["id"] == $causal_id)
  370. {
  371. if (isset($records_in[$filter][$r["parent_id"]]))
  372. $records_in[$filter][$r["parent_id"]] += $amount;
  373. else
  374. $records_in[$filter][$r["parent_id"]] = $amount;
  375. if ($r["parent_id"] > 0)
  376. $this->updateParentYear("IN", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  377. }
  378. }
  379. }
  380. if ($type == "OUT")
  381. {
  382. foreach($this->rows_out as $r)
  383. {
  384. if ($r["id"] == $causal_id)
  385. {
  386. if (isset($records_out[$filter][$r["parent_id"]]))
  387. $records_out[$filter][$r["parent_id"]] += $amount;
  388. else
  389. $records_out[$filter][$r["parent_id"]] = $amount;
  390. if ($r["parent_id"] > 0)
  391. $this->updateParentYear("OUT", $r["parent_id"], $amount, $filter, $records_in, $records_out);
  392. }
  393. }
  394. }
  395. }
  396. public function getCausal($causal)
  397. {
  398. $ret = '';
  399. if ($causal > 0)
  400. {
  401. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  402. }
  403. return $ret;
  404. }
  405. public function getMonth($str)
  406. {
  407. $ret = '';
  408. list($m, $y) = explode("-", $str);
  409. switch ($m) {
  410. case 'x':
  411. $ret = '';
  412. break;
  413. case '01':
  414. $ret = 'Gennaio ';
  415. break;
  416. case '02':
  417. $ret = 'Febbraio ';
  418. break;
  419. case '03':
  420. $ret = 'Marzo ';
  421. break;
  422. case '04':
  423. $ret = 'Aprile ';
  424. break;
  425. case '05':
  426. $ret = 'Maggio ';
  427. break;
  428. case '06':
  429. $ret = 'Giugno ';
  430. break;
  431. case '07':
  432. $ret = 'Luglio ';
  433. break;
  434. case '08':
  435. $ret = 'Agosto ';
  436. break;
  437. case '09':
  438. $ret = 'Settembre ';
  439. break;
  440. case '10':
  441. $ret = 'Ottobre ';
  442. break;
  443. case '11':
  444. $ret = 'Novembre ';
  445. break;
  446. case '12':
  447. $ret = 'Dicembre ';
  448. break;
  449. default:
  450. $ret = '';
  451. break;
  452. }
  453. if ($m == 'x') {
  454. $nextY = $y+1;
  455. $ret = "{$y}/{$nextY}";
  456. } else {
  457. $ret .= $y;
  458. }
  459. return $ret;
  460. }
  461. public function clear()
  462. {
  463. $this->columns = [];
  464. $this->datas = [];
  465. $this->records_out = [];
  466. $this->records_in = [];
  467. $this->emit('load-select');
  468. $this->emit('reset-collapse');
  469. //$this->showData = false;
  470. }
  471. public function remove($idx)
  472. {
  473. if (sizeof($this->datas) > 1)
  474. array_splice($this->datas, $idx, 1);
  475. //unset($this->datas[$idx]);
  476. else
  477. $this->datas = array();
  478. $this->show('', '');
  479. }
  480. public function export()
  481. {
  482. $rows_in = array();
  483. $rows_out = array();
  484. if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0)
  485. {
  486. foreach($this->rows_in as $r)
  487. {
  488. if (in_array($r["id"], $this->filterCausalsIn) || in_array($r["parent_id"], $this->filterCausalsIn) || in_array($r["first_parent_id"], $this->filterCausalsIn))
  489. {
  490. $rows_in[] = $r;
  491. }
  492. }
  493. }
  494. else
  495. {
  496. $rows_in = $this->rows_in;
  497. }
  498. if ($this->filterCausalsOut != null && sizeof($this->filterCausalsOut) > 0)
  499. {
  500. foreach($this->rows_out as $r)
  501. {
  502. if (in_array($r["id"], $this->filterCausalsOut) || in_array($r["parent_id"], $this->filterCausalsOut) || in_array($r["first_parent_id"], $this->filterCausalsOut))
  503. {
  504. $rows_out[] = $r;
  505. }
  506. }
  507. }
  508. else
  509. {
  510. $rows_out = $this->rows_out;
  511. }
  512. $path = $this->generateExcel($this->columns, $rows_in, $this->records_in, $rows_out, $this->records_out,false);
  513. return response()->download($path)->deleteFileAfterSend();
  514. }
  515. public function exportYear($year)
  516. {
  517. $records_in = [];
  518. $records_out = [];
  519. $datas = [];
  520. if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1)
  521. {
  522. if (date("m") < env('FISCAL_YEAR_MONTH_FROM', 1))
  523. $year -= 1;
  524. for($m=env('FISCAL_YEAR_MONTH_FROM', 1);$m<=12;$m++)
  525. {
  526. $datas[] = $m . "-" . $year;
  527. }
  528. for($m=1;$m<=env('FISCAL_YEAR_MONTH_TO', 12);$m++)
  529. {
  530. $datas[] = $m . "-" . ($year + 1);
  531. }
  532. }
  533. else
  534. {
  535. for($m=1;$m<=12;$m++)
  536. {
  537. $datas[] = $m . "-" . $year;
  538. }
  539. }
  540. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  541. foreach($datas as $filter)
  542. {
  543. $columns[] = $filter;
  544. $records = \App\Models\Record::where('type', 'IN')
  545. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  546. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  547. ->where(function ($query) {
  548. $query->where('deleted', false)->orWhere('deleted', null);
  549. })
  550. ->where(function ($query) {
  551. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  552. })
  553. ->whereNotIn('member_id', $exclude_from_records)
  554. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  555. //$records = $records->orderBy('date', 'DESC')->get();
  556. foreach($records as $record)
  557. {
  558. $amount = $record->amount;
  559. $amount += getVatValue($amount, $record->vat_id);
  560. $when = sizeof(json_decode($record->when));
  561. if ($when > 1)
  562. {
  563. $amount = $amount / $when;
  564. //$record->amount = $amount;
  565. }
  566. // Aggiungo/aggiorno i dati
  567. if (isset($records_in[$filter][$record->causal_id]))
  568. $records_in[$filter][$record->causal_id] += $amount;
  569. else
  570. $records_in[$filter][$record->causal_id] = $amount;
  571. // Aggiorno i dati del padre
  572. $this->updateParentYear("IN", $record->causal_id, $amount, $filter, $records_in, $records_out);
  573. }
  574. $records = \App\Models\Record::where('type', 'OUT')
  575. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  576. ->whereNotIn('records_rows.causal_id', $this->excludeCausals)
  577. ->where(function ($query) {
  578. $query->where('deleted', false)->orWhere('deleted', null);
  579. })
  580. ->where(function ($query) {
  581. $query->where('financial_movement', false)->orWhere('financial_movement', null);
  582. })
  583. ->whereNotIn('member_id', $exclude_from_records)
  584. ->where('records_rows.when', 'like', '%"' . $filter . '"%')->get();
  585. //$records = $records->orderBy('date', 'DESC')->get();
  586. foreach($records as $record)
  587. {
  588. $amount = $record->amount;
  589. $when = sizeof(json_decode($record->when));
  590. if ($when > 1)
  591. {
  592. $amount = $amount / $when;
  593. $record->amount = $amount;
  594. }
  595. // Aggiungo/aggiorno i dati
  596. if (isset($records_out[$filter][$record->causal_id]))
  597. $records_out[$filter][$record->causal_id] += $amount;
  598. else
  599. $records_out[$filter][$record->causal_id] = $amount;
  600. $this->updateParentYear("OUT", $record->causal_id, $amount, $filter, $records_in, $records_out);
  601. }
  602. }
  603. $path = $this->generateExcel($columns, $this->rows_in, $records_in, $this->rows_out, $records_out, true);
  604. return response()->download($path)->deleteFileAfterSend();
  605. }
  606. public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out, $isYearExport)
  607. {
  608. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
  609. $spreadsheet = new Spreadsheet();
  610. $activeWorksheet = $spreadsheet->getActiveSheet();
  611. $activeWorksheet->setCellValue('A1', 'Entrate');
  612. foreach($columns as $idx => $column)
  613. {
  614. $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
  615. }
  616. $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
  617. $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  618. $count = 2;
  619. $totals = [];
  620. foreach($rows_in as $in)
  621. {
  622. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $in["level"]) . $in["name"]);
  623. foreach($columns as $idx => $column)
  624. {
  625. if(isset($records_in[$column][$in["id"]]))
  626. {
  627. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
  628. if ($in["level"] == 0)
  629. {
  630. if (isset($totals[$idx]))
  631. $totals[$idx] += $records_in[$column][$in["id"]];
  632. else
  633. $totals[$idx] = $records_in[$column][$in["id"]];
  634. }
  635. }
  636. }
  637. if ($in["level"] == 0)
  638. {
  639. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  640. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('b1ed5c');
  641. }
  642. $count += 1;
  643. }
  644. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  645. foreach($totals as $idx => $total)
  646. {
  647. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  648. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  649. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
  650. }
  651. $count += 2;
  652. $activeWorksheet->setCellValue('A' . $count, "Uscite");
  653. foreach($columns as $idx => $column)
  654. {
  655. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
  656. }
  657. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  658. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  659. $count += 1;
  660. $totals = [];
  661. foreach($rows_out as $out)
  662. {
  663. $activeWorksheet->setCellValue('A' . $count, str_repeat(" ", $out["level"]) . $out["name"]);
  664. foreach($columns as $idx => $column)
  665. {
  666. if(isset($records_out[$column][$out["id"]]))
  667. {
  668. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
  669. if ($out["level"] == 0)
  670. {
  671. if (isset($totals[$idx]))
  672. $totals[$idx] += $records_out[$column][$out["id"]];
  673. else
  674. $totals[$idx] = $records_out[$column][$out["id"]];
  675. }
  676. }
  677. }
  678. if ($out["level"] == 0)
  679. {
  680. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  681. //$activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('ed6d61');
  682. }
  683. $count += 1;
  684. }
  685. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  686. foreach($totals as $idx => $total)
  687. {
  688. $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
  689. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
  690. $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
  691. }
  692. $activeWorksheet->getColumnDimension('A')->setWidth(35);
  693. for($i = 1; $i < count($letters); $i++) {
  694. $activeWorksheet->getColumnDimension($letters[$i])->setWidth(20);
  695. }
  696. $fileSuffix = $isYearExport ? 'AnnoFiscale' : 'Selezione';
  697. $writer = new Xlsx($spreadsheet);
  698. $writer->save($path = storage_path(date("Ymd") .'_Gestionale_' . $fileSuffix . '.xlsx'));
  699. return $path;
  700. }
  701. }