Преглед изворни кода

fix salva e chiudi e managing errore visibile

FabioFratini пре 9 месеци
родитељ
комит
8ca1affaa7
2 измењених фајлова са 289 додато и 82 уклоњено
  1. 227 82
      app/Http/Livewire/Member.php
  2. 62 0
      resources/views/livewire/member.blade.php

+ 227 - 82
app/Http/Livewire/Member.php

@@ -6,6 +6,7 @@ use Livewire\Component;
 use Livewire\Attributes\Url;
 use Livewire\WithFileUploads;
 use Livewire\WithPagination;
+use Illuminate\Support\Facades\Log;
 
 use DateTime;
 
@@ -126,12 +127,15 @@ class Member extends Component
     public $member_categories = array(), $category_category_id;
 
     // Courses data
-    public $member_courses = array(), $course_course_id, $course_date_from, $course_date_to, $course_when = array(), $addCourse, $updateCourse, $courseDataId, $course_course_subscription_id, $course_status, $course_note, $course_price, $course_subscription_price, $course_subscribed, $course_months = array(), $course_exist;
+    public $member_courses = array(), $course_course_id, $course_course_type, $course_date_from, $course_date_to, $course_when = array(), $addCourse, $updateCourse, $courseDataId, $course_course_subscription_id, $course_status, $course_note, $course_price, $course_subscription_price, $course_subscribed, $course_months = array(), $course_exist;
+
+    public $course_months_list = [];
+    public $course_price_list = [];
 
     public $course_name, $course_level_id, $course_type_id, $course_frequency_id;
 
     // Certificates data
-    public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
+    public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId, $cardCertificateId;
 
     public $filterCard = [];
     public $filterCategory = [];
@@ -139,7 +143,7 @@ class Member extends Component
     public $filterCertAgonistic = 0;
     public $filterCertScaduto = 0;
     public $filterCertInScadenza = 0;
