AbsenceReport.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class AbsenceReport extends Component
  5. {
  6. public $records;
  7. public function mount()
  8. {
  9. setlocale(LC_ALL, 'it_IT');
  10. }
  11. public function render()
  12. {
  13. setlocale(LC_ALL, 'it_IT');
  14. $this->records = [];
  15. $to = date("Y-m-d 23:59:59");
  16. $calendars = \App\Models\Calendar::where('from', '<=', $to)->orderBy('from')->get();
  17. foreach ($calendars as $calendar) {
  18. $presences = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', '<>', 99);
  19. $presences = $presences->pluck('member_id')->toArray();
  20. $presences_annullate = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', 99)->pluck('member_id')->toArray();
  21. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  22. $dow = date('w', strtotime($calendar->from));
  23. $d = $days[$dow];
  24. $h = date('H:i', strtotime($calendar->from));
  25. // Elenco corsi per tipologia in base al calendario
  26. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to);
  27. $courses = $courses->pluck('id')->toArray();
  28. $months = date("n", strtotime($calendar->from));
  29. // Elenco utenti iscritti al corso "padre"
  30. $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->get();
  31. //$members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  32. foreach ($members as $member) {
  33. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  34. if (!in_array($member->member->id, $presences))
  35. {
  36. if (!in_array($member->member->id, $presences_annullate))
  37. {
  38. if (array_key_exists($member->member->id, $this->records))
  39. {
  40. $this->records[$member->member->id]['total'] += 1;
  41. $this->records[$member->member->id]['date'] .= ", " . date("d/m/Y", strtotime($calendar->from));
  42. }
  43. else
  44. $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/Y", strtotime($calendar->from)));
  45. }
  46. }
  47. }
  48. }
  49. array_multisort(array_column($this->records, 'total'), SORT_DESC, $this->records);
  50. return view('livewire.absence_report');
  51. }
  52. }