Record.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
  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. $activeWorksheet->setCellValue('E1', "Stato");
  270. $idx = 0;
  271. foreach ($this->payments as $p) {
  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. $activeWorksheet->setCellValue('E2', "");
  289. foreach ($this->payments as $p) {
  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. } elseif ($p->type == 'IN') {
  299. if ($idx >= count($letters)) {
  300. break;
  301. }
  302. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  303. $idx++;
  304. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  305. $idx++;
  306. } elseif ($p->type == 'OUT') {
  307. if ($idx >= count($letters)) {
  308. break;
  309. }
  310. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  311. $idx++;
  312. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  313. $idx++;
  314. }
  315. }
  316. $activeWorksheet->getStyle('A1:Q1')->getFont()->setBold(true);
  317. $activeWorksheet->getStyle('A2:Q2')->getFont()->setBold(true);
  318. // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  319. $count = 3;
  320. $totals = [];
  321. foreach ($this->records as $causal => $record) {
  322. $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
  323. list($d, $c, $j, $k, $deleted, $numeroLinea) = explode("§", $check);
  324. $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
  325. $activeWorksheet->setCellValue('B' . $count, $c);
  326. $activeWorksheet->setCellValue('C' . $count, $k);
  327. $activeWorksheet->setCellValue('D' . $count, $j);
  328. $stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
  329. $activeWorksheet->setCellValue('E' . $count, $stato);
  330. if ($stato === 'ANNULLATA') {
  331. $activeWorksheet->getStyle('E' . $count)->getFont()->getColor()->setARGB('FFFF0000');
  332. }
  333. $idx = 0;
  334. foreach ($this->payments as $p) {
  335. if (isset($record[$p->name])) {
  336. if (isset($record[$p->name]["IN"])) {
  337. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
  338. } else {
  339. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  340. }
  341. $idx++;
  342. if (isset($record[$p->name]["OUT"])) {
  343. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
  344. } else {
  345. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  346. }
  347. $idx++;
  348. } else {
  349. if ($idx >= count($letters)) {
  350. break;
  351. }
  352. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  353. $idx++;
  354. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  355. $idx++;
  356. }
  357. }
  358. $count += 1;
  359. }
  360. $count += 1;
  361. $idx = 0;
  362. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  363. $activeWorksheet->setCellValue('B' . $count, '');
  364. $activeWorksheet->setCellValue('C' . $count, '');
  365. $activeWorksheet->setCellValue('D' . $count, '');
  366. $activeWorksheet->setCellValue('E' . $count, '');
  367. foreach ($this->payments as $p) {
  368. if (isset($this->totals[$p->name])) {
  369. if ($p->type == 'ALL') {
  370. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  371. $idx++;
  372. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  373. $idx++;
  374. } elseif ($p->type == 'IN') {
  375. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  376. $idx++;
  377. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  378. $idx++;
  379. } elseif ($p->type == 'OUT') {
  380. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  381. $idx++;
  382. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  383. $idx++;
  384. }
  385. } else {
  386. if ($p->type == 'ALL') {
  387. if ($idx >= count($letters)) {
  388. break;
  389. }
  390. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  391. $idx++;
  392. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  393. $idx++;
  394. } elseif ($p->type == 'IN') {
  395. if ($idx >= count($letters)) {
  396. break;
  397. }
  398. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  399. $idx++;
  400. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  401. $idx++;
  402. } elseif ($p->type == 'OUT') {
  403. if ($idx >= count($letters)) {
  404. break;
  405. }
  406. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  407. $idx++;
  408. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  409. $idx++;
  410. }
  411. }
  412. }
  413. $activeWorksheet->getStyle('A' . $count . ':Q' . $count)->getFont()->setBold(true);
  414. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  415. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  416. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  417. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  418. $activeWorksheet->getColumnDimension('E')->setWidth(20);
  419. foreach ($letters as $l)
  420. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  421. $writer = new Xlsx($spreadsheet);
  422. $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
  423. return response()->download($path)->deleteFileAfterSend();
  424. }
  425. }