瀏覽代碼

member - presence - riscritta logica per visualizzazione presenze

ferrari 1 月之前
父節點
當前提交
67708d04f4
共有 2 個文件被更改,包括 360 次插入369 次删除
  1. 305 322
      app/Http/Livewire/Member.php
  2. 55 47
      resources/views/livewire/member.blade.php

+ 305 - 322
app/Http/Livewire/Member.php

@@ -49,14 +49,14 @@ class Member extends Component
         if ($type === 'corsi' && $this->dataId > 0) {
             $this->loadMemberCards();
             $this->checkCourseAvailability();
-        }        
+        }
         if ($type === 'presenze') {
             //$this->emit('load-chart', $this->mesi, $this->valori);
             //if (sizeof($this->mesi) == 0 && sizeof($this->valori) == 0)
-                $this->loadMemberPresences();
+            $this->loadMemberPresences();
             //else
             //    $this->emit('load-chart', $this->mesi, $this->valori);
-        }        
+        }
         $this->dispatchBrowserEvent('scroll-to-top');
     }
     public function sortBy($field)
@@ -162,6 +162,9 @@ class Member extends Component
     public $presenceTitleFilter = '';
     public $member_presences = [];
 
+    public $presenceCourses = [];
+    public $presenceCourseFilter = '';
+
     public $totals = 0;
     public $presenze = 0;
     public $assenze = 0;
@@ -302,16 +305,6 @@ class Member extends Component
         $this->image_old = '';
     }
 
-    public function updatedPresenceYearFilter()
-    {
-        $this->loadMemberPresences();   
-    }
-
-    public function updatedPresenceTitleFilter()
-    {
-        $this->loadMemberPresences();   
-    }
-
     public function updatedCourseName()
     {
         $this->course_course_id = null;
@@ -383,7 +376,6 @@ class Member extends Component
                 $this->course_course_id = $course->id;
                 $this->course_price = formatPrice($course->price);
                 $this->course_subscription_price = formatPrice($course->subscription_price);
-
             } else {
                 $this->course_price = 0;
                 $this->course_subscription_price = 0;
@@ -530,8 +522,6 @@ class Member extends Component
             $this->festivita[] = $pasqua;     // Domenica di Pasqua
             $this->festivita[] = $pasquetta;  // Lunedì dell'Angelo
         }
-
-
     }
 
     public function updated()
@@ -650,10 +640,10 @@ class Member extends Component
 
 
         $datas = [];
-        
+
         $this->loadMemberCards();
         $this->loadMemberCourses();
-        
+
         $this->loadMemberCategories();
         $this->loadMemberCertificates();
 
