Dashboard.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Carbon\Carbon;
  5. class Dashboard extends Component
  6. {
  7. public $totMembers = 0;
  8. public $totSuppliers = 0;
  9. public $totTodayIn = 0;
  10. public $totTodayOut = 0;
  11. public $dayName;
  12. public $in;
  13. public $out;
  14. public $members;
  15. public array $membersDatas = [];
  16. public array $recordDatas = [];
  17. public array $labels = [];
  18. public function render()
  19. {
  20. return view('livewire.dashboard');
  21. }
  22. public function mount()
  23. {
  24. $this->dayName = Carbon::now()->locale('it_IT')->dayName;
  25. $this->totMembers = \App\Models\Member::count();
  26. $this->totSuppliers = \App\Models\Supplier::count();
  27. $this->totTodayIn = 0;
  28. $tmp = \App\Models\Record::where('type', 'IN')->where('date', date("Y-m-d"))->get();
  29. foreach($tmp as $t)
  30. {
  31. foreach($t->rows as $r)
  32. {
  33. $this->totTodayIn += $r->amount;
  34. }
  35. }
  36. //$this->totTodayIn = \App\Models\Record::where('type', 'IN')->where('date', date("Y-m-d"))->rows->sum('amount');
  37. $tmp = \App\Models\Record::where('type', 'OUT')->where('date', date("Y-m-d"))->get();
  38. foreach($tmp as $t)
  39. {
  40. foreach($t->rows as $r)
  41. {
  42. $this->totTodayOut += $r->amount;
  43. }
  44. }
  45. //$this->totTodayOut = \App\Models\Record::where('type', 'OUT')->where('date', date("Y-m-d"))->rows->sum('amount');
  46. $this->members = \App\Models\Member::whereBetween('created_at', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("COUNT(*) as total, created_at"))->groupBy('created_at')->get();
  47. $this->in = \App\Models\Record::where('type', 'IN')->whereBetween('date', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
  48. $this->out = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [date("y-m-d", strtotime('-7 days')), date("Y-m-d 23:59:59")])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
  49. $this->labels = $this->getLabels();
  50. $this->memberDatas = [
  51. [
  52. 'label' => 'Utenti',
  53. 'backgroundColor' => 'blue',
  54. 'borderColor' => 'blue',
  55. // 'data' => $this->getRandomData(),
  56. 'data' => $this->getMemberData(),
  57. ]
  58. ];
  59. $this->recordDatas = [
  60. [
  61. 'label' => 'Entrate',
  62. 'backgroundColor' => 'green',
  63. 'borderColor' => 'green',
  64. // 'data' => $this->getRandomData(),
  65. 'data' => $this->getRecordData('IN'),
  66. ],
  67. [
  68. 'label' => 'Uscite',
  69. 'backgroundColor' => 'red',
  70. 'borderColor' => 'red',
  71. 'data' => $this->getRecordData('OUT'),
  72. ]
  73. ];
  74. }
  75. private function getLabels()
  76. {
  77. $labels = array();
  78. for($i=0; $i<=7; $i++)
  79. {
  80. $labels[] = date("d/M", strtotime('-' . $i . ' days'));
  81. }
  82. return array_reverse($labels);
  83. }
  84. private function getRecordData($type)
  85. {
  86. $data = [];
  87. for($i=0; $i<=7; $i++)
  88. {
  89. if ($type == 'IN')
  90. {
  91. $found = false;
  92. foreach($this->in as $in)
  93. {
  94. if (date("Y-m-d", strtotime($in->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  95. {
  96. $data[] = $in->total;
  97. $found = true;
  98. }
  99. }
  100. if (!$found)
  101. $data[] = 0;
  102. }
  103. if ($type == 'OUT')
  104. {
  105. $found = false;
  106. foreach($this->out as $out)
  107. {
  108. if (date("Y-m-d", strtotime($out->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  109. {
  110. $data[] = $out->total;
  111. $found = true;
  112. }
  113. }
  114. if (!$found)
  115. $data[] = 0;
  116. }
  117. }
  118. return array_reverse($data);
  119. }
  120. private function getMemberData()
  121. {
  122. $data = [];
  123. for($i=0; $i<=7; $i++)
  124. {
  125. $found = false;
  126. foreach($this->members as $member)
  127. {
  128. if (date("Y-m-d", strtotime($member->created_at)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  129. {
  130. $data[] = $member->total;
  131. $found = true;
  132. }
  133. }
  134. if (!$found)
  135. $data[] = 0;
  136. }
  137. return array_reverse($data);
  138. }
  139. public function addMember()
  140. {
  141. return redirect()->to('/members?new=1');
  142. }
  143. public function addSupplier()
  144. {
  145. return redirect()->to('/suppliers?new=1');
  146. }
  147. public function addIn()
  148. {
  149. return redirect()->to('/in?new=1');
  150. }
  151. public function addOut()
  152. {
  153. return redirect()->to('/out?new=1');
  154. }
  155. }