PresenceReport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 $search;
  26. public function mount()
  27. {
  28. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  29. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  30. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  31. $this->from = "00:00:00";
  32. $this->to = "23:59:59";
  33. $this->date = date("Y-m-d");
  34. setlocale(LC_ALL, 'it_IT');
  35. }
  36. public function render()
  37. {
  38. setlocale(LC_ALL, 'it_IT');
  39. $this->records = [];
  40. $this->courses = [];
  41. $from = $this->date . " " . $this->from;
  42. $to = $this->date . " " . $this->to;
  43. $calendars = \App\Models\Calendar::where('from', '>=', $from)->where('from', '<=', $to)->orderBy('from')->get();
  44. foreach ($calendars as $calendar) {
  45. $presences = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', '<>', 99);
  46. $presences_annullate = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', 99);
  47. // filtra per campo court_id
  48. if (!is_null($this->court_id) && $this->court_id > 0) {
  49. $presences->where('court_id', $this->court_id);
  50. $presences_annullate->where('court_id', $this->court_id);
  51. }
  52. // filtra per campo istructor_id/user_id
  53. if (!is_null($this->instructor_id) && $this->instructor_id > 0) {
  54. $presences->where(function ($query) {
  55. $query->where('instructor_id', $this->instructor_id)
  56. ->orWhere('user_id', $this->instructor_id);
  57. });
  58. $presences_annullate->where(function ($query) {
  59. $query->where('instructor_id', $this->instructor_id)
  60. ->orWhere('user_id', $this->instructor_id);
  61. });
  62. }
  63. // filtra per campo search (nome/cognome)
  64. if (!is_null($this->search) && $this->search != "") {
  65. $search_value = $this->search;
  66. $presences->whereHas('member', function ($q) use ($search_value) {
  67. $q->where(function ($qq) use ($search_value) {
  68. $qq->whereRaw("CONCAT(TRIM(first_name), ' ', TRIM(last_name)) LIKE ?", ["%{$search_value}%"])
  69. ->orWhereRaw("CONCAT(TRIM(last_name), ' ', TRIM(first_name)) LIKE ?", ["%{$search_value}%"]);
  70. });
  71. });
  72. $presences_annullate->whereHas('member', function ($q) use ($search_value) {
  73. $q->where(function ($qq) use ($search_value) {
  74. $qq->whereRaw("CONCAT(TRIM(first_name), ' ', TRIM(last_name)) LIKE ?", ["%{$search_value}%"])
  75. ->orWhereRaw("CONCAT(TRIM(last_name), ' ', TRIM(first_name)) LIKE ?", ["%{$search_value}%"]);
  76. });
  77. });
  78. }
  79. $presences = $presences->pluck('member_id')->toArray();
  80. $presences_annullate = $presences_annullate->pluck('member_id')->toArray();
  81. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  82. $dow = date('w', strtotime($calendar->from));
  83. $d = $days[$dow];
  84. $h = date('H:i', strtotime($calendar->from));
  85. // Elenco corsi per tipologia in base al calendario
  86. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to);
  87. if (!is_null($this->course_name)) {
  88. $courses = $courses->where('name', $this->course_name);
  89. }
  90. $courses = $courses->pluck('id')->toArray();
  91. $mids = [];
  92. // Elenco utenti iscritti al corso "padre"
  93. // $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('course_id', $courses)->get();
  94. // $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  95. $members = \App\Models\MemberCourse::whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')",[$d, $h])->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  96. foreach ($members as $member) {
  97. $court = '';
  98. $instructor = '';
  99. $motivation = '';
  100. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  101. if ($presence) {
  102. $court = $presence->court ? $presence->court->name : "";
  103. $instructor = $presence->user->name . ($presence->instructor && $presence->instructor !== $presence->user ? ", " . $presence->instructor->name : "");
  104. $motivation = $presence->motivation ? $presence->motivation->name : "";
  105. }
  106. $status = '';
  107. if (in_array($member->member->id, $presences)) {
  108. $status = "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  109. } else {
  110. if (in_array($member->member->id, $presences_annullate)) {
  111. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  112. } else {
  113. if (date("Ymd") > date("Ymd", strtotime($calendar->from))) {
  114. $status = "<span class='fw-bold' style='color:red'>Assente</span>";
  115. }
  116. }
  117. }
  118. if ($calendar->status == 99) {
  119. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  120. }
  121. $this->records[$calendar->name][$h][] = array(
  122. "last_name" => $member->member->last_name,
  123. "first_name" => $member->member->first_name,
  124. "court" => $court,
  125. "instructor" => $instructor,
  126. "status" => $status,
  127. 'motivation' => $motivation
  128. );
  129. $mids[] = $member->member->id;
  130. }
  131. $presences_recuperi = \App\Models\Presence::where('calendar_id', $calendar->id)->whereNotIn('member_id', $mids);
  132. if (!is_null($this->court_id) && $this->court_id > 0) {
  133. $presences_recuperi->where('court_id', $this->court_id);
  134. }
  135. if (!is_null($this->instructor_id) && $this->instructor_id > 0) {
  136. $presences_recuperi->where(function ($query) {
  137. $query->where('instructor_id', $this->instructor_id)
  138. ->orWhere('user_id', $this->instructor_id);
  139. });
  140. }
  141. if (!is_null($this->search) && $this->search != "") {
  142. $search_value = $this->search;
  143. $presences_recuperi->whereHas('member', function ($q) use ($search_value) {
  144. $q->where(function ($qq) use ($search_value) {
  145. $qq->whereRaw("CONCAT(TRIM(first_name), ' ', TRIM(last_name)) LIKE ?", ["%{$search_value}%"])
  146. ->orWhereRaw("CONCAT(TRIM(last_name), ' ', TRIM(first_name)) LIKE ?", ["%{$search_value}%"]);
  147. });
  148. });
  149. }
  150. $presences_recuperi = $presences_recuperi->get();
  151. foreach ($presences_recuperi as $p) {
  152. $court = $p->court ? $p->court->name : "";
  153. $instructor = $p->user->name . ($p->instructor && $p->instructor !== $p->user ? ", " . $p->instructor->name : "");
  154. $motivation = $p->motivation ? $p->motivation->name : "";
  155. $status = "<span class='fw-bold' style='color:gray'>Recupero</span>";
  156. $this->records[$calendar->name][$h][] = array(
  157. "last_name" => $p->member->last_name,
  158. "first_name" => $p->member->first_name,
  159. "court" => $court,
  160. "instructor" => $instructor,
  161. "status" => $status,
  162. 'motivation' => $motivation
  163. );
  164. }
  165. /*
  166. $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('id', $calendar->id)->pluck('id')->toArray();
  167. $presences_recuperi = \App\Models\Presence::whereIn('calendar_id', $calendar_recuperi)->where('member_id', $this->dataId)->get();
  168. foreach($presences_recuperi as $p)
  169. {
  170. $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();
  171. $this->recuperi += 1;
  172. }
  173. */
  174. //array_push($this->courses, $calendar->course_id);
  175. // sort records per cognome-nome
  176. if (isset($this->records[$calendar->name]) && isset($this->records[$calendar->name][$h])) {
  177. usort($this->records[$calendar->name][$h], function($a, $b) {
  178. $last_name_compare = strcmp($a['last_name'], $b['last_name']);
  179. $first_name_compare = strcmp($a['first_name'], $b['first_name']);
  180. return $last_name_compare != 0 ? $last_name_compare : $first_name_compare;
  181. });
  182. }
  183. }
  184. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  185. /*$this->courses = array_unique($this->courses);
  186. $this->courses = array_map(function ($course_id) {
  187. try {
  188. return \App\Models\Course::findOrFail($course_id);
  189. } catch (\Throwable $e) {
  190. return null;
  191. }
  192. }, $this->courses);
  193. $this->courses = array_filter($this->courses);*/
  194. return view('livewire.presence_report');
  195. }
  196. public function prev()
  197. {
  198. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  199. }
  200. public function next()
  201. {
  202. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  203. }
  204. public function today()
  205. {
  206. $this->date = date("Y-m-d");
  207. }
  208. }