PresenceReport.php 12 KB

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