Record.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use DateInterval;
  5. use DatePeriod;
  6. use DateTime;
  7. class Record extends Component
  8. {
  9. public $records, $dataId;
  10. public $in;
  11. public $out;
  12. public array $recordDatas = [];
  13. public array $labels = [];
  14. public $selectedFilter = 0;
  15. public function mount()
  16. {
  17. }
  18. public function render()
  19. {
  20. $fromDate = date("Y-m-01");
  21. $toDate = date("Y-m-t 23:59:59");
  22. if ($this->selectedFilter == 1)
  23. {
  24. $fromDate = date("Y-01-01");
  25. $toDate = date("Y-12-31 23:59:59");
  26. }
  27. if ($this->selectedFilter == 2)
  28. {
  29. $fromDate = date("Y-01-01");
  30. $toDate = date("Y-12-31 23:59:59");
  31. }
  32. $this->records = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  33. $this->in = \App\Models\Record::where('type', 'IN')->whereBetween('date', [$fromDate, $toDate])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
  34. $this->out = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
  35. $this->labels = $this->getLabels($fromDate, $toDate);
  36. $this->recordDatas = [
  37. [
  38. 'label' => 'Entrate',
  39. 'backgroundColor' => 'green',
  40. 'borderColor' => 'green',
  41. // 'data' => $this->getRandomData(),
  42. 'data' => $this->getRecordData('IN', $fromDate, $toDate),
  43. ],
  44. [
  45. 'label' => 'Uscite',
  46. 'backgroundColor' => 'red',
  47. 'borderColor' => 'red',
  48. 'data' => $this->getRecordData('OUT', $fromDate, $toDate),
  49. ]
  50. ];
  51. return view('livewire.records');
  52. }
  53. private function getLabels($fromDate, $toDate)
  54. {
  55. $begin = new DateTime($fromDate);
  56. $end = new DateTime($toDate);
  57. $interval = DateInterval::createFromDateString('1 day');
  58. $date_range = new DatePeriod($begin, $interval, $end);
  59. foreach ($date_range as $date)
  60. {
  61. $labels[] = $date->format('d/M');
  62. }
  63. return $labels;
  64. }
  65. private function getRecordData($type, $fromDate, $toDate)
  66. {
  67. $data = [];
  68. $begin = new DateTime($fromDate);
  69. $end = new DateTime($toDate);
  70. $interval = DateInterval::createFromDateString('1 day');
  71. $date_range = new DatePeriod($begin, $interval, $end);
  72. foreach ($date_range as $date)
  73. {
  74. if ($type == 'IN')
  75. {
  76. $found = false;
  77. foreach($this->in as $in)
  78. {
  79. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  80. {
  81. $data[] = number_format($in->total, 0, "", "");
  82. $found = true;
  83. }
  84. }
  85. if (!$found)
  86. $data[] = 0;
  87. }
  88. if ($type == 'OUT')
  89. {
  90. $found = false;
  91. foreach($this->out as $out)
  92. {
  93. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  94. {
  95. $data[] = number_format($out->total, 0, "", "");
  96. $found = true;
  97. }
  98. }
  99. if (!$found)
  100. $data[] = 0;
  101. }
  102. }
  103. return $data;
  104. }
  105. }