AbsenceReport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Illuminate\Support\Carbon;
  4. use Livewire\Component;
  5. class AbsenceReport extends Component
  6. {
  7. public $records;
  8. public $record_assenze;
  9. public $fiscalStartMonth = 9;
  10. public $year;
  11. public function mount()
  12. {
  13. setlocale(LC_ALL, 'it_IT');
  14. $this->record_assenze = [];
  15. $today = now();
  16. $this->year = ($today->month >= $this->fiscalStartMonth) ? $today->year : $today->year - 1;
  17. $dayMap = [
  18. 'lun' => 1,
  19. 'mar' => 2,
  20. 'mer' => 3,
  21. 'gio' => 4,
  22. 'ven' => 5,
  23. 'sab' => 6,
  24. 'dom' => 0,
  25. ];
  26. try {
  27. $courses = \App\Models\Course::whereDate('date_from', '<=', now())
  28. ->whereDate('date_to', '>=', now())
  29. ->where('active', true)
  30. ->where('enabled', true)
  31. ->get();
  32. $limit = now()->endOfDay();
  33. foreach ($courses as $course) {
  34. $course_members = \App\Models\MemberCourse::with(['member' => function ($q) {
  35. $q->where(function ($q) {
  36. $q->where('is_archived', false)->orWhereNull('is_archived');
  37. })->where(function ($q) {
  38. $q->where('is_deleted', false)->orWhereNull('is_deleted');
  39. });
  40. }])
  41. ->where('course_id', $course->id)
  42. ->whereHas('member', function ($q) {
  43. $q->where(function ($q) {
  44. $q->where('is_archived', false)->orWhereNull('is_archived');
  45. })->where(function ($q) {
  46. $q->where('is_deleted', false)->orWhereNull('is_deleted');
  47. });
  48. })
  49. ->get();
  50. $courseCalendars = \App\Models\Calendar::where('course_id', $course->id)->where('from', '<=', $limit)->get();
  51. $calendarIndex = [];
  52. foreach ($courseCalendars as $cal) {
  53. $fromKey = Carbon::parse($cal->from)->toDateTimeString();
  54. $toKey = Carbon::parse($cal->to)->toDateTimeString();
  55. $key = $fromKey . '|' . $toKey;
  56. $calendarIndex[$key] = $cal;
  57. }
  58. $memberIds = $course_members->pluck('member_id')->unique()->values();
  59. $presences = \App\Models\Presence::whereIn('calendar_id', $courseCalendars->pluck('id'))->whereIn('member_id', $memberIds)->get();
  60. $presenceIndex = [];
  61. foreach ($presences as $p) {
  62. $presenceIndex[$p->member_id . '|' . $p->calendar_id] = true;
  63. }
  64. $this->record_assenze[$course->id] = [
  65. 'course' => $course,
  66. 'members' => [],
  67. ];
  68. foreach ($course_members as $course_member) {
  69. $this->record_assenze[$course->id]['members'][$course_member->id] = [
  70. 'member' => $course_member->member,
  71. 'count' => 0,
  72. 'dates' => [],
  73. ];
  74. $months = array_column(
  75. array_filter(json_decode($course_member->months, true), fn($m) => $m['status'] != 2),
  76. 'm'
  77. );
  78. sort($months);
  79. $when = json_decode($course_member->when, true);
  80. $memberCalendars = [];
  81. foreach ($when as $period) {
  82. $days = $period['day'];
  83. if (!$days) {
  84. continue;
  85. }
  86. $fromTime = $period['from'];
  87. $toTime = $period['to'];
  88. $ranges = $this->generateFiscalDateRanges($months, $days, $dayMap, $fromTime, $toTime);
  89. foreach ($ranges as $range) {
  90. $key = $range['from']->toDateTimeString() . '|' . $range['to']->toDateTimeString();
  91. if (isset($calendarIndex[$key])) {
  92. $memberCalendars[] = $calendarIndex[$key];
  93. }
  94. }
  95. }
  96. usort($memberCalendars, fn($a, $b) => $b->to <=> $a->to);
  97. foreach ($memberCalendars as $calendar) {
  98. $pKey = $course_member->member_id . '|' . $calendar->id;
  99. $hasPresence = isset($presenceIndex[$pKey]);
  100. if (!$hasPresence) {
  101. $this->record_assenze[$course->id]['members'][$course_member->id]['count']++;
  102. $this->record_assenze[$course->id]['members'][$course_member->id]['dates'][] = [
  103. 'calendar_id' => $calendar->id,
  104. 'date' => Carbon::parse($calendar->from)->translatedFormat('d/m'),
  105. ];
  106. } else {
  107. break;
  108. }
  109. }
  110. if ($this->record_assenze[$course->id]['members'][$course_member->id]['count'] < 2) {
  111. unset($this->record_assenze[$course->id]['members'][$course_member->id]);
  112. }
  113. }
  114. if (empty($this->record_assenze[$course->id]['members'])) {
  115. unset($this->record_assenze[$course->id]);
  116. } else {
  117. $members = $this->record_assenze[$course->id]['members'];
  118. usort($members, function ($a, $b) {
  119. if ($a['count'] !== $b['count']) {
  120. return $b['count'] <=> $a['count'];
  121. }
  122. $last = strcmp($a['member']->last_name, $b['member']->last_name);
  123. if ($last !== 0) {
  124. return $last;
  125. }
  126. return strcmp($a['member']->first_name, $b['member']->first_name);
  127. });
  128. $this->record_assenze[$course->id]['members'] = $members;
  129. }
  130. }
  131. } catch (\Throwable $e) {
  132. dd($e->getMessage());
  133. }
  134. }
  135. public function render()
  136. {
  137. setlocale(LC_ALL, 'it_IT');
  138. // $this->records = [];
  139. // $to = date("Y-m-d 23:59:59");
  140. // $calendars = \App\Models\Calendar::where('from', '<=', $to)->orderBy('from')->get();
  141. // $month = date("n");
  142. // $this->year = ($month >= 9) ? date("Y") : date("Y") - 1;
  143. // $start = date("Y-m-d H:i:s", mktime(0, 0, 0, 9, 1, $this->year));
  144. // $end = date("Y-m-d 23:59:59");
  145. // $calendars = \App\Models\Calendar::whereBetween('from', [$start, $end])->orderBy('from')->get();
  146. // foreach ($calendars as $calendar) {
  147. // $presences = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', '<>', 99);
  148. // $presences = $presences->pluck('member_id')->toArray();
  149. // $presences_annullate = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', 99)->pluck('member_id')->toArray();
  150. // $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  151. // $dow = date('w', strtotime($calendar->from));
  152. // $d = $days[$dow];
  153. // $h = date('H:i', strtotime($calendar->from));
  154. // // Elenco corsi per tipologia in base al calendario
  155. // $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to);
  156. // $courses = $courses->pluck('id')->toArray();
  157. // $months = date("n", strtotime($calendar->from));
  158. // // Elenco utenti iscritti al corso "padre"
  159. // $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")
  160. // ->where('when', 'like', '%"from":"' . $h . '"%')
  161. // ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
  162. // ->whereDate('date_from', '<=', $calendar->from)
  163. // ->whereDate('date_to', '>=', $calendar->from)
  164. // ->whereIn('course_id', $courses)
  165. // ->orderBy('member_id')
  166. // ->get();
  167. // //$members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  168. // foreach ($members as $member) {
  169. // $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  170. // if (!in_array($member->member->id, $presences)) {
  171. // if (!in_array($member->member->id, $presences_annullate)) {
  172. // if (array_key_exists($member->member->id, $this->records)) {
  173. // $this->records[$member->member->id]['total'] += 1;
  174. // $this->records[$member->member->id]['date'] .= " - " . date("d/m", strtotime($calendar->from));
  175. // } else
  176. // $this->records[$member->member->id] = array("last_name" => $member->member->last_name, "first_name" => $member->member->first_name, "course" => $calendar->name, "total" => 1, "date" => date("d/m", strtotime($calendar->from)));
  177. // }
  178. // } else {
  179. // if (array_key_exists($member->member->id, $this->records))
  180. // unset($this->records[$member->member->id]);
  181. // }
  182. // }
  183. // }
  184. // array_multisort(array_column($this->records, 'total'), SORT_DESC, $this->records);
  185. return view('livewire.absence_report');
  186. }
  187. protected function generateFiscalDateRanges($months, $days, $dayMap, $fromTime, $toTime)
  188. {
  189. $limit = now()->endOfDay();
  190. $today = $limit;
  191. $startYear = ($today->month >= $this->fiscalStartMonth) ? $today->year : $today->year - 1;
  192. $allowedDow = collect($days)
  193. ->map(fn($d) => $dayMap[$d] ?? null)
  194. ->filter(fn($v) => $v !== null)
  195. ->unique()
  196. ->values();
  197. $fromCarbon = Carbon::parse($fromTime);
  198. $toCarbon = Carbon::parse($toTime);
  199. $fromHour = $fromCarbon->hour;
  200. $fromMinute = $fromCarbon->minute;
  201. $fromSecond = $fromCarbon->second;
  202. $toHour = $toCarbon->hour;
  203. $toMinute = $toCarbon->minute;
  204. $toSecond = $toCarbon->second;
  205. $ranges = [];
  206. foreach ($months as $month) {
  207. $yearForMonth = ($month >= $this->fiscalStartMonth)
  208. ? $startYear
  209. : $startYear + 1;
  210. if (
  211. $yearForMonth > $limit->year ||
  212. ($yearForMonth === $limit->year && $month > $limit->month)
  213. ) {
  214. continue;
  215. }
  216. $firstOfMonth = Carbon::create($yearForMonth, $month, 1)->startOfDay();
  217. foreach ($allowedDow as $dow) {
  218. $offset = ($dow - $firstOfMonth->dayOfWeek + 7) % 7;
  219. $current = $firstOfMonth->copy()->addDays($offset);
  220. while ($current->month === $month) {
  221. $fromDateTime = $current->copy()->setTime($fromHour, $fromMinute, $fromSecond);
  222. $toDateTime = $current->copy()->setTime($toHour, $toMinute, $toSecond);
  223. if ($fromDateTime->gt($limit)) {
  224. break;
  225. }
  226. $ranges[] = [
  227. 'from' => $fromDateTime,
  228. 'to' => $toDateTime,
  229. ];
  230. $current->addWeek();
  231. }
  232. }
  233. }
  234. usort($ranges, fn($a, $b) => $a['from'] <=> $b['from']);
  235. return $ranges;
  236. }
  237. }