-
+    public $already_existing = false;
     protected $rules = [
         'first_name' => 'required',
         'last_name' => 'required',
@@ -208,6 +212,7 @@ class Member extends Component
         $this->age = '';
 
         $this->error_fc = false;
+        $this->already_existing = false;
 
         $this->isSaving = false;
 
@@ -238,6 +243,9 @@ class Member extends Component
     public function resetCourseFields()
     {
         $this->course_course_id = null;
+        $this->course_course_type = 'standard';
+        $this->course_months_list = [];
+        $this->course_price_list = [];
         $this->course_when = array();
         $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
         $this->course_date_from = null;
@@ -275,7 +283,6 @@ class Member extends Component
     {
         $this->course_course_id = null;
         $this->course_level_id = '';
-        $this->course_type_id = '';
         $this->course_frequency_id = '';
         $levels_ids = [];
         if ($this->course_name != '') {
@@ -287,28 +294,26 @@ class Member extends Component
             }
         }
         $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
-        $this->course_types = [];
         $this->course_frequencies = [];
     }
 
     public function updatedCourseLevelId()
     {
         $this->course_course_id = null;
-        $this->course_type_id = '';
         $this->course_frequency_id = '';
-        $types_ids = [];
+        $frequencies_ids = [];
         if ($this->course_level_id != '') {
             list($n, $y) = explode("(", $this->course_name);
             $y = trim(str_replace(")", "", $y));
             $all = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
             foreach ($all as $a) {
-                $types_ids[] = $a->course_type_id;
+                $frequencies_ids[] = $a->course_frequency_id;
             }
         }
-        $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
-        $this->course_frequencies = [];
+        $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
     }
 
+    /*
     public function updatedCourseTypeId()
     {
         $this->course_course_id = null;
@@ -323,7 +328,7 @@ class Member extends Component
             }
         }
         $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
-    }
+    }*/
 
     public function updatedCourseFrequencyId()
     {
@@ -331,7 +336,36 @@ class Member extends Component
         if ($this->course_frequency_id != '') {
             list($n, $y) = explode("(", $this->course_name);
             $y = trim(str_replace(")", "", $y));
-            $this->course_course_id = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->where('course_frequency_id', $this->course_frequency_id)->first()->id;
+            $this->course_course_id = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('course_level_id', $this->course_level_id)->where('course_frequency_id', $this->course_frequency_id)->first()->id;
+
+            $c = \App\Models\Course::findOrFail($this->course_course_id);
+            $this->course_price = formatPrice($c->price);
+            $this->course_subscription_price = formatPrice($c->subscription_price);
+            $this->course_date_from = $c->date_from;
+            $this->course_date_to = $c->date_to;
+            // Controllo se sono già iscritto la corso
+            $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
+
+            // Carico gli abbonamenti e i mesi
+            $period = \Carbon\CarbonPeriod::create($c->date_from, '1 month', $c->date_to);
+            $this->course_months_list = [];
+            foreach ($period as $dt) {
+                $this->course_months_list[] = $dt->format("m");
+            }
+
+            $course_subscription_ids = [];
+            $this->course_price_list = [];
+            if ($c->prices != null) {
+                foreach (json_decode($c->prices) as $z) {
+                    $this->course_price_list[$z->course_subscription_id] = $z->price;
+                    if ($z->price > 0)
+                        $course_subscription_ids[] = $z->course_subscription_id;
+                }
+            }
+
+            $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
+
+            /*
 
             if ($this->course_course_id > 0) {
                 $c = \App\Models\Course::findOrFail($this->course_course_id);
@@ -343,7 +377,7 @@ class Member extends Component
                 $this->course_price = 0;
                 $this->course_subscription_price = 0;
                 $this->course_exist = false;
-            }
+            }*/
         } else {
             $this->course_price = 0;
             $this->course_subscription_price = 0;
@@ -351,6 +385,20 @@ class Member extends Component
         }
     }
 
+    public function updatedCourseCourseType()
+    {
+        $this->course_level_id = '';
+        $this->course_frequency_id = '';
+        $this->course_course_id = null;
+    }
+
+    public function updatedCourseCourseSubscriptionId()
+    {
+        $this->course_price = 0;
+        if (isset($this->course_price_list[$this->course_course_subscription_id]))
+            $this->course_price = $this->course_price_list[$this->course_course_subscription_id];
+    }
+
     public $documents = [];
 
     public $father_documents = [];
@@ -454,9 +502,7 @@ class Member extends Component
 
         $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
 
-        $this->courses = \App\Models\Course::select('id', 'name')->where('enabled', null)->get();
 
-        $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
 
         // $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->get(), 0);
 
@@ -612,7 +658,7 @@ class Member extends Component
     {
 
         $this->course_names = [];
-        $allC = \App\Models\Course::orderBy('name')->get();
+        $allC = \App\Models\Course::where('type', $this->course_course_type)->orderBy('name')->get();
         foreach ($allC as $c) {
             $cN = $c->name . " (" . $c->year . ")";
             if (!in_array($cN, $this->course_names))
@@ -705,6 +751,9 @@ class Member extends Component
         $this->loadMemberCategories();
         $this->loadMemberCertificates();
 
+        $this->courses = \App\Models\Course::select('id', 'name')->where('type', $this->course_course_type)->get();
+        // $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
+
         return view('livewire.member', ['datas' => $datas]);
     }
 
@@ -714,8 +763,29 @@ class Member extends Component
             $c = \App\Models\Course::findOrFail($this->course_course_id);
             $this->course_price = formatPrice($c->price);
             $this->course_subscription_price = formatPrice($c->subscription_price);
+            $this->course_date_from = $c->date_from;
+            $this->course_date_to = $c->date_to;
             // Controllo se sono già iscritto la corso
             $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
+
+            // Carico gli abbonamenti e i mesi
+            $period = \Carbon\CarbonPeriod::create($c->date_from, '1 month', $c->date_to);
+            $this->course_months_list = [];
+            foreach ($period as $dt) {
+                $this->course_months_list[] = $dt->format("m");
+            }
+
+            $course_subscription_ids = [];
+            $this->course_price_list = [];
+            if ($c->prices != null) {
+                foreach (json_decode($c->prices) as $z) {
+                    $this->course_price_list[$z->course_subscription_id] = $z->price;
+                    if ($z->price > 0)
+                        $course_subscription_ids[] = $z->course_subscription_id;
+                }
+            }
+
+            $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
         } else {
             $this->course_price = 0;
             $this->course_subscription_price = 0;
@@ -784,11 +854,9 @@ class Member extends Component
             'email' => 'required',
             'phone' => 'required',
             'birth_nation_id' => 'required',
-            //'birth_place' => 'required',
             'address' => 'required',
             'zip_code' => 'required',
             'nation_id' => 'required',
-            'fiscal_code' => 'required',
             'birth_date' => 'before_or_equal:today',
             'gender' => 'required'
         ];
@@ -796,6 +864,7 @@ class Member extends Component
         if ($this->isBirthItaly) {
             $rules['birth_province_id'] = 'required';
             $rules['birth_city_id'] = 'required';
+            $rules['fiscal_code'] = 'required';
         } else {
             $rules['birth_place'] = 'required';
         }
@@ -825,15 +894,36 @@ class Member extends Component
 
         $this->emit('setErrorMsg', $zzz);
 
-        $existingMember = \App\Models\Member::where('fiscal_code', $this->fiscal_code)->first();
-        if ($existingMember) {
-            $this->addError('fiscal_code', 'Codice fiscale già presente.');
-            return;
+        if (!empty($this->fiscal_code)) {
+            $existingMember = \App\Models\Member::where('fiscal_code', $this->fiscal_code)->first();
+            if ($existingMember) {
+                $this->already_existing = true;
+                $this->emit('focus-error-field', 'fiscal_code');
+
+                return;
+            }
         }
 
-        $this->validate($rules);
-        //$this->emit('setErrors');
+        if ($this->under18) {
+            $rules['father_name'] = 'required_without:mother_name';
+            $rules['father_email'] = 'required_without:mother_email|email';
+            $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
+            $rules['mother_name'] = 'required_without:father_name';
+            $rules['mother_email'] = 'required_without:father_email|email';
+            $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
+        }
 
+        try {
+            $this->validate($rules);
+        } catch (\Illuminate\Validation\ValidationException $e) {
+            $this->isSaving = false;
+            $errorFields = array_keys($e->errors());
+            if (!empty($errorFields)) {
+                $this->emit('focus-error-field', $errorFields[0]);
+            }
+            Log::error('Validation failed', ['errors' => $e->errors()]);
+            return;
+        }
         try {
 
             $name = '';
@@ -848,6 +938,8 @@ class Member extends Component
 
             $mother_docs = implode("|", $this->mother_document_files);
 
+
+
             $member = \App\Models\Member::create([
                 'first_name' => strtoupper($this->first_name),
                 'last_name' => strtoupper($this->last_name),
@@ -893,6 +985,7 @@ class Member extends Component
                 'to_complete' => false,
                 'enabled' => $this->enabled
             ]);
+
             session()->flash('success, Tesserato creato');
             updateMemberData($member->id);
             $this->resetFields();
@@ -1022,11 +1115,9 @@ class Member extends Component
             'email' => 'required',
             'phone' => 'required',
             'birth_nation_id' => 'required',
-            //'birth_place' => 'required',
             'address' => 'required',
             'zip_code' => 'required',
             'nation_id' => 'required',
-            'fiscal_code' => 'required',
             'birth_date' => 'before_or_equal:today'
         ];
 
@@ -1036,6 +1127,8 @@ class Member extends Component
         } else {
             $rules['birth_place'] = 'required';
         }
+        Log::info('isItaly', ['isItaly' => $this->isItaly]);
+        $this->isItaly = $this->checkIsItaly();
 
         if ($this->isItaly) {
             $rules['province_id'] = 'required';
@@ -1062,7 +1155,27 @@ class Member extends Component
 
         $this->emit('setErrorMsg', $zzz);
 
-        $this->validate($rules);
+        if ($this->under18) {
+            $rules['father_name'] = 'required_without:mother_name';
+            $rules['father_email'] = 'required_without:mother_email|email';
+            $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
+            $rules['mother_name'] = 'required_without:father_name';
+            $rules['mother_email'] = 'required_without:father_email|email';
+            $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
+        }
+
+        try {
+            $this->validate($rules);
+        } catch (\Illuminate\Validation\ValidationException $e) {
+            $this->isSaving = false;
+            $errorFields = array_keys($e->errors());
+            if (!empty($errorFields)) {
+                $this->emit('focus-error-field', $errorFields[0]);
+            }
+            Log::error('Validation failed', ['errors' => $e->errors()]);
+            return;
+        }
+
         try {
 
             $name = '';
@@ -1150,7 +1263,7 @@ class Member extends Component
             session()->flash('success', "Tesserato eliminato");
             $this->emit('reload');
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1172,7 +1285,7 @@ class Member extends Component
                 \App\Models\Member::find($id)->delete();
             }
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
         $this->multipleAction = '';
     }
@@ -1374,7 +1487,7 @@ class Member extends Component
             \App\Models\MemberCard::find($id)->delete();
             session()->flash('success', "Tesserato eliminato");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1407,20 +1520,21 @@ class Member extends Component
         $this->validate(['course_course_id' => 'required']);
         try {
 
-            \App\Models\MemberCourse::create([
-                'member_id' => $this->dataId,
-                'course_id' => $this->course_course_id,
-                'date_from' => $this->course_date_from,
-                'date_to' => $this->course_date_to,
-                'course_subscription_id' => $this->course_course_subscription_id,
-                'status' => $this->course_status,
-                'subscribed' => false, // default non iscritto
-                'price' => currencyToDouble($this->course_price),
-                'subscription_price' => currencyToDouble($this->course_subscription_price),
-                'notes' => $this->course_note,
-                'months' => json_encode($this->course_months),
-                'when' => json_encode($this->course_when)
-            ]);
+            $mc = new \App\Models\MemberCourse();
+            $mc->member_id = $this->dataId;
+            $mc->course_id = $this->course_course_id;
+            $mc->date_from = $this->course_date_from;
+            $mc->date_to = $this->course_date_to;
+            $mc->course_subscription_id = $this->course_course_subscription_id;
+            $mc->status = $this->course_status;
+            $mc->subscribed = false;
+            $mc->price = currencyToDouble($this->course_price);
+            $mc->subscription_price = currencyToDouble($this->course_subscription_price);
+            $mc->notes = $this->course_note;
+            $mc->months = json_encode($this->course_months);
+            $mc->when = json_encode($this->course_when);
+            $mc->save();
+
             // Se il corso ha associato una categoria iscrivo anche al gruppo
             $c = \App\Models\Course::findOrFail($this->course_course_id);
             if ($c) {
@@ -1432,6 +1546,53 @@ class Member extends Component
                     ]);
                 }
             }
