PresenceReport.php 12 KB

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