PresenceReport.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 . "%")
  67. ->where('when', 'like', '%"from":"' . $h . '"%')
  68. ->whereDate('date_from', '<=', $calendar->from)
  69. ->whereDate('date_to', '>=', $calendar->from)
  70. ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
  71. ->whereIn('course_id', $courses)->get();
  72. //$members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  73. foreach ($members as $member) {
  74. $court = '';
  75. $instructor = '';
  76. $motivation = '';
  77. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  78. if ($presence) {
  79. $court = $presence->court ? $presence->court->name : "";
  80. $instructor = $presence->instructor ? $presence->instructor->name : "";
  81. $motivation = $presence->motivation ? $presence->motivation->name : "";
  82. }
  83. $status = '';
  84. if (in_array($member->member->id, $presences)) {
  85. $status = "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  86. } else {
  87. if (in_array($member->member->id, $presences_annullate)) {
  88. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  89. } else {
  90. if (date("Ymd") > date("Ymd", strtotime($calendar->from))) {
  91. $status = "<span class='fw-bold' style='color:red'>Assente</span>";
  92. }
  93. }
  94. }
  95. if ($calendar->status == 99) {
  96. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  97. }
  98. $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);
  99. $mids[] = $member->member->id;
  100. }
  101. $presences_recuperi = \App\Models\Presence::where('calendar_id', $calendar->id)->whereNotIn('member_id', $mids)->get();
  102. foreach($presences_recuperi as $p)
  103. {
  104. $court = $p->court ? $p->court->name : "";
  105. $instructor = $p->instructor ? $p->instructor->name : "";
  106. $motivation = $p->motivation ? $p->motivation->name : "";
  107. $status = "<span class='fw-bold' style='color:gray'>Recupero</span>";
  108. $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);
  109. }
  110. /*
  111. $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('id', $calendar->id)->pluck('id')->toArray();
  112. $presences_recuperi = \App\Models\Presence::whereIn('calendar_id', $calendar_recuperi)->where('member_id', $this->dataId)->get();
  113. foreach($presences_recuperi as $p)
  114. {
  115. $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();
  116. $this->recuperi += 1;
  117. }
  118. */
  119. //array_push($this->courses, $calendar->course_id);
  120. }
  121. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  122. /*$this->courses = array_unique($this->courses);
  123. $this->courses = array_map(function ($course_id) {
  124. try {
  125. return \App\Models\Course::findOrFail($course_id);
  126. } catch (\Throwable $e) {
  127. return null;
  128. }
  129. }, $this->courses);
  130. $this->courses = array_filter($this->courses);*/
  131. return view('livewire.presence_report');
  132. }
  133. public function prev()
  134. {
  135. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  136. }
  137. public function next()
  138. {
  139. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  140. }
  141. public function today()
  142. {
  143. $this->date = date("Y-m-d");
  144. }
  145. }