@@ -686,12 +676,12 @@ class Member extends Component
         $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
 
         if ($this->dataId) {
-            $order = [9,10,11,12,1,2,3,4,5,6,7,8];
+            $order = [9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8];
 
-            $this->member_courses = $this->member_courses->map(function($course) use ($order) {
+            $this->member_courses = $this->member_courses->map(function ($course) use ($order) {
                 $months = json_decode($course->months, true);
 
-                usort($months, function($a, $b) use ($order) {
+                usort($months, function ($a, $b) use ($order) {
                     $posA = array_search($a['m'], $order);
                     $posB = array_search($b['m'], $order);
                     return $posA <=> $posB;
@@ -715,263 +705,266 @@ class Member extends Component
         // return view('livewire.member');
     }
 
-    public function loadMemberPresences()
+    public function updatedPresenceYearFilter()
+    {
+        $this->presenceCourseFilter = '';
+        $this->loadPresenceCombos();
+        $this->loadMemberPresences();
+    }
+
+    public function updatedPresenceCourseFilter()
+    {
+        $this->loadMemberPresences();
+    }
+
+    public function loadPresenceCombos()
     {
+        $this->presenceYears = [];
+        $this->presenceCourses = [];
 
-        $this->mesi = array('Settembre', 'Ottobre', 'Novembre', 'Dicembre', 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto');
-        $this->valori = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+        if (!$this->dataId) {
+            $this->presenceYearFilter = '';
+            $this->presenceCourseFilter = '';
+            return;
+        }
 
-        $this->loadCombo();
+        $subscribedCourseIds = \App\Models\MemberCourse::where('member_id', $this->dataId)
+            ->pluck('course_id')
+            ->all();
 
-        $this->member_presences = [];
+        $makeupCourseIds = \App\Models\Presence::where('member_id', $this->dataId)
+            ->where('status', '<>', 99)
+            ->whereNotNull('motivation_course_id')
+            ->pluck('motivation_course_id')
+            ->all();
 
-        // Carico tutti i calendar_id delle presenza
-        $presences = \App\Models\Presence::where('member_id', $this->dataId)->where('status', '<>', 99)->pluck('calendar_id')->toArray();
-        $presences_annullate = \App\Models\Presence::where('member_id', $this->dataId)->where('status', 99)->pluck('calendar_id')->toArray();
+        $allCourseIds = array_values(array_unique(array_merge($subscribedCourseIds, $makeupCourseIds)));
+
+        if (!empty($allCourseIds)) {
+            $this->presenceYears = \App\Models\Course::whereIn('id', $allCourseIds)
+                ->orderBy('year', 'desc')
+                ->groupBy('year')
+                ->pluck('year')
+                ->map(fn($y) => (string)$y)
+                ->all();
+        }
 
-        $course_ids = array();
-        
-        // Tutti i calendari
-        if ($this->presenceYearFilter == '' && sizeof($this->presenceYears) > 0)
+        if (($this->presenceYearFilter === '' || !in_array((string)$this->presenceYearFilter, $this->presenceYears, true)) && !empty($this->presenceYears)) {
             $this->presenceYearFilter = $this->presenceYears[0];
+        }
 
-        if ($this->presenceTitleFilter == '' && sizeof($this->presenceTitle) > 0)
-            $this->presenceTitleFilter = $this->presenceTitle[0];
+        if ($this->presenceYearFilter !== '' && !empty($allCourseIds)) {
+            $this->presenceCourses = \App\Models\Course::whereIn('id', $allCourseIds)
+                ->where('year', $this->presenceYearFilter)
+                ->orderBy('name')
+                ->get(['id', 'name', 'year', 'course_level_id', 'course_type_id', 'course_frequency_id']);
+        }
 
-        $curses_ids = \App\Models\Course::where('year', $this->presenceYearFilter)->pluck('id')->toArray();
+        $validCourseIds = collect($this->presenceCourses)->pluck('id')->map(fn($id) => (string)$id)->all();
+        if (($this->presenceCourseFilter === '' || !in_array((string)$this->presenceCourseFilter, $validCourseIds, true)) && !empty($validCourseIds)) {
+            $this->presenceCourseFilter = $validCourseIds[0];
+        }
+    }
 
-        $calendars = \App\Models\Calendar::whereNull('manual')->whereIn('course_id', $curses_ids)->where('name', $this->presenceTitleFilter)->orderBy('from')->get();
+    public function loadMemberPresences()
+    {
+        $this->mesi = ['Settembre', 'Ottobre', 'Novembre', 'Dicembre', 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto'];
+        $this->valori = array_fill(0, 12, 0);
+
+        $monthMap = [9 => 0, 10 => 1, 11 => 2, 12 => 3, 1 => 4, 2 => 5, 3 => 6, 4 => 7, 5 => 8, 6 => 9, 7 => 10, 8 => 11];
 
+        // reset output
+        $this->member_presences = [];
         $this->totals = 0;
         $this->presenze = 0;
         $this->assenze = 0;
         $this->annullate = 0;
         $this->recuperi = [];
 
-        define("PRESENZE", 0);
-        define("ASSENZE", 1);
-        // define("RECUPERO", 2);
-        define("ANNULLATE", 2);
+        $this->loadPresenceCombos();
+
+        if (!$this->dataId || $this->presenceYearFilter === '' || $this->presenceCourseFilter === '') {
+            $this->emit('load-chart', $this->mesi, $this->valori, []);
+            return;
+        }
+
+        $courseId = (int)$this->presenceCourseFilter;
 
         $chartData = [
-            PRESENZE => [
-                "label" => "Presenze",
-                "backgroundColor" => "#0c6197",
-                "data" => array_fill(0, 12, 0),
-                "grouped" => true,
-                "stack" => "chartData",
-                "barThickness" => "flex",
-                "barPercentage" => 0.5,
-                "categoryPercentage" => 0.3,
-            ],
-            ASSENZE => [
-                "label" => "Assenze",
-                "backgroundColor" => "#ff0000",
-                "data" => array_fill(0, 12, 0),
-                "grouped" => true,
-                "stack" => "chartData",
-                "barThickness" => "flex",
-                "barPercentage" => 0.5,
-                "categoryPercentage" => 0.3,
-                
-            ],
-            // RECUPERO => [
-            //     "label" => "Recupero",
-            //     "backgroundColor" => "#7136f6",
-            //     "data" => array_fill(0, 12, 0),
-            //     "grouped" => true,
-            //     "stack" => "chartData",
-            //     "barThickness" => "flex",
-            //     "barPercentage" => 0.5,
-            //     "categoryPercentage" => 0.3,
-            // ],
-            ANNULLATE => [
-                "label" => "Annullate",
-                "backgroundColor" => "#808080",
-                "data" => array_fill(0, 12, 0),
-                "grouped" => true,
-                "stack" => "chartData",
-                "barThickness" => "flex",
-                "barPercentage" => 0.5,
-                "categoryPercentage" => 0.3,
-                
-            ],
-        ];
-        $monthMap = [
-            9 => 0,
-            10 => 1,
-            11 => 2,
-            12 => 3,
-            1 => 4,
-            2 => 5,
-            3 => 6,
-            4 => 7,
-            5 => 8,
-            6 => 9,
-            7 => 10,
-            8 => 11,
+            0 => ["label" => "Presenze", "backgroundColor" => "#0c6197", "data" => array_fill(0, 12, 0), "grouped" => true, "stack" => "chartData", "barThickness" => "flex", "barPercentage" => 0.5, "categoryPercentage" => 0.3],
+            1 => ["label" => "Assenze", "backgroundColor" => "#ff0000", "data" => array_fill(0, 12, 0), "grouped" => true, "stack" => "chartData", "barThickness" => "flex", "barPercentage" => 0.5, "categoryPercentage" => 0.3],
+            2 => ["label" => "Annullate", "backgroundColor" => "#808080", "data" => array_fill(0, 12, 0), "grouped" => true, "stack" => "chartData", "barThickness" => "flex", "barPercentage" => 0.5, "categoryPercentage" => 0.3],
         ];
 
-        foreach($calendars as $calendar)
-        {
-            
-            $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
-            $dow = date('w', strtotime($calendar->from));
-            $d = $days[$dow];
+        $calendars = \App\Models\Calendar::whereNull('manual')
+            ->where('course_id', $courseId)
+            ->orderBy('from')
+            ->get(['id', 'from', 'to', 'status']);
+
+        $calendarIds = $calendars->pluck('id')->all();
+
+        $presencesByCalendar = [];
+        if (!empty($calendarIds)) {
+            $presencesByCalendar = \App\Models\Presence::where('member_id', $this->dataId)
+                ->whereIn('calendar_id', $calendarIds)
+                ->with('motivation:id,name,show_in_member_presences')
+                ->get(['calendar_id', 'status', 'motivation_id'])
+                ->keyBy('calendar_id')
+                ->all();
+        }
+
+        $memberCourse = \App\Models\MemberCourse::where('member_id', $this->dataId)
+            ->where('course_id', $courseId)
+            ->first();
+
+        $expected = null;
+
+        if ($memberCourse) {
+            $months = json_decode($memberCourse->months, true) ?: [];
+            $monthAllowed = [];
+            foreach ($months as $m) {
+                $mm = (int)($m['m'] ?? 0);
+                $st = (string)($m['status'] ?? '');
+                if ($mm >= 1 && $mm <= 12 && $st !== '2') {
+                    $monthAllowed[$mm] = true;
+                }
+            }
 
-            $h = date('H:i', strtotime($calendar->from));
+            $when = json_decode($memberCourse->when, true) ?: [];
+            $allowedSlots = [];
+            foreach ($when as $period) {
+                $days = $period['day'] ?? [];
+                $from = $period['from'] ?? null;
+                if (!$from || empty($days)) continue;
+                $hhmm = substr($from, 0, 5);
+                foreach ($days as $d) {
+                    $allowedSlots[$d . '|' . $hhmm] = true;
+                }
+            }
 
-            // Elenco corsi per tipologia in base al calendario
-            $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to)->pluck('id')->toArray();
+            $expected = [
+                'date_from' => $memberCourse->date_from,
+                'date_to'   => $memberCourse->date_to,
+                'monthAllowed' => $monthAllowed,
+                'allowedSlots' => $allowedSlots,
+            ];
+        }
 
-            $months = date("n", strtotime($calendar->from));
+        $isExpected = function (string $calendarFrom) use ($expected) {
+            if (!$expected) return false;
 
-            // Elenco utenti iscritti al corso "padre"
-            if (\App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')->whereDate('date_from', '<=', $calendar->from)->whereDate('date_to', '>=', $calendar->from)->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->where('member_id', $this->dataId)->first())
-            {
-                $status = '';
-                $annullata = false;
-                $motivation = null;
-                if (in_array($calendar->id, $presences))
-                {
-                    $status = "<span style=\"color:#0c6197\">Presenza</span>";
-                    $this->presenze += 1;
-                    $this->valori[$monthMap[date('n', strtotime($calendar->from))]] += 1;
-                    $chartData[PRESENZE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
-
-                    $presence = \App\Models\Presence::where('member_id', $this->dataId)->where('status', '<>', 99)->where('calendar_id', $calendar->id)->first();
-                    if ($presence->motivation && $presence->motivation->show_in_member_presences) {
-                        $motivation = $presence->motivation->name;
-                        if (!isset($this->recuperi[$motivation])) $this->recuperi[$motivation] = 0;
+            if ($calendarFrom < $expected['date_from'] || $calendarFrom > $expected['date_to']) return false;
 
-                        $this->recuperi[$motivation] += 1;
-                    }
-                }
-                else
-                {
-                    if (in_array($calendar->id, $presences_annullate))
-                    {
-                        $status = "<span style=\"color:gray\">Annullata</span>";
-                        $this->annullate += 1;
-
-                        $chartData[ANNULLATE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
-                        $annullata = true;
-                    }
-                    else
-                    {
-                        if (date("Ymd") > date("Ymd", strtotime($calendar->from)))
-                        {
-                            $status = "<span style=\"color:red\">Assenza</span>";
-                            $this->assenze += 1;
+            $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
+            $dow = (int)date('w', strtotime($calendarFrom));
+            $d = $days[$dow];
+            $h = date('H:i', strtotime($calendarFrom));
+            $m = (int)date('n', strtotime($calendarFrom));
 
-                            $chartData[ASSENZE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
-                        }
-                    }
-                }
+            if (empty($expected['monthAllowed'][$m])) return false;
+            if (empty($expected['allowedSlots'][$d . '|' . $h])) return false;
 
-                if ($calendar->status == 99 && !$annullata)
-                {
-                    $status = "<span style=\"color:gray\">Annullata</span>";
-                    $this->annullate += 1;
+            return true;
+        };
 
-                    $chartData[ANNULLATE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
-                }
-                
-                $this->member_presences[] = array('calendar_id' => $calendar->id, 'from' => $calendar->from, 'to' => $calendar->to, 'status' => $status, 'motivation' => $motivation);//\App\Models\Presence::where('member_id', $this->dataId)->get();
-            }
+        if ($expected) {
+            foreach ($calendars as $calendar) {
+                if (!$isExpected($calendar->from)) continue;
 
-            //$courses = array(1);
-            foreach($courses as $c)
-            {
-                $course_ids[] = $c;    
-            }
+                $idxMonth = $monthMap[(int)date('n', strtotime($calendar->from))] ?? null;
 
-            //$course_ids = array_push($course_ids, $courses);
+                $presence = $presencesByCalendar[$calendar->id] ?? null;
 
-        }
+                $calendarCancelled = ((int)$calendar->status === 99);
+                $presenceCancelled = ($presence && (int)$presence->status === 99);
+
+                $statusHtml = '';
+                $motivationName = null;
 
-        // Manuali (recuperi)
-        // $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('name', $this->presenceTitleFilter)->pluck('id')->toArray();
-        $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('name', $this->presenceTitleFilter)->pluck('id')->toArray();
-        $presences_recuperi = \App\Models\Presence::whereIn('calendar_id', $calendar_recuperi)->where('member_id', $this->dataId)->get();
-        foreach($presences_recuperi as $p)
-        {
-            if ($p->motivation && $p->motivation->show_in_member_presences) {
-                // $this->member_presences[] = array('calendar_id' => $p->calendar->id, 'from' => $p->calendar->from, 'to' => $p->calendar->to, 'status' => '<span style="color:#7136f6">Recupero</span>', 'motivation' => $p->motivation->name);//\App\Models\Presence::where('member_id', $this->dataId)->get();
-                $this->member_presences[] = array('calendar_id' => $p->calendar->id, 'from' => $p->calendar->from, 'to' => $p->calendar->to, 'status' => '<span style=\"color:#0c6197\">Presenza</span>', 'motivation' => $p->motivation->name);//\App\Models\Presence::where('member_id', $this->dataId)->get();
+                if ($calendarCancelled || $presenceCancelled) {
+                    $statusHtml = "<span style=\"color:gray\">Annullata</span>";
+                    $this->annullate++;
+                    if ($idxMonth !== null) $chartData[2]['data'][$idxMonth]++;
+                } elseif ($presence) {
+                    $statusHtml = "<span style=\"color:#0c6197\">Presenza</span>";
+                    $this->presenze++;
+                    if ($idxMonth !== null) {
+                        $this->valori[$idxMonth]++;
+                        $chartData[0]['data'][$idxMonth]++;
+                    }
 
-                if (!isset($this->recuperi[$p->motivation->name])) $this->recuperi[$p->motivation->name] = 0;
+                    if ($presence->motivation && $presence->motivation->show_in_member_presences) {
+                        $motivationName = ucfirst($presence->motivation->name);
+                        $this->recuperi[$motivationName] = ($this->recuperi[$motivationName] ?? 0) + 1;
+                    }
+                } else {
+                    if (date('Ymd') > date('Ymd', strtotime($calendar->from))) {
+                        $statusHtml = "<span style=\"color:red\">Assenza</span>";
+                        $this->assenze++;
+                        if ($idxMonth !== null) $chartData[1]['data'][$idxMonth]++;
+                    } else {
+                        $statusHtml = "";
+                    }
+                }
 
-                $this->recuperi[$p->motivation->name] += 1;
-                $this->presenze += 1;
-                
-                // $chartData[RECUPERO]["data"][$monthMap[date('n', strtotime($p->calendar->from))]] += 1;
-                $chartData[PRESENZE]["data"][$monthMap[date('n', strtotime($p->calendar->from))]] += 1;
+                $this->member_presences[] = [
+                    'calendar_id' => $calendar->id,
+                    'from' => $calendar->from,
+                    'to'   => $calendar->to,
+                    'status' => $statusHtml,
+                    'motivation' => $motivationName,
+                ];
             }
         }
 
-        $sortVariable='from';
-        usort(
-            $this->member_presences, 
-            fn(array $a, array $b): int => $a[$sortVariable] <=> $b[$sortVariable]
-        );
+        $makeups = \App\Models\Presence::where('member_id', $this->dataId)
+            ->where('status', '<>', 99)
+            ->where('motivation_course_id', $courseId)
+            ->with([
+                'calendar:id,from,to,status',
+                'motivation:id,name,show_in_member_presences',
+            ])
+            ->get(['calendar_id', 'status', 'motivation_id', 'motivation_course_id']);
 
-        
-        $this->emit('load-chart', $this->mesi, $this->valori, $chartData);
+        $alreadyCalendar = array_flip(array_column($this->member_presences, 'calendar_id'));
 
-        //usort($this->member_presences, $this->cmp);
-        //usort($this->member_presences, function ($a, $b) { return $a['from'] > $b['from']; });
-        
-        //$this->presenceYears = \App\Models\Course::whereIn('id', $course_ids)->orderBy('year')->groupBy('year')->pluck('year')->toArray();
-        //if (sizeof($this->presenceYears) > 0)
-        //    $this->presenceYearFilter = $this->presenceYears[0];
-        //$this->presenceTitle = \App\Models\Course::whereIn('id', $course_ids)->orderBy('name')->groupBy('name')->pluck('name')->toArray();
+        foreach ($makeups as $p) {
+            if (!$p->calendar) continue;
 
-        
-        // return view('livewire.member');
-    }
+            if (isset($alreadyCalendar[$p->calendar_id])) continue;
 
-    public function loadCombo()
-    {
-        $course_ids = array();
-        
-        //$calendars = \App\Models\Calendar::whereNull('manual')->orderBy('from')->get();
+            if (!$p->motivation || !$p->motivation->show_in_member_presences) continue;
 
-        /*
-        foreach($calendars as $calendar)
-        {
-  
-            $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
-            $dow = date('w', strtotime($calendar->from));
-            $d = $days[$dow];
+            $idxMonth = $monthMap[(int)date('n', strtotime($p->calendar->from))] ?? null;
 
-            // Elenco corsi per tipologia in base al calendario
-            $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to)->pluck('id')->toArray();
+            if ((int)$p->calendar->status === 99) {
+                $statusHtml = "<span style=\"color:gray\">Annullata</span>";
+                $this->annullate++;
+                if ($idxMonth !== null) $chartData[2]['data'][$idxMonth]++;
+            } else {
+                $statusHtml = "<span style=\"color:#0c6197\">Presenza</span>";
+                $this->presenze++;
+                if ($idxMonth !== null) {
+                    $this->valori[$idxMonth]++;
+                    $chartData[0]['data'][$idxMonth]++;
+                }
 
-            //$courses = array(1);
-            foreach($courses as $c)
-            {
-                $course_ids[] = $c;    
+                $mot = ucfirst($p->motivation->name);
+                $this->recuperi[$mot] = ($this->recuperi[$mot] ?? 0) + 1;
             }
 
-            //$course_ids = array_push($course_ids, $courses);
-
+            $this->member_presences[] = [
+                'calendar_id' => $p->calendar->id,
+                'from' => $p->calendar->from,
+                'to'   => $p->calendar->to,
+                'status' => $statusHtml,
+                'motivation' => $p->motivation->name,
+            ];
         }
-        */
-        $course_ids = \App\Models\MemberCourse::where('member_id', $this->dataId)->pluck('course_id')->toArray();
-        
-        // Manuali (recuperi)
-        $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->pluck('name')->toArray();
-        
-        // $this->presenceYears = \App\Models\Course::where('created_at', '>', '2025-08-01')->whereIn('id', $course_ids)->orderBy('year')->groupBy('year')->pluck('year')->toArray();
-        $this->presenceYears = \App\Models\Course::whereIn('id', $course_ids)->orderBy('year')->groupBy('year')->pluck('year')->toArray();
-        $this->presenceTitle = \App\Models\Course::whereIn('id', $course_ids)->orderBy('name')->groupBy('name')->pluck('name')->toArray();
 
-        foreach($calendar_recuperi as $cr)
-        {
-            $this->presenceTitle[] = $cr;
-        }
+        usort($this->member_presences, fn($a, $b) => strcmp($a['from'], $b['from']));
 
+        $this->emit('load-chart', $this->mesi, $this->valori, $chartData);
     }
 
     public function showDetailF($id)
@@ -1113,7 +1106,7 @@ class Member extends Component
             $to_complete = false;
             try {
                 $this->validate($std_rules);
-            } catch(\Illuminate\Validation\ValidationException $e) {
+            } catch (\Illuminate\Validation\ValidationException $e) {
                 $to_complete = true;
             }
 
@@ -1173,9 +1166,9 @@ class Member extends Component
                 $this->emit('saved-and-continue', $this->type);
                 $this->dispatchBrowserEvent('scroll-to-top');
                 if ($this->birth_nation_id != null)
-                    $this->isBirthItaly = \App\Models\Nation::findOrFail($this->birth_nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
+                    $this->isBirthItaly = \App\Models\Nation::findOrFail($this->birth_nation_id)->first()->is_italy; // $this->nation_id;//$this->checkIsItaly();
                 if ($this->nation_id != null)
-                    $this->isItaly = \App\Models\Nation::findOrFail($this->nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
+                    $this->isItaly = \App\Models\Nation::findOrFail($this->nation_id)->first()->is_italy; // $this->nation_id;//$this->checkIsItaly();
             }
             $this->emit('setEdit', false);
         } catch (\Exception $ex) {
@@ -1388,7 +1381,7 @@ class Member extends Component
             $to_complete = false;
             try {
                 $this->validate($std_rules);
-            } catch(\Illuminate\Validation\ValidationException $e) {
+            } catch (\Illuminate\Validation\ValidationException $e) {
                 $to_complete = true;
             }
 
@@ -1444,13 +1437,13 @@ class Member extends Component
                 //$this->resetFields();
                 //$this->update = false;
             } else {
-                
+
                 $this->emit('saved-and-continue', $this->type);
                 $this->dispatchBrowserEvent('scroll-to-top');
                 if ($this->birth_nation_id != null)
-                    $this->isBirthItaly = \App\Models\Nation::findOrFail($this->birth_nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
+                    $this->isBirthItaly = \App\Models\Nation::findOrFail($this->birth_nation_id)->first()->is_italy; // $this->nation_id;//$this->checkIsItaly();
                 if ($this->nation_id != null)
-                    $this->isItaly = \App\Models\Nation::findOrFail($this->nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
+                    $this->isItaly = \App\Models\Nation::findOrFail($this->nation_id)->first()->is_italy; // $this->nation_id;//$this->checkIsItaly();
             }
             $this->emit('setEdit', false);
         } catch (\Exception $ex) {
@@ -1770,83 +1763,76 @@ class Member extends Component
                 $course_name = $c->name;
             }
 
-            $days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
+            $days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
 
             //foreach($this->course_months as $m)
             //{
 
-                $from = date("Y-m-d", strtotime($c->date_from));
-                $to = date("Y-m-d", strtotime($c->date_to));
-
-                $endDate = strtotime($to);
-
-                foreach($this->course_when as $d)
-                {
-
-                    foreach($d["day"] as $dd)
-                    {
-
-                        $day = '';
-                        switch ($dd) {
-                            case 'lun':
-                                $day = $days[0];
-                                break;
-                            case 'mar':
-                                $day = $days[1];
-                                break;
-                            case 'mer':
-                                $day = $days[2];
-                                break;
-                            case 'gio':
-                                $day = $days[3];
-                                break;
-                            case 'ven':
-                                $day = $days[4];
-                                break;
-                            case 'sab':
-                                $day = $days[5];
-                                break;
-                            case 'dom':
-                                $day = $days[6];
-                                break;
-                            default:
-                                $day = '';
-                                break;
-                        }
-                        
-                        if ($day != '')
-                        {
-                            for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
-                            {
-
-                                // Controllo che non esiste un corso così
-                                $exist = \App\Models\Calendar::where('from', date('Y-m-d ' . $d["from"] . ":00", $i))->where('to', date('Y-m-d ' . $d["to"] . ":00", $i))->where('name', $course_name)->first();
-
-                                if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita))
-                                {
-
-                                    // Creo il calendario del corso
-                                    $calendar = new \App\Models\Calendar();
-                                    $calendar->course_id = $this->course_course_id;
-                                    $calendar->court_id = null;
-                                    $calendar->name = $course_name;
-                                    $calendar->course_type_id = null;
-                                    $calendar->course_duration_id = null;
-                                    $calendar->course_frequency_id = null;
-                                    $calendar->course_level_id = null;
-                                    $calendar->instructor_id = null;
-                                    $calendar->from = date('Y-m-d ' . $d["from"] . ":00", $i);
-                                    $calendar->to = date('Y-m-d ' . $d["to"] . ":00", $i);
-                                    $calendar->note = '';
-                                    $calendar->status = 0;
-                                    $calendar->save();
+            $from = date("Y-m-d", strtotime($c->date_from));
+            $to = date("Y-m-d", strtotime($c->date_to));
 
-                                }
-                                
+            $endDate = strtotime($to);
+
+            foreach ($this->course_when as $d) {
+
+                foreach ($d["day"] as $dd) {
+
+                    $day = '';
+                    switch ($dd) {
+                        case 'lun':
+                            $day = $days[0];
+                            break;
+                        case 'mar':
+                            $day = $days[1];
+                            break;
+                        case 'mer':
+                            $day = $days[2];
+                            break;
+                        case 'gio':
+                            $day = $days[3];
+                            break;
+                        case 'ven':
+                            $day = $days[4];
+                            break;
+                        case 'sab':
+                            $day = $days[5];
+                            break;
+                        case 'dom':
+                            $day = $days[6];
+                            break;
+                        default:
+                            $day = '';
+                            break;
+                    }
+
+                    if ($day != '') {
+                        for ($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i)) {
+
+                            // Controllo che non esiste un corso così
+                            $exist = \App\Models\Calendar::where('from', date('Y-m-d ' . $d["from"] . ":00", $i))->where('to', date('Y-m-d ' . $d["to"] . ":00", $i))->where('name', $course_name)->first();
+
+                            if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita)) {
+
+                                // Creo il calendario del corso
+                                $calendar = new \App\Models\Calendar();
+                                $calendar->course_id = $this->course_course_id;
+                                $calendar->court_id = null;
+                                $calendar->name = $course_name;
+                                $calendar->course_type_id = null;
+                                $calendar->course_duration_id = null;
+                                $calendar->course_frequency_id = null;
+                                $calendar->course_level_id = null;
+                                $calendar->instructor_id = null;
+                                $calendar->from = date('Y-m-d ' . $d["from"] . ":00", $i);
+                                $calendar->to = date('Y-m-d ' . $d["to"] . ":00", $i);
+                                $calendar->note = '';
+                                $calendar->status = 0;
+                                $calendar->save();
                             }
                         }
                     }
                 }
+            }
             //}
 
             session()->flash('success, Corso creato');
@@ -1912,7 +1898,7 @@ class Member extends Component
                 }
                 $this->courseDataId = $memberCourse->id;
 
-                
+
                 $this->updateCourse = true;
                 $this->addCourse = false;
                 $this->emit('setEditCorso', true);
@@ -1957,7 +1943,7 @@ class Member extends Component
                 $course_name = $c->name;
             }
 
-            $days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
+            $days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
 
             //foreach($this->course_months as $m)
             //{
@@ -1967,11 +1953,9 @@ class Member extends Component
 
             $endDate = strtotime($to);
 
-            foreach($this->course_when as $d)
-            {
+            foreach ($this->course_when as $d) {
 
-                foreach($d["day"] as $dd)
-                {
+                foreach ($d["day"] as $dd) {
 
                     $day = '';
                     switch ($dd) {
@@ -2000,17 +1984,14 @@ class Member extends Component
                             $day = '';
                             break;
                     }
-                    
-                    if ($day != '')
-                    {
-                        for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
-                        {
+
+                    if ($day != '') {
+                        for ($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i)) {
 
                             // Controllo che non esiste un corso così
                             $exist = \App\Models\Calendar::where('from', date('Y-m-d ' . $d["from"] . ":00", $i))->where('to', date('Y-m-d ' . $d["to"] . ":00", $i))->where('name', $course_name)->first();
 
-                            if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita))
-                            {
+                            if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita)) {
 
                                 // Creo il calendario del corso
                                 $calendar = new \App\Models\Calendar();
@@ -2027,9 +2008,7 @@ class Member extends Component
                                 $calendar->note = '';
                                 $calendar->status = 0;
                                 $calendar->save();
-
                             }
-                            
                         }
                     }
                 }
@@ -2327,7 +2306,9 @@ class Member extends Component
             $this->course_months[] = array("m" => $m, "status" => "");
         }
 
-        $rank = function (int $mm) { return ($mm - 9 + 12) % 12; };
+        $rank = function (int $mm) {
+            return ($mm - 9 + 12) % 12;
+        };
         usort($this->course_months, function ($a, $b) use ($rank) {
             return $rank((int)$a['m']) <=> $rank((int)$b['m']);
         });
@@ -2348,7 +2329,8 @@ class Member extends Component
 
                 $this->course_date_from = $start->format('Y-m-d');
                 $this->course_date_to   = $end->format('Y-m-d');
-            } catch (\Throwable $e) {}
+            } catch (\Throwable $e) {
+            }
         }
     }
 
@@ -2371,7 +2353,7 @@ class Member extends Component
                         if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
                             $tot = sizeof(json_decode($row->when));
                             // foreach (json_decode($row->when) as $m) {
-                                $price -= $row->amount / $tot;
+                            $price -= $row->amount / $tot;
                             // }
                         }
                     }
@@ -2634,7 +2616,8 @@ class Member extends Component
         return $ret;
     }
 
-    public function archiveMultiple($ids) {
+    public function archiveMultiple($ids)
+    {
         $success = true;
         foreach ($ids as $id) {
             $success = $this->archive($id, true);

+ 55 - 47
resources/views/livewire/member.blade.php

@@ -1133,86 +1133,94 @@
                                 @if($type == 'presenze')
 
                                     <div class="form--wrapper">
-                                        <div class="row ">
+                                        <div class="row">
                                             <div class="col-md-6">
                                                 <select class="form-control" wire:model="presenceYearFilter">
+                                                    <option value="">-- Stagione --</option>
                                                     @foreach($presenceYears as $y)
                                                         <option value="{{$y}}">{{$y}}</option>
                                                     @endforeach
                                                 </select>
                                             </div>
+
                                             <div class="col-md-6">
-                                                <select class="form-control"  wire:model="presenceTitleFilter">                                                    
-                                                    @foreach($presenceTitle as $t)
-                                                        <option value="{{$t}}">{{$t}}</option>
+                                                <select class="form-control" wire:model="presenceCourseFilter" @if(empty($presenceCourses)) disabled @endif>
+                                                    <option value="">-- Corso --</option>
+                                                    @foreach($presenceCourses as $c)
+                                                        <option value="{{$c->id}}">
+                                                            {{$c->getDetailsName()}}
+                                                        </option>
                                                     @endforeach
                                                 </select>
                                             </div>
                                         </div>
+
                                         <br>
+
                                         <div class="row align-items-center">
-                                            <div class="col-md-3">
+                                            <div class="col">
                                                 <div class="box-presenze">
-                                                    Presenze<br>
-                                                    {{$presenze}}
-                                                </div>                                                
+                                                    <span><b>Presenze</b>: {{ $presenze }}</span>
+                                                </div>
                                             </div>
-                                            <div class="col-md-3">
+                                            <div class="col">
                                                 <div class="box-assenze">
-                                                    Assenze<br>
-                                                    {{$assenze}}
+                                                    <span><b>Assenze</b>: {{ $assenze }}</span>
                                                 </div>
                                             </div>
-                                            <div class="col-md-3" style="border-right: 1px solid gray">
+                                            <div class="col"@if (!empty($recuperi)) style="border-right: 1px solid gray"@endif>
                                                 <div class="box-annullate">
-                                                    Annullate<br>
-                                                    {{$annullate}}
+                                                    <span><b>Annullate</b>: {{ $annullate }}</span>
                                                 </div>
                                             </div>
-                                            <div class="col-md-3">
-                                                <div class="row">
-                                                    {{-- <div class="col-12"><div class="box-recupero">Recupero {{$recuperi}}</div></div>
-                                                    <div class="col-12"><div class="box-recupero">Recupero {{$recuperi}}</div></div> --}}
-                                                    @foreach ($recuperi as $name => $count)
-                                                    <div class="col-12"><div class="box-presenze">{!!$name!!} {{$count}}</div></div>
-                                                    @endforeach
-                                                    {{-- <div class="col-12"><div class="box-presenze">Recupero {{$recuperi}}</div></div> --}}
+                                            @if (!empty($recuperi))
+                                                <div class="col">
+                                                    <div class="box-presenze text-start">
+                                                        @foreach ($recuperi as $name => $count)
+                                                            <span><b>{!! $name !!}</b>: {{ $count }}</span><br>
+                                                        @endforeach
+                                                    </div>
                                                 </div>
-                                            </div>
-                                        </div>                                        
-                                        <div class="row ">
+                                            @endif
+                                        </div>
+
+                                        <div class="row">
                                             <div class="col-md-12">
                                                 <div class="presenzechart-wrapper">
                                                     <canvas id="presenzeChart" style="padding:20px"></canvas>
-                                                    <button class="btn--ui download-png" onclick="downloadChart('presenzeChart')" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Scarica grafico"><i class="fas fa-download"></i></button>
+                                                    <button class="btn--ui download-png" onclick="downloadChart('presenzeChart')" data-bs-toggle="popover"
+                                                        data-bs-trigger="hover focus" data-bs-placement="bottom" data-bs-content="Scarica grafico">
+                                                        <i class="fas fa-download"></i>
+                                                    </button>
                                                 </div>
                                             </div>
                                         </div>
+
                                         @if (!empty($member_presences))
-                                        <div class="row " style="height: 300px; overflow:auto; ">
-                                            <div class="col-md-12">
-                                                <table class="table tablesaw tableHead tablesaw-stack tabella--presenze" id="tablesaw-350-2" style="min-width:700px">
-                                                    <thead>
-                                                        <tr>
-                                                            <th>Data</th>
-                                                            <th>Orario</th>
-                                                            <th>Stato</th>
-                                                            <th>Motivazione</th>
-                                                        </tr>                                                        
-                                                    </thead>
-                                                    <tbody>
-                                                        @foreach($member_presences as $mp)
+                                            <div class="row" style="height: 300px; overflow:auto;">
+                                                <div class="col-md-12">
+                                                    <table class="table tablesaw tableHead tablesaw-stack tabella--presenze" style="min-width:700px">
+                                                        <thead>
                                                             <tr>
-                                                                <td>{{date("d/m/Y", strtotime($mp["from"]))}}</td>
-                                                                <td>{{date("H:i", strtotime($mp["from"]))}} - {{date("H:i", strtotime($mp["to"]))}}</td>
-                                                                <td>{!!$mp["status"]!!}</td>
-                                                                <td>{!!$mp["motivation"]!!}</td>
+                                                                <th style="width: 20%">Data</th>
+                                                                <th style="width: 20%">Orario</th>
+                                                                <th style="width: 15%">Stato</th>
+                                                                <th style="width: 45%">Motivazione</th>
                                                             </tr>
-                                                        @endforeach
-                                                    </tbody>
-                                                </table>
+                                                        </thead>
+                                                        <tbody>
+                                                            @foreach($member_presences as $mp)
+                                                                <tr>
+                                                                    <td>{{ date("d/m/Y", strtotime($mp["from"])) }}</td>
+                                                                    <td>{{ date("H:i", strtotime($mp["from"])) }} - {{ date("H:i", strtotime($mp["to"])) }}</td>
+                                                                    <td>{!! $mp["status"] !!}</td>
+                                                                    <td>{!! $mp["motivation"] !!}</td>
+                                                                </tr>
+                                                            @endforeach
+                                                        </tbody>
+                                                    </table>
+                                                </div>
                                             </div>
-                                        </div>
                                         @endif
                                     </div>