Просмотр исходного кода

modifica update dati presenza a database

ferrari 3 месяцев назад
Родитель
Сommit
7d05a253b3
1 измененных файлов с 50 добавлено и 10 удалено
  1. 50 10
      app/Http/Livewire/Presence.php

+ 50 - 10
app/Http/Livewire/Presence.php

@@ -85,7 +85,7 @@ class Presence extends Component
 
             // Elenco utenti iscritti al corso "padre"
             // $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('course_id', $courses)->pluck('member_id')->toArray();
-            $members_courses = \App\Models\MemberCourse::whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')",[$d, $h])->whereIn('course_id', $courses)->pluck('member_id')->toArray();
+            $members_courses = \App\Models\MemberCourse::whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])->whereIn('course_id', $courses)->pluck('member_id')->toArray();
 
             if ($this->filter != '') {
                 $filter = $this->filter;
@@ -236,31 +236,71 @@ class Presence extends Component
             $this->calendar->motivation_manual_id = $this->motivation_manual_id;
         $this->calendar->save();
 
-        $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
-        $mid = null;
-        if ($x) {
-            $mid = $x->motivation_id;
-            $x->delete();
-        }
+        // $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
+        // $mid = null;
+        // if ($x) {
+        //     $mid = $x->motivation_id;
+        //     $x->delete();
+        // }
+
+        // Recupero gli ID delle ultime presenze inserite per ogni membro
+        // (l'ID massimo per ogni member_id, filtrato per calendar_id, utente loggato e status diverso da 99)
+        $userId     = \Auth::user()->id;
+        $calendarId = $this->calendar->id;
+        $lastEditData = \App\Models\Presence::query()
+            ->select('member_id', 'motivation_id')
+            ->where('calendar_id', $calendarId)
+            ->where('user_id', $userId)
+            ->where('status', '<>', 99)
+            ->whereIn('id', function ($q) use ($calendarId, $userId) {
+                $q->selectRaw('MAX(id)')
+                    ->from('presences')
+                    ->where('calendar_id', $calendarId)
+                    ->where('user_id', $userId)
+                    ->where('status', '<>', 99)
+                    ->groupBy('member_id');
+            })
+            ->get()
+            ->keyBy('member_id')   // -> [member_id => (obj con motivation_id)]
+            ->map(fn($row) => $row->motivation_id)
+            ->toArray();
+
+        // Elimino tutti i dati correnti che devono essere sostituiti
+        \App\Models\Presence::query()
+            ->where('calendar_id', $calendarId)
+            ->where('user_id', $userId)
+            ->where('status', '<>', 99)
+            ->delete();
+
+        // Ricreo le presenze per ogni membro contenuto in $ids
         foreach ($ids as $id) {
             $p = new \App\Models\Presence();
             $p->member_id = $id;
-            $p->calendar_id = $this->calendar->id;
-            $p->motivation_id = $mid;
-            $p->user_id = \Auth::user()->id;
+            $p->calendar_id = $calendarId;
+
+            // Se per quel membro esisteva un motivation_id, lo riuso, altrimenti lo lascio null
+            $p->motivation_id = $lastEditData[$id] ?? null;
+            $p->user_id = $userId;
             $p->status = 0;
+
+            // Salvo eventuale court_id (se presente e maggiore di 0)
             if ($this->save_court_id > 0) {
                 $p->court_id = $this->save_court_id;
             }
+
+            // Salvo eventuale instructor_id (se presente e maggiore di 0)
             if ($this->save_instructor_id > 0) {
                 $p->instructor_id = $this->save_instructor_id;
             }
+
+            // Salvo eventuali note (se non vuote)
             if ($this->save_notes != '') {
                 $p->notes = $this->save_notes;
             }
 
             $p->save();
         }
+
         $this->emit('setSaving');
     }