PresenceReport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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',
  65. 'instructor:id,name,cognome',
  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. $discipline = \App\Models\Discipline::query()
  100. ->where('name', $calendar->name)
  101. ->pluck('id')
  102. ->all();
  103. $courseIds = \App\Models\Course::query()
  104. ->where(function($q) use($discipline, $calendar){
  105. $q->where('name', $calendar->name)
  106. ->orWhereIn('discipline_id', $discipline);
  107. })
  108. ->where('date_from', '<=', $calendar->from)
  109. ->where('date_to', '>=', $calendar->to)
  110. ->when(!empty($this->course_name), fn($q) => $q->where('name', $this->course_name))
  111. ->pluck('id')
  112. ->toArray();
  113. if (empty($courseIds)) {
  114. continue;
  115. }
  116. $slotCourseIds = \App\Models\Course::query()
  117. ->whereIn('id', $courseIds)
  118. ->get(['id', 'when'])
  119. ->filter(fn($c) => $this->courseMatchesSlot($c->when, $d, $h))
  120. ->pluck('id')
  121. ->all();
  122. if (empty($slotCourseIds)) {
  123. continue;
  124. }
  125. $membersQuery = \App\Models\MemberCourse::query()
  126. ->with(['member', 'course.level'])
  127. ->whereIn('course_id', $slotCourseIds)
  128. ->whereDate('date_from', '<=', $calendar->from)
  129. ->whereDate('date_to', '>=', $calendar->from)
  130. ->whereHas('member', function ($query) {
  131. $query->where(function ($q) {
  132. $q->where('is_archived', false)
  133. ->orWhereNull('is_archived');
  134. })->where(function ($q) {
  135. $q->where('is_deleted', false)
  136. ->orWhereNull('is_deleted');
  137. });
  138. });
  139. if (!empty($this->search)) {
  140. $s = trim(mb_strtolower($this->search));
  141. $membersQuery->whereHas('member', function ($mq) use ($s) {
  142. $mq->where(function ($qq) use ($s) {
  143. $qq->whereRaw('LOWER(CONCAT(TRIM(first_name), " ", TRIM(last_name))) LIKE ?', ["%{$s}%"])
  144. ->orWhereRaw('LOWER(CONCAT(TRIM(last_name), " ", TRIM(first_name))) LIKE ?', ["%{$s}%"]);
  145. });
  146. });
  147. }
  148. $members = $membersQuery->get();
  149. $expectedMemberIds = [];
  150. foreach ($members as $mc) {
  151. $mid = $mc->member->id;
  152. $expectedMemberIds[] = $mid;
  153. $p = $presenceByCalMember[$calendar->id . '|' . $mid] ?? null;
  154. [$court, $instructor, $motivation, $motivation_course] = $this->presenceMeta($p);
  155. $status = $this->presenceStatusHtml($p, $calendar);
  156. $course_level = '';
  157. if ($mc->course && $mc->course->level) {
  158. $course_level = trim($mc->course->level->name);
  159. }
  160. $this->records[$calendar->name][$h][] = [
  161. 'course_level' => $course_level,
  162. 'last_name' => $mc->member->last_name,
  163. 'first_name' => $mc->member->first_name,
  164. 'court' => $court,
  165. 'instructor' => $instructor,
  166. 'status' => $status,
  167. 'motivation' => $motivation,
  168. ];
  169. }
  170. $extras = $presencesByCalendar[$calendar->id] ?? [];
  171. foreach ($extras as $p) {
  172. if (in_array($p->member_id, $expectedMemberIds, true)) {
  173. continue;
  174. }
  175. if (!empty($this->course_name)) {
  176. $motCourseName = $p->motivationCourse?->name;
  177. if (!$motCourseName || $motCourseName !== $this->course_name) {
  178. continue;
  179. }
  180. }
  181. [$court, $instructor, $motivation, $motivation_course] = $this->presenceMeta($p);
  182. $status = $this->presenceStatusHtml($p, $calendar);
  183. $course_level = '';
  184. if ($calendar->course && $calendar->course->level) {
  185. $course_level = trim($calendar->course->level->name);
  186. }
  187. if ($motivation_course) {
  188. $course_level = $motivation_course->level?->name;
  189. }
  190. $this->records[$calendar->name][$h][] = [
  191. 'course_level' => $course_level,
  192. 'last_name' => $p->member?->last_name ?? '',
  193. 'first_name' => $p->member?->first_name ?? '',
  194. 'court' => $court,
  195. 'instructor' => $instructor,
  196. 'status' => $status,
  197. 'motivation' => $motivation,
  198. ];
  199. }
  200. if (isset($this->records[$calendar->name][$h])) {
  201. usort($this->records[$calendar->name][$h], function ($a, $b) {
  202. $course_level_compare = strcmp($a['course_level'], $b['course_level']);
  203. if ($course_level_compare !== 0) return $course_level_compare;
  204. $last_name_compare = strcmp($a['last_name'], $b['last_name']);
  205. if ($last_name_compare !== 0) return $last_name_compare;
  206. return strcmp($a['first_name'], $b['first_name']);
  207. });
  208. }
  209. }
  210. $this->courses = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  211. return view('livewire.presence_report');
  212. }
  213. private function courseMatchesSlot(?string $whenJson, string $day, string $hhmm): bool
  214. {
  215. if (!$whenJson) return false;
  216. $when = json_decode($whenJson, true);
  217. if (!is_array($when)) return false;
  218. foreach ($when as $period) {
  219. $days = $period['day'] ?? [];
  220. $from = $period['from'] ?? null;
  221. if (!$from || empty($days)) continue;
  222. $from = substr((string)$from, 0, 5);
  223. if ($from === $hhmm && in_array($day, $days, true)) {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. protected function presenceStatusHtml($presence, $calendar): string
  230. {
  231. if ($calendar->status == 99) {
  232. return "<span class='fw-bold' style='color:gray'>Annullata</span>";
  233. }
  234. if ($presence) {
  235. if ((int)$presence->status === 99) {
  236. return "<span class='fw-bold' style='color:gray'>Annullata</span>";
  237. }
  238. return "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  239. }
  240. if (Carbon::now()->format('Ymd') > Carbon::parse($calendar->from)->format('Ymd')) {
  241. return "<span class='fw-bold' style='color:red'>Assente</span>";
  242. }
  243. return '';
  244. }
  245. protected function presenceMeta($presence): array
  246. {
  247. if (!$presence) return ['', '', '', null];
  248. $court = $presence->court?->name ?? '';
  249. $user_instructor = \App\Models\User::where('master_user_id', $presence->user_id)->first();
  250. $main_instructor = $user_instructor?->name;
  251. $instructorParts = [
  252. $main_instructor ?? '',
  253. ($presence->instructor) ? $presence->instructor->name . " " . $presence->instructor->cognome : '',
  254. ];
  255. $instructor = implode(', ', array_values(array_filter(array_unique($instructorParts))));
  256. $motivation = $presence->motivation?->name ?? '';
  257. $motivation_course = $presence->motivationCourse ?? null;
  258. return [$court, $instructor, $motivation, $motivation_course];
  259. }
  260. public function prev()
  261. {
  262. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  263. }
  264. public function next()
  265. {
  266. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  267. }
  268. public function today()
  269. {
  270. $this->date = date("Y-m-d");
  271. }
  272. }