PresenceReport.php 11 KB

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