Dashboard.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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")])
  47. ->where(function($query) {
  48. $query->where('is_archived', false)
  49. ->orWhereNull('is_archived');
  50. })
  51. ->where(function($query) {
  52. $query->where('is_deleted', false)
  53. ->orWhereNull('is_deleted');
  54. })->select(\DB::raw("COUNT(*) as total, created_at"))->groupBy('created_at')->get();
  55. $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();
  56. $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();
  57. $this->labels = $this->getLabels();
  58. $this->memberDatas = [
  59. [
  60. 'label' => 'Utenti',
  61. 'backgroundColor' => 'blue',
  62. 'borderColor' => 'blue',
  63. // 'data' => $this->getRandomData(),
  64. 'data' => $this->getMemberData(),
  65. ]
  66. ];
  67. $this->recordDatas = [
  68. [
  69. 'label' => 'Entrate',
  70. 'backgroundColor' => 'green',
  71. 'borderColor' => 'green',
  72. // 'data' => $this->getRandomData(),
  73. 'data' => $this->getRecordData('IN'),
  74. ],
  75. [
  76. 'label' => 'Uscite',
  77. 'backgroundColor' => 'red',
  78. 'borderColor' => 'red',
  79. 'data' => $this->getRecordData('OUT'),
  80. ]
  81. ];
  82. }
  83. private function getLabels()
  84. {
  85. $labels = array();
  86. for($i=0; $i<=7; $i++)
  87. {
  88. $labels[] = date("d/M", strtotime('-' . $i . ' days'));
  89. }
  90. return array_reverse($labels);
  91. }
  92. private function getRecordData($type)
  93. {
  94. $data = [];
  95. for($i=0; $i<=7; $i++)
  96. {
  97. if ($type == 'IN')
  98. {
  99. $found = false;
  100. foreach($this->in as $in)
  101. {
  102. if (date("Y-m-d", strtotime($in->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  103. {
  104. $data[] = $in->total;
  105. $found = true;
  106. }
  107. }
  108. if (!$found)
  109. $data[] = 0;
  110. }
  111. if ($type == 'OUT')
  112. {
  113. $found = false;
  114. foreach($this->out as $out)
  115. {
  116. if (date("Y-m-d", strtotime($out->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  117. {
  118. $data[] = $out->total;
  119. $found = true;
  120. }
  121. }
  122. if (!$found)
  123. $data[] = 0;
  124. }
  125. }
  126. return array_reverse($data);
  127. }
  128. private function getMemberData()
  129. {
  130. $data = [];
  131. for($i=0; $i<=7; $i++)
  132. {
  133. $found = false;
  134. foreach($this->members as $member)
  135. {
  136. if (date("Y-m-d", strtotime($member->created_at)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  137. {
  138. $data[] = $member->total;
  139. $found = true;
  140. }
  141. }
  142. if (!$found)
  143. $data[] = 0;
  144. }
  145. return array_reverse($data);
  146. }
  147. public function addMember()
  148. {
  149. return redirect()->to('/members?new=1');
  150. }
  151. public function addSupplier()
  152. {
  153. return redirect()->to('/suppliers?new=1');
  154. }
  155. public function addIn()
  156. {
  157. return redirect()->to('/in?new=1');
  158. }
  159. public function addOut()
  160. {
  161. return redirect()->to('/out?new=1');
  162. }
  163. }