PresenceReport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $months = date("n", strtotime($calendar->from));
  93. // Elenco utenti iscritti al corso "padre"
  94. // $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->get();
  95. // $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  96. $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();
  97. foreach ($members as $member) {
  98. $court = '';
  99. $instructor = '';
  100. $motivation = '';
  101. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  102. if ($presence) {
  103. $court = $presence->court ? $presence->court->name : "";
  104. $instructor = $presence->user->name . ($presence->instructor && $presence->instructor !== $presence->user ? ", " . $presence->instructor->name : "");
  105. $motivation = $presence->motivation ? $presence->motivation->name : "";
  106. }
  107. $status = '';
  108. if (in_array($member->member->id, $presences)) {
  109. $status = "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  110. } else {
  111. if (in_array($member->member->id, $presences_annullate)) {
  112. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  113. } else {
  114. if (date("Ymd") > date("Ymd", strtotime($calendar->from))) {
  115. $status = "<span class='fw-bold' style='color:red'>Assente</span>";
  116. }
  117. }
  118. }
  119. if ($calendar->status == 99) {
  120. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  121. }
  122. $this->records[$calendar->name][$h][] = array(
  123. "last_name" => $member->member->last_name,
  124. "first_name" => $member->member->first_name,
  125. "court" => $court,
  126. "instructor" => $instructor,
  127. "status" => $status,
  128. 'motivation' => $motivation
  129. );
  130. $mids[] = $member->member->id;
  131. }
  132. $presences_recuperi = \App\Models\Presence::where('calendar_id', $calendar->id)->whereNotIn('member_id', $mids);
  133. if (!is_null($this->court_id) && $this->court_id > 0) {
  134. $presences_recuperi->where('court_id', $this->court_id);
  135. }
  136. if (!is_null($this->instructor_id) && $this->instructor_id > 0) {
  137. $presences_recuperi->where(function ($query) {
  138. $query->where('instructor_id', $this->instructor_id)
  139. ->orWhere('user_id', $this->instructor_id);
  140. });
  141. }
  142. if (!is_null($this->search) && $this->search != "") {
  143. $search_value = $this->search;
  144. $presences_recuperi->whereHas('member', function ($q) use ($search_value) {
  145. $q->where(function ($qq) use ($search_value) {
  146. $qq->whereRaw("CONCAT(TRIM(first_name), ' ', TRIM(last_name)) LIKE ?", ["%{$search_value}%"])
  147. ->orWhereRaw("CONCAT(TRIM(last_name), ' ', TRIM(first_name)) LIKE ?", ["%{$search_value}%"]);
  148. });
  149. });
  150. }
  151. $presences_recuperi = $presences_recuperi->get();
  152. foreach ($presences_recuperi as $p) {
  153. $court = $p->court ? $p->court->name : "";
  154. $instructor = $p->user->name . ($p->instructor && $p->instructor !== $p->user ? ", " . $p->instructor->name : "");
  155. $motivation = $p->motivation ? $p->motivation->name : "";
  156. $status = "<span class='fw-bold' style='color:gray'>Recupero</span>";
  157. $this->records[$calendar->name][$h][] = array(
  158. "last_name" => $p->member->last_name,
  159. "first_name" => $p->member->first_name,
  160. "court" => $court,
  161. "instructor" => $instructor,
  162. "status" => $status,
  163. 'motivation' => $motivation
  164. );
  165. }
  166. /*
  167. $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('id', $calendar->id)->pluck('id')->toArray();
  168. $presences_recuperi = \App\Models\Presence::whereIn('calendar_id', $calendar_recuperi)->where('member_id', $this->dataId)->get();
  169. foreach($presences_recuperi as $p)
  170. {
  171. $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();
  172. $this->recuperi += 1;
  173. }
  174. */
  175. //array_push($this->courses, $calendar->course_id);
  176. // sort records per cognome-nome
  177. if (isset($this->records[$calendar->name]) && isset($this->records[$calendar->name][$h])) {
  178. usort($this->records[$calendar->name][$h], function($a, $b) {
  179. $last_name_compare = strcmp($a['last_name'], $b['last_name']);
  180. $first_name_compare = strcmp($a['first_name'], $b['first_name']);
  181. return $last_name_compare != 0 ? $last_name_compare : $first_name_compare;
  182. });
  183. }
  184. }
  185. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  186. /*$this->courses = array_unique($this->courses);
  187. $this->courses = array_map(function ($course_id) {
  188. try {
  189. return \App\Models\Course::findOrFail($course_id);
  190. } catch (\Throwable $e) {
  191. return null;
  192. }
  193. }, $this->courses);
  194. $this->courses = array_filter($this->courses);*/
  195. return view('livewire.presence_report');
  196. }
  197. public function prev()
  198. {
  199. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  200. }
  201. public function next()
  202. {
  203. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  204. }
  205. public function today()
  206. {
  207. $this->date = date("Y-m-d");
  208. }
  209. }