Record.php 18 KB

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