PresenceReport.php 12 KB

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