Dashboard.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = \App\Models\Record::where('type', 'IN')->where('date', date("Y-m-d"))->sum('amount');
  28. $this->totTodayOut = \App\Models\Record::where('type', 'OUT')->where('date', date("Y-m-d"))->sum('amount');
  29. $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();
  30. $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();
  31. $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();
  32. $this->labels = $this->getLabels();
  33. $this->memberDatas = [
  34. [
  35. 'label' => 'Utenti',
  36. 'backgroundColor' => 'blue',
  37. 'borderColor' => 'blue',
  38. // 'data' => $this->getRandomData(),
  39. 'data' => $this->getMemberData(),
  40. ]
  41. ];
  42. $this->recordDatas = [
  43. [
  44. 'label' => 'Entrate',
  45. 'backgroundColor' => 'green',
  46. 'borderColor' => 'green',
  47. // 'data' => $this->getRandomData(),
  48. 'data' => $this->getRecordData('IN'),
  49. ],
  50. [
  51. 'label' => 'Uscite',
  52. 'backgroundColor' => 'red',
  53. 'borderColor' => 'red',
  54. 'data' => $this->getRecordData('OUT'),
  55. ]
  56. ];
  57. }
  58. private function getLabels()
  59. {
  60. $labels = array();
  61. for($i=0; $i<=7; $i++)
  62. {
  63. $labels[] = date("d/M", strtotime('-' . $i . ' days'));
  64. }
  65. return array_reverse($labels);
  66. }
  67. private function getRecordData($type)
  68. {
  69. $data = [];
  70. for($i=0; $i<=7; $i++)
  71. {
  72. if ($type == 'IN')
  73. {
  74. $found = false;
  75. foreach($this->in as $in)
  76. {
  77. if (date("Y-m-d", strtotime($in->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  78. {
  79. $data[] = $in->total;
  80. $found = true;
  81. }
  82. }
  83. if (!$found)
  84. $data[] = 0;
  85. }
  86. if ($type == 'OUT')
  87. {
  88. $found = false;
  89. foreach($this->out as $out)
  90. {
  91. if (date("Y-m-d", strtotime($out->date)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  92. {
  93. $data[] = $out->total;
  94. $found = true;
  95. }
  96. }
  97. if (!$found)
  98. $data[] = 0;
  99. }
  100. }
  101. return array_reverse($data);
  102. }
  103. private function getMemberData()
  104. {
  105. $data = [];
  106. for($i=0; $i<=7; $i++)
  107. {
  108. $found = false;
  109. foreach($this->members as $member)
  110. {
  111. if (date("Y-m-d", strtotime($member->created_at)) == date("Y-m-d", strtotime('-' . $i . ' days')))
  112. {
  113. $data[] = $member->total;
  114. $found = true;
  115. }
  116. }
  117. if (!$found)
  118. $data[] = 0;
  119. }
  120. return array_reverse($data);
  121. }
  122. }