PresenceReport.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class PresenceReport extends Component
  5. {
  6. public $calendar;
  7. public $records;
  8. public $date;
  9. public $member_ids = [];
  10. public $courses = [];
  11. public $courts = [];
  12. public $instructors = [];
  13. public $motivations = [];
  14. public $court_filter;
  15. public $instructor_filter;
  16. public $motivation_filter;
  17. public $members = [];
  18. public $newMembers = [];
  19. public $ids = [];
  20. public $course_name;
  21. public $from;
  22. public $to;
  23. public $court_id;
  24. public $instructor_id;
  25. public function mount()
  26. {
  27. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  28. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  29. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  30. $this->from = "00:00:00";
  31. $this->to = "23:59:59";
  32. $this->date = date("Y-m-d");
  33. setlocale(LC_ALL, 'it_IT');
  34. }
  35. public function render()
  36. {
  37. setlocale(LC_ALL, 'it_IT');
  38. $this->records = [];
  39. $this->courses = [];
  40. $from = $this->date . " " . $this->from;
  41. $to = $this->date . " " . $this->to;
  42. $calendars = \App\Models\Calendar::where('from', '>=', $from)->where('from', '<=', $to)->orderBy('from')->get();
  43. foreach ($calendars as $calendar) {
  44. $presences = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', '<>', 99);
  45. if (!is_null($this->court_id)) {
  46. $presences = $presences->where('court_id', $this->court_id);
  47. }
  48. if (!is_null($this->instructor_id)) {
  49. $presences = $presences->where('instructor_id', $this->instructor_id);
  50. }
  51. $presences = $presences->pluck('member_id')->toArray();
  52. $presences_annullate = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', 99)->pluck('member_id')->toArray();
  53. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  54. $dow = date('w', strtotime($calendar->from));
  55. $d = $days[$dow];
  56. $h = date('H:i', strtotime($calendar->from));
  57. // Elenco corsi per tipologia in base al calendario
  58. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to);
  59. if (!is_null($this->course_name)) {
  60. $courses = $courses->where('name', $this->course_name);
  61. }
  62. $courses = $courses->pluck('id')->toArray();
  63. $mids = [];
  64. $months = date("n", strtotime($calendar->from));
  65. // Elenco utenti iscritti al corso "padre"
  66. $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->get();
  67. //$members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  68. foreach ($members as $member) {
  69. $court = '';
  70. $instructor = '';
  71. $motivation = '';
  72. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  73. if ($presence) {
  74. $court = $presence->court ? $presence->court->name : "";
  75. $instructor = $presence->instructor ? $presence->instructor->name : "";
  76. $motivation = $presence->motivation ? $presence->motivation->name : "";
  77. }
  78. $status = '';
  79. if (in_array($member->member->id, $presences)) {
  80. $status = "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  81. } else {
  82. if (in_array($member->member->id, $presences_annullate)) {
  83. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  84. } else {
  85. if (date("Ymd") > date("Ymd", strtotime($calendar->from))) {
  86. $status = "<span class='fw-bold' style='color:red'>Assente</span>";
  87. }
  88. }
  89. }
  90. if ($calendar->status == 99) {
  91. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  92. }
  93. $this->records[$calendar->name][$h][] = array("last_name" => $member->member->last_name, "first_name" => $member->member->first_name, "court" => $court, "instructor" => $instructor, "status" => $status, 'motivation' => $motivation);
  94. $mids[] = $member->member->id;
  95. }
  96. $presences_recuperi = \App\Models\Presence::where('calendar_id', $calendar->id)->whereNotIn('member_id', $mids)->get();
  97. foreach($presences_recuperi as $p)
  98. {
  99. $court = $p->court ? $p->court->name : "";
  100. $instructor = $p->instructor ? $p->instructor->name : "";
  101. $motivation = $p->motivation ? $p->motivation->name : "";
  102. $status = "<span class='fw-bold' style='color:gray'>Recupero</span>";
  103. $this->records[$calendar->name][$h][] = array("last_name" => $p->member->last_name, "first_name" => $p->member->first_name, "court" => $court, "instructor" => $instructor, "status" => $status, 'motivation' => $motivation);
  104. }
  105. /*
  106. $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('id', $calendar->id)->pluck('id')->toArray();
  107. $presences_recuperi = \App\Models\Presence::whereIn('calendar_id', $calendar_recuperi)->where('member_id', $this->dataId)->get();
  108. foreach($presences_recuperi as $p)
  109. {
  110. $this->member_presences[] = array('calendar_id' => $p->calendar->id, 'from' => $p->calendar->from, 'to' => $p->calendar->to, 'status' => '<span style="color:#7136f6">Recupero</span>');//\App\Models\Presence::where('member_id', $this->dataId)->get();
  111. $this->recuperi += 1;
  112. }
  113. */
  114. //array_push($this->courses, $calendar->course_id);
  115. }
  116. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  117. /*$this->courses = array_unique($this->courses);
  118. $this->courses = array_map(function ($course_id) {
  119. try {
  120. return \App\Models\Course::findOrFail($course_id);
  121. } catch (\Throwable $e) {
  122. return null;
  123. }
  124. }, $this->courses);
  125. $this->courses = array_filter($this->courses);*/
  126. return view('livewire.presence_report');
  127. }
  128. public function prev()
  129. {
  130. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  131. }
  132. public function next()
  133. {
  134. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  135. }
  136. public function today()
  137. {
  138. $this->date = date("Y-m-d");
  139. }
  140. }