File size: 3,749 Bytes
89a1081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace App\Service\Admin\Core;

use App\Enums\StatusEnum;
use App\Models\PricingPlan;
use App\Models\Subscription;

class PricingPlanService
{
    public function planLog() {

        return PricingPlan::search(['name'])
                            ->filter(['status'])
                            ->orderBy('recommended_status', 'DESC')
                            ->latest()
                            ->date()
                            ->paginate(paginateNumber(site_settings("paginate_number")))->onEachSide(1)
                            ->appends(request()->all());
    }

    public function statusUpdate($request) {
        
        $status = "error";
        $message = "Something went wrong";
        
        try {
            $status   = true;
            $reload   = false;
            $message  = translate('Pricing plan status updated successfully');
            $gateway  = PricingPlan::where("id",$request->input('id'))->first();
            $column   = $request->input("column");
            
            if($column != "recommended_status" && $request->value == StatusEnum::TRUE->status()) {
                
                $gateway->status = StatusEnum::FALSE->status();
                if($gateway->recommended_status == StatusEnum::TRUE->status()) {

                    $gateway->recommended_status = StatusEnum::FALSE->status();
                    $reload = true;
                }
                $gateway->update();

            } elseif($column != "recommended_status" && $request->value == StatusEnum::FALSE->status()) {

                $gateway->status = StatusEnum::TRUE->status();
                $gateway->update();

            } elseif($column == "recommended_status") {
                
                $reload = true;
                $message  = translate('Recommended plan updated successfully');
                PricingPlan::where('id', '!=',$request->id)->update(["recommended_status" => StatusEnum::FALSE->status()]);
                $gateway->$column = StatusEnum::TRUE->status();
                $gateway->status  = StatusEnum::TRUE->status();
                
                $gateway->update();
            } else {

                $status = false;
                $message = translate("Something went wrong while updating this gateway");
            }

        } catch (\Exception $error) {

            $status  = false;
            $message = $error->getMessage();
        }

        return json_encode([
            'reload'  => $reload,
            'status'  => $status,
            'message' => $message
        ]);
    }

    /**
     * 
     * @param $id
     *
     * @return array
     */
    public function deleteplan($id): array {

        $plan = $this->fetchWithId($id);
        
        if($plan && $plan->recommended_status != StatusEnum::TRUE->status()) {
            
            $plan->delete();
            Subscription::where('plan_id',$id)->delete();
            $status  = 'success';
            $message = translate("plan ").$plan->name.translate(' has been deleted successfully from admin panel');
        } elseif($plan->recommended_status == StatusEnum::TRUE->status()) {

            $status  = 'error';
            $message = translate("Can not delete recommended plan. Please inactive the plan or make another plan recommended in order to delete this plan."); 
        } else {

            $status  = 'error';
            $message = translate("plan couldn't be found"); 
        }
        return [
            
            $status, 
            $message
        ];
    }

    /**
     * 
     * @param string $id
     *
     * @return plan $plan
     */
    public function fetchWithId($id) {

        return PricingPlan::where("id", $id)->first();
     }
}