Record.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use DateInterval;
  5. use DatePeriod;
  6. use DateTime;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. class Record extends Component
  10. {
  11. public $records, $dataId, $totals;
  12. public $in;
  13. public $out;
  14. public $payments = [];
  15. public $fromDate;
  16. public $toDate;
  17. public $filterCausals = null;
  18. public $filterMember = null;
  19. public array $recordDatas = [];
  20. public array $labels = [];
  21. public array $causals = [];
  22. public $members = array();
  23. public function hydrate()
  24. {
  25. $this->emit('load-select');
  26. }
  27. public function mount()
  28. {
  29. $this->fromDate = date("Y-m-d");
  30. $this->toDate = date("Y-m-d");
  31. $this->getCausals(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->get(), 0);
  32. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  33. $this->payments = \App\Models\PaymentMethod::select('id', 'name','type')->where('enabled', true)->where('money', false)->get();
  34. }
  35. public function getCausals($records, $indentation)
  36. {
  37. foreach($records as $record)
  38. {
  39. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  40. if(count($record->childs))
  41. $this->getCausals($record->childs, $indentation + 1);
  42. }
  43. }
  44. public function getMonth($m)
  45. {
  46. $ret = '';
  47. switch ($m) {
  48. case 1:
  49. $ret = 'Gennaio';
  50. break;
  51. case 2:
  52. $ret = 'Febbraio';
  53. break;
  54. case 3:
  55. $ret = 'Marzo';
  56. break;
  57. case 4:
  58. $ret = 'Aprile';
  59. break;
  60. case 5:
  61. $ret = 'Maggio';
  62. break;
  63. case 6:
  64. $ret = 'Giugno';
  65. break;
  66. case 7:
  67. $ret = 'Luglio';
  68. break;
  69. case 8:
  70. $ret = 'Agosto';
  71. break;
  72. case 9:
  73. $ret = 'Settembre';
  74. break;
  75. case 10:
  76. $ret = 'Ottobre';
  77. break;
  78. case 11:
  79. $ret = 'Novembre';
  80. break;
  81. case 12:
  82. $ret = 'Dicembre';
  83. break;
  84. default:
  85. $ret = '';
  86. break;
  87. }
  88. return $ret;
  89. }
  90. public function render()
  91. {
  92. $month = 0;
  93. $year = 0;
  94. $this->records = array();
  95. $this->totals = array();
  96. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  97. /*
  98. $fromDate = '';
  99. $toDate = '';
  100. if ($this->selectedFilterFromDay != '' && $this->selectedFilterFromMonth != '' && $this->selectedFilterFromYear != '')
  101. {
  102. $fromDate = date($this->selectedFilterFromYear . "-" . $this->selectedFilterFromMonth . "-" . $this->selectedFilterFromDay);
  103. }
  104. if ($this->selectedFilterToDay != '' && $this->selectedFilterToMonth != '' && $this->selectedFilterToYear != '')
  105. {
  106. $toDate = date($this->selectedFilterToYear . "-" . $this->selectedFilterToMonth . "-" . $this->selectedFilterToDay . " 23:59:59");
  107. }
  108. */
  109. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  110. ->select('records.*', 'records_rows.*') // Ensure all columns are selected
  111. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  112. ->whereBetween('date', [$this->fromDate, $this->toDate])
  113. ->where(function ($query) {
  114. $query->where('type', 'OUT')
  115. ->orWhere(function ($query) {
  116. $query->where('records.corrispettivo_fiscale', true)
  117. ->orWhere('records.commercial', false);
  118. });
  119. })
  120. /*->where(function ($query) {
  121. $query->where('deleted', false)->orWhere('deleted', null);
  122. })*/
  123. ->where(function ($query) use ($exclude_from_records) {
  124. $query->where('type', 'OUT')
  125. ->orWhere(function ($subquery) use ($exclude_from_records) {
  126. $subquery->whereNotIn('member_id', $exclude_from_records);
  127. });
  128. });
  129. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0)
  130. {
  131. $causals = array();
  132. foreach($this->filterCausals as $z)
  133. {
  134. $causals[] = $z;
  135. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  136. foreach($childs as $c)
  137. {
  138. $causals[] = $c->id;
  139. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  140. foreach($childsX as $cX)
  141. {
  142. $causals[] = $cX->id;
  143. }
  144. }
  145. }
  146. $datas->whereIn('causal_id', $causals);
  147. }
  148. if ($this->filterMember != null && $this->filterMember > 0)
  149. {
  150. $datas->where('member_id', $this->filterMember);
  151. }
  152. $datas = $datas->orderBy('date', 'ASC')
  153. ->orderBy('records.created_at', 'ASC')
  154. ->orderBy('records_rows.id', 'ASC') // Important to maintain row order
  155. ->get();
  156. foreach($datas as $idx => $data)
  157. {
  158. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  159. $paymentCheck = $data->payment_method->money;
  160. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first))
  161. {
  162. if (!$data->deleted)
  163. {
  164. $amount = $data->amount;
  165. $amount += getVatValue($amount, $data->vat_id);
  166. }
  167. else
  168. {
  169. $amount = $data->amount;
  170. }
  171. /*$when = sizeof(json_decode($data->when));
  172. if ($when > 1)
  173. {
  174. $amount = $amount / $when;
  175. }*/
  176. $prefix = '';
  177. if (!$data->commercial)
  178. $prefix = $idx . "$";
  179. // aggiungere il nome * * *
  180. //$causal = $prefix . $data->date . "§" . $causalCheck->getTree();
  181. $causal = $prefix . $data->date . "§" . $causalCheck->getTree() . "§" .
  182. ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") :
  183. $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '') .
  184. "§" . $data->numero_linea;
  185. if (!isset($this->records[$causal][$data->payment_method->name][$data->type])) {
  186. $this->records[$causal][$data->payment_method->name][$data->type] = 0;
  187. }
  188. // Add to the records array
  189. $this->records[$causal][$data->payment_method->name][$data->type] += $amount;
  190. // Initialize totals if needed
  191. if (!isset($this->totals[$data->payment_method->name])) {
  192. $this->totals[$data->payment_method->name]["IN"] = 0;
  193. $this->totals[$data->payment_method->name]["OUT"] = 0;
  194. }
  195. // Update totals if not deleted
  196. if (!$data->deleted)
  197. $this->totals[$data->payment_method->name][$data->type] += $amount;// $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  198. }
  199. }
  200. return view('livewire.records');
  201. }
  202. private function getLabels($fromDate, $toDate)
  203. {
  204. $begin = new DateTime($fromDate);
  205. $end = new DateTime($toDate);
  206. $interval = DateInterval::createFromDateString('1 day');
  207. $date_range = new DatePeriod($begin, $interval, $end);
  208. foreach ($date_range as $date)
  209. {
  210. $labels[] = $date->format('d/M');
  211. }
  212. return $labels;
  213. }
  214. private function getRecordData($type, $fromDate, $toDate)
  215. {
  216. $data = [];
  217. $begin = new DateTime($fromDate);
  218. $end = new DateTime($toDate);
  219. $interval = DateInterval::createFromDateString('1 day');
  220. $date_range = new DatePeriod($begin, $interval, $end);
  221. foreach ($date_range as $date)
  222. {
  223. if ($type == 'IN')
  224. {
  225. $found = false;
  226. foreach($this->in as $in)
  227. {
  228. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  229. {
  230. $data[] = number_format($in->total, 0, "", "");
  231. $found = true;
  232. }
  233. }
  234. if (!$found)
  235. $data[] = 0;
  236. }
  237. if ($type == 'OUT')
  238. {
  239. $found = false;
  240. foreach($this->out as $out)
  241. {
  242. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  243. {
  244. $data[] = number_format($out->total, 0, "", "");
  245. $found = true;
  246. }
  247. }
  248. if (!$found)
  249. $data[] = 0;
  250. }
  251. }
  252. return $data;
  253. }
  254. public function export()
  255. {
  256. $letters = array('E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  257. $spreadsheet = new Spreadsheet();
  258. $activeWorksheet = $spreadsheet->getActiveSheet();
  259. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  260. $activeWorksheet->setCellValue('A1', "Data");
  261. $activeWorksheet->setCellValue('B1', "Causale");
  262. $activeWorksheet->setCellValue('C1', "Dettaglio");
  263. $activeWorksheet->setCellValue('D1', "Nominativo");
  264. $idx = 0;
  265. foreach($this->payments as $p)
  266. {
  267. if ($idx >= count($letters)) {
  268. break;
  269. }
  270. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  271. $idx++;
  272. if ($idx >= count($letters)) {
  273. break;
  274. }
  275. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  276. $idx++;
  277. }
  278. $idx = 0;
  279. $activeWorksheet->setCellValue('A2', "");
  280. $activeWorksheet->setCellValue('B2', "");
  281. $activeWorksheet->setCellValue('C2', "");
  282. $activeWorksheet->setCellValue('D2', "");
  283. foreach($this->payments as $p)
  284. {
  285. if($p->type == 'ALL'){
  286. if ($idx >= count($letters)) {
  287. break;
  288. }
  289. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  290. $idx++;
  291. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  292. $idx++;
  293. }
  294. elseif($p->type == 'IN'){
  295. if ($idx >= count($letters)) {
  296. break;
  297. }
  298. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  299. $idx++;
  300. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  301. $idx++;
  302. }
  303. elseif($p->type == 'OUT'){
  304. if ($idx >= count($letters)) {
  305. break;
  306. }
  307. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  308. $idx++;
  309. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  310. $idx++;
  311. }
  312. }
  313. $activeWorksheet->getStyle('A1:P1')->getFont()->setBold(true);
  314. $activeWorksheet->getStyle('A2:P2')->getFont()->setBold(true);
  315. // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  316. $count = 3;
  317. $totals = [];
  318. foreach($this->records as $causal => $record)
  319. {
  320. $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
  321. list($d, $c, $j, $k) = explode("§", $check);
  322. $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
  323. $activeWorksheet->setCellValue('B' . $count, $c);
  324. $activeWorksheet->setCellValue('C' . $count, $k);
  325. $activeWorksheet->setCellValue('D' . $count, $j);
  326. $idx = 0;
  327. foreach($this->payments as $p)
  328. {
  329. if(isset($record[$p->name]))
  330. {
  331. if(isset($record[$p->name]["IN"]))
  332. {
  333. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
  334. }
  335. else
  336. {
  337. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  338. }
  339. $idx++;
  340. if(isset($record[$p->name]["OUT"]))
  341. {
  342. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
  343. }
  344. else
  345. {
  346. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  347. }
  348. $idx++;
  349. }
  350. else
  351. {
  352. if ($idx >= count($letters)) {
  353. break;
  354. }
  355. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  356. $idx++;
  357. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  358. $idx++;
  359. }
  360. }
  361. $count += 1;
  362. }
  363. $count += 1;
  364. $idx = 0;
  365. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  366. $activeWorksheet->setCellValue('B' . $count, '');
  367. $activeWorksheet->setCellValue('C' . $count, '');
  368. $activeWorksheet->setCellValue('D' . $count, '');
  369. foreach($this->payments as $p)
  370. {
  371. if(isset($this->totals[$p->name]))
  372. {
  373. if($p->type == 'ALL'){
  374. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  375. $idx++;
  376. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  377. $idx++;
  378. }
  379. elseif($p->type == 'IN'){
  380. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  381. $idx++;
  382. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  383. $idx++;
  384. }
  385. elseif($p->type == 'OUT'){
  386. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  387. $idx++;
  388. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  389. $idx++;
  390. }
  391. }
  392. else
  393. {
  394. if($p->type == 'ALL'){
  395. if ($idx >= count($letters)) {
  396. break;
  397. }
  398. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  399. $idx++;
  400. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  401. $idx++;
  402. }
  403. elseif($p->type == 'IN'){
  404. if ($idx >= count($letters)) {
  405. break;
  406. }
  407. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  408. $idx++;
  409. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  410. $idx++;
  411. }
  412. elseif($p->type == 'OUT'){
  413. if ($idx >= count($letters)) {
  414. break;
  415. }
  416. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  417. $idx++;
  418. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  419. $idx++;
  420. }
  421. }
  422. }
  423. $activeWorksheet->getStyle('A' . $count . ':P' . $count)->getFont()->setBold(true);
  424. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  425. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  426. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  427. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  428. foreach($letters as $l)
  429. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  430. $writer = new Xlsx($spreadsheet);
  431. $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
  432. return response()->download($path)->deleteFileAfterSend();
  433. }
  434. }