+
+            // Creo i pagamenti in base alla tipologia
+            $r = \App\Models\CourseSubscription::findOrFail($this->course_course_subscription_id);
+
+            $start = $this->course_date_from;
+
+            // Creo il pagamento per l'iscrizione
+            $rate = new \App\Models\Rate();
+            $rate->member_id = $this->dataId;
+            $rate->member_course_id = $mc->id;
+            $rate->course_subscription_id = null;
+            $rate->price = currencyToDouble($this->course_subscription_price);
+            $rate->date = $start;
+            $rate->months = json_encode([]);
+            $rate->note = '';
+            $rate->status = 0;
+            $rate->is_subscription = true;
+            $rate->save();
+
+            $go = true;
+            while ($go) {
+
+                $mms = [];
+                $mms[] = date("n", strtotime($start));
+                for ($jj = 1; $jj < $r->months; $jj++) {
+                    $mms[] = date('n', strtotime("+" . $jj . " months", strtotime($start)));
+                }
+
+                $rate = new \App\Models\Rate();
+                $rate->member_id = $this->dataId;
+                $rate->member_course_id = $mc->id;
+                $rate->course_subscription_id = $this->course_course_subscription_id;
+                $rate->price = currencyToDouble($this->course_price);
+                $rate->date = $start;
+                $rate->months = json_encode($mms);
+                $rate->note = '';
+                $rate->status = 0;
+                $rate->save();
+
+                $start = date('Y-m-d', strtotime("+" . $r->months . " months", strtotime($start)));
+
+                if ($start > $this->course_date_to) {
+                    $go = false;
+                    break;
+                }
+            }
+
             session()->flash('success, Corso creato');
             $this->resetCourseFields();
             $this->addCourse = false;
