Completed
Push — development ( e90f69...2fb2af )
by Ashutosh
11:39
created

SlaController::getSlas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Licence;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\licence\Licence;
7
use App\Model\Licence\LicencedOrganization;
8
use App\Model\licence\Sla;
9
use App\Model\licence\SlaServiceRelation;
10
use App\Model\Product\Service;
11
use App\Organization;
12
use Illuminate\Http\Request;
13
14
class SlaController extends Controller
15
{
16
    public $sla;
17
    public $slaServiceRelation;
18
    public $service;
19
    public $licence;
20
    public $organization;
21
    public $licencedOrganization;
22
23
    public function __construct()
24
    {
25
        $this->middleware('auth');
26
        $this->middleware('service.provider', ['only' => ['create', 'store', 'edit', 'update']]);
27
        //$this->middleware('admin');
28
        $cart = new \App\Http\Controllers\Front\CheckoutController();
29
        $auth = ''; //$cart->GetXdeskAuthOrganization();
30
        $this->org = $auth;
31
32
        $sla = new Sla();
33
        $this->sla = $sla;
34
35
        $slaServiceRelation = new SlaServiceRelation();
36
        $this->slaServiceRelation = $slaServiceRelation;
37
38
        $service = new Service();
39
        $this->service = $service;
40
41
        $licence = new Licence();
42
        $this->licence = $licence;
43
44
        $organization = new Organization();
45
        $this->organization = $organization;
46
47
        $licencedOrganization = new LicencedOrganization();
48
        $this->licencedOrganization = $licencedOrganization;
49
    }
50
51
    public function index()
52
    {
53
        try {
54
            return view('themes.default1.sla.index');
55
        } catch (\Exception $ex) {
56
            return redirect()->back()->with('fails', $ex->getMessage());
57
        }
58
    }
59
60
    // public function getSlas()
61
    // {
62
    //     return \Datatable::collection($this->sla->get())
63
    //                     ->addColumn('licence_id', function ($model) {
64
    //                         $licence_name = $this->licence->where('id', $model->licence_id)->first()->name;
65
66
    //                         return $licence_name;
67
    //                     })
68
    //                     ->showColumns('name', 'description')
69
    //                     ->addColumn('service', function ($model) {
70
    //                         $serviceid = $this->slaServiceRelation->where('sla_id', $model->id)->first()->service_id;
71
72
    //                         return $this->service->where('id', $serviceid)->first()->name;
73
    //                     })
74
    //                     ->addColumn('organization_id', function ($model) {
75
    //                         $name = $this->organization->where('id', $model->organization_id)->where('type', 'client')->first()->name;
76
77
    //                         return $name;
78
    //                     })
79
    //                     ->addColumn('service_provider_id', function ($model) {
80
    //                         $name = $this->organization->where('id', $model->service_provider_id)->where('type', 'service_provider')->first()->name;
81
82
    //                         return $name;
83
    //                     })
84
    //                     ->showColumns('start_date', 'end_date', 'grace_period')
85
    //                     ->addColumn('action', function ($model) {
86
    //                         return '<a href='.url('slas/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
87
    //                     })
88
    //                     ->searchColumns('name')
89
    //                     ->orderColumns('name')
90
    //                     ->make();
91
    // }
92
93
    public function create()
94
    {
95
        try {
96
            //dd($this->org);
97
            $organizations = $this->organization->where('type', 'client')->lists('name', 'id')->toArray();
98
            if ($this->org->type == 'xdesk') {
0 ignored issues
show
Bug introduced by
The property type does not exist on string.
Loading history...
99
                $licences = $this->licence->lists('name', 'id')->toArray();
100
                $serviceProviders = $this->organization->where('type', 'service_provider')->lists('name', 'id')->toArray();
101
            } else {
102
                $purchased = $this->licencedOrganization->where('organization_id', $this->org->id)->where('payment_status', 'success')->distinct('licence_name')->lists('licence_name');
0 ignored issues
show
Bug introduced by
The property id does not exist on string.
Loading history...
103
                if (count($purchased) == 0) {
104
                    return redirect()->back()->with('fails', 'No licence to create SLA');
105
                }
106
                $licences = $this->licence->whereIn('name', $purchased)->lists('name', 'id')->toArray();
107
                $serviceProviders = [$this->org->id => $this->org->name];
0 ignored issues
show
Bug introduced by
The property name does not exist on string.
Loading history...
108
            }
109
            $services = $this->service->lists('name', 'id')->toArray();
110
111
            return view('themes.default1.sla.create', compact('organizations', 'serviceProviders', 'services', 'licences'));
112
        } catch (\Exception $ex) {
113
            return redirect()->back()->with('fails', $ex->getMessage());
114
        }
115
    }
116
117
    // public function store(Request $request)
118
    // {
119
    //     try {
120
    //         if ($this->org->type == 'xdesk') {
121
    //             $serviceprovider_id = $request->input('service_provider_id');
122
    //         } else {
123
    //             $serviceprovider_id = $this->org->id;
124
    //         }
125
    //         $service_id = $request->input('service');
126
127
    //         $serviceprovider = $this->organization->where('id', $serviceprovider_id)->where('type', 'service_provider')->first();
128
    //         $licenced = $this->licencedOrganization->where('organization_id', $serviceprovider->id)->lists('number_of_slas', 'licence_name')->toArray();
129
    //         //dd($licenced);
130
    //         //check if service provider purchased licence
131
    //         if (count($licenced) == 0) {
132
    //             return redirect()->back()->with('fails', 'No Licence');
133
    //         }
134
    //         $CountOfSlaCanCreate = array_sum($licenced);
135
    //         //dd($CountOfSlaCanCreate);
136
    //         $CountOfSlaCreated = $this->sla->where('service_provider_id', $serviceprovider->id)->count();
137
138
    //         //check if service provider has enough licence to create slas OR it is unlimited licence
139
    //         if ($CountOfSlaCanCreate > $CountOfSlaCreated || in_array('0', $licenced)) {
140
    //             $this->sla->fill($request->input())->save();
141
    //             $this->slaServiceRelation->create(['sla_id' => $this->sla->id, 'service_id' => $service_id]);
142
143
    //             return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
144
    //         } else {
145
    //             return redirect()->back()->with('fails', 'Licence Over to create Slas ! ');
146
    //         }
147
    //     } catch (\Exception $ex) {
148
    //         return redirect()->back()->with('fails', $ex->getMessage());
149
    //     }
150
    // }
151
152
    // public function edit($id)
153
    // {
154
    //     try {
155
    //         $organizations = $this->organization->where('type', 'client')->lists('name', 'id')->toArray();
156
    //         if ($this->org->type == 'xdesk') {
157
    //             $licences = $this->licence->lists('name', 'id')->toArray();
158
    //             $serviceProviders = $this->organization->where('type', 'service_provider')->lists('name', 'id')->toArray();
159
    //         } else {
160
    //             $purchased = $this->licencedOrganization->where('organization_id', $this->org->id)->where('payment_status', 'success')->distinct('licence_name')->lists('licence_name');
161
    //             if (count($purchased) == 0) {
162
    //                 return redirect()->back()->with('fails', 'No licence to create SLA');
163
    //             }
164
    //             $licences = $this->licence->whereIn('name', $purchased)->lists('name', 'id')->toArray();
165
    //             $serviceProviders = $this->organization->where('type', 'service_provider')->where('id', $this->org->id)->lists('name', 'id')->toArray();
166
    //         }
167
    //         $services = $this->service->lists('name', 'id')->toArray();
168
    //         $sla = $this->sla->where('id', $id)->first();
169
170
    //         return view('themes.default1.sla.edit', compact('organizations', 'serviceProviders', 'services', 'sla', 'licences'));
171
    //     } catch (\Exception $ex) {
172
    //         return redirect()->back()->with('fails', $ex->getMessage());
173
    //     }
174
    // }
175
176
    public function update($id, Request $request)
177
    {
178
        try {
179
            $service = $this->slaServiceRelation->where('sla_id', $id)->first();
180
            $service->delete();
181
182
            $service_id = $request->input('service');
183
            $sla = $this->sla->where('id', $id)->first();
184
            $sla->fill($request->input())->save();
185
            $this->slaServiceRelation->create(['sla_id' => $id, 'service_id' => $service_id]);
186
187
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
188
        } catch (\Exception $ex) {
189
            return redirect()->back()->with('fails', $ex->getMessage());
190
        }
191
    }
192
}
193