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