Record.php 16 KB

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