Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 78
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BusinessGoals
0.00% covered (danger)
0.00%
0 / 78
0.00% covered (danger)
0.00%
0 / 4
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 list_business_goals
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 business_goals
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
306
 update_business_goals
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Exceptions\AppException;
6use App\Models\TblBusinessGoals;
7use App\Services\ResultCache;
8use Illuminate\Contracts\Routing\ResponseFactory;
9use Illuminate\Http\Request;
10use Illuminate\Http\Response;
11use Illuminate\Support\Facades\App;
12use Illuminate\Support\Facades\Cache;
13use Illuminate\Support\Facades\DB;
14
15class BusinessGoals extends Controller
16{
17    private $locale;
18
19    private $userId;
20
21    private array $business_goals = [];
22
23    public function __construct()
24    {
25        $this->locale = request()->header('Locale-Id');
26        $this->userId = request()->header('User-Id');
27
28        App::setLocale($this->locale);
29    }
30
31    public function list_business_goals(Request $request): ResponseFactory|Response
32    {
33
34        try {
35
36            $data = $request->all();
37
38            $this->business_goals($data);
39
40            return response([
41                'message' => 'OK',
42                'data' => $this->business_goals,
43            ]);
44
45        } catch (\Exception $e) {
46            report(AppException::fromException($e, 'LIST_BUSINESS_GOALS_EXCEPTION'));
47
48            return response(['message' => 'KO', 'error' => $e->getMessage()]);
49        }
50
51    }
52
53    public function business_goals(array $params): void
54    {
55
56        $where = '';
57
58        if (isset($params['budget_type_group_id']) && $params['budget_type_group_id'] != null) {
59            $budgetTypeGroupIds = implode(',', $params['budget_type_group_id']);
60            if (count($params['budget_type_group_id']) > 0) {
61                $where .= " AND bg.budget_type_group_id IN ({$budgetTypeGroupIds}, 999999999)";
62            }
63        } else {
64            $where .= ' AND bg.budget_type_group_id > 0';
65        }
66
67        if (isset($params['user_id']) && $params['user_id'] != null) {
68            $userIds = implode(',', $params['user_id']);
69            if (count($params['user_id']) > 0) {
70                $where .= " AND bg.user_id IN ({$userIds}) OR (bg.is_default = 1 {$where})";
71            }
72        }
73
74        if (isset($params['role_id']) && $params['role_id'] != null) {
75            $roleIds = implode(',', $params['role_id']);
76            if (count($params['role_id']) > 0) {
77                $where .= " AND bg.role_id IN ({$roleIds}, 999999999)";
78            }
79        }
80
81        $query = "SELECT
82                    bg.id,
83                    bg.budget_type_group_id,
84                    CASE 
85                        WHEN bg.budget_type_group_id = 999999999 THEN 'Default'
86                        ELSE btg.name
87                    END budget_type,
88                    CASE
89                        WHEN bg.is_commercial_type = 1 THEN r.name 
90                        ELSE u.name 
91                    END users, 
92                    bg.is_commercial_type, 
93                    bg.issue_objective, 
94                    bg.acceptance_objective, 
95                    bg.new_objective, 
96                    bg.role_id, 
97                    bg.user_id,
98                    bg.is_default,
99                    bg.is_amount
100                FROM
101                    tbl_business_goals bg 
102                LEFT JOIN tbl_budget_type_groups btg 
103                    ON btg.budget_type_group_id = bg.budget_type_group_id
104                LEFT JOIN tbl_roles r 
105                    ON bg.role_id = r.role_id 
106                LEFT JOIN tbl_users u 
107                    ON bg.user_id = u.id
108                WHERE bg.id > 0 {$where}
109                ORDER BY 
110                    FIELD(bg.role_id, 999999999) DESC,
111                    FIELD(btg.budget_type_group_id, 999999999) DESC,
112                    FIELD(bg.is_commercial_type, 1) DESC,
113                    bg.role_id ASC,
114                    btg.priority ASC";
115
116        $data = DB::select($query);
117
118        $result = ['users' => [], 'roles' => [], 'budget_types' => []];
119
120        foreach ($data as $item) {
121
122            if ($item->budget_type != null) {
123                $key = $item->is_commercial_type === 1 ? 'roles' : 'users';
124
125                $id = $item->user_id;
126                if ($key == 'roles') {
127                    $id = $item->role_id;
128                }
129
130                /** @phpstan-ignore-next-line */
131                if (! array_key_exists($id, $result[$key])) {
132
133                    $result[$key][$id] = [
134                        $key == 'roles' ? 'role_id' : 'user_id' => $id,
135                        'name' => $item->is_default === 1 ? 'Default' : $item->users,
136                        'id' => $item->id,
137                        'is_default' => $item->is_default,
138                        'is_amount' => $item->is_amount,
139                        'budget_types' => [],
140                    ];
141
142                }
143
144                $result[$key][$id]['budget_types'][] = [
145                    'id' => $item->id,
146                    'budget_type_group_id' => $item->budget_type_group_id,
147                    'budget_type' => $item->budget_type,
148                    'issue_objective' => $item->issue_objective,
149                    'acceptance_objective' => $item->acceptance_objective,
150                    'new_objective' => $item->new_objective,
151                ];
152
153                array_push($result['budget_types'], $item->budget_type);
154            }
155        }
156
157        $result['users'] = array_values($result['users']);
158        $result['roles'] = array_values($result['roles']);
159        $result['budget_types'] = array_values(array_unique($result['budget_types']));
160
161        $this->business_goals = $result;
162    }
163
164    public function update_business_goals(Request $request): ResponseFactory|Response
165    {
166
167        try {
168
169            $data = $request->all();
170
171            foreach ($data as $item) {
172                $id = $item['id'];
173                unset($item['id']);
174
175                $item['updated_at'] = date('Y-m-d H:i:s');
176
177                if (isset($item['is_amount'])) {
178                    $bg = TblBusinessGoals::where('id', $id)->first();
179                    if (is_numeric($bg->user_id)) {
180                        TblBusinessGoals::where('user_id', $bg->user_id)->update($item);
181                    }
182
183                    if (is_numeric($bg->role_id)) {
184                        TblBusinessGoals::where('role_id', $bg->role_id)->update($item);
185                    }
186                }
187
188                TblBusinessGoals::where('id', $id)->update($item);
189            }
190
191            // FIRE-1145: was Cache::flush() — goals aggregate into the campaign dashboards.
192            ResultCache::forgetDomain('digital_campaign_analytics');
193
194            return response([
195                'message' => 'OK',
196            ]);
197
198        } catch (\Exception $e) {
199            report(AppException::fromException($e, 'UPDATE_BUSINESS_GOALS_EXCEPTION'));
200
201            return response(['message' => 'KO', 'error' => $e->getMessage()]);
202        }
203
204    }
205}