Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 116
0.00% covered (danger)
0.00%
0 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
Approvals
0.00% covered (danger)
0.00%
0 / 116
0.00% covered (danger)
0.00%
0 / 14
930
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get_approvals
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 update_approval
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 list_approvers
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 list_workflow_approvers
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 create_approver
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 create_approver_v2
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 create_workflow_approver
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 delete_approver
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 delete_approver_v2
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 delete_workflow_approver
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 list_approval_budget_types
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 create_approval_budget_type
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 delete_approval_budget_type
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Models\TblApprovalBudgetTypes;
6use App\Models\TblApprovals;
7use App\Models\TblApprovers;
8use App\Models\TblApproversV2;
9use App\Models\TblWorkflowApprover;
10use App\Models\TblUsers;
11use App\Models\TblBudgetTypes;
12use Illuminate\Contracts\Routing\ResponseFactory;
13use Illuminate\Http\Request;
14use Illuminate\Http\Response;
15use Illuminate\Support\Facades\DB;
16use Illuminate\Support\Facades\App;
17use App\Exceptions\AppException;
18
19class Approvals extends Controller
20{
21    private $locale;
22    private $userId;
23
24    public function __construct(){
25        $this->locale = request()->header('Locale-Id');
26        $this->userId = request()->header('User-Id');
27
28        App::setLocale($this->locale);
29    }
30
31    function get_approvals(): ResponseFactory|Response{
32
33        try {
34
35            $result = TblApprovals::first();
36
37            return response([
38                'message' => 'OK',
39                'data' => $result,
40            ]);
41
42        } catch (\Exception $e) {
43            report(AppException::fromException($e, 'GET_APPROVALS_EXCEPTION'));
44            return response(['message' => 'KO', 'error' => $e->getMessage()]);
45        }
46
47    }
48
49    function update_approval(Request $request, $approvalId): ResponseFactory|Response{
50
51        try {
52
53            $data = $request->all();
54            $approvalId = addslashes((string) $approvalId);
55            
56            $data['updated_at'] = date('Y-m-d H:i:s');
57            $result = TblApprovals::where('approval_id', $approvalId)->update($data);
58
59            return response([
60                'message' => 'OK',
61                'data' => $result,
62            ]);
63
64        } catch (\Exception $e) {
65            report(AppException::fromException($e, 'UPDATE_APPROVAL_EXCEPTION'));
66            return response(['message' => 'KO', 'error' => $e->getMessage()]);
67        }
68
69    }
70
71
72    function list_approvers($companyId): ResponseFactory|Response{
73
74        try {
75            
76            $companyId = addslashes((string) $companyId);
77
78            $companyId = intval($companyId);
79
80            $query = 'SELECT 
81                        a.id, 
82                        a.name, 
83                        a.email
84                    FROM 
85                        tbl_users a
86                        LEFT JOIN tbl_approvers b
87                            ON a.id = b.user_id
88                    WHERE b.company_id = ?
89                    ORDER BY a.name ASC';
90
91            $result = DB::select($query, [$companyId]);
92
93            $query = 'SELECT 
94                        a.id, 
95                        a.name, 
96                        a.email
97                    FROM 
98                        tbl_users a
99                        LEFT JOIN tbl_approvers_v2 b
100                            ON a.id = b.user_id
101                    WHERE b.company_id = ?
102                    ORDER BY a.name ASC';
103
104            $resultV2 = DB::select($query, [$companyId]);
105
106            return response([
107                'message' => 'OK',
108                'data' => $result,
109                'dataV2' => $resultV2,
110            ]);
111
112        } catch (\Exception $e) {
113            report(AppException::fromException($e, 'LIST_APPROVERS_EXCEPTION'));
114            return response(['message' => 'KO', 'error' => $e->getMessage()]);
115        }
116
117    }
118
119    function list_workflow_approvers($companyId): ResponseFactory|Response{
120
121        try {
122            
123            $companyId = addslashes((string) $companyId);
124
125            $companyId = intval($companyId);
126
127            $query = 'SELECT 
128                        a.id, 
129                        a.name, 
130                        a.email, 
131                        (
132                        SELECT 
133                            approver_id 
134                        FROM 
135                            tbl_workflow_approvers
136                        WHERE 
137                            user_id = a.id
138                            AND company_id = ?
139                        ) is_selected 
140                    FROM 
141                        tbl_users a
142                    ORDER BY a.name ASC';
143
144            $result = DB::select($query, [$companyId]);
145
146            return response([
147                'message' => 'OK',
148                'data' => $result,
149            ]);
150
151        } catch (\Exception $e) {
152            report(AppException::fromException($e, 'LIST_WORKFLOW_APPROVERS_EXCEPTION'));
153            return response(['message' => 'KO', 'error' => $e->getMessage()]);
154        }
155
156    }
157
158    public function create_approver(Request $request)
159    {
160
161        try {
162
163            $data = $request->all();
164
165            $count = TblApprovers::where('user_id', $data['user_id'])->where('company_id', $data['company_id'])->count();
166
167            if ($count == 0) {
168                TblApprovers::create($data);
169            }
170
171            TblApproversV2::where('user_id', $data['user_id'])->where('company_id', $data['company_id'])->delete();
172
173            return $this->list_approvers($data['company_id']);
174
175        } catch (\Exception $e) {
176            report(AppException::fromException($e, 'CREATE_APPROVER_EXCEPTION'));
177            return response(['message' => 'KO', 'error' => $e->getMessage()]);
178        }
179
180    }
181
182    public function create_approver_v2(Request $request)
183    {
184
185        try {
186
187            $data = $request->all();
188
189            $count = TblApproversV2::where('user_id', $data['user_id'])->where('company_id', $data['company_id'])->count();
190
191            if ($count == 0) {
192                TblApproversV2::create($data);
193            }
194
195            TblApprovers::where('user_id', $data['user_id'])->where('company_id', $data['company_id'])->delete();
196
197            return $this->list_approvers($data['company_id']);
198
199        } catch (\Exception $e) {
200            report(AppException::fromException($e, 'CREATE_APPROVER_V2_EXCEPTION'));
201            return response(['message' => 'KO', 'error' => $e->getMessage()]);
202        }
203
204    }
205
206    public function create_workflow_approver(Request $request)
207    {
208
209        try {
210
211            $data = $request->all();
212
213            $count = TblWorkflowApprover::where('user_id', $data['user_id'])->where('company_id', $data['company_id'])->count();
214
215            if ($count == 0) {
216                TblWorkflowApprover::create($data);
217            }
218
219            return $this->list_workflow_approvers($data['company_id']);
220
221        } catch (\Exception $e) {
222            report(AppException::fromException($e, 'CREATE_WORKFLOW_APPROVER_EXCEPTION'));
223            return response(['message' => 'KO', 'error' => $e->getMessage()]);
224        }
225
226    }
227
228    function delete_approver($userId): ResponseFactory|Response{
229
230        try {
231            
232            $userId = addslashes((string) $userId);
233
234            TblApprovers::where('user_id', $userId)->delete();
235
236            return response(['message' => 'OK']);
237
238        } catch (\Exception $e) {
239            report(AppException::fromException($e, 'DELETE_APPROVER_EXCEPTION'));
240            return response(['message' => 'KO', 'error' => $e->getMessage()]);
241        }
242
243    }
244
245    function delete_approver_v2($userId): ResponseFactory|Response{
246
247        try {
248            
249            $userId = addslashes((string) $userId);
250
251            TblApproversV2::where('user_id', $userId)->delete();
252
253            return response(['message' => 'OK']);
254
255        } catch (\Exception $e) {
256            report(AppException::fromException($e, 'DELETE_APPROVER_V2_EXCEPTION'));
257            return response(['message' => 'KO', 'error' => $e->getMessage()]);
258        }
259
260    }
261
262    function delete_workflow_approver($userId): ResponseFactory|Response{
263
264        try {
265            
266            $userId = addslashes((string) $userId);
267
268            TblWorkflowApprover::where('user_id', $userId)->delete();
269
270            return response(['message' => 'OK']);
271
272        } catch (\Exception $e) {
273            report(AppException::fromException($e, 'DELETE_WORKFLOW_APPROVER_EXCEPTION'));
274            return response(['message' => 'KO', 'error' => $e->getMessage()]);
275        }
276
277    }
278
279    function list_approval_budget_types(): ResponseFactory|Response{
280
281        try {
282
283            $query = 'SELECT 
284                        a.budget_type_id, 
285                        a.name, 
286                        (
287                        SELECT 
288                            id 
289                        FROM 
290                            tbl_approval_budget_types 
291                        WHERE 
292                            budget_type_id = a.budget_type_id
293                        ) is_selected
294                    FROM 
295                        tbl_budget_types a
296                    ORDER BY a.name ASC';
297
298            $result = DB::select($query);
299
300            return response([
301                'message' => 'OK',
302                'data' => $result,
303            ]);
304
305        } catch (\Exception $e) {
306            report(AppException::fromException($e, 'LIST_APPROVAL_BUDGET_TYPES_EXCEPTION'));
307            return response(['message' => 'KO', 'error' => $e->getMessage()]);
308        }
309
310    }
311
312    public function create_approval_budget_type(Request $request)
313    {
314
315        try {
316
317            $data = $request->all();
318
319            TblApprovalBudgetTypes::create($data);
320
321            return $this->list_approval_budget_types();
322
323        } catch (\Exception $e) {
324            report(AppException::fromException($e, 'CREATE_APPROVAL_BUDGET_TYPE_EXCEPTION'));
325            return response(['message' => 'KO', 'error' => $e->getMessage()]);
326        }
327
328    }
329
330    public function delete_approval_budget_type($budgetTypeId)
331    {
332
333        try {
334            
335            $budgetTypeId = addslashes((string) $budgetTypeId);
336
337            TblApprovalBudgetTypes::where('budget_type_id', $budgetTypeId)->delete();
338
339            return $this->list_approval_budget_types();
340
341        } catch (\Exception $e) {
342            report(AppException::fromException($e, 'DELETE_APPROVAL_BUDGET_TYPE_EXCEPTION'));
343            return response(['message' => 'KO', 'error' => $e->getMessage()]);
344        }
345
346    }
347}