Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
13.06% covered (danger)
13.06%
97 / 743
9.23% covered (danger)
9.23%
6 / 65
CRAP
0.00% covered (danger)
0.00%
0 / 1
Configurations
13.06% covered (danger)
13.06%
97 / 743
9.23% covered (danger)
9.23%
6 / 65
21952.77
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 list_customer_types
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 list_segments
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_reasons
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_budget_types
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
 list_budget_type_groups
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_visit_types
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_visit_type_groups
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_budget_status
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_source
66.67% covered (warning)
66.67%
8 / 12
0.00% covered (danger)
0.00%
0 / 1
3.33
 list_reason_for_not_following_up
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 list_reason_for_rejection
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 customer_types
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 segments
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 reasons
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 budget_types
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 budget_type_groups
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 visit_types
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 visit_type_groups
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 budget_status
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 source
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
4
 reason_for_not_following_up
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 reason_for_rejection
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_customer_types
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
2.11
 add_segments
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_reasons
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_budget_types
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_budget_type_groups
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_visit_types
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_visit_type_groups
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_budget_status
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_source
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_reason_for_not_following_up
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 add_reason_for_rejection
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 update_customer_types
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
5.16
 update_segments
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 update_reasons
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 update_budget_types
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 update_budget_type_groups
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 update_visit_types
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 update_visit_type_groups
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 update_budget_status
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 update_source
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 update_reason_for_not_following_up
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 update_reason_for_rejection
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 delete_customer_types
82.35% covered (warning)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
3.05
 delete_segments
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_reasons
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_budget_types
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_budget_type_groups
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_visit_types
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_visit_type_groups
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_budget_status
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_source
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
20
 list_codigos
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
 add_codigo
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
 update_codigo
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
20
 delete_codigo
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 delete_reason_for_not_following_up
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 delete_reason_for_rejection
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 list_work_status
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 work_status
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update_work_status
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 delete_work_status
41.18% covered (danger)
41.18%
7 / 17
0.00% covered (danger)
0.00%
0 / 1
4.83
 add_work_status
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
2.11
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Exceptions\AppException;
6use App\Models\TblBudgetStatus;
7use App\Models\TblBudgetTypeGroups;
8use App\Models\TblBudgetTypes;
9use App\Models\TblCustomerTypes;
10use App\Models\TblOngoingJobs;
11use App\Models\TblPipelines;
12use App\Models\TblQuotations;
13use App\Models\TblReasonForNotFollowingUp;
14use App\Models\TblReasonForRejection;
15use App\Models\TblReasons;
16use App\Models\TblSegments;
17use App\Models\TblSourceCodigos;
18use App\Models\TblSources;
19use App\Models\TblVisitTypeGroups;
20use App\Models\TblVisitTypes;
21use App\Models\TblWorkStatus;
22use App\Services\ResultCache;
23use App\Services\ZeusBridge;
24use Illuminate\Contracts\Routing\ResponseFactory;
25use Illuminate\Http\Request;
26use Illuminate\Http\Response;
27use Illuminate\Support\Facades\App;
28use Illuminate\Support\Facades\Cache;
29use Illuminate\Support\Facades\DB;
30
31class Configurations extends Controller
32{
33    private $locale;
34
35    private $customer_types = [];
36
37    private $segments = [];
38
39    private $reasons = [];
40
41    private $budget_types = [];
42
43    private $budget_status = [];
44
45    private $budget_type_groups = [];
46
47    private $source = [];
48
49    private $reason_for_not_following_up = [];
50
51    private $reason_for_rejection = [];
52
53    private $work_status = [];
54
55    private $visit_types = [];
56
57    private $visit_type_groups = [];
58
59    public function __construct()
60    {
61        $this->locale = request()->header('Locale-Id');
62
63        App::setLocale($this->locale);
64    }
65
66    public function list_customer_types(): ResponseFactory|Response
67    {
68
69        try {
70
71            $this->customer_types();
72
73            return response([
74                'message' => 'OK',
75                'data' => $this->customer_types,
76            ]);
77
78        } catch (\Exception $e) {
79            report(AppException::fromException($e, 'LIST_CUSTOMER_TYPES_EXCEPTION'));
80
81            return response(['message' => 'KO', 'error' => $e->getMessage()]);
82        }
83
84    }
85
86    public function list_segments(): ResponseFactory|Response
87    {
88
89        try {
90
91            $this->segments();
92
93            return response([
94                'message' => 'OK',
95                'data' => $this->segments,
96            ]);
97
98        } catch (\Exception $e) {
99            report(AppException::fromException($e, 'LIST_SEGMENTS_EXCEPTION'));
100
101            return response(['message' => 'KO', 'error' => $e->getMessage()]);
102        }
103
104    }
105
106    public function list_reasons(): ResponseFactory|Response
107    {
108
109        try {
110
111            $this->reasons();
112
113            return response([
114                'message' => 'OK',
115                'data' => $this->reasons,
116            ]);
117
118        } catch (\Exception $e) {
119            report(AppException::fromException($e, 'LIST_REASONS_EXCEPTION'));
120
121            return response(['message' => 'KO', 'error' => $e->getMessage()]);
122        }
123
124    }
125
126    public function list_budget_types(): ResponseFactory|Response
127    {
128
129        try {
130
131            $this->budget_types();
132
133            return response([
134                'message' => 'OK',
135                'data' => $this->budget_types,
136            ]);
137
138        } catch (\Exception $e) {
139            report(AppException::fromException($e, 'LIST_BUDGET_TYPES_EXCEPTION'));
140
141            return response(['message' => 'KO', 'error' => $e->getMessage()]);
142        }
143
144    }
145
146    public function list_budget_type_groups(): ResponseFactory|Response
147    {
148
149        try {
150
151            $this->budget_type_groups();
152
153            return response([
154                'message' => 'OK',
155                'data' => $this->budget_type_groups,
156            ]);
157
158        } catch (\Exception $e) {
159            report(AppException::fromException($e, 'LIST_BUDGET_TYPE_GROUPS_EXCEPTION'));
160
161            return response(['message' => 'KO', 'error' => $e->getMessage()]);
162        }
163
164    }
165
166    public function list_visit_types(): ResponseFactory|Response
167    {
168
169        try {
170
171            $this->visit_types();
172
173            return response([
174                'message' => 'OK',
175                'data' => $this->visit_types,
176            ]);
177
178        } catch (\Exception $e) {
179            report(AppException::fromException($e, 'LIST_VISIT_TYPES_EXCEPTION'));
180
181            return response(['message' => 'KO', 'error' => $e->getMessage()]);
182        }
183
184    }
185
186    public function list_visit_type_groups(): ResponseFactory|Response
187    {
188
189        try {
190
191            $this->visit_type_groups();
192
193            return response([
194                'message' => 'OK',
195                'data' => $this->visit_type_groups,
196            ]);
197
198        } catch (\Exception $e) {
199            report(AppException::fromException($e, 'LIST_VISIT_TYPE_GROUPS_EXCEPTION'));
200
201            return response(['message' => 'KO', 'error' => $e->getMessage()]);
202        }
203
204    }
205
206    public function list_budget_status(): ResponseFactory|Response
207    {
208
209        try {
210
211            $this->budget_status();
212
213            return response([
214                'message' => 'OK',
215                'data' => $this->budget_status,
216            ]);
217
218        } catch (\Exception $e) {
219            report(AppException::fromException($e, 'LIST_BUDGET_STATUS_EXCEPTION'));
220
221            return response(['message' => 'KO', 'error' => $e->getMessage()]);
222        }
223
224    }
225
226    public function list_source(): ResponseFactory|Response
227    {
228
229        try {
230
231            // Titan â†’ Zeus bridge (read) â€” region from the request/header.
232            $zeus = app(ZeusBridge::class);
233            $forward = $zeus->listMasterData(request(), '/api/listSource');
234
235            if ($zeus->shouldReturnZeusResponse($forward)) {
236                return $forward->toTitanResponse();
237            }
238
239            $this->source();
240
241            return response([
242                'message' => 'OK',
243                'data' => $this->source,
244            ]);
245
246        } catch (\Exception $e) {
247            report(AppException::fromException($e, 'LIST_SOURCE_EXCEPTION'));
248
249            return response(['message' => 'KO', 'error' => $e->getMessage()]);
250        }
251
252    }
253
254    public function list_reason_for_not_following_up(): ResponseFactory|Response
255    {
256
257        try {
258
259            $this->reason_for_not_following_up();
260
261            return response([
262                'message' => 'OK',
263                'data' => $this->reason_for_not_following_up,
264            ]);
265
266        } catch (\Exception $e) {
267            report(AppException::fromException($e, 'LIST_REASON_FOR_NOT_FOLLOWING_UP_EXCEPTION'));
268
269            return response(['message' => 'KO', 'error' => $e->getMessage()]);
270        }
271
272    }
273
274    public function list_reason_for_rejection(): ResponseFactory|Response
275    {
276
277        try {
278
279            $this->reason_for_rejection();
280
281            return response([
282                'message' => 'OK',
283                'data' => $this->reason_for_rejection,
284            ]);
285
286        } catch (\Exception $e) {
287            report(AppException::fromException($e, 'LIST_REASON_FOR_REJECTION_EXCEPTION'));
288
289            return response(['message' => 'KO', 'error' => $e->getMessage()]);
290        }
291
292    }
293
294    public function customer_types(): void
295    {
296
297        $this->customer_types = TblCustomerTypes::orderByRaw('ISNULL(priority), priority ASC')->get();
298
299    }
300
301    public function segments(): void
302    {
303
304        $this->segments = TblSegments::orderByRaw('ISNULL(priority), priority ASC')->get();
305
306    }
307
308    public function reasons(): void
309    {
310
311        $this->reasons = TblReasons::orderByRaw('ISNULL(priority), priority ASC')->get();
312
313    }
314
315    public function budget_types(): void
316    {
317
318        $query = "SELECT
319                    bt.budget_type_id,
320                    bt.name,
321                    bt.duration,
322                    bt.priority,
323                    bt.color,
324                    bt.include,
325                    bt.budget_type_group_id,
326                    btg.name budget_type_group,
327                    bt.disabled,
328                    bt.created_by,
329                    bt.created_at
330                FROM tbl_budget_types bt
331                LEFT JOIN tbl_budget_type_groups btg
332                    ON bt.budget_type_group_id = btg.budget_type_group_id
333                WHERE bt.name != ''
334                ORDER BY ISNULL(bt.priority), bt.priority ASC";
335
336        $this->budget_types = DB::select($query);
337
338    }
339
340    public function budget_type_groups(): void
341    {
342        $this->budget_type_groups = TblBudgetTypeGroups::orderByRaw('ISNULL(priority), priority ASC')->get();
343    }
344
345    public function visit_types(): void
346    {
347
348        $query = 'SELECT
349                    vt.visit_type_id,
350                    vt.name,
351                    vt.priority,
352                    vt.visit_type_group_id,
353                    vtg.name visit_type_group,
354                    vt.disabled,
355                    vt.created_by,
356                    vt.created_at
357                FROM tbl_visit_types vt
358                LEFT JOIN tbl_visit_type_groups vtg
359                    ON vt.visit_type_group_id = vtg.visit_type_group_id
360                ORDER BY ISNULL(vt.priority), vt.priority ASC';
361
362        $this->visit_types = DB::select($query);
363
364    }
365
366    public function visit_type_groups(): void
367    {
368        $this->visit_type_groups = TblVisitTypeGroups::orderByRaw('ISNULL(priority), priority ASC')->get();
369    }
370
371    public function budget_status(): void
372    {
373        $this->budget_status = TblBudgetStatus::orderByRaw('ISNULL(priority), priority ASC')->get();
374    }
375
376    public function source(): void
377    {
378        $sources = TblSources::orderByRaw('ISNULL(priority), priority ASC')->get();
379
380        $counts = TblQuotations::whereNotNull('source_id')
381            ->selectRaw('source_id, COUNT(*) as total')
382            ->groupBy('source_id')
383            ->pluck('total', 'source_id');
384
385        $byRegion = DB::select(
386            "SELECT q.source_id, COALESCE(c.region, 'Unknown') AS region, COUNT(*) AS total
387             FROM tbl_quotations q
388             LEFT JOIN tbl_companies c ON q.company_id = c.company_id
389             WHERE q.source_id IS NOT NULL
390             GROUP BY q.source_id, COALESCE(c.region, 'Unknown')"
391        );
392
393        $regionMap = [];
394        foreach ($byRegion as $row) {
395            if (! isset($regionMap[$row->source_id])) {
396                $regionMap[$row->source_id] = [];
397            }
398            $regionMap[$row->source_id][$row->region] = (int) $row->total;
399        }
400
401        $result = [];
402        foreach ($sources as $source) {
403            $row = $source->toArray();
404            $row['total_quotations'] = (int) ($counts[$source->source_id] ?? 0);
405            $row['by_region'] = (object) ($regionMap[$source->source_id] ?? []);
406            $result[] = $row;
407        }
408
409        $this->source = $result;
410    }
411
412    public function reason_for_not_following_up(): void
413    {
414        $this->reason_for_not_following_up = TblReasonForNotFollowingUp::orderByRaw('ISNULL(priority), priority ASC')->get();
415    }
416
417    public function reason_for_rejection(): void
418    {
419
420        $this->reason_for_rejection = TblReasonForRejection::orderByRaw('ISNULL(priority), priority ASC')->get();
421
422    }
423
424    public function add_customer_types(Request $request): ResponseFactory|Response
425    {
426
427        try {
428
429            $data = $request->all();
430
431            TblCustomerTypes::create($data);
432
433            $this->customer_types();
434
435            return response([
436                'message' => 'OK',
437                'data' => $this->customer_types,
438            ]);
439
440        } catch (\Exception $e) {
441            report(AppException::fromException($e, 'ADD_CUSTOMER_TYPES_EXCEPTION'));
442
443            return response(['message' => 'KO', 'error' => $e->getMessage()]);
444        }
445
446    }
447
448    public function add_segments(Request $request): ResponseFactory|Response
449    {
450
451        try {
452
453            $data = $request->all();
454
455            TblSegments::create($data);
456
457            $this->segments();
458
459            return response([
460                'message' => 'OK',
461                'data' => $this->segments,
462            ]);
463
464        } catch (\Exception $e) {
465            report(AppException::fromException($e, 'ADD_SEGMENTS_EXCEPTION'));
466
467            return response(['message' => 'KO', 'error' => $e->getMessage()]);
468        }
469
470    }
471
472    public function add_reasons(Request $request): ResponseFactory|Response
473    {
474
475        try {
476
477            $data = $request->all();
478
479            TblReasons::create($data);
480
481            $this->reasons();
482
483            return response([
484                'message' => 'OK',
485                'data' => $this->reasons,
486            ]);
487
488        } catch (\Exception $e) {
489            report(AppException::fromException($e, 'ADD_REASONS_EXCEPTION'));
490
491            return response(['message' => 'KO', 'error' => $e->getMessage()]);
492        }
493
494    }
495
496    public function add_budget_types(Request $request): ResponseFactory|Response
497    {
498
499        try {
500
501            $data = $request->all();
502
503            TblBudgetTypes::create($data);
504
505            $this->budget_types();
506
507            return response([
508                'message' => 'OK',
509                'data' => $this->budget_types,
510            ]);
511
512        } catch (\Exception $e) {
513            report(AppException::fromException($e, 'ADD_BUDGET_TYPES_EXCEPTION'));
514
515            return response(['message' => 'KO', 'error' => $e->getMessage()]);
516        }
517
518    }
519
520    public function add_budget_type_groups(Request $request): ResponseFactory|Response
521    {
522
523        try {
524
525            $data = $request->all();
526
527            TblBudgetTypeGroups::create($data);
528
529            $this->budget_type_groups();
530
531            return response([
532                'message' => 'OK',
533                'data' => $this->budget_type_groups,
534            ]);
535
536        } catch (\Exception $e) {
537            report(AppException::fromException($e, 'ADD_BUDGET_TYPE_GROUPS_EXCEPTION'));
538
539            return response(['message' => 'KO', 'error' => $e->getMessage()]);
540        }
541
542    }
543
544    public function add_visit_types(Request $request): ResponseFactory|Response
545    {
546
547        try {
548
549            $data = $request->all();
550
551            TblVisitTypes::create($data);
552
553            $this->visit_types();
554
555            return response([
556                'message' => 'OK',
557                'data' => $this->visit_types,
558            ]);
559
560        } catch (\Exception $e) {
561            report(AppException::fromException($e, 'ADD_VISIT_TYPES_EXCEPTION'));
562
563            return response(['message' => 'KO', 'error' => $e->getMessage()]);
564        }
565
566    }
567
568    public function add_visit_type_groups(Request $request): ResponseFactory|Response
569    {
570
571        try {
572
573            $data = $request->all();
574
575            TblVisitTypeGroups::create($data);
576
577            $this->visit_type_groups();
578
579            return response([
580                'message' => 'OK',
581                'data' => $this->visit_type_groups,
582            ]);
583
584        } catch (\Exception $e) {
585            report(AppException::fromException($e, 'ADD_VISIT_TYPE_GROUPS_EXCEPTION'));
586
587            return response(['message' => 'KO', 'error' => $e->getMessage()]);
588        }
589
590    }
591
592    public function add_budget_status(Request $request): ResponseFactory|Response
593    {
594
595        try {
596
597            $data = $request->all();
598
599            TblBudgetStatus::create($data);
600
601            $this->budget_status();
602
603            return response([
604                'message' => 'OK',
605                'data' => $this->budget_status,
606            ]);
607
608        } catch (\Exception $e) {
609            report(AppException::fromException($e, 'ADD_BUDGET_STATUS_EXCEPTION'));
610
611            return response(['message' => 'KO', 'error' => $e->getMessage()]);
612        }
613
614    }
615
616    public function add_source(Request $request): ResponseFactory|Response
617    {
618
619        try {
620
621            $data = $request->all();
622
623            TblSources::create($data);
624
625            $this->source();
626
627            return response([
628                'message' => 'OK',
629                'data' => $this->source,
630            ]);
631
632        } catch (\Exception $e) {
633            report(AppException::fromException($e, 'ADD_SOURCE_EXCEPTION'));
634
635            return response(['message' => 'KO', 'error' => $e->getMessage()]);
636        }
637
638    }
639
640    public function add_reason_for_not_following_up(Request $request): ResponseFactory|Response
641    {
642
643        try {
644
645            $data = $request->all();
646
647            TblReasonForNotFollowingUp::create($data);
648
649            $this->reason_for_not_following_up();
650
651            return response([
652                'message' => 'OK',
653                'data' => $this->reason_for_not_following_up,
654            ]);
655
656        } catch (\Exception $e) {
657            report(AppException::fromException($e, 'ADD_REASON_FOR_NOT_FOLLOWING_UP_EXCEPTION'));
658
659            return response(['message' => 'KO', 'error' => $e->getMessage()]);
660        }
661
662    }
663
664    public function add_reason_for_rejection(Request $request): ResponseFactory|Response
665    {
666
667        try {
668
669            $data = $request->all();
670
671            TblReasonForRejection::create($data);
672
673            $this->reason_for_rejection();
674
675            return response([
676                'message' => 'OK',
677                'data' => $this->reason_for_rejection,
678            ]);
679
680        } catch (\Exception $e) {
681            report(AppException::fromException($e, 'ADD_REASON_FOR_REJECTION_EXCEPTION'));
682
683            return response(['message' => 'KO', 'error' => $e->getMessage()]);
684        }
685
686    }
687
688    public function update_customer_types(Request $request, $customerTypeId): ResponseFactory|Response
689    {
690
691        try {
692
693            $data = $request->all();
694            $customerTypeId = addslashes((string) $customerTypeId);
695
696            if (isset($data['data'])) {
697                $priority = $data['data'];
698                if (count($priority) > 0) {
699                    foreach ($priority as $item) {
700                        TblCustomerTypes::where('customer_type_id', $item['customer_type_id'])->update(['priority' => $item['priority']]);
701                    }
702                }
703            } else {
704                TblCustomerTypes::where('customer_type_id', $customerTypeId)->update($data);
705            }
706
707            $this->customer_types();
708
709            return response([
710                'message' => 'OK',
711                'data' => $this->customer_types,
712            ]);
713
714        } catch (\Exception $e) {
715            report(AppException::fromException($e, 'UPDATE_CUSTOMER_TYPES_EXCEPTION'));
716
717            return response(['message' => 'KO', 'error' => $e->getMessage()]);
718        }
719
720    }
721
722    public function update_segments(Request $request, $segmentId): ResponseFactory|Response
723    {
724
725        try {
726
727            $data = $request->all();
728            $segmentId = addslashes((string) $segmentId);
729
730            if (isset($data['data'])) {
731                $priority = $data['data'];
732                if (count($priority) > 0) {
733                    foreach ($priority as $item) {
734                        TblSegments::where('segment_id', $item['segment_id'])->update(['priority' => $item['priority']]);
735                    }
736                }
737            } else {
738                TblSegments::where('segment_id', $segmentId)->update($data);
739            }
740
741            $this->segments();
742
743            return response([
744                'message' => 'OK',
745                'data' => $this->segments,
746            ]);
747
748        } catch (\Exception $e) {
749            report(AppException::fromException($e, 'UPDATE_SEGMENTS_EXCEPTION'));
750
751            return response(['message' => 'KO', 'error' => $e->getMessage()]);
752        }
753
754    }
755
756    public function update_reasons(Request $request, $reasonId): ResponseFactory|Response
757    {
758
759        try {
760
761            $data = $request->all();
762            $reasonId = addslashes((string) $reasonId);
763
764            if (isset($data['data'])) {
765                $priority = $data['data'];
766                if (count($priority) > 0) {
767                    foreach ($priority as $item) {
768                        TblReasons::where('reason_id', $item['reason_id'])->update(['priority' => $item['priority']]);
769                    }
770                }
771            } else {
772                TblReasons::where('reason_id', $reasonId)->update($data);
773            }
774
775            $this->reasons();
776
777            return response([
778                'message' => 'OK',
779                'data' => $this->reasons,
780            ]);
781
782        } catch (\Exception $e) {
783            report(AppException::fromException($e, 'UPDATE_REASONS_EXCEPTION'));
784
785            return response(['message' => 'KO', 'error' => $e->getMessage()]);
786        }
787
788    }
789
790    public function update_budget_types(Request $request, $budgetTypeId): ResponseFactory|Response
791    {
792
793        try {
794
795            $data = $request->all();
796            $budgetTypeId = addslashes((string) $budgetTypeId);
797
798            if (isset($data['data'])) {
799                $priority = $data['data'];
800                if (count($priority) > 0) {
801                    foreach ($priority as $item) {
802                        TblBudgetTypes::where('budget_type_id', $item['budget_type_id'])->update(['priority' => $item['priority']]);
803                    }
804                }
805            } else {
806                TblBudgetTypes::where('budget_type_id', $budgetTypeId)->update($data);
807            }
808
809            // FIRE-1145: was Cache::flush() â€” lookup names join into both quotations and ongoing_jobs lists.
810            ResultCache::forgetDomain(['quotations', 'ongoing_jobs']);
811            $this->budget_types();
812
813            return response([
814                'message' => 'OK',
815                'data' => $this->budget_types,
816            ]);
817
818        } catch (\Exception $e) {
819            report(AppException::fromException($e, 'UPDATE_BUDGET_TYPES_EXCEPTION'));
820
821            return response(['message' => 'KO', 'error' => $e->getMessage()]);
822        }
823
824    }
825
826    public function update_budget_type_groups(Request $request, $budgetTypeGroupId): ResponseFactory|Response
827    {
828
829        try {
830
831            $data = $request->all();
832            $budgetTypeGroupId = addslashes((string) $budgetTypeGroupId);
833
834            if (isset($data['data'])) {
835                $priority = $data['data'];
836                if (count($priority) > 0) {
837                    foreach ($priority as $item) {
838                        TblBudgetTypeGroups::where('budget_type_group_id', $item['budget_type_group_id'])->update(['priority' => $item['priority']]);
839                    }
840                }
841            } else {
842                TblBudgetTypeGroups::where('budget_type_group_id', $budgetTypeGroupId)->update($data);
843            }
844
845            // FIRE-1145: was Cache::flush() â€” group names join into both quotations and ongoing_jobs lists.
846            ResultCache::forgetDomain(['quotations', 'ongoing_jobs']);
847            $this->budget_type_groups();
848
849            return response([
850                'message' => 'OK',
851                'data' => $this->budget_type_groups,
852            ]);
853
854        } catch (\Exception $e) {
855            report(AppException::fromException($e, 'UPDATE_BUDGET_TYPE_GROUPS_EXCEPTION'));
856
857            return response(['message' => 'KO', 'error' => $e->getMessage()]);
858        }
859
860    }
861
862    public function update_visit_types(Request $request, $visitTypeId): ResponseFactory|Response
863    {
864
865        try {
866
867            $data = $request->all();
868            $visitTypeId = addslashes((string) $visitTypeId);
869
870            if (isset($data['data'])) {
871                $priority = $data['data'];
872                if (count($priority) > 0) {
873                    foreach ($priority as $item) {
874                        TblVisitTypes::where('visit_type_id', $item['visit_type_id'])->update(['priority' => $item['priority']]);
875                    }
876                }
877            } else {
878                TblVisitTypes::where('visit_type_id', $visitTypeId)->update($data);
879            }
880
881            $this->visit_types();
882
883            return response([
884                'message' => 'OK',
885                'data' => $this->visit_types,
886            ]);
887
888        } catch (\Exception $e) {
889            report(AppException::fromException($e, 'UPDATE_VISIT_TYPES_EXCEPTION'));
890
891            return response(['message' => 'KO', 'error' => $e->getMessage()]);
892        }
893
894    }
895
896    public function update_visit_type_groups(Request $request, $visitTypeGroupId): ResponseFactory|Response
897    {
898
899        try {
900
901            $data = $request->all();
902            $visitTypeGroupId = addslashes((string) $visitTypeGroupId);
903
904            if (isset($data['data'])) {
905                $priority = $data['data'];
906                if (count($priority) > 0) {
907                    foreach ($priority as $item) {
908                        TblVisitTypeGroups::where('visit_type_group_id', $item['visit_type_group_id'])->update(['priority' => $item['priority']]);
909                    }
910                }
911            } else {
912                TblVisitTypeGroups::where('visit_type_group_id', $visitTypeGroupId)->update($data);
913            }
914
915            // FIRE-1145: was Cache::flush() â€” visit groups join into list_ongoing_jobs.
916            ResultCache::forgetDomain('ongoing_jobs');
917            $this->visit_type_groups();
918
919            return response([
920                'message' => 'OK',
921                'data' => $this->visit_type_groups,
922            ]);
923
924        } catch (\Exception $e) {
925            report(AppException::fromException($e, 'UPDATE_VISIT_TYPE_GROUPS_EXCEPTION'));
926
927            return response(['message' => 'KO', 'error' => $e->getMessage()]);
928        }
929
930    }
931
932    public function update_budget_status(Request $request, $budgetStatusId): ResponseFactory|Response
933    {
934
935        try {
936
937            $data = $request->all();
938            $budgetStatusId = addslashes((string) $budgetStatusId);
939
940            if (isset($data['data'])) {
941                $priority = $data['data'];
942                if (count($priority) > 0) {
943                    foreach ($priority as $item) {
944                        TblBudgetStatus::where('budget_status_id', $item['budget_status_id'])->update(['priority' => $item['priority']]);
945                    }
946                }
947            } else {
948                TblBudgetStatus::where('budget_status_id', $budgetStatusId)->update($data);
949            }
950
951            $this->budget_status();
952
953            return response([
954                'message' => 'OK',
955                'data' => $this->budget_status,
956            ]);
957
958        } catch (\Exception $e) {
959            report(AppException::fromException($e, 'UPDATE_BUDGET_STATUS_EXCEPTION'));
960
961            return response(['message' => 'KO', 'error' => $e->getMessage()]);
962        }
963
964    }
965
966    public function update_source(Request $request, $sourceId): ResponseFactory|Response
967    {
968
969        try {
970
971            $data = $request->all();
972            $sourceId = addslashes((string) $sourceId);
973
974            if (isset($data['data'])) {
975                $priority = $data['data'];
976                if (count($priority) > 0) {
977                    foreach ($priority as $item) {
978                        TblSources::where('source_id', $item['source_id'])->update(['priority' => $item['priority']]);
979                    }
980                }
981            } else {
982                TblSources::where('source_id', $sourceId)->update($data);
983            }
984
985            $this->source();
986
987            return response([
988                'message' => 'OK',
989                'data' => $this->source,
990            ]);
991
992        } catch (\Exception $e) {
993            report(AppException::fromException($e, 'UPDATE_SOURCE_EXCEPTION'));
994
995            return response(['message' => 'KO', 'error' => $e->getMessage()]);
996        }
997
998    }
999
1000    public function update_reason_for_not_following_up(Request $request, $reasonForNotFollowingUpId): ResponseFactory|Response
1001    {
1002
1003        try {
1004
1005            $data = $request->all();
1006            $reasonForNotFollowingUpId = addslashes((string) $reasonForNotFollowingUpId);
1007
1008            if (isset($data['data'])) {
1009                $priority = $data['data'];
1010                if (count($priority) > 0) {
1011                    foreach ($priority as $item) {
1012                        TblSources::where('reason_for_not_following_up_id', $item['reason_for_not_following_up_id'])->update(['priority' => $item['priority']]);
1013                    }
1014                }
1015            } else {
1016                TblReasonForNotFollowingUp::where('reason_for_not_following_up_id', $reasonForNotFollowingUpId)->update($data);
1017            }
1018
1019            $this->reason_for_not_following_up();
1020
1021            return response([
1022                'message' => 'OK',
1023                'data' => $this->reason_for_not_following_up,
1024            ]);
1025
1026        } catch (\Exception $e) {
1027            report(AppException::fromException($e, 'UPDATE_REASON_FOR_NOT_FOLLOWING_UP_EXCEPTION'));
1028
1029            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1030        }
1031
1032    }
1033
1034    public function update_reason_for_rejection(Request $request, $reasonForRejectionId): ResponseFactory|Response
1035    {
1036
1037        try {
1038
1039            $data = $request->all();
1040            $reasonForRejectionId = addslashes((string) $reasonForRejectionId);
1041
1042            if (isset($data['data'])) {
1043                $priority = $data['data'];
1044                if (count($priority) > 0) {
1045                    foreach ($priority as $item) {
1046                        TblReasonForRejection::where('reason_for_rejection_id', $item['reason_for_rejection_id'])->update(['priority' => $item['priority']]);
1047                    }
1048                }
1049            } else {
1050                TblReasonForRejection::where('reason_for_rejection_id', $reasonForRejectionId)->update($data);
1051            }
1052
1053            $this->reason_for_rejection();
1054
1055            return response([
1056                'message' => 'OK',
1057                'data' => $this->reason_for_rejection,
1058            ]);
1059
1060        } catch (\Exception $e) {
1061            report(AppException::fromException($e, 'UPDATE_REASON_FOR_REJECTION_EXCEPTION'));
1062
1063            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1064        }
1065
1066    }
1067
1068    public function delete_customer_types($customerTypeId): ResponseFactory|Response
1069    {
1070
1071        try {
1072
1073            $customerTypeId = addslashes((string) $customerTypeId);
1074
1075            $budgets = TblQuotations::where('customer_type_id', $customerTypeId)->count();
1076
1077            if ($budgets > 0) {
1078                return response([
1079                    'message' => 'KO',
1080                    'error' => __('language.customer_types.delete_message.message1').' '.$budgets.' '.__('language.customer_types.delete_message.message2'),
1081                ]);
1082            }
1083
1084            $customerType = TblCustomerTypes::where('customer_type_id', $customerTypeId)->first();
1085            TblCustomerTypes::where('customer_type_id', $customerTypeId)->delete();
1086
1087            $this->customer_types();
1088
1089            return response([
1090                'message' => 'OK',
1091                'data' => $this->customer_types,
1092            ]);
1093
1094        } catch (\Exception $e) {
1095            report(AppException::fromException($e, 'DELETE_CUSTOMER_TYPES_EXCEPTION'));
1096
1097            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1098        }
1099
1100    }
1101
1102    public function delete_segments($segmentId): ResponseFactory|Response
1103    {
1104
1105        try {
1106
1107            $segmentId = addslashes((string) $segmentId);
1108
1109            $budgets = TblQuotations::where('segment_id', $segmentId)->count();
1110
1111            if ($budgets > 0) {
1112                return response([
1113                    'message' => 'KO',
1114                    'error' => __('language.segments.delete_message.message1').' '.$budgets.' '.__('language.segments.delete_message.message2'),
1115                ]);
1116            }
1117
1118            $segment = TblSegments::where('segment_id', $segmentId)->first();
1119            TblSegments::where('segment_id', $segmentId)->delete();
1120
1121            $this->segments();
1122
1123            return response([
1124                'message' => 'OK',
1125                'data' => $this->segments,
1126            ]);
1127
1128        } catch (\Exception $e) {
1129            report(AppException::fromException($e, 'DELETE_SEGMENTS_EXCEPTION'));
1130
1131            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1132        }
1133
1134    }
1135
1136    public function delete_reasons($reasonId): ResponseFactory|Response
1137    {
1138
1139        try {
1140
1141            $reasonId = addslashes((string) $reasonId);
1142
1143            $budgets = TblQuotations::where('reason_id', $reasonId)->count();
1144
1145            if ($budgets > 0) {
1146                return response([
1147                    'message' => 'KO',
1148                    'error' => __('language.reasons.delete_message.message1').' '.$budgets.' '.__('language.reasons.delete_message.message2'),
1149                ]);
1150            }
1151
1152            $reasons = TblReasons::where('reason_id', $reasonId)->first();
1153            TblReasons::where('reason_id', $reasonId)->delete();
1154
1155            $this->reasons();
1156
1157            return response([
1158                'message' => 'OK',
1159                'data' => $this->reasons,
1160            ]);
1161
1162        } catch (\Exception $e) {
1163            report(AppException::fromException($e, 'DELETE_REASONS_EXCEPTION'));
1164
1165            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1166        }
1167
1168    }
1169
1170    public function delete_budget_types($budgetTypeId): ResponseFactory|Response
1171    {
1172
1173        try {
1174
1175            $budgetTypeId = addslashes((string) $budgetTypeId);
1176
1177            $budgets = TblQuotations::where('budget_type_id', $budgetTypeId)->count();
1178
1179            if ($budgets > 0) {
1180                return response([
1181                    'message' => 'KO',
1182                    'error' => __('language.budget_types.delete_message.message1').' '.$budgets.' '.__('language.budget_types.delete_message.message2'),
1183                ]);
1184            }
1185
1186            $budgetType = TblBudgetTypes::where('budget_type_id', $budgetTypeId)->first();
1187            TblBudgetTypes::where('budget_type_id', $budgetTypeId)->delete();
1188
1189            $this->budget_types();
1190
1191            return response([
1192                'message' => 'OK',
1193                'data' => $this->budget_types,
1194            ]);
1195
1196        } catch (\Exception $e) {
1197            report(AppException::fromException($e, 'DELETE_BUDGET_TYPES_EXCEPTION'));
1198
1199            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1200        }
1201
1202    }
1203
1204    public function delete_budget_type_groups($budgetTypeGroupId): ResponseFactory|Response
1205    {
1206
1207        try {
1208
1209            $budgetTypeGroupId = addslashes((string) $budgetTypeGroupId);
1210
1211            $budgets = TblBudgetTypes::where('budget_type_group_id', $budgetTypeGroupId)->count();
1212
1213            if ($budgets > 0) {
1214                return response([
1215                    'message' => 'KO',
1216                    'error' => __('language.budget_type_groups.delete_message.message1').' '.$budgets.' '.__('language.budget_type_groups.delete_message.message2'),
1217                ]);
1218            }
1219
1220            $budgetTypeGroups = TblBudgetTypeGroups::where('budget_type_group_id', $budgetTypeGroupId)->first();
1221            TblBudgetTypeGroups::where('budget_type_group_id', $budgetTypeGroupId)->delete();
1222
1223            $this->budget_type_groups();
1224
1225            return response([
1226                'message' => 'OK',
1227                'data' => $this->budget_type_groups,
1228            ]);
1229
1230        } catch (\Exception $e) {
1231            report(AppException::fromException($e, 'DELETE_BUDGET_TYPE_GROUPS_EXCEPTION'));
1232
1233            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1234        }
1235
1236    }
1237
1238    public function delete_visit_types($visitTypeId): ResponseFactory|Response
1239    {
1240
1241        try {
1242
1243            $visitTypeId = addslashes((string) $visitTypeId);
1244
1245            $pipeline = TblPipelines::where('visit_type_id', $visitTypeId)->count();
1246
1247            if ($pipeline > 0) {
1248                return response([
1249                    'message' => 'KO',
1250                    'error' => __('language.visit_types.delete_message.message1').' '.$pipeline.' '.__('language.visit_types.delete_message.message2'),
1251                ]);
1252            }
1253
1254            $visitType = TblVisitTypes::where('visit_type_id', $visitTypeId)->first();
1255            TblVisitTypes::where('visit_type_id', $visitTypeId)->delete();
1256
1257            $this->visit_types();
1258
1259            return response([
1260                'message' => 'OK',
1261                'data' => $this->visit_types,
1262            ]);
1263
1264        } catch (\Exception $e) {
1265            report(AppException::fromException($e, 'DELETE_VISIT_TYPES_EXCEPTION'));
1266
1267            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1268        }
1269
1270    }
1271
1272    public function delete_visit_type_groups($visitTypeGroupId): ResponseFactory|Response
1273    {
1274
1275        try {
1276
1277            $visitTypeGroupId = addslashes((string) $visitTypeGroupId);
1278
1279            $visitType = TblVisitTypes::where('visit_type_group_id', $visitTypeGroupId)->count();
1280
1281            if ($visitType > 0) {
1282                return response([
1283                    'message' => 'KO',
1284                    'error' => __('language.visit_type_groups.delete_message.message1').' '.$visitType.' '.__('language.visit_type_groups.delete_message.message2'),
1285                ]);
1286            }
1287
1288            $visitTypeGroups = TblVisitTypeGroups::where('visit_type_group_id', $visitTypeGroupId)->first();
1289            TblVisitTypeGroups::where('visit_type_group_id', $visitTypeGroupId)->delete();
1290
1291            $this->visit_type_groups();
1292
1293            return response([
1294                'message' => 'OK',
1295                'data' => $this->visit_type_groups,
1296            ]);
1297
1298        } catch (\Exception $e) {
1299            report(AppException::fromException($e, 'DELETE_VISIT_TYPE_GROUPS_EXCEPTION'));
1300
1301            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1302        }
1303
1304    }
1305
1306    public function delete_budget_status($budgetStatusId): ResponseFactory|Response
1307    {
1308
1309        try {
1310
1311            $budgetStatusId = addslashes((string) $budgetStatusId);
1312
1313            $budgets = TblQuotations::where('budget_status_id', $budgetStatusId)->count();
1314
1315            if ($budgets > 0) {
1316                return response([
1317                    'message' => 'KO',
1318                    'error' => __('language.budget_status.delete_message.message1').' '.$budgets.' '.__('language.budget_status.delete_message.message2'),
1319                ]);
1320            }
1321
1322            $budgetStatus = TblBudgetStatus::where('budget_status_id', $budgetStatusId)->first();
1323            TblBudgetStatus::where('budget_status_id', $budgetStatusId)->delete();
1324
1325            $this->budget_status();
1326
1327            return response([
1328                'message' => 'OK',
1329                'data' => $this->budget_status,
1330            ]);
1331
1332        } catch (\Exception $e) {
1333            report(AppException::fromException($e, 'DELETE_BUDGET_STATUS_EXCEPTION'));
1334
1335            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1336        }
1337
1338    }
1339
1340    public function delete_source($sourceId): ResponseFactory|Response
1341    {
1342
1343        try {
1344
1345            $sourceId = addslashes((string) $sourceId);
1346
1347            $budgets = TblQuotations::where('source_id', $sourceId)->count();
1348
1349            if ($budgets > 0) {
1350                return response([
1351                    'message' => 'KO',
1352                    'error' => __('language.source.delete_message.message1').' '.$budgets.' '.__('language.source.delete_message.message2'),
1353                ]);
1354            }
1355
1356            // FIRE-1260 â€” block delete when child códigos exist. Ops must
1357            // remove the códigos first (mirrors the quotations guard above).
1358            $codigos = TblSourceCodigos::where('source_id', $sourceId)->count();
1359            if ($codigos > 0) {
1360                return response([
1361                    'message' => 'KO',
1362                    'error' => "No se puede eliminar la fuente: tiene {$codigos} códigos asociados. Elimine los códigos primero.",
1363                ]);
1364            }
1365
1366            $source = TblSources::where('source_id', $sourceId)->first();
1367            TblSources::where('source_id', $sourceId)->delete();
1368
1369            $this->source();
1370
1371            return response([
1372                'message' => 'OK',
1373                'data' => $this->source,
1374            ]);
1375
1376        } catch (\Exception $e) {
1377            report(AppException::fromException($e, 'DELETE_SOURCE_EXCEPTION'));
1378
1379            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1380        }
1381
1382    }
1383
1384    /**
1385     * FIRE-1260 â€” list códigos, optionally filtered by source and/or region.
1386     * FE calls this on quotation form load and whenever the fuente dropdown
1387     * changes, so it must stay cheap. Disabled códigos are excluded from the
1388     * listing (they stay in the DB for audit/undelete).
1389     */
1390    public function list_codigos(Request $request): ResponseFactory|Response
1391    {
1392        try {
1393            // Titan â†’ Zeus bridge (read). This endpoint takes `region` as an
1394            // explicit query param, so that is the signal â€” the header is only
1395            // the fallback.
1396            $zeus = app(ZeusBridge::class);
1397            $forward = $zeus->listMasterData($request, '/api/listCodigos');
1398
1399            if ($zeus->shouldReturnZeusResponse($forward)) {
1400                return $forward->toTitanResponse();
1401            }
1402
1403            $sourceId = $request->query('source_id');
1404            $region = $request->query('region');
1405
1406            $query = TblSourceCodigos::where('disabled', 0)
1407                ->orderByRaw('ISNULL(priority), priority ASC')
1408                ->orderBy('name');
1409
1410            if ($sourceId !== null && $sourceId !== '') {
1411                $query->where('source_id', $sourceId);
1412            }
1413            if ($region !== null && $region !== '') {
1414                $query->where('region', $region);
1415            }
1416
1417            return response([
1418                'message' => 'OK',
1419                'data' => $query->get(),
1420            ]);
1421        } catch (\Exception $e) {
1422            report(AppException::fromException($e, 'LIST_CODIGOS_EXCEPTION'));
1423
1424            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1425        }
1426    }
1427
1428    /**
1429     * FIRE-1260 â€” create a código under a source Ã— región. Payload must
1430     * include source_id, region, name. Rejects duplicates on the unique
1431     * (source_id, region, name) triple with a friendly error message.
1432     */
1433    public function add_codigo(Request $request): ResponseFactory|Response
1434    {
1435        try {
1436            $data = $request->all();
1437
1438            if (empty($data['source_id']) || empty($data['region']) || empty($data['name'])) {
1439                return response([
1440                    'message' => 'KO',
1441                    'error' => 'source_id, region y name son obligatorios',
1442                ], 422);
1443            }
1444
1445            $exists = TblSourceCodigos::where('source_id', $data['source_id'])
1446                ->where('region', $data['region'])
1447                ->where('name', $data['name'])
1448                ->exists();
1449            if ($exists) {
1450                return response([
1451                    'message' => 'KO',
1452                    'error' => 'Ya existe un código con ese nombre para esta fuente y región',
1453                ], 409);
1454            }
1455
1456            TblSourceCodigos::create(array_merge($data, [
1457                'created_by' => $data['created_by'] ?? (request()->header('User') ?? 'System'),
1458                'created_at' => now(),
1459            ]));
1460
1461            return response([
1462                'message' => 'OK',
1463            ]);
1464        } catch (\Exception $e) {
1465            report(AppException::fromException($e, 'ADD_CODIGO_EXCEPTION'));
1466
1467            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1468        }
1469    }
1470
1471    /**
1472     * FIRE-1260 â€” update a código's editable fields (name, region, priority,
1473     * disabled, landing_url). source_id changes are allowed but validate the
1474     * new (source_id, region, name) triple isn't a duplicate.
1475     */
1476    public function update_codigo(Request $request, $codigoId): ResponseFactory|Response
1477    {
1478        try {
1479            $codigoId = (int) $codigoId;
1480            $data = $request->all();
1481
1482            $current = TblSourceCodigos::where('id', $codigoId)->first();
1483            if (! $current) {
1484                return response(['message' => 'KO', 'error' => 'Código no encontrado'], 404);
1485            }
1486
1487            $newSource = $data['source_id'] ?? $current->source_id;
1488            $newRegion = $data['region'] ?? $current->region;
1489            $newName = $data['name'] ?? $current->name;
1490
1491            $collision = TblSourceCodigos::where('source_id', $newSource)
1492                ->where('region', $newRegion)
1493                ->where('name', $newName)
1494                ->where('id', '!=', $codigoId)
1495                ->exists();
1496            if ($collision) {
1497                return response([
1498                    'message' => 'KO',
1499                    'error' => 'Ya existe otro código con ese nombre para esta fuente y región',
1500                ], 409);
1501            }
1502
1503            TblSourceCodigos::where('id', $codigoId)->update($data);
1504
1505            return response(['message' => 'OK']);
1506        } catch (\Exception $e) {
1507            report(AppException::fromException($e, 'UPDATE_CODIGO_EXCEPTION'));
1508
1509            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1510        }
1511    }
1512
1513    /**
1514     * FIRE-1260 â€” delete a código. Blocks when quotations reference it,
1515     * same shape as delete_source's guard against orphaning tbl_quotations
1516     * rows.
1517     */
1518    public function delete_codigo($codigoId): ResponseFactory|Response
1519    {
1520        try {
1521            $codigoId = (int) $codigoId;
1522
1523            $used = TblQuotations::where('codigo_id', $codigoId)->count();
1524            if ($used > 0) {
1525                return response([
1526                    'message' => 'KO',
1527                    'error' => "No se puede eliminar el código: hay {$used} presupuestos que lo usan.",
1528                ]);
1529            }
1530
1531            TblSourceCodigos::where('id', $codigoId)->delete();
1532
1533            return response(['message' => 'OK']);
1534        } catch (\Exception $e) {
1535            report(AppException::fromException($e, 'DELETE_CODIGO_EXCEPTION'));
1536
1537            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1538        }
1539    }
1540
1541    public function delete_reason_for_not_following_up($reasonForNotFollowingUpId): ResponseFactory|Response
1542    {
1543
1544        try {
1545
1546            $reasonForNotFollowingUpId = addslashes((string) $reasonForNotFollowingUpId);
1547
1548            $budgets = TblQuotations::where('reason_for_not_following_up_id', $reasonForNotFollowingUpId)->count();
1549
1550            if ($budgets > 0) {
1551                return response([
1552                    'message' => 'KO',
1553                    'error' => __('language.source.delete_message.message1').' '.$budgets.' '.__('language.source.delete_message.message2'),
1554                ]);
1555            }
1556
1557            $reasonForNotFollowingUp = TblReasonForNotFollowingUp::where('reason_for_not_following_up_id', $reasonForNotFollowingUpId)->first();
1558            TblReasonForNotFollowingUp::where('reason_for_not_following_up_id', $reasonForNotFollowingUpId)->delete();
1559
1560            $this->reason_for_not_following_up();
1561
1562            return response([
1563                'message' => 'OK',
1564                'data' => $this->reason_for_not_following_up,
1565            ]);
1566
1567        } catch (\Exception $e) {
1568            report(AppException::fromException($e, 'DELETE_REASON_FOR_NOT_FOLLOWING_UP_EXCEPTION'));
1569
1570            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1571        }
1572
1573    }
1574
1575    public function delete_reason_for_rejection($reasonForRejectionId): ResponseFactory|Response
1576    {
1577
1578        try {
1579
1580            $reasonForRejectionId = addslashes((string) $reasonForRejectionId);
1581
1582            $budgets = TblQuotations::where('reason_for_rejection_id', $reasonForRejectionId)->count();
1583
1584            if ($budgets > 0) {
1585                return response([
1586                    'message' => 'KO',
1587                    'error' => __('language.source.delete_message.message1').' '.$budgets.' '.__('language.source.delete_message.message2'),
1588                ]);
1589            }
1590
1591            $reasonForRejection = TblReasonForRejection::where('reason_for_rejection_id', $reasonForRejectionId)->first();
1592            TblReasonForRejection::where('reason_for_rejection_id', $reasonForRejectionId)->delete();
1593
1594            $this->reason_for_rejection();
1595
1596            return response([
1597                'message' => 'OK',
1598                'data' => $this->reason_for_rejection,
1599            ]);
1600
1601        } catch (\Exception $e) {
1602            report(AppException::fromException($e, 'DELETE_REASON_FOR_REJECTION_EXCEPTION'));
1603
1604            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1605        }
1606
1607    }
1608
1609    public function list_work_status(): ResponseFactory|Response
1610    {
1611
1612        try {
1613
1614            $this->work_status();
1615
1616            return response([
1617                'message' => 'OK',
1618                'data' => $this->work_status,
1619            ]);
1620
1621        } catch (\Exception $e) {
1622            report(AppException::fromException($e, 'LIST_WORK_STATUS_EXCEPTION'));
1623
1624            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1625        }
1626
1627    }
1628
1629    public function work_status(): void
1630    {
1631
1632        $this->work_status = TblWorkStatus::orderByRaw('ISNULL(priority), priority ASC')->get();
1633
1634    }
1635
1636    public function update_work_status(Request $request, $workStatusId): ResponseFactory|Response
1637    {
1638
1639        try {
1640
1641            $data = $request->all();
1642            $workStatusId = addslashes((string) $workStatusId);
1643
1644            if (isset($data['data'])) {
1645                $priority = $data['data'];
1646                if (count($priority) > 0) {
1647                    foreach ($priority as $item) {
1648                        TblWorkStatus::where('work_status_id', $item['work_status_id'])->update(['priority' => $item['priority']]);
1649                    }
1650                }
1651            } else {
1652                TblWorkStatus::where('work_status_id', $workStatusId)->update($data);
1653            }
1654
1655            $this->work_status();
1656
1657            return response([
1658                'message' => 'OK',
1659                'data' => $this->work_status,
1660            ]);
1661
1662        } catch (\Exception $e) {
1663            report(AppException::fromException($e, 'UPDATE_WORK_STATUS_EXCEPTION'));
1664
1665            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1666        }
1667
1668    }
1669
1670    public function delete_work_status($workStatusId): ResponseFactory|Response
1671    {
1672
1673        try {
1674
1675            $workStatusId = addslashes((string) $workStatusId);
1676
1677            $job = TblOngoingJobs::where('work_status_id', $workStatusId)->count();
1678
1679            if ($job > 0) {
1680                return response([
1681                    'message' => 'KO',
1682                    'error' => __('language.work_status.delete_message.message1').' '.$job.' '.__('language.work_status.delete_message.message2'),
1683                ]);
1684            }
1685
1686            $workStatus = TblWorkStatus::where('work_status_id', $workStatusId)->first();
1687            TblWorkStatus::where('work_status_id', $workStatusId)->delete();
1688
1689            $this->work_status();
1690
1691            return response([
1692                'message' => 'OK',
1693                'data' => $this->work_status,
1694            ]);
1695
1696        } catch (\Exception $e) {
1697            report(AppException::fromException($e, 'DELETE_WORK_STATUS_EXCEPTION'));
1698
1699            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1700        }
1701
1702    }
1703
1704    public function add_work_status(Request $request): ResponseFactory|Response
1705    {
1706
1707        try {
1708
1709            $data = $request->all();
1710
1711            TblWorkStatus::create($data);
1712
1713            $this->work_status();
1714
1715            return response([
1716                'message' => 'OK',
1717                'data' => $this->work_status,
1718            ]);
1719
1720        } catch (\Exception $e) {
1721            report(AppException::fromException($e, 'ADD_WORK_STATUS_EXCEPTION'));
1722
1723            return response(['message' => 'KO', 'error' => $e->getMessage()]);
1724        }
1725
1726    }
1727}