PresenceReport.php 14 KB

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