Record.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. {
  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. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  98. ->select('records.*', 'records_rows.*') // Ensure all columns are selected
  99. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  100. ->whereBetween('date', [$this->fromDate, $this->toDate])
  101. ->where(function ($query) {
  102. $query->where('type', 'OUT')
  103. ->orWhere(function ($query) {
  104. $query->where('records.corrispettivo_fiscale', true)
  105. ->orWhere('records.commercial', false);
  106. });
  107. })
  108. ->where(function ($query) use ($exclude_from_records) {
  109. $query->where('type', 'OUT')
  110. ->orWhere(function ($subquery) use ($exclude_from_records) {
  111. $subquery->whereNotIn('member_id', $exclude_from_records);
  112. });
  113. });
  114. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0)
  115. {
  116. $causals = array();
  117. foreach($this->filterCausals as $z)
  118. {
  119. $causals[] = $z;
  120. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  121. foreach($childs as $c)
  122. {
  123. $causals[] = $c->id;
  124. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  125. foreach($childsX as $cX)
  126. {
  127. $causals[] = $cX->id;
  128. }
  129. }
  130. }
  131. $datas->whereIn('causal_id', $causals);
  132. }
  133. if ($this->filterMember != null && $this->filterMember > 0)
  134. {
  135. $datas->where('member_id', $this->filterMember);
  136. }
  137. $datas = $datas->orderBy('date', 'ASC')
  138. ->orderBy('records.created_at', 'ASC')
  139. ->orderBy('records_rows.id', 'ASC') // Important to maintain row order
  140. ->get();
  141. foreach($datas as $idx => $data)
  142. {
  143. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  144. $paymentCheck = $data->payment_method->money;
  145. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first))
  146. {
  147. if (!$data->deleted)
  148. {
  149. $amount = $data->amount;
  150. $amount += getVatValue($amount, $data->vat_id);
  151. }
  152. else
  153. {
  154. $amount = $data->amount;
  155. }
  156. $prefix = '';
  157. if (!$data->commercial)
  158. $prefix = $idx . "$";
  159. // aggiungere il nome * * *
  160. $causal = $prefix . $data->date . "§" . $causalCheck->getTree() . "§" .
  161. ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") :
  162. $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '') .
  163. "§" . $data->numero_linea;
  164. if (!isset($this->records[$causal][$data->payment_method->name][$data->type])) {
  165. $this->records[$causal][$data->payment_method->name][$data->type] = 0;
  166. }
  167. // Add to the records array
  168. $this->records[$causal][$data->payment_method->name][$data->type] += $amount;
  169. // Initialize totals if needed
  170. if (!isset($this->totals[$data->payment_method->name])) {
  171. $this->totals[$data->payment_method->name]["IN"] = 0;
  172. $this->totals[$data->payment_method->name]["OUT"] = 0;
  173. }
  174. // Update totals if not deleted
  175. if (!$data->deleted)
  176. $this->totals[$data->payment_method->name][$data->type] += $amount;// $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  177. }
  178. }
  179. return view('livewire.records');
  180. }
  181. private function getLabels($fromDate, $toDate)
  182. {
  183. $begin = new DateTime($fromDate);
  184. $end = new DateTime($toDate);
  185. $interval = DateInterval::createFromDateString('1 day');
  186. $date_range = new DatePeriod($begin, $interval, $end);
  187. foreach ($date_range as $date)
  188. {
  189. $labels[] = $date->format('d/M');
  190. }
  191. return $labels;
  192. }
  193. private function getRecordData($type, $fromDate, $toDate)
  194. {
  195. $data = [];
  196. $begin = new DateTime($fromDate);
  197. $end = new DateTime($toDate);
  198. $interval = DateInterval::createFromDateString('1 day');
  199. $date_range = new DatePeriod($begin, $interval, $end);
  200. foreach ($date_range as $date)
  201. {
  202. if ($type == 'IN')
  203. {
  204. $found = false;
  205. foreach($this->in as $in)
  206. {
  207. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  208. {
  209. $data[] = number_format($in->total, 0, "", "");
  210. $found = true;
  211. }
  212. }
  213. if (!$found)
  214. $data[] = 0;
  215. }
  216. if ($type == 'OUT')
  217. {
  218. $found = false;
  219. foreach($this->out as $out)
  220. {
  221. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  222. {
  223. $data[] = number_format($out->total, 0, "", "");
  224. $found = true;
  225. }
  226. }
  227. if (!$found)
  228. $data[] = 0;
  229. }
  230. }
  231. return $data;
  232. }
  233. public function export()
  234. {
  235. $letters = array('E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  236. $spreadsheet = new Spreadsheet();
  237. $activeWorksheet = $spreadsheet->getActiveSheet();
  238. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  239. $activeWorksheet->setCellValue('A1', "Data");
  240. $activeWorksheet->setCellValue('B1', "Causale");
  241. $activeWorksheet->setCellValue('C1', "Dettaglio");
  242. $activeWorksheet->setCellValue('D1', "Nominativo");
  243. $idx = 0;
  244. foreach($this->payments as $p)
  245. {
  246. if ($idx >= count($letters)) {
  247. break;
  248. }
  249. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  250. $idx++;
  251. if ($idx >= count($letters)) {
  252. break;
  253. }
  254. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  255. $idx++;
  256. }
  257. $idx = 0;
  258. $activeWorksheet->setCellValue('A2', "");
  259. $activeWorksheet->setCellValue('B2', "");
  260. $activeWorksheet->setCellValue('C2', "");
  261. $activeWorksheet->setCellValue('D2', "");
  262. foreach($this->payments as $p)
  263. {
  264. if($p->type == 'ALL'){
  265. if ($idx >= count($letters)) {
  266. break;
  267. }
  268. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  269. $idx++;
  270. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  271. $idx++;
  272. }
  273. elseif($p->type == 'IN'){
  274. if ($idx >= count($letters)) {
  275. break;
  276. }
  277. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  278. $idx++;
  279. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  280. $idx++;
  281. }
  282. elseif($p->type == 'OUT'){
  283. if ($idx >= count($letters)) {
  284. break;
  285. }
  286. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  287. $idx++;
  288. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  289. $idx++;
  290. }
  291. }
  292. $activeWorksheet->getStyle('A1:P1')->getFont()->setBold(true);
  293. $activeWorksheet->getStyle('A2:P2')->getFont()->setBold(true);
  294. // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  295. $count = 3;
  296. $totals = [];
  297. foreach($this->records as $causal => $record)
  298. {
  299. $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
  300. list($d, $c, $j, $k) = explode("§", $check);
  301. $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
  302. $activeWorksheet->setCellValue('B' . $count, $c);
  303. $activeWorksheet->setCellValue('C' . $count, $k);
  304. $activeWorksheet->setCellValue('D' . $count, $j);
  305. $idx = 0;
  306. foreach($this->payments as $p)
  307. {
  308. if(isset($record[$p->name]))
  309. {
  310. if(isset($record[$p->name]["IN"]))
  311. {
  312. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
  313. }
  314. else
  315. {
  316. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  317. }
  318. $idx++;
  319. if(isset($record[$p->name]["OUT"]))
  320. {
  321. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
  322. }
  323. else
  324. {
  325. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  326. }
  327. $idx++;
  328. }
  329. else
  330. {
  331. if ($idx >= count($letters)) {
  332. break;
  333. }
  334. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  335. $idx++;
  336. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  337. $idx++;
  338. }
  339. }
  340. $count += 1;
  341. }
  342. $count += 1;
  343. $idx = 0;
  344. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  345. $activeWorksheet->setCellValue('B' . $count, '');
  346. $activeWorksheet->setCellValue('C' . $count, '');
  347. $activeWorksheet->setCellValue('D' . $count, '');
  348. foreach($this->payments as $p)
  349. {
  350. if(isset($this->totals[$p->name]))
  351. {
  352. if($p->type == 'ALL'){
  353. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  354. $idx++;
  355. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  356. $idx++;
  357. }
  358. elseif($p->type == 'IN'){
  359. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  360. $idx++;
  361. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  362. $idx++;
  363. }
  364. elseif($p->type == 'OUT'){
  365. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  366. $idx++;
  367. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  368. $idx++;
  369. }
  370. }
  371. else
  372. {
  373. if($p->type == 'ALL'){
  374. if ($idx >= count($letters)) {
  375. break;
  376. }
  377. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  378. $idx++;
  379. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  380. $idx++;
  381. }
  382. elseif($p->type == 'IN'){
  383. if ($idx >= count($letters)) {
  384. break;
  385. }
  386. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  387. $idx++;
  388. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  389. $idx++;
  390. }
  391. elseif($p->type == 'OUT'){
  392. if ($idx >= count($letters)) {
  393. break;
  394. }
  395. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  396. $idx++;
  397. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  398. $idx++;
  399. }
  400. }
  401. }
  402. $activeWorksheet->getStyle('A' . $count . ':P' . $count)->getFont()->setBold(true);
  403. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  404. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  405. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  406. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  407. foreach($letters as $l)
  408. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  409. $writer = new Xlsx($spreadsheet);
  410. $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
  411. return response()->download($path)->deleteFileAfterSend();
  412. }
  413. }