PresenceReport.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class PresenceReport extends Component
  5. {
  6. public $calendar;
  7. public $records;
  8. public $date;
  9. public $member_ids = [];
  10. public $courses = [];
  11. public $courts = [];
  12. public $instructors = [];
  13. public $motivations = [];
  14. public $court_filter;
  15. public $instructor_filter;
  16. public $motivation_filter;
  17. public $members = [];
  18. public $newMembers = [];
  19. public $ids = [];
  20. public $course_id;
  21. public $from;
  22. public $to;
  23. public $court_id;
  24. public $instructor_id;
  25. public function mount()
  26. {
  27. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  28. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  29. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  30. $this->from = "00:00:00";
  31. $this->to = "23:59:59";
  32. $this->date = date("Y-m-d");
  33. setlocale(LC_ALL, 'it_IT');
  34. }
  35. public function render()
  36. {
  37. setlocale(LC_ALL, 'it_IT');
  38. $this->records = [];
  39. $this->courses = [];
  40. $from = $this->date . " " . $this->from;
  41. $to = $this->date . " " . $this->to;
  42. $calendars = \App\Models\Calendar::where('from', '>=', $from)->where('from', '<=', $to)->orderBy('from')->get();
  43. foreach ($calendars as $calendar) {
  44. $presences = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', '<>', 99);
  45. if (!is_null($this->court_id)) {
  46. $presences = $presences->where('court_id', $this->court_id);
  47. }
  48. if (!is_null($this->instructor_id)) {
  49. $presences = $presences->where('instructor_id', $this->instructor_id);
  50. }
  51. $presences = $presences->pluck('member_id')->toArray();
  52. $presences_annullate = \App\Models\Presence::where('calendar_id', $calendar->id)->where('status', 99)->pluck('member_id')->toArray();
  53. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  54. $dow = date('w', strtotime($calendar->from));
  55. $d = $days[$dow];
  56. $h = date('H:i', strtotime($calendar->from));
  57. // Elenco corsi per tipologia in base al calendario
  58. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to);
  59. if (!is_null($this->course_id)) {
  60. $courses = $courses->where('id', $this->course_id);
  61. }
  62. $courses = $courses->pluck('id')->toArray();
  63. // Elenco utenti iscritti al corso "padre"
  64. // $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('course_id', $courses)->get();
  65. $members = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('member_id', $presences)->whereIn('course_id', $courses)->get();
  66. foreach ($members as $member) {
  67. $court = '';
  68. $instructor = '';
  69. $motivation = '';
  70. $presence = \App\Models\Presence::where('member_id', $member->member->id)->where('calendar_id', $calendar->id)->first();
  71. if ($presence) {
  72. $court = $presence->court ? $presence->court->name : "";
  73. $instructor = $presence->instructor ? $presence->instructor->name : "";
  74. $motivation = $presence->motivation ? $presence->motivation->name : "";
  75. }
  76. $status = '';
  77. if (in_array($member->member->id, $presences)) {
  78. $status = "<span class='fw-bold' style='color:#0c6197'>Presente</span>";
  79. } else {
  80. if (in_array($member->member->id, $presences_annullate)) {
  81. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  82. } else {
  83. if (date("Ymd") > date("Ymd", strtotime($calendar->from))) {
  84. $status = "<span class='fw-bold' style='color:red'>Assente</span>";
  85. }
  86. }
  87. }
  88. if ($calendar->status == 99) {
  89. $status = "<span class='fw-bold' style='color:gray'>Annullata</span>";
  90. }
  91. $this->records[$calendar->name][$h][] = array("last_name" => $member->member->last_name, "first_name" => $member->member->first_name, "court" => $court, "instructor" => $instructor, "status" => $status, 'motivation' => $motivation);
  92. }
  93. array_push($this->courses, $calendar->course_id);
  94. }
  95. $this->courses = array_unique($this->courses);
  96. $this->courses = array_map(function ($course_id) {
  97. try {
  98. return \App\Models\Course::findOrFail($course_id);
  99. } catch (\Throwable $e) {
  100. return null;
  101. }
  102. }, $this->courses);
  103. $this->courses = array_filter($this->courses);
  104. return view('livewire.presence_report');
  105. }
  106. public function prev()
  107. {
  108. $this->date = date("Y-m-d", strtotime("-1 day", strtotime($this->date)));
  109. }
  110. public function next()
  111. {
  112. $this->date = date("Y-m-d", strtotime("+1 day", strtotime($this->date)));
  113. }
  114. public function today()
  115. {
  116. $this->date = date("Y-m-d");
  117. }
  118. }