Record.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  111. //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
  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. ->whereNotIn('member_id', $exclude_from_records);
  124. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0)
  125. {
  126. $causals = array();
  127. foreach($this->filterCausals as $z)
  128. {
  129. $causals[] = $z;
  130. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  131. foreach($childs as $c)
  132. {
  133. $causals[] = $c->id;
  134. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  135. foreach($childsX as $cX)
  136. {
  137. $causals[] = $cX->id;
  138. }
  139. }
  140. }
  141. $datas->whereIn('causal_id', $causals);
  142. }
  143. if ($this->filterMember != null && $this->filterMember > 0)
  144. {
  145. $datas->where('member_id', $this->filterMember);
  146. }
  147. $datas = $datas->orderBy('date', 'ASC')->orderBy('records.created_at', 'ASC')
  148. ->get();
  149. foreach($datas as $idx => $data)
  150. {
  151. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  152. $paymentCheck = $data->payment_method->money;
  153. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first))
  154. {
  155. if (!$data->deleted)
  156. {
  157. $amount = $data->amount;
  158. $amount += getVatValue($amount, $data->vat_id);
  159. }
  160. else
  161. {
  162. $amount = $data->amount;
  163. }
  164. /*$when = sizeof(json_decode($data->when));
  165. if ($when > 1)
  166. {
  167. $amount = $amount / $when;
  168. }*/
  169. $prefix = '';
  170. if (!$data->commercial)
  171. $prefix = $idx . "$";
  172. // aggiungere il nome * * *
  173. //$causal = $prefix . $data->date . "§" . $causalCheck->getTree();
  174. $causal = $prefix . $data->date . "§" . $causalCheck->getTree() . "§" . ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") : $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '');
  175. if (isset($this->records[$causal]))
  176. {
  177. if (isset($this->records[$causal][$data->payment_method->name]))
  178. {
  179. if ($data->commercial)
  180. {
  181. if ($data->deleted && $this->records[$causal][$data->payment_method->name][$data->type])
  182. $amount += $this->records[$causal][$data->payment_method->name][$data->type];
  183. }
  184. }
  185. }
  186. if (!isset($this->totals[$data->payment_method->name]))
  187. {
  188. $this->totals[$data->payment_method->name]["IN"] = 0;
  189. $this->totals[$data->payment_method->name]["OUT"] = 0;
  190. }
  191. $this->records[$causal][$data->payment_method->name][$data->type] = $amount;
  192. if (!$data->deleted)
  193. $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  194. }
  195. }
  196. return view('livewire.records');
  197. }
  198. private function getLabels($fromDate, $toDate)
  199. {
  200. $begin = new DateTime($fromDate);
  201. $end = new DateTime($toDate);
  202. $interval = DateInterval::createFromDateString('1 day');
  203. $date_range = new DatePeriod($begin, $interval, $end);
  204. foreach ($date_range as $date)
  205. {
  206. $labels[] = $date->format('d/M');
  207. }
  208. return $labels;
  209. }
  210. private function getRecordData($type, $fromDate, $toDate)
  211. {
  212. $data = [];
  213. $begin = new DateTime($fromDate);
  214. $end = new DateTime($toDate);
  215. $interval = DateInterval::createFromDateString('1 day');
  216. $date_range = new DatePeriod($begin, $interval, $end);
  217. foreach ($date_range as $date)
  218. {
  219. if ($type == 'IN')
  220. {
  221. $found = false;
  222. foreach($this->in as $in)
  223. {
  224. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  225. {
  226. $data[] = number_format($in->total, 0, "", "");
  227. $found = true;
  228. }
  229. }
  230. if (!$found)
  231. $data[] = 0;
  232. }
  233. if ($type == 'OUT')
  234. {
  235. $found = false;
  236. foreach($this->out as $out)
  237. {
  238. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  239. {
  240. $data[] = number_format($out->total, 0, "", "");
  241. $found = true;
  242. }
  243. }
  244. if (!$found)
  245. $data[] = 0;
  246. }
  247. }
  248. return $data;
  249. }
  250. public function export()
  251. {
  252. $letters = array('E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  253. $spreadsheet = new Spreadsheet();
  254. $activeWorksheet = $spreadsheet->getActiveSheet();
  255. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  256. $activeWorksheet->setCellValue('A1', "Data");
  257. $activeWorksheet->setCellValue('B1', "Causale");
  258. $activeWorksheet->setCellValue('C1', "Dettaglio");
  259. $activeWorksheet->setCellValue('D1', "Nominativo");
  260. $idx = 0;
  261. foreach($this->payments as $p)
  262. {
  263. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  264. $idx++;
  265. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  266. $idx++;
  267. }
  268. $idx = 0;
  269. $activeWorksheet->setCellValue('A2', "");
  270. $activeWorksheet->setCellValue('B2', "");
  271. $activeWorksheet->setCellValue('C2', "");
  272. $activeWorksheet->setCellValue('D2', "");
  273. foreach($this->payments as $p)
  274. {
  275. if($p->type == 'ALL'){
  276. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  277. $idx++;
  278. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  279. $idx++;
  280. }
  281. elseif($p->type == 'IN'){
  282. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  283. $idx++;
  284. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  285. $idx++;
  286. }
  287. elseif($p->type == 'OUT'){
  288. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  289. $idx++;
  290. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  291. $idx++;
  292. }
  293. }
  294. $activeWorksheet->getStyle('A1:P1')->getFont()->setBold(true);
  295. $activeWorksheet->getStyle('A2:P2')->getFont()->setBold(true);
  296. // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  297. $count = 3;
  298. $totals = [];
  299. foreach($this->records as $causal => $record)
  300. {
  301. $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
  302. list($d, $c, $j, $k) = explode("§", $check);
  303. $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
  304. $activeWorksheet->setCellValue('B' . $count, $c);
  305. $activeWorksheet->setCellValue('C' . $count, $k);
  306. $activeWorksheet->setCellValue('D' . $count, $j);
  307. $idx = 0;
  308. foreach($this->payments as $p)
  309. {
  310. if(isset($record[$p->name]))
  311. {
  312. if(isset($record[$p->name]["IN"]))
  313. {
  314. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
  315. }
  316. else
  317. {
  318. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  319. }
  320. $idx++;
  321. if(isset($record[$p->name]["OUT"]))
  322. {
  323. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
  324. }
  325. else
  326. {
  327. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  328. }
  329. $idx++;
  330. }
  331. else
  332. {
  333. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  334. $idx++;
  335. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  336. $idx++;
  337. }
  338. }
  339. $count += 1;
  340. }
  341. $count += 1;
  342. $idx = 0;
  343. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  344. $activeWorksheet->setCellValue('B' . $count, '');
  345. $activeWorksheet->setCellValue('C' . $count, '');
  346. $activeWorksheet->setCellValue('D' . $count, '');
  347. foreach($this->payments as $p)
  348. {
  349. if(isset($this->totals[$p->name]))
  350. {
  351. if($p->type == 'ALL'){
  352. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  353. $idx++;
  354. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  355. $idx++;
  356. }
  357. elseif($p->type == 'IN'){
  358. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  359. $idx++;
  360. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  361. $idx++;
  362. }
  363. elseif($p->type == 'OUT'){
  364. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  365. $idx++;
  366. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  367. $idx++;
  368. }
  369. }
  370. else
  371. {
  372. if($p->type == 'ALL'){
  373. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  374. $idx++;
  375. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  376. $idx++;
  377. }
  378. elseif($p->type == 'IN'){
  379. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  380. $idx++;
  381. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  382. $idx++;
  383. }
  384. elseif($p->type == 'OUT'){
  385. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  386. $idx++;
  387. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  388. $idx++;
  389. }
  390. }
  391. }
  392. $activeWorksheet->getStyle('A' . $count . ':P' . $count)->getFont()->setBold(true);
  393. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  394. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  395. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  396. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  397. foreach($letters as $l)
  398. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  399. $writer = new Xlsx($spreadsheet);
  400. $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
  401. return response()->download($path)->deleteFileAfterSend();
  402. }
  403. }