Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.03% covered (warning)
72.03%
85 / 118
21.43% covered (danger)
21.43%
3 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
Approvals
72.03% covered (warning)
72.03%
85 / 118
21.43% covered (danger)
21.43%
3 / 14
54.40
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get_approvals
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 update_approval
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
2.08
 list_approvers
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 list_workflow_approvers
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
2.06
 create_approver
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
3.33
 create_approver_v2
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
3.33
 create_workflow_approver
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
3.47
 delete_approver
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
3.71
 delete_approver_v2
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
3.71
 delete_workflow_approver
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
2.50
 list_approval_budget_types
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
2.11
 create_approval_budget_type
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
2.50
 delete_approval_budget_type
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
2.50
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    /**
239     * Removes the user as technical approver of ONE company. Before, the
240     * delete was keyed on user_id alone and wiped the person's rows in every
241     * company (2026-09-25: removing Jordi Fire from company 9 also removed
242     * him from Grupo Fire Catalunya, which was left with no approver).
243     * Without a company the call is refused rather than deleting everywhere,
244     * so an old cached frontend can't repeat that.
245     */
246    public function delete_approver($userId, $companyId = null): ResponseFactory|Response
247    {
248
249        try {
250
251            if (! $companyId) {
252                return response(['message' => 'KO', 'error' => 'company_id is required to remove an approver'], 422);
253            }
254
255            TblApprovers::where('user_id', (int) $userId)->where('company_id', (int) $companyId)->delete();
256
257            return response(['message' => 'OK']);
258
259        } catch (\Exception $e) {
260            report(AppException::fromException($e, 'DELETE_APPROVER_EXCEPTION'));
261
262            return response(['message' => 'KO', 'error' => $e->getMessage()]);
263        }
264
265    }
266
267    /** Financial-approver twin of delete_approver: one company only. */
268    public function delete_approver_v2($userId, $companyId = null): ResponseFactory|Response
269    {
270
271        try {
272
273            if (! $companyId) {
274                return response(['message' => 'KO', 'error' => 'company_id is required to remove an approver'], 422);
275            }
276
277            TblApproversV2::where('user_id', (int) $userId)->where('company_id', (int) $companyId)->delete();
278
279            return response(['message' => 'OK']);
280
281        } catch (\Exception $e) {
282            report(AppException::fromException($e, 'DELETE_APPROVER_V2_EXCEPTION'));
283
284            return response(['message' => 'KO', 'error' => $e->getMessage()]);
285        }
286
287    }
288
289    public function delete_workflow_approver($userId): ResponseFactory|Response
290    {
291
292        try {
293
294            $userId = addslashes((string) $userId);
295
296            TblWorkflowApprover::where('user_id', $userId)->delete();
297
298            return response(['message' => 'OK']);
299
300        } catch (\Exception $e) {
301            report(AppException::fromException($e, 'DELETE_WORKFLOW_APPROVER_EXCEPTION'));
302
303            return response(['message' => 'KO', 'error' => $e->getMessage()]);
304        }
305
306    }
307
308    public function list_approval_budget_types(): ResponseFactory|Response
309    {
310
311        try {
312
313            $query = 'SELECT 
314                        a.budget_type_id, 
315                        a.name, 
316                        (
317                        SELECT 
318                            id 
319                        FROM 
320                            tbl_approval_budget_types 
321                        WHERE 
322                            budget_type_id = a.budget_type_id
323                        ) is_selected
324                    FROM 
325                        tbl_budget_types a
326                    ORDER BY a.name ASC';
327
328            $result = DB::select($query);
329
330            return response([
331                'message' => 'OK',
332                'data' => $result,
333            ]);
334
335        } catch (\Exception $e) {
336            report(AppException::fromException($e, 'LIST_APPROVAL_BUDGET_TYPES_EXCEPTION'));
337
338            return response(['message' => 'KO', 'error' => $e->getMessage()]);
339        }
340
341    }
342
343    public function create_approval_budget_type(Request $request)
344    {
345
346        try {
347
348            $data = $request->all();
349
350            TblApprovalBudgetTypes::create($data);
351
352            return $this->list_approval_budget_types();
353
354        } catch (\Exception $e) {
355            report(AppException::fromException($e, 'CREATE_APPROVAL_BUDGET_TYPE_EXCEPTION'));
356
357            return response(['message' => 'KO', 'error' => $e->getMessage()]);
358        }
359
360    }
361
362    public function delete_approval_budget_type($budgetTypeId)
363    {
364
365        try {
366
367            $budgetTypeId = addslashes((string) $budgetTypeId);
368
369            TblApprovalBudgetTypes::where('budget_type_id', $budgetTypeId)->delete();
370
371            return $this->list_approval_budget_types();
372
373        } catch (\Exception $e) {
374            report(AppException::fromException($e, 'DELETE_APPROVAL_BUDGET_TYPE_EXCEPTION'));
375
376            return response(['message' => 'KO', 'error' => $e->getMessage()]);
377        }
378
379    }
380}