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