Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
4.53% |
21 / 464 |
|
0.00% |
0 / 24 |
CRAP | |
0.00% |
0 / 1 |
| Users | |
4.53% |
21 / 464 |
|
0.00% |
0 / 24 |
8116.47 | |
0.00% |
0 / 1 |
| __construct | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
2.26 | |||
| create_users | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
90 | |||
| get_users | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| get_user | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
30 | |||
| getUserByName | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| getAllUserNames | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| get_user_by_email | |
80.00% |
12 / 15 |
|
0.00% |
0 / 1 |
5.20 | |||
| add_company_user | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
30 | |||
| delete_company_user | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| update_users | |
0.00% |
0 / 94 |
|
0.00% |
0 / 1 |
272 | |||
| delete_users | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| get_roles | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_all_commercials | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| get_g3w_warning_fields | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
| get_created_by | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
| get_commercial_with_pendings | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| get_responsible_for_work | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| get_job_created_by | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| get_accepted_by | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| get_commercials | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
| update_users_itv | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| list_roles | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| update_role | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| delete_role | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\TblUsers; |
| 6 | use App\Models\TblRoles; |
| 7 | use App\Models\TblCompanyUsers; |
| 8 | use App\Models\TblQuotations; |
| 9 | use App\Models\TblOngoingJobs; |
| 10 | use App\Http\Controllers\Quotations; |
| 11 | use Illuminate\Http\Request; |
| 12 | use Illuminate\Support\Str; |
| 13 | use Illuminate\Support\Facades\Log; |
| 14 | use Illuminate\Support\Facades\DB; |
| 15 | use Illuminate\Support\Facades\App; |
| 16 | use Illuminate\Support\Facades\Cache; |
| 17 | use Carbon\Carbon; |
| 18 | |
| 19 | class Users extends Controller |
| 20 | { |
| 21 | private $locale; |
| 22 | private $userId; |
| 23 | private $region; |
| 24 | private $companyIds; |
| 25 | private $companyId; |
| 26 | |
| 27 | public function __construct(){ |
| 28 | $this->locale = @getallheaders()['Locale-ID']; |
| 29 | $this->userId = @getallheaders()['User-ID']; |
| 30 | $this->region = @getallheaders()['Region']; |
| 31 | |
| 32 | App::setLocale($this->locale); |
| 33 | |
| 34 | $this->companyIds = array(); |
| 35 | |
| 36 | $this->region = json_decode($this->region, true); |
| 37 | |
| 38 | if (!empty($this->region)) { |
| 39 | |
| 40 | $this->region = implode(',', array_map(fn($r) => "'" . urldecode($r) ."'", $this->region)); |
| 41 | |
| 42 | $query = "SELECT |
| 43 | b.company_id |
| 44 | FROM |
| 45 | tbl_company_users a |
| 46 | LEFT JOIN tbl_companies b ON a.company_id = b.company_id |
| 47 | WHERE |
| 48 | a.user_id = {$this->userId} |
| 49 | AND b.region IN ({$this->region})"; |
| 50 | |
| 51 | $this->companyIds = DB::select($query); |
| 52 | |
| 53 | $this->companyIds = collect($this->companyIds)->pluck('company_id')->toArray(); |
| 54 | }else{ |
| 55 | $this->companyIds = TblCompanyUsers::where('user_id', $this->userId)->pluck('company_id')->all(); |
| 56 | } |
| 57 | |
| 58 | $this->companyId = implode(',', $this->companyIds); |
| 59 | } |
| 60 | |
| 61 | public function create_users(Request $request){ |
| 62 | |
| 63 | // try { |
| 64 | $data = $request->all(); |
| 65 | $data['role_id'] = 2; |
| 66 | |
| 67 | $sData = array( |
| 68 | 'name' => $data['name'], |
| 69 | 'email' => $data['email'], |
| 70 | 'created_by' => $data['created_by'], |
| 71 | 'role_id' => 2, |
| 72 | 'sender_email' => $data['sender_email'], |
| 73 | 'sender_enabled' => @$data['sender_enabled'], |
| 74 | 'G3W_code' => $data['G3W_code'] |
| 75 | ); |
| 76 | |
| 77 | $email = TblUsers::where('email', $data['email'])->count(); |
| 78 | |
| 79 | if($email > 0){ |
| 80 | return response(['message' => 'KO', 'error' => __('language.email_already_exist')]); |
| 81 | } |
| 82 | |
| 83 | $name = TblUsers::where('name', $data['name'])->count(); |
| 84 | |
| 85 | if($name > 0){ |
| 86 | return response(['message' => 'KO', 'error' => __('language.name_already_exist')]); |
| 87 | } |
| 88 | |
| 89 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 90 | $data['address'] = "Madrid"; |
| 91 | $data['city'] = "Madrid"; |
| 92 | $data['country'] = "Spain"; |
| 93 | $data['nickname'] = $data['name'] . "-" . base64_encode($data['name'] . date('ymdhis')); |
| 94 | $data['from_name'] = $data['name']; |
| 95 | $data['from_email'] = $data['sender_email']; |
| 96 | $data['reply_to'] = $data['sender_email']; |
| 97 | $data['reply_to_name'] = $data['name']; |
| 98 | $requestBody = $data; |
| 99 | $error = false; |
| 100 | |
| 101 | $response = $sendgrid->client->verified_senders()->post($requestBody); |
| 102 | $x = json_decode($response->body()); |
| 103 | |
| 104 | if ($response->statusCode() == 201 || is_numeric(@$x->id)) { |
| 105 | $sData['response_id'] = $x->id; |
| 106 | $result = TblUsers::create($sData); |
| 107 | Log::channel('email_log')->info('USER EMAIL: ' . $data['sender_email'] . ' - VERIFICATION SENT'); |
| 108 | } else { |
| 109 | $error = true; |
| 110 | Log::channel('email_log')->error('REQUEST BODY USER: - ' . $response->body()); |
| 111 | } |
| 112 | |
| 113 | $response = json_decode($response->body()); |
| 114 | |
| 115 | if($error){ |
| 116 | if($response->errors[0]->message == "already exists" && $response->errors[0]->field == "from_email"){ |
| 117 | $result = TblUsers::create($sData); |
| 118 | return response(['message' => 'OK', 'data' => $data, 'is_verified' => 'yes']); |
| 119 | } |
| 120 | |
| 121 | $errMessage = @$response->errors[0]->field . ': ' . @$response->errors[0]->message; |
| 122 | return response(['message' => 'KO', 'error' => $errMessage]); |
| 123 | }else{ |
| 124 | $isVerified = "no"; |
| 125 | |
| 126 | $u = TblUsers::where('id', $result->id)->first(); |
| 127 | |
| 128 | if($u->verified == 1){ |
| 129 | $isVerified = "yes"; |
| 130 | } |
| 131 | |
| 132 | return response(['message' => 'OK', 'data' => $response, 'is_verified' => $isVerified]); |
| 133 | } |
| 134 | |
| 135 | // } catch (\Exception $e) { |
| 136 | // return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 137 | // } |
| 138 | } |
| 139 | |
| 140 | public function get_users($companyId = null){ |
| 141 | |
| 142 | try { |
| 143 | |
| 144 | $column = ""; |
| 145 | |
| 146 | if($companyId != null){ |
| 147 | $column = "(SELECT id FROM tbl_company_users WHERE user_id = a.id AND company_id = {$companyId}) user,"; |
| 148 | } |
| 149 | |
| 150 | $query = "SELECT |
| 151 | a.id, |
| 152 | a.name, |
| 153 | a.email, |
| 154 | DATE_FORMAT(a.created_at, '%b %d, %Y %l:%i%p') 'created_at', |
| 155 | a.created_by, |
| 156 | DATE_FORMAT(a.created_at, '%b %d, %Y %l:%i%p') 'updated_at', |
| 157 | a.updated_by, |
| 158 | a.sender_email, |
| 159 | a.sender_enabled, |
| 160 | a.is_itv, |
| 161 | a.verified, |
| 162 | {$column} |
| 163 | a.default |
| 164 | FROM tbl_users a |
| 165 | ORDER BY a.name ASC"; |
| 166 | |
| 167 | $result = DB::select($query); |
| 168 | |
| 169 | return response(['message' => 'OK', 'data' => $result]); |
| 170 | |
| 171 | } catch (\Exception $e) { |
| 172 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function get_user($id){ |
| 177 | |
| 178 | try { |
| 179 | |
| 180 | $id = addslashes($id); |
| 181 | |
| 182 | $e = TblUsers::where('id', $id)->first(); |
| 183 | |
| 184 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 185 | |
| 186 | $response = $sendgrid->client->verified_senders()->get(null, [ |
| 187 | 'limit' => 1000, |
| 188 | 'offset' => 0 |
| 189 | ]); |
| 190 | |
| 191 | if ($response->statusCode() == 200) { |
| 192 | $x = json_decode($response->body())->results; |
| 193 | |
| 194 | foreach ($x as $item) { |
| 195 | if($item->from_email == $e->sender_email){ |
| 196 | TblUsers::where('sender_email', $item->from_email)->update(array( |
| 197 | 'verified' => $item->verified, |
| 198 | 'response_id' => $item->id |
| 199 | )); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | $query = "SELECT |
| 206 | a.id, |
| 207 | a.name, |
| 208 | a.email, |
| 209 | a.created_at, |
| 210 | a.created_by, |
| 211 | a.updated_at, |
| 212 | a.updated_by, |
| 213 | a.default, |
| 214 | a.role_id, |
| 215 | b.name 'role', |
| 216 | a.verified, |
| 217 | a.is_itv, |
| 218 | a.sender_email, |
| 219 | a.sender_enabled, |
| 220 | a.G3W_code |
| 221 | FROM tbl_users a |
| 222 | LEFT JOIN tbl_roles b |
| 223 | ON a.role_id = b.role_id |
| 224 | WHERE a.id = {$id}"; |
| 225 | |
| 226 | $result = DB::select($query); |
| 227 | |
| 228 | return response(['message' => 'OK', 'data' => $result]); |
| 229 | |
| 230 | } catch (\Exception $e) { |
| 231 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public function getUserByName($name) |
| 236 | { |
| 237 | try { |
| 238 | $formattedName = str_replace('_', ' ', $name); |
| 239 | $user = TblUsers::where('name', $formattedName)->first(); |
| 240 | |
| 241 | if (!$user) { |
| 242 | return response()->json([ |
| 243 | 'success' => false, |
| 244 | 'message' => 'User not found', |
| 245 | ], 404); |
| 246 | } |
| 247 | |
| 248 | return response()->json([ |
| 249 | 'success' => true, |
| 250 | 'data' => $user, |
| 251 | ], 200); |
| 252 | } catch (\Exception $e) { |
| 253 | return response()->json([ |
| 254 | 'success' => false, |
| 255 | 'message' => 'An error occurred', |
| 256 | 'error' => $e->getMessage(), |
| 257 | ], 500); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | public function getAllUserNames() |
| 262 | { |
| 263 | try { |
| 264 | $users = TblUsers::pluck('name'); |
| 265 | return response()->json([ |
| 266 | 'success' => true, |
| 267 | 'data' => $users, |
| 268 | ]); |
| 269 | } catch (\Exception $e) { |
| 270 | return response()->json([ |
| 271 | 'success' => false, |
| 272 | 'message' => 'Error fetching users', |
| 273 | 'error' => $e->getMessage(), |
| 274 | ], 500); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | public function get_user_by_email(Request $request){ |
| 280 | |
| 281 | try { |
| 282 | |
| 283 | $data = $request->all(); |
| 284 | $where = ""; |
| 285 | |
| 286 | if(isset($data['company_id'])){ |
| 287 | $companyId = addslashes($data['company_id']); |
| 288 | $where = " AND c.company_id = {$companyId} "; |
| 289 | } |
| 290 | |
| 291 | $query = "SELECT |
| 292 | a.id, |
| 293 | a.name, |
| 294 | a.email, |
| 295 | a.created_at, |
| 296 | a.created_by, |
| 297 | a.updated_at, |
| 298 | a.updated_by, |
| 299 | c.filename, |
| 300 | a.default, |
| 301 | a.role_id, |
| 302 | d.name role, |
| 303 | b.company_id, |
| 304 | c.logo, |
| 305 | c.name company_name, |
| 306 | c.default_page, |
| 307 | b.can_read, |
| 308 | b.can_write, |
| 309 | a.is_loggedin, |
| 310 | c.is_send, |
| 311 | c.limit_send, |
| 312 | c.is_send_follow_up, |
| 313 | c.is_send_request, |
| 314 | c.is_send_g3w, |
| 315 | c.revenue_per_employee_per_day, |
| 316 | c.minimum_margin, |
| 317 | c.general_costs, |
| 318 | c.hours_per_worker_per_day, |
| 319 | c.cost_of_hour, |
| 320 | c.hours_per_worker_per_day_percentage, |
| 321 | c.convert_to_job_amount_limit, |
| 322 | c.last_follow_up_date, |
| 323 | c.limit_reminder_emails, |
| 324 | c.workflow_budget_size, |
| 325 | c.region, |
| 326 | COALESCE(f.approver_id, e.approver_id) AS approver_id, |
| 327 | a.is_itv, |
| 328 | a.G3W_code, |
| 329 | a.api_token |
| 330 | FROM tbl_users a |
| 331 | LEFT JOIN tbl_company_users b |
| 332 | ON a.id = b.user_id AND (b.is_selected = 1 OR b.can_read = 1) |
| 333 | LEFT JOIN tbl_companies c |
| 334 | ON b.company_id = c.company_id |
| 335 | LEFT JOIN tbl_roles d |
| 336 | ON a.role_id = d.role_id |
| 337 | LEFT JOIN tbl_approvers e |
| 338 | ON a.id = e.user_id |
| 339 | LEFT JOIN tbl_approvers_v2 f |
| 340 | ON a.id = f.user_id |
| 341 | WHERE a.email = '{$data['email']}' |
| 342 | {$where} |
| 343 | ORDER BY b.is_selected DESC |
| 344 | LIMIT 1"; |
| 345 | |
| 346 | $result = DB::select($query); |
| 347 | |
| 348 | if(isset($data['switch']) && $data['switch'] == 1){ |
| 349 | $result[0]->is_loggedin = 1; |
| 350 | } |
| 351 | |
| 352 | return response(['message' => 'OK', 'data' => $result]); |
| 353 | |
| 354 | } catch (\Exception $e) { |
| 355 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | function add_company_user(Request $request){ |
| 360 | |
| 361 | try { |
| 362 | |
| 363 | $data = $request->all(); |
| 364 | $id = addslashes($data['user_id']); |
| 365 | |
| 366 | if(isset($data['company_ids'])){ |
| 367 | |
| 368 | $ids = TblCompanyUsers::where('user_id', $id)->pluck('company_id')->toArray(); |
| 369 | $companyUsers = array(); |
| 370 | |
| 371 | for ($i = 0; $i < count($data['company_ids']); $i++) { |
| 372 | if(!in_array($data['company_ids'][$i], $ids)){ |
| 373 | array_push( |
| 374 | $companyUsers, |
| 375 | array( |
| 376 | 'user_id' => $id, |
| 377 | 'company_id' => $data['company_ids'][$i], |
| 378 | 'can_read' => 1, |
| 379 | 'can_write' => 0, |
| 380 | 'created_by' => $data['created_by'] |
| 381 | ) |
| 382 | ); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | TblCompanyUsers::insert($companyUsers); |
| 387 | } |
| 388 | |
| 389 | return response(['message' => 'OK']); |
| 390 | |
| 391 | } catch (\Exception $e) { |
| 392 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | function delete_company_user(Request $request){ |
| 398 | |
| 399 | try { |
| 400 | |
| 401 | $data = $request->all(); |
| 402 | $id = addslashes($data['user_id']); |
| 403 | $companyId = addslashes($data['company_id']); |
| 404 | |
| 405 | TblCompanyUsers::where('company_id', $companyId)->where('user_id', $id)->delete(); |
| 406 | |
| 407 | return response(['message' => 'OK']); |
| 408 | |
| 409 | } catch (\Exception $e) { |
| 410 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 411 | } |
| 412 | |
| 413 | } |
| 414 | |
| 415 | public function update_users(Request $request, $id){ |
| 416 | |
| 417 | try { |
| 418 | |
| 419 | $data = $request->all(); |
| 420 | $id = addslashes($id); |
| 421 | |
| 422 | if(!isset($data['is_loggedin'])){ |
| 423 | $data['is_loggedin'] = 0; |
| 424 | }else{ |
| 425 | |
| 426 | if($data['is_loggedin'] == 1){ |
| 427 | $token = Str::random(60); |
| 428 | $data['api_token'] = $token; |
| 429 | $data['token_expires_at'] = Carbon::now()->addHours(72); |
| 430 | } else { |
| 431 | $data['api_token'] = null; |
| 432 | $data['token_expires_at'] = null; |
| 433 | } |
| 434 | |
| 435 | $result = TblUsers::where('id', $id)->update($data); |
| 436 | return response(['message' => 'OK', 'data' => $data]); |
| 437 | } |
| 438 | |
| 439 | $sData = array( |
| 440 | 'name' => $data['name'], |
| 441 | 'email' => $data['email'], |
| 442 | 'role_id' => $data['role_id'], |
| 443 | 'updated_by' => $data['updated_by'], |
| 444 | 'sender_email' => $data['sender_email'], |
| 445 | 'G3W_code' => $data['G3W_code'], |
| 446 | 'sender_enabled' => @$data['sender_enabled'] |
| 447 | ); |
| 448 | |
| 449 | if(isset($data['email'])){ |
| 450 | $email = TblUsers::where('email', $data['email'])->first(); |
| 451 | |
| 452 | if($email != null && $id != $email->id){ |
| 453 | return response(['message' => 'KO', 'error' => __('language.email_already_exist')]); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | $sData['updated_at'] = date('Y-m-d H:i:s'); |
| 458 | |
| 459 | $u = TblUsers::where('id', $id)->first(); |
| 460 | |
| 461 | if(isset($data['name'])){ |
| 462 | TblQuotations::where('commercial', $u->name)->update( |
| 463 | array( |
| 464 | 'commercial' => $data['name'], |
| 465 | ) |
| 466 | ); |
| 467 | |
| 468 | TblQuotations::where('created_by', $u->name)->update( |
| 469 | array( |
| 470 | 'created_by' => $data['name'], |
| 471 | ) |
| 472 | ); |
| 473 | |
| 474 | TblQuotations::where('updated_by', $u->name)->update( |
| 475 | array( |
| 476 | 'updated_by' => $data['name'], |
| 477 | ) |
| 478 | ); |
| 479 | |
| 480 | TblOngoingJobs::where('responsible_for_work', $u->name)->update( |
| 481 | array( |
| 482 | 'responsible_for_work' => $data['name'], |
| 483 | ) |
| 484 | ); |
| 485 | |
| 486 | TblOngoingJobs::where('created_by', $u->name)->update( |
| 487 | array( |
| 488 | 'created_by' => $data['name'], |
| 489 | ) |
| 490 | ); |
| 491 | |
| 492 | TblOngoingJobs::where('updated_by', $u->name)->update( |
| 493 | array( |
| 494 | 'updated_by' => $data['name'], |
| 495 | ) |
| 496 | ); |
| 497 | } |
| 498 | |
| 499 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 500 | $data['address'] = "Madrid"; |
| 501 | $data['city'] = "Madrid"; |
| 502 | $data['country'] = "Spain"; |
| 503 | $data['nickname'] = $data['name'] . "-" . base64_encode($data['name'] . date('ymdhis'));; |
| 504 | $data['from_name'] = $data['name']; |
| 505 | $data['from_email'] = $data['sender_email']; |
| 506 | $data['reply_to'] = $data['sender_email']; |
| 507 | $data['reply_to_name'] = $data['name']; |
| 508 | $requestBody = $data; |
| 509 | $error = false; |
| 510 | $response = array(); |
| 511 | |
| 512 | if($u->response_id && $u->sender_email == $data['sender_email']){ |
| 513 | $response = $sendgrid->client->verified_senders()->_($u->response_id)->patch($requestBody); |
| 514 | }else{ |
| 515 | $requestBody['nickname'] = $data['name'] . "-" . base64_encode($data['name'] . date('ymdhis')); |
| 516 | $response = $sendgrid->client->verified_senders()->post($requestBody); |
| 517 | } |
| 518 | |
| 519 | $x = json_decode($response->body()); |
| 520 | |
| 521 | if ($response->statusCode() == 200 || is_numeric(@$x->id)) { |
| 522 | $sData['response_id'] = $x->id; |
| 523 | $result = TblUsers::where('id', $id)->update($sData); |
| 524 | Log::channel('email_log')->info('USER EMAIL: ' . $data['sender_email'] . ' - VERIFICATION SENT'); |
| 525 | } else { |
| 526 | $error = true; |
| 527 | Log::channel('email_log')->error('REQUEST BODY USER: - ' . $response->body()); |
| 528 | } |
| 529 | |
| 530 | $response = json_decode($response->body()); |
| 531 | |
| 532 | if($error){ |
| 533 | if($response->errors[0]->message == "already exists" && $response->errors[0]->field == "from_email"){ |
| 534 | TblUsers::where('id', $id)->update($sData); |
| 535 | return response(['message' => 'OK', 'data' => $data, 'is_verified' => 'yes']); |
| 536 | } |
| 537 | |
| 538 | $errMessage = @$response->errors[0]->field . ': ' . @$response->errors[0]->message; |
| 539 | return response(['message' => 'KO', 'error' => $errMessage]); |
| 540 | }else{ |
| 541 | $isVerified = "no"; |
| 542 | |
| 543 | if($response->verified){ |
| 544 | $isVerified = "yes"; |
| 545 | } |
| 546 | |
| 547 | return response(['message' => 'OK', 'data' => $response, 'is_verified' => $isVerified]); |
| 548 | } |
| 549 | |
| 550 | } catch (\Exception $e) { |
| 551 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | public function delete_users($id){ |
| 556 | |
| 557 | try { |
| 558 | |
| 559 | $id = addslashes($id); |
| 560 | |
| 561 | $user = TblUsers::where('id', $id)->first(); |
| 562 | |
| 563 | $count = TblQuotations::where('commercial', $user->name)->where('company_id', '>', 0)->count(); |
| 564 | $countQ = TblOngoingJobs::where('responsible_for_work', $user->name)->where('company_id', '>', 0)->count(); |
| 565 | |
| 566 | if($count > 0 || $countQ > 0){ |
| 567 | $urlOrder = env('URL') . "orders?commercial={$user->name}&company_id=0"; |
| 568 | $urlJob = env('URL') . "ongoing-jobs?responsible_for_work={$user->name}&company_id=0"; |
| 569 | $urlOrder = "<a href='{$urlOrder}' target='_blank'>{$count}</a>"; |
| 570 | $urlJob = "<a href='{$urlJob}' target='_blank'>{$countQ}</a>"; |
| 571 | return response(['message' => 'user_cannot_be_deleted', 'error' => __('language.user_cannot_be_deleted'), 'total_job' => $urlJob, 'total_order' => $urlOrder]); |
| 572 | } |
| 573 | |
| 574 | $count = TblQuotations::where('created_by', $user->name)->count(); |
| 575 | |
| 576 | if($count > 0){ |
| 577 | $query = "UPDATE tbl_quotations SET created_by = 'Fire Service Titan' WHERE created_by = '{$user->name}'"; |
| 578 | DB::select($query); |
| 579 | } |
| 580 | |
| 581 | $count = TblOngoingJobs::where('created_by', $user->name)->count(); |
| 582 | |
| 583 | if($count > 0){ |
| 584 | $query = "UPDATE tbl_ongoing_jobs SET created_by = 'Fire Service Titan' WHERE created_by = '{$user->name}'"; |
| 585 | DB::select($query); |
| 586 | } |
| 587 | |
| 588 | $result = TblUsers::where('id', $id)->delete(); |
| 589 | TblCompanyUsers::where('user_id', $id)->delete(); |
| 590 | |
| 591 | return response(['message' => 'OK', 'data' => $result]); |
| 592 | |
| 593 | } catch (\Exception $e) { |
| 594 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | function get_roles(){ |
| 599 | |
| 600 | try { |
| 601 | |
| 602 | $result = TblRoles::get(); |
| 603 | |
| 604 | return response(['message' => 'OK', 'data' => $result]); |
| 605 | |
| 606 | } catch (\Exception $e) { |
| 607 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | function get_all_commercials(Request $request){ |
| 612 | |
| 613 | try { |
| 614 | |
| 615 | $data = $request->all(); |
| 616 | |
| 617 | $companyIds = implode(',', $data['company_ids']); |
| 618 | |
| 619 | $query = "SELECT |
| 620 | b.name |
| 621 | FROM |
| 622 | tbl_company_users a |
| 623 | LEFT JOIN tbl_users b ON a.user_id = b.id |
| 624 | WHERE |
| 625 | a.company_id IN ({$companyIds}) |
| 626 | GROUP BY |
| 627 | a.user_id |
| 628 | HAVING |
| 629 | COUNT(DISTINCT a.company_id) = ( |
| 630 | SELECT |
| 631 | COUNT(DISTINCT company_id) |
| 632 | FROM |
| 633 | tbl_company_users |
| 634 | WHERE |
| 635 | company_id IN ({$companyIds}) |
| 636 | )"; |
| 637 | |
| 638 | $result = DB::select($query); |
| 639 | |
| 640 | return response(['message' => 'OK', 'data' => $result]); |
| 641 | |
| 642 | } catch (\Exception $e) { |
| 643 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 644 | } |
| 645 | |
| 646 | } |
| 647 | |
| 648 | function get_g3w_warning_fields($companyId){ |
| 649 | |
| 650 | try { |
| 651 | |
| 652 | $companyId = addslashes($companyId); |
| 653 | |
| 654 | $where = ""; |
| 655 | |
| 656 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 657 | |
| 658 | $query = "SELECT |
| 659 | DISTINCT g3w_warning_fields |
| 660 | FROM |
| 661 | tbl_quotations |
| 662 | {$where} |
| 663 | ORDER BY |
| 664 | g3w_warning_fields ASC"; |
| 665 | |
| 666 | $value = Cache::get(base64_encode($query)); |
| 667 | |
| 668 | if(!$value){ |
| 669 | $result = DB::select($query); |
| 670 | |
| 671 | $g3wWarningFields = array_map(function ($row) { |
| 672 | return $row->g3w_warning_fields; |
| 673 | }, $result); |
| 674 | |
| 675 | Cache::put(base64_encode($query), $g3wWarningFields, 600); |
| 676 | }else{ |
| 677 | $g3wWarningFields = $value; |
| 678 | } |
| 679 | |
| 680 | return response([ |
| 681 | 'message' => 'OK', |
| 682 | 'g3wWarningFields' => $g3wWarningFields |
| 683 | ]); |
| 684 | |
| 685 | |
| 686 | } catch (\Exception $e) { |
| 687 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | function get_created_by($companyId){ |
| 692 | |
| 693 | try { |
| 694 | |
| 695 | $companyId = addslashes($companyId); |
| 696 | |
| 697 | $where = ""; |
| 698 | |
| 699 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 700 | |
| 701 | $query = "SELECT |
| 702 | DISTINCT created_by |
| 703 | FROM |
| 704 | tbl_quotations |
| 705 | {$where} |
| 706 | ORDER BY |
| 707 | created_by ASC"; |
| 708 | |
| 709 | $value = Cache::get(base64_encode($query)); |
| 710 | |
| 711 | if(!$value){ |
| 712 | $result = DB::select($query); |
| 713 | |
| 714 | $createdBy = array_map(function ($row) { |
| 715 | return $row->created_by; |
| 716 | }, $result); |
| 717 | |
| 718 | Cache::put(base64_encode($query), $createdBy, 600); |
| 719 | }else{ |
| 720 | $createdBy = $value; |
| 721 | } |
| 722 | |
| 723 | return response([ |
| 724 | 'message' => 'OK', |
| 725 | 'createdBy' => $createdBy |
| 726 | ]); |
| 727 | |
| 728 | } catch (\Exception $e) { |
| 729 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | function get_commercial_with_pendings($companyId){ |
| 734 | |
| 735 | try { |
| 736 | |
| 737 | $companyId = addslashes($companyId); |
| 738 | |
| 739 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 740 | |
| 741 | $query = "SELECT |
| 742 | b.id userId, |
| 743 | b.name commercial, |
| 744 | a.total_error totalError, |
| 745 | a.total_pending_follow_ups totalPendingFollowUps, |
| 746 | a.total_request_and_visits totalRequestAndVisit, |
| 747 | a.total_g3w_error totalG3WError |
| 748 | FROM |
| 749 | tbl_company_users a |
| 750 | LEFT JOIN tbl_users b |
| 751 | ON a.user_id = b.id |
| 752 | {$where} |
| 753 | ORDER BY |
| 754 | b.name ASC"; |
| 755 | |
| 756 | $value = Cache::get(base64_encode($query)); |
| 757 | |
| 758 | if(!$value){ |
| 759 | $commercial = DB::select($query); |
| 760 | |
| 761 | Cache::put(base64_encode($query), $commercial, 600); |
| 762 | }else{ |
| 763 | $commercial = $value; |
| 764 | } |
| 765 | |
| 766 | return response([ |
| 767 | 'message' => 'OK', |
| 768 | 'commercialWithPendings' => $commercial |
| 769 | ]); |
| 770 | |
| 771 | } catch (\Exception $e) { |
| 772 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | function get_responsible_for_work($companyId){ |
| 777 | |
| 778 | try { |
| 779 | |
| 780 | $companyId = addslashes($companyId); |
| 781 | |
| 782 | $where = ""; |
| 783 | |
| 784 | if($companyId != 0){ |
| 785 | $where = "WHERE company_id = {$companyId} "; |
| 786 | }else{ |
| 787 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 788 | } |
| 789 | |
| 790 | $query = "SELECT |
| 791 | DISTINCT responsible_for_work |
| 792 | FROM |
| 793 | tbl_ongoing_jobs |
| 794 | {$where} |
| 795 | ORDER BY |
| 796 | responsible_for_work ASC"; |
| 797 | |
| 798 | $value = Cache::get(base64_encode($query)); |
| 799 | |
| 800 | if(!$value){ |
| 801 | $result = DB::select($query); |
| 802 | |
| 803 | $responsibleForWork = array_map(function ($row) { |
| 804 | return $row->responsible_for_work; |
| 805 | }, $result); |
| 806 | |
| 807 | Cache::put(base64_encode($query), $responsibleForWork, 600); |
| 808 | }else{ |
| 809 | $responsibleForWork = $value; |
| 810 | } |
| 811 | |
| 812 | return response([ |
| 813 | 'message' => 'OK', |
| 814 | 'responsibleForWork' => $responsibleForWork, |
| 815 | ]); |
| 816 | |
| 817 | } catch (\Exception $e) { |
| 818 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 819 | } |
| 820 | |
| 821 | } |
| 822 | |
| 823 | function get_job_created_by($companyId){ |
| 824 | |
| 825 | try { |
| 826 | |
| 827 | $companyId = addslashes($companyId); |
| 828 | |
| 829 | $where = ""; |
| 830 | |
| 831 | if($companyId != 0){ |
| 832 | $where = "WHERE company_id = {$companyId} "; |
| 833 | }else{ |
| 834 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 835 | } |
| 836 | |
| 837 | $query = "SELECT |
| 838 | DISTINCT created_by |
| 839 | FROM |
| 840 | tbl_ongoing_jobs |
| 841 | {$where} |
| 842 | ORDER BY |
| 843 | created_by ASC"; |
| 844 | |
| 845 | $value = Cache::get(base64_encode($query)); |
| 846 | |
| 847 | if(!$value){ |
| 848 | $result = DB::select($query); |
| 849 | |
| 850 | $jobCreatedBy = array_map(function ($row) { |
| 851 | return $row->created_by; |
| 852 | }, $result); |
| 853 | |
| 854 | Cache::put(base64_encode($query), $jobCreatedBy, 600); |
| 855 | }else{ |
| 856 | $jobCreatedBy = $value; |
| 857 | } |
| 858 | |
| 859 | return response([ |
| 860 | 'message' => 'OK', |
| 861 | 'jobCreatedBy' => $jobCreatedBy, |
| 862 | ]); |
| 863 | |
| 864 | } catch (\Exception $e) { |
| 865 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | function get_accepted_by($companyId){ |
| 870 | |
| 871 | try { |
| 872 | |
| 873 | $companyId = addslashes($companyId); |
| 874 | |
| 875 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 876 | |
| 877 | $query = "SELECT |
| 878 | DISTINCT accepted_by |
| 879 | FROM |
| 880 | tbl_quotations |
| 881 | {$where} |
| 882 | ORDER BY |
| 883 | accepted_by ASC"; |
| 884 | |
| 885 | $value = Cache::get(base64_encode($query)); |
| 886 | |
| 887 | if(!$value){ |
| 888 | $result = DB::select($query); |
| 889 | |
| 890 | $acceptedBy = array_map(function ($row) { |
| 891 | return $row->accepted_by; |
| 892 | }, $result); |
| 893 | |
| 894 | Cache::put(base64_encode($query), $acceptedBy, 600); |
| 895 | }else{ |
| 896 | $acceptedBy = $value; |
| 897 | } |
| 898 | |
| 899 | return response([ |
| 900 | 'message' => 'OK', |
| 901 | 'acceptedBy' => $acceptedBy, |
| 902 | ]); |
| 903 | |
| 904 | } catch (\Exception $e) { |
| 905 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | function get_commercials($companyId){ |
| 911 | |
| 912 | try { |
| 913 | |
| 914 | $companyId = addslashes($companyId); |
| 915 | |
| 916 | $where = ""; |
| 917 | |
| 918 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 919 | |
| 920 | $query = "SELECT |
| 921 | DISTINCT commercial |
| 922 | FROM |
| 923 | tbl_quotations |
| 924 | {$where} |
| 925 | ORDER BY |
| 926 | commercial ASC"; |
| 927 | |
| 928 | $value = Cache::get(base64_encode($query)); |
| 929 | |
| 930 | if(!$value){ |
| 931 | $result = DB::select($query); |
| 932 | |
| 933 | $commercials = array_map(function ($row) { |
| 934 | return $row->commercial; |
| 935 | }, $result); |
| 936 | |
| 937 | Cache::put(base64_encode($query), $commercials, 600); |
| 938 | }else{ |
| 939 | $commercials = $value; |
| 940 | } |
| 941 | |
| 942 | return response([ |
| 943 | 'message' => 'OK', |
| 944 | 'commercials' => $commercials, |
| 945 | ]); |
| 946 | |
| 947 | } catch (\Exception $e) { |
| 948 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | function update_users_itv(Request $request){ |
| 953 | |
| 954 | try { |
| 955 | |
| 956 | $data = $request->all(); |
| 957 | |
| 958 | if(isset($data['user_ids'])){ |
| 959 | TblUsers::query()->update(['is_itv' => null]); |
| 960 | for ($i = 0; $i < count($data['user_ids']); $i++) { |
| 961 | TblUsers::where('id', $data['user_ids'][$i])->update(array('is_itv' => 1)); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | return response(['message' => 'OK']); |
| 966 | |
| 967 | } catch (\Exception $e) { |
| 968 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 969 | } |
| 970 | |
| 971 | } |
| 972 | |
| 973 | function list_roles(){ |
| 974 | |
| 975 | try { |
| 976 | |
| 977 | $result = TblRoles::get(); |
| 978 | |
| 979 | return response(['message' => 'OK', 'data' => $result]); |
| 980 | |
| 981 | } catch (\Exception $e) { |
| 982 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 983 | } |
| 984 | |
| 985 | } |
| 986 | |
| 987 | function update_role(Request $request){ |
| 988 | |
| 989 | try { |
| 990 | |
| 991 | $data = $request->all(); |
| 992 | |
| 993 | foreach ($data as $item) { |
| 994 | $id = $item['role_id']; |
| 995 | unset($item['role_id']); |
| 996 | |
| 997 | $item['updated_at'] = date('Y-m-d H:i:s'); |
| 998 | TblRoles::where('role_id', $id)->update($item); |
| 999 | } |
| 1000 | |
| 1001 | $result = TblRoles::get(); |
| 1002 | |
| 1003 | return response(['message' => 'OK', 'data' => $result]); |
| 1004 | |
| 1005 | } catch (\Exception $e) { |
| 1006 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | function delete_role($id){ |
| 1011 | |
| 1012 | try { |
| 1013 | |
| 1014 | $id = addslashes($id); |
| 1015 | |
| 1016 | TblRoles::where('role_id', $id)->delete(); |
| 1017 | |
| 1018 | $result = TblRoles::get(); |
| 1019 | |
| 1020 | return response(['message' => 'OK', 'data' => $result]); |
| 1021 | |
| 1022 | } catch (\Exception $e) { |
| 1023 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1024 | } |
| 1025 | } |
| 1026 | } |