Record.php 18 KB

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