AbsenceReport.php 3.7 KB

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