AbsenceReport.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 . "%")
  31. ->where('when', 'like', '%"from":"' . $h . '"%')
  32. ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
  33. ->whereDate('date_from', '<=', $calendar->from)
  34. ->whereDate('date_to', '>=', $calendar->from)
  35. ->whereIn('course_id', $courses)->get();
  36. //$members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  37. foreach ($members as $member) {
  38. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  39. if (!in_array($member->member->id, $presences))
  40. {
  41. if (!in_array($member->member->id, $presences_annullate))
  42. {
  43. if (array_key_exists($member->member->id, $this->records))
  44. {
  45. $this->records[$member->member->id]['total'] += 1;
  46. $this->records[$member->member->id]['date'] .= ", " . date("d/m/Y", strtotime($calendar->from));
  47. }
  48. else
  49. $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)));
  50. }
  51. }
  52. }
  53. }
  54. array_multisort(array_column($this->records, 'total'), SORT_DESC, $this->records);
  55. return view('livewire.absence_report');
  56. }
  57. }