Presence.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Presence extends Component
  5. {
  6. public $calendar;
  7. public $records;
  8. public $court_id, $instructor_id, $motivation_id, $note;
  9. public $courts = [];
  10. public $instructors = [];
  11. public $motivations = [];
  12. public $members = [];
  13. public $newMembers = [];
  14. public $ids = [];
  15. public function mount()
  16. {
  17. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  18. $this->court_id = $this->calendar->court_id;
  19. $this->instructor_id = $this->calendar->instructor_id;
  20. $this->note = $this->calendar->note;
  21. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  22. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  23. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->get();
  24. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  25. }
  26. public function render()
  27. {
  28. $this->records = [];
  29. // Carco tutti gli iscritti a quel corso
  30. $members_courses = \App\Models\MemberCourse::where('course_id', $this->calendar->course_id)->pluck('member_id')->toArray();
  31. $members = \App\Models\Member::whereIn('id', $members_courses)->get();
  32. $presenceMembers = [];
  33. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  34. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  35. foreach($members as $member)
  36. {
  37. $presenceMembers[] = $member->id;
  38. $this->records[] = $this->getMember($member);
  39. }
  40. // Aggiungo i membri iscritti
  41. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  42. $members = \App\Models\Member::whereIn('id', $members_presences)->get();
  43. foreach($members as $member)
  44. {
  45. $this->records[] = $this->getMember($member);
  46. }
  47. foreach($this->newMembers as $m)
  48. {
  49. $member = \App\Models\Member::findOrFail($m);
  50. $this->records[] = $this->getMember($member);
  51. }
  52. /*$calendars = \App\Models\Calendar::get();
  53. foreach($calendars as $c)
  54. {
  55. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  56. }*/
  57. return view('livewire.presence');
  58. }
  59. public function getMember($member)
  60. {
  61. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  62. ->orderBy('expire_date', 'desc')
  63. ->first();
  64. $certificate = '';
  65. if ($latestCert)
  66. {
  67. $latest_date = $latestCert->expire_date;
  68. $status = '';
  69. if ($latest_date < date("Y-m-d")) {
  70. $status = "0"; // Expired
  71. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  72. $status = "1"; // Expiring soon
  73. } else {
  74. $status = "2"; // Valid
  75. }
  76. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  77. }
  78. $presence = false;
  79. $my_presence = false;
  80. $status = 0;
  81. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  82. if ($has_presence)
  83. {
  84. $presence = true;
  85. $my_presence = $has_presence->user_id == \Auth::user()->id;
  86. $status = $has_presence->status;
  87. }
  88. return array('id' => $member->id, 'first_name' => $member->first_name, 'last_name' => $member->last_name, 'certificate' => $y, 'presence' => $presence, 'my_presence' => $my_presence, 'status' => $status);
  89. }
  90. public function save($ids)
  91. {
  92. $this->calendar->court_id = $this->court_id;
  93. $this->calendar->instructor_id = $this->instructor_id;
  94. $this->calendar->note = $this->note;
  95. if ($this->motivation_id != "" && $this->motivation_id != null)
  96. $this->calendar->motivation_id = $this->motivation_id;
  97. $this->calendar->save();
  98. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->delete();
  99. foreach($ids as $id)
  100. {
  101. $p = new \App\Models\Presence();
  102. $p->member_id = $id;
  103. $p->calendar_id = $this->calendar->id;
  104. $p->user_id = \Auth::user()->id;
  105. $p->status = 0;
  106. $p->save();
  107. }
  108. return redirect()->to('/calendar');
  109. }
  110. public function cancel($ids)
  111. {
  112. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('user_id', $ids)->get();
  113. foreach($presences as $presence)
  114. {
  115. $presence->status = 99;
  116. $presence->save();
  117. }
  118. }
  119. public function addMember($id)
  120. {
  121. if (!in_array($id, $this->newMembers))
  122. $this->newMembers[] = $id;
  123. $this->emit('reload');
  124. }
  125. public function cancelCalendar()
  126. {
  127. $this->calendar->motivation_id = $this->motivation_id;
  128. $this->calendar->status = 99;
  129. $this->calendar->save();
  130. return redirect()->to('/calendar');
  131. }
  132. }