Sfoglia il codice sorgente

fix salva in update

FabioFratini 9 mesi fa
parent
commit
75aa5d4f52
2 ha cambiato i file con 58 aggiunte e 55 eliminazioni
  1. 57 54
      app/Http/Livewire/Member.php
  2. 1 1
      app/Http/Livewire/User.php

+ 57 - 54
app/Http/Livewire/Member.php

@@ -349,17 +349,14 @@ class Member extends Component
             // 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)
-            {
+            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)
-                {
+            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;
@@ -774,17 +771,14 @@ class Member extends Component
             // 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)
-            {
+            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)
-                {
+            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;
@@ -792,7 +786,6 @@ class Member extends Component
             }
 
             $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;
@@ -1126,7 +1119,6 @@ class Member extends Component
             'address' => 'required',
             'zip_code' => 'required',
             'nation_id' => 'required',
-            'fiscal_code' => 'required',
             'birth_date' => 'before_or_equal:today'
         ];
 
@@ -1162,7 +1154,38 @@ class Member extends Component
 
         $this->emit('setErrorMsg', $zzz);
 
-        $this->validate($rules);
+
+        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;
+            }
+        }
+
+        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 = '';
@@ -1553,13 +1576,11 @@ class Member extends Component
             $rate->save();
 
             $go = true;
-            while($go)
-            {
+            while ($go) {
 
                 $mms = [];
                 $mms[] = date("n", strtotime($start));
-                for($jj=1; $jj<$r->months;$jj++)
-                {
+                for ($jj = 1; $jj < $r->months; $jj++) {
                     $mms[] = date('n', strtotime("+" . $jj . " months", strtotime($start)));
                 }
 
@@ -1576,8 +1597,7 @@ class Member extends Component
 
                 $start = date('Y-m-d', strtotime("+" . $r->months . " months", strtotime($start)));
 
-                if ($start > $this->course_date_to)
-                {
+                if ($start > $this->course_date_to) {
                     $go = false;
                     break;
                 }
@@ -1980,25 +2000,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;
                             }
                         }
@@ -2058,21 +2072,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;
                                     }
@@ -2138,22 +2147,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;
                                 }
@@ -2164,7 +2168,6 @@ class Member extends Component
                         $class = 'orange half';
                     //$class .= $extraC;
                 }
-
             }
         }
 

+ 1 - 1
app/Http/Livewire/User.php

@@ -114,7 +114,7 @@ class User extends Component
             \App\Models\User::find($id)->delete();
             session()->flash('success',"Dato eliminato");
         }catch(\Exception $e){
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error','Errore (' . $e->getMessage() . ')');
         }
     }
 }