AbsenceReport.php 11 KB

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