Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.96% covered (success)
97.96%
48 / 49
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApprovalThresholdsController
97.96% covered (success)
97.96%
48 / 49
83.33% covered (warning)
83.33%
5 / 6
14
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
 list
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
6.01
 create
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 update
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 delete
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 whitelist
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Exceptions\AppException;
6use App\Models\TblApprovalThresholds;
7use Illuminate\Contracts\Routing\ResponseFactory;
8use Illuminate\Http\Request;
9use Illuminate\Http\Response;
10use Illuminate\Support\Facades\App;
11
12/**
13 * Admin CRUD for the approval threshold matrix (`tbl_approval_thresholds`),
14 * SOP v2.0 "Circuito de aprobación de presupuestos". Powers the Phase 7
15 * config screen that lets an admin view/edit the two thresholds, the financial
16 * minimums and the disabled flag per matrix row.
17 *
18 * Follows the Approvals controller conventions: try/catch, success returns
19 * ['message' => 'OK', ...] and any thrown exception is reported and turned into
20 * ['message' => 'KO', 'error' => ...]. Writes are whitelisted to the model's
21 * $fillable columns so the client can never set arbitrary attributes.
22 */
23class ApprovalThresholdsController extends Controller
24{
25    private $locale;
26
27    private $userId;
28
29    public function __construct()
30    {
31        $this->locale = request()->header('Locale-Id');
32        $this->userId = request()->header('User-Id');
33
34        App::setLocale($this->locale);
35    }
36
37    /**
38     * List matrix rows, optionally filtered by company (route param) and/or
39     * region (query string). Ordered so specific rows precede the catch-all.
40     */
41    public function list(Request $request, $companyId = null): ResponseFactory|Response
42    {
43        try {
44
45            $query = TblApprovalThresholds::query();
46
47            if ($companyId !== null && $companyId !== '' && strtolower((string) $companyId) !== 'null') {
48                $query->where('company_id', intval(addslashes((string) $companyId)));
49            }
50
51            $region = $request->query('region');
52            if (! empty($region)) {
53                $query->where('region', $region);
54            }
55
56            $result = $query
57                ->orderBy('company_id')
58                ->orderBy('is_catch_all')
59                ->orderBy('priority')
60                ->orderBy('id')
61                ->get();
62
63            return response([
64                'message' => 'OK',
65                'data' => $result,
66            ]);
67
68        } catch (\Exception $e) {
69            report(AppException::fromException($e, 'LIST_APPROVAL_THRESHOLDS_EXCEPTION'));
70
71            return response(['message' => 'KO', 'error' => $e->getMessage()]);
72        }
73    }
74
75    /**
76     * Insert a new matrix row. Only $fillable attributes are accepted.
77     */
78    public function create(Request $request): ResponseFactory|Response
79    {
80        try {
81
82            $data = $this->whitelist($request);
83
84            $row = TblApprovalThresholds::create($data);
85
86            return response([
87                'message' => 'OK',
88                'data' => $row,
89            ]);
90
91        } catch (\Exception $e) {
92            report(AppException::fromException($e, 'CREATE_APPROVAL_THRESHOLD_EXCEPTION'));
93
94            return response(['message' => 'KO', 'error' => $e->getMessage()]);
95        }
96    }
97
98    /**
99     * Update an existing matrix row. Only $fillable attributes are accepted.
100     */
101    public function update(Request $request, $id): ResponseFactory|Response
102    {
103        try {
104
105            $id = intval(addslashes((string) $id));
106
107            $data = $this->whitelist($request);
108
109            $affected = TblApprovalThresholds::where('id', $id)->update($data);
110
111            return response([
112                'message' => 'OK',
113                'data' => $affected,
114            ]);
115
116        } catch (\Exception $e) {
117            report(AppException::fromException($e, 'UPDATE_APPROVAL_THRESHOLD_EXCEPTION'));
118
119            return response(['message' => 'KO', 'error' => $e->getMessage()]);
120        }
121    }
122
123    /**
124     * Delete a matrix row.
125     */
126    public function delete($id): ResponseFactory|Response
127    {
128        try {
129
130            $id = intval(addslashes((string) $id));
131
132            TblApprovalThresholds::where('id', $id)->delete();
133
134            return response(['message' => 'OK']);
135
136        } catch (\Exception $e) {
137            report(AppException::fromException($e, 'DELETE_APPROVAL_THRESHOLD_EXCEPTION'));
138
139            return response(['message' => 'KO', 'error' => $e->getMessage()]);
140        }
141    }
142
143    /**
144     * Reduce the request to the model's fillable columns so a client can never
145     * set arbitrary attributes (id, timestamps, etc.).
146     *
147     * @return array<string, mixed>
148     */
149    private function whitelist(Request $request): array
150    {
151        $fillable = (new TblApprovalThresholds)->getFillable();
152
153        return $request->only($fillable);
154    }
155}