PresenceReport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Illuminate\Support\Carbon;
  5. use App\Http\Middleware\TenantMiddleware;
  6. class PresenceReport extends Component
  7. {
  8. public $records = [];
  9. public $date;
  10. public $courses = [];
  11. public $courts = [];
  12. public $instructors = [];
  13. public $motivations = [];
  14. public $course_name;
  15. public $from;
  16. public $to;
  17. public $court_id;
  18. public $instructor_id;
  19. public $search;
  20. public $court_name = '';
  21. public $instructor_name = '';
  22. public function boot()
  23. {
  24. app(TenantMiddleware::class)->setupTenantConnection();
  25. }
  26. public function mount()
  27. {
  28. $this->courts = \App\Models\Court::where('enabled', true)->orderBy('name')->get();
  29. $this->instructors = \App\Models\User::where('level', 2)->where('enabled', true)->orderBy('name')->get();
  30. $this->motivations = \App\Models\Motivation::where('enabled', true)->where('type', 'del')->orderBy('name')->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. $fromDt = Carbon::parse($this->date . ' ' . ($this->from ?: '00:00:00'));
  41. $toDt = Carbon::parse($this->date . ' ' . ($this->to ?: '23:59:59'));
  42. $this->court_name = '';
  43. if (!empty($this->court_id)) {
  44. $court = $this->courts->firstWhere('id', (int)$this->court_id);
  45. $this->court_name = $court?->name ?? '';
  46. }
  47. $this->instructor_name = '';
  48. if (!empty($this->instructor_id)) {
  49. $instr = $this->instructors->firstWhere('id', (int)$this->instructor_id);
  50. $this->instructor_name = $instr?->name ?? '';
  51. }
  52. $calendars = \App\Models\Calendar::with(['course.level'])
  53. ->whereBetween('from', [$fromDt->toDateTimeString(), $toDt->toDateTimeString()])
  54. ->orderBy('from')
  55. ->get();
  56. if ($calendars->isEmpty()) {
  57. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  58. return view('livewire.presence_report');
  59. }
  60. $calendarIds = $calendars->pluck('id')->all();
  61. $presencesAll = \App\Models\Presence::with([
  62. 'member:id,first_name,last_name',
  63. 'court:id,name',
  64. 'user:id,name',
  65. 'instructor:id,name',
  66. 'motivation:id,name',
  67. 'motivationCourse:id,name,course_level_id',
  68. 'motivationCourse.level:id,name',
  69. ])
  70. ->whereIn('calendar_id', $calendarIds)
  71. ->when(!empty($this->court_id), fn($q) => $q->where('court_id', (int)$this->court_id))
  72. ->when(!empty($this->instructor_id), function ($q) {
  73. $iid = (int)$this->instructor_id;
  74. $q->where(function ($qq) use ($iid) {
  75. $qq->where('instructor_id', $iid)->orWhere('user_id', $iid);
  76. });
  77. })
  78. ->when(!empty($this->search), function ($q) {
  79. $s = trim($this->search);
  80. $q->whereHas('member', function ($mq) use ($s) {
  81. $mq->where(function ($qq) use ($s) {
  82. $qq->whereRaw("CONCAT(TRIM(first_name), ' ', TRIM(last_name)) LIKE ?", ["%{$s}%"])
  83. ->orWhereRaw("CONCAT(TRIM(last_name), ' ', TRIM(first_name)) LIKE ?", ["%{$s}%"]);
  84. });
  85. });
  86. })
  87. ->get();
  88. $presenceByCalMember = [];
  89. $presencesByCalendar = [];
  90. foreach ($presencesAll as $p) {
  91. $presenceByCalMember[$p->calendar_id . '|' . $p->member_id] = $p;
  92. $presencesByCalendar[$p->calendar_id][] = $p;
  93. }
  94. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  95. foreach ($calendars as $calendar) {
  96. $h = Carbon::parse($calendar->from)->format('H:i');
  97. $dow = (int) Carbon::parse($calendar->from)->format('w'); // 0..6
  98. $d = $days[$dow];
  99. $courseIds = \App\Models\Course::query()
  100. ->where('name', $calendar->name)
  101. ->where('date_from', '<=', $calendar->from)
  102. ->where('date_to', '>=', $calendar->to)
  103. ->when(!empty($this->course_name), fn($q) => $q->where('name', $this->course_name))
  104. ->pluck('id')
  105. ->toArray();
  106. if (empty($courseIds)) {
  107. continue;
  108. }
  109. $slotCourseIds = \App\Models\Course::query()
  110. ->whereIn('id', $courseIds)
  111. ->get(['id', 'when'])
  112. ->filter(fn($c) => $this->courseMatchesSlot($c->when, $d, $h))
  113. ->pluck('id')
  114. ->all();
  115. if (empty($slotCourseIds)) {
  116. continue;
  117. }
  118. $membersQuery = \App\Models\MemberCourse::query()
  119. ->whereIn('course_id', $slotCourseIds)
  120. ->whereDate('date_from', '<=', $calendar->from)
  121. ->whereDate('date_to', '>=', $calendar->from)
  122. ->with(['member', 'course.level']);
  123. if (!empty($this->search)) {
  124. $s = trim(mb_strtolower($this->search));
  125. $membersQuery->whereHas('member', function ($mq) use ($s) {
  126. $mq->where(function ($qq) use ($s) {
  127. $qq->whereRaw('LOWER(CONCAT(TRIM(first_name), " ", TRIM(last_name))) LIKE ?', ["%{$s}%"])
  128. ->orWhereRaw('LOWER(CONCAT(TRIM(last_name), " ", TRIM(first_name))) LIKE ?', ["%{$s}%"]);
  129. });
  130. });
  131. }
  132. $members = $membersQuery->get();
  133. $expectedMemberIds = [];
  134. foreach ($members as $mc) {
  135. $mid = $mc->member->id;
  136. $expectedMemberIds[] = $mid;
  137. $p = $presenceByCalMember[$calendar->id . '|' . $mid] ?? null;
  138. [$court, $instructor, $motivation, $motivation_course] = $this->presenceMeta($p);
  139. $status = $this->presenceStatusHtml($p, $calendar);
  140. $course_level = '';
  141. if ($mc->course && $mc->course->level) {
  142. $course_level = trim($mc->course->level->name);
  143. }
  144. $this->records[$calendar->name][$h][] = [
  145. 'course_level' => $course_level,
  146. 'last_name' => $mc->member->last_name,
  147. 'first_name' => $mc->member->first_name,
  148. 'court' => $court,
  149. 'instructor' => $instructor,
  150. 'status' => $status,
  151. 'motivation' => $motivation,
  152. ];
  153. }
  154. $extras = $presencesByCalendar[$calendar->id] ?? [];
  155. foreach ($extras as $p) {
  156. if (in_array($p->member_id, $expectedMemberIds, true)) {
  157. continue;
  158. }
  159. if (!empty($this->course_name)) {
  160. $motCourseName = $p->motivationCourse?->name;
  161. if (!$motCourseName || $motCourseName !== $this->course_name) {
  162. continue;
  163. }
  164. }
  165. [$court, $instructor, $motivation, $motivation_course] = $this->presenceMeta($p);
  166. $status = $this->presenceStatusHtml($p, $calendar);
  167. $course_level = '';
  168. if ($calendar->course && $calendar->course->level) {
  169. $course_level = trim($calendar->course->level->name);
  170. }
  171. if ($motivation_course) {
  172. $course_level = $motivation_course->level?->name;
  173. }
  174. $this->records[$calendar->name][$h][] = [
  175. 'course_level' => $course_level,
  176. 'last_name' => $p->member?->last_name ?? '',
  177. 'first_name' => $p->member?->first_name ?? '',
  178. 'court' => $court,
  179. 'instructor' => $instructor,
  180. 'status' => $status,
  181. 'motivation' => $motivation,
  182. ];
  183. }
  184. if (isset($this->records[$calendar->name][$h])) {
  185. usort($this->records[$calendar->name][$h], function ($a, $b) {
  186. $course_level_compare = strcmp($a['course_level'], $b['course_level']);
  187. if ($course_level_compare !== 0) return $course_level_compare;
  188. $last_name_compare = strcmp($a['last_name'], $b['last_name']);
  189. if ($last_name_compare !== 0) return $last_name_compare;
  190. return strcmp($a['first_name'], $b['first_name']);
  191. });
  192. }
  193. }
  194. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  195. return view('livewire.presence_report');
  196. }
  197. private function courseMatchesSlot(?string $whenJson, string $day, string $hhmm): bool
  198. {
  199. if (!$whenJson) return false;
  200. $when = json_decode($whenJson, true);
  201. if (!is_array($when)) return false;
  202. foreach ($when as $period) {
  203. $days = $period['day'] ?? [];
  204. $from = $period['from'] ?? null;
  205. if (!$from || empty($days)) continue;
  206. $from = substr((string)$from, 0, 5);
  207. if ($from === $hhmm && in_array($day, $days, true)) {
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. protected function presenceStatusHtml($presence, $calendar): string
  214. {
  215. if ($calendar->status == 99) {
  216. return "<span class='fw-bold' style='color:gray'>Annullata</span>";
  217. }
  218. if ($presence) {
  219. if ((int)$presence->status === 99) {
  220. return "<span class='fw-bold' style='color:gray'>Annullata</span>";
  221. }
  222. return "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  223. }
  224. if (Carbon::now()->format('Ymd') > Carbon::parse($calendar->from)->format('Ymd')) {
  225. return "<span class='fw-bold' style='color:red'>Assente</span>";
  226. }
  227. return '';
  228. }
  229. protected function presenceMeta($presence): array
  230. {
  231. if (!$presence) return ['', '', '', null];
  232. $court = $presence->court?->name ?? '';
  233. $instructorParts = [
  234. $presence->user?->name ?? '',
  235. ($presence->instructor && $presence->user && $presence->instructor->id !== $presence->user->id)
  236. ? $presence->instructor->name
  237. : ($presence->instructor?->name ?? ''),
  238. ];
  239. $instructor = implode(', ', array_values(array_filter(array_unique($instructorParts))));
  240. $motivation = $presence->motivation?->name ?? '';
  241. $motivation_course = $presence->motivationCourse ?? null;
  242. return [$court, $instructor, $motivation, $motivation_course];
  243. }
  244. public function prev()
  245. {
  246. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  247. }
  248. public function next()
  249. {
  250. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  251. }
  252. public function today()
  253. {
  254. $this->date = date("Y-m-d");
  255. }
  256. }