Record.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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')->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 = 0;
  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. $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  193. }
  194. }
  195. return view('livewire.records');
  196. }
  197. private function getLabels($fromDate, $toDate)
  198. {
  199. $begin = new DateTime($fromDate);
  200. $end = new DateTime($toDate);
  201. $interval = DateInterval::createFromDateString('1 day');
  202. $date_range = new DatePeriod($begin, $interval, $end);
  203. foreach ($date_range as $date)
  204. {
  205. $labels[] = $date->format('d/M');
  206. }
  207. return $labels;
  208. }
  209. private function getRecordData($type, $fromDate, $toDate)
  210. {
  211. $data = [];
  212. $begin = new DateTime($fromDate);
  213. $end = new DateTime($toDate);
  214. $interval = DateInterval::createFromDateString('1 day');
  215. $date_range = new DatePeriod($begin, $interval, $end);
  216. foreach ($date_range as $date)
  217. {
  218. if ($type == 'IN')
  219. {
  220. $found = false;
  221. foreach($this->in as $in)
  222. {
  223. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  224. {
  225. $data[] = number_format($in->total, 0, "", "");
  226. $found = true;
  227. }
  228. }
  229. if (!$found)
  230. $data[] = 0;
  231. }
  232. if ($type == 'OUT')
  233. {
  234. $found = false;
  235. foreach($this->out as $out)
  236. {
  237. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  238. {
  239. $data[] = number_format($out->total, 0, "", "");
  240. $found = true;
  241. }
  242. }
  243. if (!$found)
  244. $data[] = 0;
  245. }
  246. }
  247. return $data;
  248. }
  249. public function export()
  250. {
  251. $letters = array('E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  252. $spreadsheet = new Spreadsheet();
  253. $activeWorksheet = $spreadsheet->getActiveSheet();
  254. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  255. $activeWorksheet->setCellValue('A1', "Data");
  256. $activeWorksheet->setCellValue('B1', "Causale");
  257. $activeWorksheet->setCellValue('C1', "Dettaglio");
  258. $activeWorksheet->setCellValue('D1', "Nominativo");
  259. $idx = 0;
  260. foreach($this->payments as $p)
  261. {
  262. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  263. $idx++;
  264. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  265. $idx++;
  266. }
  267. $idx = 0;
  268. $activeWorksheet->setCellValue('A2', "");
  269. $activeWorksheet->setCellValue('B2', "");
  270. $activeWorksheet->setCellValue('C2', "");
  271. $activeWorksheet->setCellValue('D2', "");
  272. foreach($this->payments as $p)
  273. {
  274. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  275. $idx++;
  276. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  277. $idx++;
  278. }
  279. $activeWorksheet->getStyle('A1:P1')->getFont()->setBold(true);
  280. $activeWorksheet->getStyle('A2:P2')->getFont()->setBold(true);
  281. // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  282. $count = 3;
  283. $totals = [];
  284. foreach($this->records as $causal => $record)
  285. {
  286. $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
  287. list($d, $c, $j, $k) = explode("§", $check);
  288. $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
  289. $activeWorksheet->setCellValue('B' . $count, $c);
  290. $activeWorksheet->setCellValue('C' . $count, $k);
  291. $activeWorksheet->setCellValue('D' . $count, $j);
  292. $idx = 0;
  293. foreach($this->payments as $p)
  294. {
  295. if(isset($record[$p->name]))
  296. {
  297. if(isset($record[$p->name]["IN"]))
  298. {
  299. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
  300. }
  301. else
  302. {
  303. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  304. }
  305. $idx++;
  306. if(isset($record[$p->name]["OUT"]))
  307. {
  308. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
  309. }
  310. else
  311. {
  312. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  313. }
  314. $idx++;
  315. }
  316. else
  317. {
  318. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  319. $idx++;
  320. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  321. $idx++;
  322. }
  323. }
  324. $count += 1;
  325. }
  326. $count += 1;
  327. $idx = 0;
  328. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  329. $activeWorksheet->setCellValue('B' . $count, '');
  330. $activeWorksheet->setCellValue('C' . $count, '');
  331. $activeWorksheet->setCellValue('D' . $count, '');
  332. foreach($this->payments as $p)
  333. {
  334. if(isset($this->totals[$p->name]))
  335. {
  336. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  337. $idx++;
  338. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  339. $idx++;
  340. }
  341. else
  342. {
  343. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  344. $idx++;
  345. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  346. $idx++;
  347. }
  348. }
  349. $activeWorksheet->getStyle('A' . $count . ':P' . $count)->getFont()->setBold(true);
  350. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  351. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  352. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  353. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  354. foreach($letters as $l)
  355. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  356. $writer = new Xlsx($spreadsheet);
  357. $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
  358. return response()->download($path)->deleteFileAfterSend();
  359. }
  360. }