@@ -1455,6 +1616,7 @@ class Member extends Component
                 $c = \App\Models\Course::findOrFail($this->course_course_id);
                 $this->course_name = $c->name . " (" . $c->year . ")";
                 $this->course_level_id = $c->course_level_id;
+                $this->course_course_type = $c->type;
                 $this->course_type_id = $c->course_type_id;
                 $this->course_frequency_id = $c->course_frequency_id;
 
@@ -1546,7 +1708,7 @@ class Member extends Component
             \App\Models\MemberCourse::find($id)->delete();
             session()->flash('success', "Corso eliminato");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1680,7 +1842,7 @@ class Member extends Component
             \App\Models\MemberCertificate::find($id)->delete();
             session()->flash('success', "Tesserato eliminato");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1736,7 +1898,7 @@ class Member extends Component
             \App\Models\MemberCategory::find($id)->delete();
             session()->flash('success', "Associazione eliminata");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1828,25 +1990,19 @@ class Member extends Component
 
         $price = $course["price"];
 
-        if (sizeof($this->payMonths) == 1)
-        {
+        if (sizeof($this->payMonths) == 1) {
             $month = $this->payMonths[0];
             $records = \App\Models\Record::where('member_course_id', $this->selectedCourseMember)->where('deleted', 0)->get();
-            foreach ($records as $record)
-            {
+            foreach ($records as $record) {
+
+                if (in_array($month, json_decode($record->months))) {
 
-                if (in_array($month, json_decode($record->months)))
-                {
+                    foreach ($record->rows as $row) {
 
-                    foreach ($record->rows as $row)
-                    {
 
-                        
-                        if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
-                        {
+                        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)
-                            {
+                            foreach (json_decode($row->when) as $m) {
                                 $price -= $row->amount / $tot;
                             }
                         }
@@ -1906,21 +2062,16 @@ class Member extends Component
                 if ($mm->m == $m) {
                     if ($mm->status == "")
                         $this->payMonths[] = $m;
-                    if ($mm->status == "1")
-                    {
+                    if ($mm->status == "1") {
                         $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
                         $price = $mc->price;
                         $payed = 0;
                         $extraC = '';
                         $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
-                        foreach ($recordsPayed as $record)
-                        {
-                            if (in_array($m, json_decode($record->months)))
-                            {
-                                foreach ($record->rows as $row)
-                                {
-                                    if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
-                                    {
+                        foreach ($recordsPayed as $record) {
+                            if (in_array($m, json_decode($record->months))) {
+                                foreach ($record->rows as $row) {
+                                    if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
                                         $tot = sizeof(json_decode($row->when));
                                         $payed += $row->amount / $tot;
                                     }
@@ -1930,7 +2081,7 @@ class Member extends Component
                             }
                         }
                     }
-                        
+
                     if ($mm->status == "2")
                         $this->suspendedMonths[] = $m;
                 }
@@ -1986,22 +2137,17 @@ class Member extends Component
                         $class = "yellow";
                 }
 
-                if ($class == 'green')
-                {
+                if ($class == 'green') {
 
                     $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
                     $price = $mc->price;
                     $payed = 0;
                     $extraC = '';
                     $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
-                    foreach ($recordsPayed as $record)
-                    {
-                        if (in_array($m, json_decode($record->months)))
-                        {
-                            foreach ($record->rows as $row)
-                            {
-                                if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
-                                {
+                    foreach ($recordsPayed as $record) {
+                        if (in_array($m, json_decode($record->months))) {
+                            foreach ($record->rows as $row) {
+                                if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
                                     $tot = sizeof(json_decode($row->when));
                                     $payed += $row->amount / $tot;
                                 }
@@ -2012,7 +2158,6 @@ class Member extends Component
                         $class = 'orange half';
                     //$class .= $extraC;
                 }
-
             }
         }
 

+ 62 - 0
resources/views/livewire/member.blade.php

@@ -1900,6 +1900,15 @@
         color: black;
         font-weight:normal;
     }
+    .persistent-invalid
+    {
+        border-color: #dc3545 !important;
+        padding-right: calc(1.5em + 0.75rem) !important;
+        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23dc3545' stroke='none'/%3e%3c/svg%3e") !important;
+        background-repeat: no-repeat !important;
+        background-position: right calc(0.375em + 0.1875rem) center !important;
+        background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem) !important;
+    }
     </style>
     <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
     <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
@@ -2643,5 +2652,58 @@
             setupBackButtonHandlers();
         });
 
+        document.addEventListener('livewire:load', () => {
+            Livewire.on('focus-error-field', (fieldId) => {
+                console.log('Validation errors received:', fieldId);
+                let element;
+
+                if (typeof fieldId === 'string') {
+                    element = document.getElementById(fieldId);
+                } else if (Array.isArray(fieldId) && fieldId.length > 0) {
+                    const firstFieldId = fieldId[0];
+                    element = document.getElementById(firstFieldId);
+                    fieldId = firstFieldId; // Use the first fieldId for highlighting
+                }
+
+                if (element) {
+                    element.focus();
+                    element.scrollIntoView({
+                        behavior: 'smooth',
+                        block: 'center'
+                    });
+
+                    // Add the is-invalid class to highlight the field
+                    element.classList.add('is-invalid');
+
+                    // Also highlight the label
+                    const label = document.querySelector(`label[for="${fieldId}"]`);
+                    if (label) {
+                        label.classList.add('text-danger');
+                    }
+
+                    // Prevent the class from being removed
+                    const preserveInvalidClass = () => {
+                        if (!element.classList.contains('is-invalid')) {
+                            element.classList.add('is-invalid');
+                        }
+                    };
+
+                    // Set an interval to ensure the class stays applied
+                    const intervalId = setInterval(preserveInvalidClass, 100);
+
+                    // Stop preserving after user interaction
+                    element.addEventListener('input', () => {
+                        clearInterval(intervalId);
+                        // Optional: Remove the highlighting when user edits the field
+                        // element.classList.remove('is-invalid');
+                        // if (label) label.classList.remove('text-danger');
+                    });
+
+                } else {
+                    console.warn('Element with id ' + fieldId + ' not found');
+                }
+            });
+        });
+
     </script>
 @endpush