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