Ver código fonte

sospendi riattiva corsi pagamenti

FabioFratini 10 meses atrás
pai
commit
30abe17431

+ 119 - 4
app/Http/Livewire/CourseList.php

@@ -3,6 +3,8 @@
 namespace App\Http\Livewire;
 
 use Livewire\Component;
+use App\Models\MemberCourse;
+use Illuminate\Support\Facades\Log;
 
 class CourseList extends Component
 {
@@ -271,11 +273,11 @@ class CourseList extends Component
 
     public function newPayment($course_id, $months, $member_id, $id, $subscription)
     {
-        
+
         $newMonths = array();
         if ($months != '')
         {
-            $mm = explode(",", $months);        
+            $mm = explode(",", $months);
             foreach($mm as $month)
             {
                 if ($month < 5) $month += 12;
@@ -283,12 +285,125 @@ class CourseList extends Component
                 $newMonths[] = $month;
             }
         }
-        
+
         $c = \App\Models\Course::findOrFail($course_id);
-        $m = \App\Models\MemberCourse::findOrFail($id);
+        $m = MemberCourse::findOrFail($id);
         return redirect()->to('/in?new=1&memberId=' . $member_id . (sizeof($newMonths) > 0 ? '&causalId=' . $c->causal_id : '') . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=' . ($subscription ? 1 : 0) . (sizeof($newMonths) > 0 ? '&months=' . implode("|", $newMonths) : '') . (sizeof($newMonths) > 0 ? '&price=' . $m->price : '') . '&subscription_price=' . $m->subscription_price . "&courseId=" . $id);
 
     }
+    public function suspendPayment($course_id, $month, $member_id, $id, $subscription)
+    {
+
+        $monthMap = [
+            1 => 9,  // September
+            2 => 10, // October
+            3 => 11, // November
+            4 => 12, // December
+            5 => 1,  // January
+            6 => 2,  // February
+            7 => 3,  // March
+            8 => 4,  // April
+            9 => 5,  // May
+            10 => 6, // June
+            11 => 7, // July
+            12 => 8  // August
+        ];
+
+        $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
+
+
+        $memberCourse = MemberCourse::where('id', $id)
+            ->where('member_id', $member_id)
+            ->where('course_id', $course_id)
+            ->first();
+
+
+        if (!$memberCourse) {
+            return response()->json(['error' => 'Non Trovato'], 404);
+        }
+
+        $monthsData = json_decode($memberCourse->months, true);
+        if (!is_array($monthsData)) {
+            return response()->json(['error' => 'Invalid months data format'], 400);
+        }
+
+        $monthUpdated = false;
+        foreach ($monthsData as &$monthData) {
+            if ($monthData['m'] == $dbMonth) {
+                $monthData['status'] = 2;
+                $monthUpdated = true;
+            }
+        }
+
+        if (!$monthUpdated) {
+            return response()->json(['error' => 'Month not found in data'], 404);
+        }
+
+        $memberCourse->months = json_encode($monthsData);
+        $memberCourse->save();
+        session()->flash('success', 'Payment suspended successfully');
+        return redirect()->to('/course_list');
+    }
+
+    public function resumePayment($course_id, $month, $member_id, $id, $subscription)
+    {
+        Log::info('resumePayment');
+        log::info($course_id);
+        log::info($month);
+        log::info($member_id);
+        log::info($id);
+
+        $monthMap = [
+            1 => 9,  // September
+            2 => 10, // October
+            3 => 11, // November
+            4 => 12, // December
+            5 => 1,  // January
+            6 => 2,  // February
+            7 => 3,  // March
+            8 => 4,  // April
+            9 => 5,  // May
+            10 => 6, // June
+            11 => 7, // July
+            12 => 8  // August
+        ];
+
+        $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
+
+
+        $memberCourse = MemberCourse::where('id', $id)
+            ->where('member_id', $member_id)
+            ->where('course_id', $course_id)
+            ->first();
+
+        if (!$memberCourse) {
+            return response()->json(['error' => 'Non Trovato'], 404);
+        }
+
+        $monthsData = json_decode($memberCourse->months, true);
+        Log::info('data mese',$monthsData);
+        if (!is_array($monthsData)) {
+            return response()->json(['error' => 'Invalid months data format'], 400);
+        }
+
+        $monthUpdated = false;
+        foreach ($monthsData as &$monthData) {
+            if ($monthData['m'] == $dbMonth) {
+                $monthData['status'] = "";
+                $monthUpdated = true;
+            }
+        }
+        Log::info($monthUpdated);
+
+        if (!$monthUpdated) {
+            return response()->json(['error' => 'Month not found in data'], 404);
+        }
+
+        $memberCourse->months = json_encode($monthsData);
+        $memberCourse->save();
+        session()->flash('success', 'Payment resumed successfully');
+        return redirect()->to('/course_list');
+    }
     /*
     public function newPayment()
     {

+ 111 - 1
resources/views/livewire/course_list.blade.php

@@ -121,6 +121,9 @@
     <div class="row text-right">
         <div class="col-md-12" style="text-align:right">
             <button style="display:none; width:200px;" class="btn--ui btNewPayment" onclick="newPayment()">NUOVA ENTRATA</button>
+            <button class="btn--ui primary btStopPayment" style="display:none; width:200px; color:white" onclick="suspendPayment()">Sospendi</button>
+            <button class="btn--ui primary btResumePayment" style="display:none; width:200px; color:white" onclick="resumePayment()">Riattiva</button>
+
         </div>
     </div>
 
@@ -593,6 +596,10 @@
                             {
                                 var ret = '<a style="width:100%;float:right; text-align:right; display:block; ' + (x[0] == 1 ? '' : 'cursor:pointer;') + '" class="' + (x[0] == 1 ? 'green' : 'orange') + ' sub_' + row["column_16"] + '" onclick="setSubscription(' + row["column_15"] + ', ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block;" class="' + (x[0] == 1 ? 'green' : 'orange') + '"><small>' + x[1] + '</small></a>';
@@ -610,6 +617,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_1_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 1, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_1_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 1, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -628,6 +639,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_2_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 2, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_2_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ',2, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -646,6 +661,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_3_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 3, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_3_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 3, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -664,6 +683,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_4_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 4, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_4_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 4, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -682,6 +705,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_5_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 5, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_5_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 5, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -700,6 +727,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_6_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 6, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_6_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 6, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -718,6 +749,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_7_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 7, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_7_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 7, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -736,6 +771,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_8_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 8, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_8_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 8, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -754,6 +793,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_9_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 9, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_9_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 9, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -772,6 +815,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_10_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 10, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_10_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 10, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -790,6 +837,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_11_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 11, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_11_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 11, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -808,6 +859,10 @@
                             {
                                 ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_12_' + row["column_17"] + '" onclick="setPayMonth(' + row["column_15"] + ', 12, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
                             }
+                            else if (x[0] == 'yellow')
+                            {
+                                ret = '<a style="width:100%;float:right; text-align:right; display:block; cursor:pointer;" class="' + x[0] + ' lnk_' + row["column_16"] + '" id="lnk_' + row["column_15"] + '_9_' + row["column_17"] + '" onclick="resumePayMonth(' + row["column_15"] + ', 12, ' + row["column_17"] + ', ' + row["column_16"] + ')" style="cursor: pointer;"><small>' + x[1] + '</small></a>';
+                            }
                             else
                             {
                                 if (x[0] != 'wgrey')
@@ -1000,8 +1055,11 @@
         var selectedCourseId = 0;
         var selectedMemberId = 0;
         var selectedMemberCourseId = 0;
+        var selectedMonth = 0;
+
         function setPayMonth(course_id, month, member_id, id)
         {
+
             if (selectedMemberId > 0 && selectedMemberId != member_id)
             {
                 months = [];
@@ -1015,7 +1073,7 @@
             selectedCourseId = course_id;
             selectedMemberId = member_id;
             selectedMemberCourseId = id;
-
+            selectedMonth = month;
             if(!months.includes(month))
             {
                 months.push(month);
@@ -1029,10 +1087,52 @@
             if (months.length > 0)
             {
                 $(".btNewPayment").show();
+                $(".btStopPayment").show();
+
             }
             else
             {
                 $(".btNewPayment").hide();
+                $(".btStopPayment").hide();
+            }
+        }
+
+        function resumePayMonth(course_id, month, member_id, id)
+        {
+
+            if (selectedMemberId > 0 && selectedMemberId != member_id)
+            {
+                months = [];
+                subscription = false;
+            }
+            if (selectedMemberCourseId != id)
+            {
+                $(".sub_" + selectedMemberCourseId).removeClass("orange").addClass("blue").addClass("selected");
+                $(".lnk_" + selectedMemberCourseId).removeClass("orange").addClass("blue").addClass("selected");
+            }
+            selectedCourseId = course_id;
+            selectedMemberId = member_id;
+            selectedMemberCourseId = id;
+            selectedMonth = month;
+            console.log(month);
+            if(!months.includes(month))
+            {
+                months.push(month);
+                $("#lnk_" + course_id + "_" + month + "_" + member_id).removeClass("blue").removeClass("selected").addClass("orange").addClass("selected");
+            }
+            else
+            {
+                months.splice(months.indexOf(months), 1);
+                $("#lnk_" + course_id + "_" + month + "_" + member_id).removeClass("orange").addClass("blue").addClass("selected");
+            }
+            if (months.length > 0)
+            {
+                $(".btResumePayment").show();
+
+            }
+            else
+            {
+                $(".btResumePayment").hide();
             }
         }
 
@@ -1067,6 +1167,16 @@
             @this.newPayment(selectedCourseId, months.toString(), selectedMemberId, selectedMemberCourseId, subscription);
         }
 
+        function suspendPayment()
+        {
+            @this.suspendPayment(selectedCourseId, selectedMonth, selectedMemberId, selectedMemberCourseId, subscription);
+        }
+
+        function resumePayment()
+        {
+            @this.resumePayment(selectedCourseId, selectedMonth, selectedMemberId, selectedMemberCourseId, subscription);
+        }
+
         $( document ).ready( function(){
 
             setMaxWidth();