AbsenceReport.php 11 KB

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