Przeglądaj źródła

check stato tesseameto e certificato per corsi

FabioFratini 7 miesięcy temu
rodzic
commit
cc35914fd7

+ 49 - 40
app/Http/Livewire/Member.php

@@ -31,6 +31,7 @@ class Member extends Component
 
     public $selectedCourseMember = 0;
 
+    public $hasCertificate = false;
     protected $messages = [
         'birth_nation_id.required' => 'birth_nation_id',
         'birth_province_id.required' => 'birth_province_id',
@@ -44,6 +45,13 @@ class Member extends Component
     public function change($type)
     {
         $this->type = $type;
+            if ($type === 'corsi' && $this->dataId > 0) {
+            $this->loadMemberCards();
+            $this->loadMemberCertificates();
+            $this->checkCourseAvailability();
+        }
+
+        $this->dispatchBrowserEvent('scroll-to-top');
     }
 
     public function sortBy($field)
@@ -1509,6 +1517,8 @@ class Member extends Component
             session()->flash('success, Tesserato creato');
             $this->resetCardFields();
             $this->addCard = false;
+            $this->loadMemberCards();
+            $this->checkCourseAvailability();
         } catch (\Exception $ex) {
             session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
@@ -1543,7 +1553,7 @@ class Member extends Component
         $this->validate(['card_card_id' => 'required']);
 
         try {
-            \Illuminate\Support\Facades\Log::info('Starting card update', [
+            Log::info('Starting card update', [
                 'member_id' => $this->dataId,
                 'card_id' => $this->card_card_id,
                 'card_number' => $this->card_number
@@ -1553,7 +1563,7 @@ class Member extends Component
             if ($this->card_date != '') {
                 $card = \App\Models\Card::findOrFail($this->card_card_id);
 
-                \Illuminate\Support\Facades\Log::info('Card details', [
+                Log::info('Card details', [
                     'card_id' => $card->id,
                     'next_day_expire' => $card->next_day_expire,
                     'next_month_expire' => $card->next_month_expire,
@@ -1578,7 +1588,7 @@ class Member extends Component
                         $expire_date = $next_exp_obj->format('Y-m-d');
                     }
 
-                    \Illuminate\Support\Facades\Log::info('Calculated expiration date (next_day_expire/next_month_expire rule)', [
+                    Log::info('Calculated expiration date (next_day_expire/next_month_expire rule)', [
                         'input_date' => $this->card_date,
                         'next_exp' => $next_exp,
                         'expire_date' => $expire_date,
@@ -1588,7 +1598,7 @@ class Member extends Component
                     if ($card->one_year_expire) {
                         $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
 
-                        \Illuminate\Support\Facades\Log::info('Calculated expiration date (one_year_expire rule)', [
+                        Log::info('Calculated expiration date (one_year_expire rule)', [
                             'input_date' => $this->card_date,
                             'expire_date' => $expire_date
                         ]);
@@ -1596,7 +1606,7 @@ class Member extends Component
                 }
             }
 
-            \Illuminate\Support\Facades\Log::info('Updating member card', [
+            Log::info('Updating member card', [
                 'card_id' => $this->cardDataId,
                 'member_id' => $this->dataId,
                 'card_number' => $this->card_number,
@@ -1620,7 +1630,7 @@ class Member extends Component
 
             updateMemberData($this->dataId);
 
-            \Illuminate\Support\Facades\Log::info('Card updated successfully', [
+            Log::info('Card updated successfully', [
                 'card_id' => $this->cardDataId,
                 'member_id' => $this->dataId
             ]);
@@ -1628,8 +1638,10 @@ class Member extends Component
             session()->flash('success', 'Tesserato aggiornato');
             $this->resetCardFields();
             $this->updateCard = false;
+            $this->loadMemberCards();
+            $this->checkCourseAvailability();
         } catch (\Exception $ex) {
-            \Illuminate\Support\Facades\Log::error('Error updating card', [
+            Log::error('Error updating card', [
                 'card_id' => $this->cardDataId,
                 'member_id' => $this->dataId,
                 'error_message' => $ex->getMessage(),
@@ -1656,24 +1668,9 @@ class Member extends Component
         }
     }
 
-    // Courses
-
     public function addCourse()
     {
         $this->resetCourseFields();
-        /*if ($this->under18)
-        {
-            $this->course_months[] = array("m" => 9, "status" => "");
-            $this->course_months[] = array("m" => 10, "status" => "");
-            $this->course_months[] = array("m" => 11, "status" => "");
-            $this->course_months[] = array("m" => 12, "status" => "");
-            $this->course_months[] = array("m" => 1, "status" => "");
-            $this->course_months[] = array("m" => 2, "status" => "");
-            $this->course_months[] = array("m" => 3, "status" => "");
-            $this->course_months[] = array("m" => 4, "status" => "");
-            $this->course_months[] = array("m" => 5, "status" => "");
-            $this->course_months[] = array("m" => 6, "status" => "");
-        }*/
         $this->addCourse = true;
         $this->updateCourse = false;
         $this->emit('setEditCorso', true);
@@ -1729,24 +1726,6 @@ class Member extends Component
             $rate->status = 0;
             $rate->is_subscription = true;
             $rate->save();
-
-            // Se la data inizio pagamenti è "inizio corso" allora prendo quella come start date
-            /*if ($r->when_start == 'COURSE')
-                $start = $c->date_from;
-
-            //
-            $month_day = $r->month_day;
-            if ($month_day > 0)
-            {
-                if ($month_day >= 29)
-                    $start = date("Y-m-t", strtotime($start));
-                else
-                    $start = date('Y-m-d', strtotime('+' . ($month_day - 1) . ' days', strtotime('first day of ' . ((int)date('j', strtotime($start)) < $month_day ? 'this' : 'next' ) . ' month', strtotime($start))));
-            }
-
-            if ($r->months > 1)
-                $start = date('Y-m-' . ($month_day >= 29 ? 't' : 'd'), strtotime("+" . ($r->months - 1) . " months", strtotime($start)));
-            */
             $go = true;
             while ($go) {
 
@@ -1944,6 +1923,8 @@ class Member extends Component
             session()->flash('success', 'Certificato creato');
             $this->resetCertificateFields();
             $this->addCertificate = false;
+            $this->loadMemberCertificates();
+            $this->checkCourseAvailability();
         } catch (\Exception $ex) {
             session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
@@ -1991,6 +1972,8 @@ class Member extends Component
             session()->flash('success', 'Certificato aggiornato');
             $this->resetCertificateFields();
             $this->updateCertificate = false;
+            $this->loadMemberCertificates();
+            $this->checkCourseAvailability();
         } catch (\Exception $ex) {
             session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
@@ -2441,6 +2424,32 @@ class Member extends Component
         }
         return $ret;
     }
+
+    private function checkCourseAvailability()
+    {
+        $this->active = $this->getActiveStatus();
+        $this->hasCertificate = $this->checkHasCertificate();
+
+        $this->emit('course-availability-updated');
+    }
+
+    private function getActiveStatus()
+    {
+        if ($this->dataId > 0) {
+            $member = \App\Models\Member::find($this->dataId);
+            if ($member) {
+                return $member->isActive();
+            }
+        }
+        return ["status" => 0, "date" => null];
+    }
+    private function checkHasCertificate()
+    {
+        if ($this->dataId > 0) {
+            return $this->member_certificates->where('expire_date', '>=', date('Y-m-d'))->count() > 0;
+        }
+        return false;
+    }
 }
 
 class codicefiscale

+ 32 - 1
resources/views/livewire/member.blade.php

@@ -1002,6 +1002,19 @@
                                 @if($type == 'corsi')
                                     @if($dataId > 0)
                                         @if(!$addCourse && !$updateCourse)
+                                            @if($active["status"] != 1 && !$hasCertificate)
+                                                <div class="alert alert-warning" role="alert">
+                                                    <strong>Attenzione:</strong> Per aggiungere corsi è necessario avere un tesseramento attivo o un certificato medico valido.
+                                                </div>
+                                            @elseif($active["status"] != 1)
+                                                <div class="alert alert-info" role="alert">
+                                                    <strong>Info:</strong> Il tesseramento non è attivo, ma è presente un certificato medico valido.
+                                                </div>
+                                            @elseif(!$hasCertificate)
+                                                <div class="alert alert-info" role="alert">
+                                                    <strong>Info:</strong> Non è presente un certificato medico valido, ma il tesseramento è attivo.
+                                                </div>
+                                            @endif
                                             <table class="table tablesaw tableHead tablesaw-stack tabella--corsi" id="tablesaw-350-2" style="min-width:800px">
                                                 <tbody id="checkall-target">
                                                     @foreach($member_courses as $member_course)
@@ -1131,7 +1144,10 @@
                                             </table>
                                             @if($dataId > 0)
                                                 @if ($active["status"] == 1 || $hasCertificate)
-                                                    <button class="btn--ui primary"wire:click.prevent="addCourse()" style="max-width:200px">Aggiungi</button>
+                                                    <button class="btn--ui primary" wire:click.prevent="addCourse()" style="max-width:200px">Aggiungi</button>
+                                                @else
+                                                    <button class="btn--ui lightGrey" disabled style="max-width:200px">Aggiungi corso (non disponibile)</button>
+                                                    <small class="text-muted d-block mt-1">Completa prima il tesseramento o carica un certificato medico valido</small>
                                                 @endif
                                             @endif
                                         @else
@@ -2363,5 +2379,20 @@
                 }
             }, 200);
         });
+        Livewire.on('course-availability-updated', () => {
+            console.log('Course availability updated');
+            // You can add any additional UI updates here if needed
+        });
+
+        // Update the existing card/certificate save handlers
+        window.addEventListener('card-saved', event => {
+            console.log('Card saved, checking course availability');
+            // The course availability will be automatically updated by the Livewire component
+        });
+
+        window.addEventListener('certificate-saved', event => {
+            console.log('Certificate saved, checking course availability');
+            // The course availability will be automatically updated by the Livewire component
+        });
     </script>
 @endpush