PresenceReport.php 11 KB

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