Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 766 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
| Itv | |
0.00% |
0 / 766 |
|
0.00% |
0 / 16 |
38220 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| list_itv | |
0.00% |
0 / 249 |
|
0.00% |
0 / 1 |
5700 | |||
| get_dates | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| update_itv | |
0.00% |
0 / 94 |
|
0.00% |
0 / 1 |
1332 | |||
| create_itv | |
0.00% |
0 / 75 |
|
0.00% |
0 / 1 |
870 | |||
| delete_itv | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
20 | |||
| get_itv | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| human_filesize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| get_itv_files | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| delete_itv_file | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| download_itv_file | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| isEmailValid | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| email_reminders | |
0.00% |
0 / 128 |
|
0.00% |
0 / 1 |
306 | |||
| email_reminders_mileage | |
0.00% |
0 / 90 |
|
0.00% |
0 / 1 |
56 | |||
| get_distincts | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| get_all_users | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\TblItv; |
| 6 | use App\Models\TblCompanyUsers; |
| 7 | use App\Models\TblItvFiles; |
| 8 | use App\Models\TblUsers; |
| 9 | use App\Models\TblItvEmailLogs; |
| 10 | use Illuminate\Support\Facades\App; |
| 11 | use Illuminate\Support\Facades\Cache; |
| 12 | use Illuminate\Http\Request; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Illuminate\Support\Facades\Storage; |
| 15 | use Illuminate\Support\Facades\Log; |
| 16 | use SendGrid\Mail\Mail; |
| 17 | use GuzzleHttp\Client; |
| 18 | |
| 19 | class Itv extends Controller |
| 20 | { |
| 21 | public function __construct(){ |
| 22 | $this->locale = @getallheaders()['Locale-ID']; |
| 23 | $this->userId = @getallheaders()['User-ID']; |
| 24 | $this->region = @getallheaders()['Region']; |
| 25 | |
| 26 | App::setLocale($this->locale); |
| 27 | |
| 28 | $this->companyIds = array(); |
| 29 | |
| 30 | if($this->region != null && $this->region != "" && $this->region != "All"){ |
| 31 | $this->region = urldecode($this->region); |
| 32 | |
| 33 | $query = "SELECT |
| 34 | b.company_id |
| 35 | FROM |
| 36 | tbl_company_users a |
| 37 | LEFT JOIN tbl_companies b ON a.company_id = b.company_id |
| 38 | WHERE |
| 39 | a.user_id = {$this->userId} |
| 40 | AND b.region = '{$this->region}'"; |
| 41 | |
| 42 | $this->companyIds = DB::select($query); |
| 43 | |
| 44 | $this->companyIds = collect($this->companyIds)->pluck('company_id')->toArray(); |
| 45 | }else{ |
| 46 | $this->companyIds = TblCompanyUsers::where('user_id', $this->userId)->pluck('company_id')->all(); |
| 47 | } |
| 48 | |
| 49 | $this->companyId = implode(',', $this->companyIds); |
| 50 | } |
| 51 | |
| 52 | public function list_itv(Request $request){ |
| 53 | |
| 54 | try { |
| 55 | |
| 56 | $data = $request->all(); |
| 57 | $companyId = addslashes($data['company_id']); |
| 58 | $userId = addslashes($data['user_id']); |
| 59 | $filter = $data['filterModel']; |
| 60 | $sort = $data['sortModel']; |
| 61 | $result = array(); |
| 62 | $subquery = ""; |
| 63 | $where = ""; |
| 64 | $having = ""; |
| 65 | $orderBy = ""; |
| 66 | $start = addslashes($data['start']); |
| 67 | $end = addslashes($data['end']); |
| 68 | $totalRowCount = 0; |
| 69 | $withFilters = ""; |
| 70 | |
| 71 | $filterType = array( |
| 72 | 'contains' => "LIKE '%[value]%'", |
| 73 | 'notContains' => "NOT LIKE '%[value]%'", |
| 74 | 'equals' => "= '[value]'", |
| 75 | 'notEqual' => "<> '[value]'", |
| 76 | 'startsWith' => "LIKE '[value]%'", |
| 77 | 'endsWith' => "LIKE '%[value]'", |
| 78 | 'blank' => "IS NULL", |
| 79 | 'notBlank' => "IS NOT NULL", |
| 80 | 'lessThan' => "< [value]", |
| 81 | 'lessThanOrEqual' => "<= [value]", |
| 82 | 'greaterThan' => "> [value]", |
| 83 | 'greaterThanOrEqual' => ">= [value]", |
| 84 | 'inRange' => "BETWEEN [value1] AND [value2]" |
| 85 | ); |
| 86 | |
| 87 | |
| 88 | if(isset($data['ids']) && count($data['ids']) > 0){ |
| 89 | $ids = implode(",", $data['ids']); |
| 90 | $where = " AND a.id IN ({$ids}) "; |
| 91 | } |
| 92 | |
| 93 | if(isset($data['ids_not_in']) && count($data['ids_not_in']) > 0){ |
| 94 | $ids = implode(",", $data['ids_not_in']); |
| 95 | $where = " AND a.id NOT IN ({$ids}) "; |
| 96 | } |
| 97 | |
| 98 | $matchScoreCol = ""; |
| 99 | $matchScoreOrderBy = ""; |
| 100 | |
| 101 | if(isset($data['searchText']) && $data['searchText'] != null){ |
| 102 | |
| 103 | $availableParameters = [ |
| 104 | 'a.region', |
| 105 | 'b.name', |
| 106 | 'a.brand', |
| 107 | 'a.vehicle_type', |
| 108 | 'a.license_plate', |
| 109 | 'a.mileage', |
| 110 | 'a.last_itv_date', |
| 111 | 'a.next_itv_date', |
| 112 | 'a.mileage_threshold', |
| 113 | 'a.is_booked', |
| 114 | 'a.driver', |
| 115 | 'a.responsible_name', |
| 116 | 'a.responsible_email', |
| 117 | 'a.created_by', |
| 118 | 'a.created_at', |
| 119 | 'a.updated_by', |
| 120 | 'a.updated_at' |
| 121 | ]; |
| 122 | |
| 123 | $searchText = addslashes($data['searchText']); |
| 124 | $searchTextArray = explode(" ", $searchText); |
| 125 | |
| 126 | $searchArray = array(); |
| 127 | $splitSearchArray = array(); |
| 128 | $matchScoreArray = array(); |
| 129 | $sc = 1; |
| 130 | foreach ($availableParameters as $field) { |
| 131 | if($field == 'a.license_plate' || $field == 'a.created_at'){ |
| 132 | $sc = 3; |
| 133 | }elseif($field == 'a.last_itv_date'){ |
| 134 | $sc = 2; |
| 135 | }else{ |
| 136 | $sc = 1; |
| 137 | } |
| 138 | |
| 139 | $l = "{$field} LIKE '%{$searchText}%'"; |
| 140 | |
| 141 | $d = "IFNULL((LENGTH(LOWER({$field})) - LENGTH(REPLACE(LOWER({$field}), LOWER('{$searchText}'), ''))) / LENGTH(LOWER('{$searchText}')), 0) * {$sc}"; |
| 142 | |
| 143 | if(count($searchTextArray) > 1){ |
| 144 | foreach ($searchTextArray as $word) { |
| 145 | if(!is_numeric($word)){ |
| 146 | $d .= " + IFNULL((LENGTH(LOWER({$field})) - LENGTH(REPLACE(LOWER({$field}), LOWER('{$word}'), ''))) / LENGTH(LOWER('{$word}')), 0) * {$sc}"; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | array_push($matchScoreArray, $d); |
| 152 | |
| 153 | if(is_numeric($searchText)){ |
| 154 | array_push($searchArray, "({$l} OR {$field} = CAST('{$searchText}' AS UNSIGNED))"); |
| 155 | }else{ |
| 156 | array_push($searchArray, "({$l} OR DATE_FORMAT({$field}, '%d/%m/%Y') = DATE_FORMAT(STR_TO_DATE('{$searchText}', '%d/%m/%Y'), '%d/%m/%Y'))"); |
| 157 | } |
| 158 | |
| 159 | if(count($searchTextArray) > 1){ |
| 160 | foreach ($searchTextArray as $word) { |
| 161 | |
| 162 | $l = "{$field} LIKE '%{$word}%'"; |
| 163 | |
| 164 | if(is_numeric($word)){ |
| 165 | array_push($splitSearchArray, "{$l} OR {$field} = CAST('{$word}' AS UNSIGNED)"); |
| 166 | }else{ |
| 167 | array_push($splitSearchArray, "{$l} OR DATE_FORMAT({$field}, '%d/%m/%Y') = DATE_FORMAT(STR_TO_DATE('{$word}', '%d/%m/%Y'), '%d/%m/%Y')"); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $sc = 1; |
| 173 | } |
| 174 | |
| 175 | if(count($splitSearchArray) > 0){ |
| 176 | $splitSearchArray = implode(" OR ", $splitSearchArray); |
| 177 | $splitSearchArray = " OR ({$splitSearchArray}) "; |
| 178 | }else{ |
| 179 | $splitSearchArray = ""; |
| 180 | } |
| 181 | |
| 182 | $searchArray = implode(" OR ", $searchArray); |
| 183 | $matchScoreArray = implode(",", $matchScoreArray); |
| 184 | $matchScoreCol = ", GREATEST({$matchScoreArray}) match_score"; |
| 185 | $matchScoreOrderBy = "match_score DESC,"; |
| 186 | $where .= " AND ({$searchArray} {$splitSearchArray})"; |
| 187 | } |
| 188 | |
| 189 | if(count($sort) > 0){ |
| 190 | $field = $sort[0]['colId']; |
| 191 | $sortBy = $sort[0]['sort']; |
| 192 | |
| 193 | if(strpos($field, "translate") !== false){ |
| 194 | $field = str_replace("_translate", "", $field); |
| 195 | }else{ |
| 196 | if($field == "company_name"){ |
| 197 | $field = "b.name"; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if($matchScoreOrderBy){ |
| 202 | $matchScoreOrderBy = ", match_score DESC"; |
| 203 | } |
| 204 | |
| 205 | $orderBy = " ORDER BY {$field} {$sortBy} {$matchScoreOrderBy}"; |
| 206 | }else{ |
| 207 | $orderBy = " ORDER BY {$matchScoreOrderBy} a.id DESC"; |
| 208 | } |
| 209 | |
| 210 | foreach ($filter as $key => $data) { |
| 211 | if(strpos($key, "translate") !== false){ |
| 212 | |
| 213 | $field = str_replace("_translate", "", $key); |
| 214 | |
| 215 | if($field == "created_at"){ |
| 216 | $field = "a.created_at"; |
| 217 | }elseif($field == "updated_at"){ |
| 218 | $field = "a.updated_at"; |
| 219 | }elseif($field == "last_itv_date"){ |
| 220 | $field = "a.last_itv_date"; |
| 221 | }elseif($field == "next_itv_date"){ |
| 222 | $field = "a.next_itv_date"; |
| 223 | } |
| 224 | |
| 225 | $whereDates = ""; |
| 226 | $z = 0; |
| 227 | |
| 228 | if(isset($data['filters']) && !empty($data['filters'])){ |
| 229 | foreach ($data['filters'] as $yearKey => $yearData) { |
| 230 | $yearsMonths = array(); |
| 231 | $yearsWeeks = array(); |
| 232 | |
| 233 | if($z > 0){ |
| 234 | $whereDates .= " OR (YEAR($field) = {$yearKey} "; |
| 235 | }else{ |
| 236 | $whereDates .= " (YEAR($field) = {$yearKey} "; |
| 237 | } |
| 238 | |
| 239 | for ($i = 0; $i < count($yearData['months']); $i++) { |
| 240 | if($yearData['months'][$i]['isChecked']){ |
| 241 | array_push($yearsMonths, $yearData['months'][$i]['value']); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | $yearsMonths = implode("','", $yearsMonths); |
| 246 | $whereDates .= " AND (MONTH({$field}) IN ('{$yearsMonths}')"; |
| 247 | |
| 248 | for ($i = 0; $i < count($yearData['weeks']); $i++) { |
| 249 | if($yearData['weeks'][$i]['isChecked']){ |
| 250 | array_push($yearsWeeks, $yearData['weeks'][$i]['value']); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | $yearsWeeks = implode("','", $yearsWeeks); |
| 255 | if($yearsWeeks != ''){ |
| 256 | $whereDates .= " OR WEEK({$field}) IN ('{$yearsWeeks}') "; |
| 257 | } |
| 258 | |
| 259 | $whereDates .= ")) "; |
| 260 | $z++; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | $whereDataUptoToday = ""; |
| 265 | if(isset($data['isDataUptoToday'])){ |
| 266 | if($data['isDataUptoToday']){ |
| 267 | $whereDates = ""; |
| 268 | $whereDataUptoToday .= " AND {$field} < NOW() AND {$field} > 0 "; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | $whereBlanks = ""; |
| 273 | if(isset($data['isBlanks'])){ |
| 274 | if($data['isBlanks']){ |
| 275 | $conj = "OR"; |
| 276 | if($whereDates == ""){ |
| 277 | $conj = ""; |
| 278 | } |
| 279 | $whereBlanks .= " {$conj} {$field} IS NULL "; |
| 280 | }else{ |
| 281 | $conj = "AND"; |
| 282 | if($whereDates == ""){ |
| 283 | $conj = ""; |
| 284 | } |
| 285 | $whereBlanks .= " {$conj} {$field} IS NOT NULL "; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | $where .= " AND ({$whereDates} {$whereBlanks} {$whereDataUptoToday}) "; |
| 290 | }else{ |
| 291 | if($data['filterType'] == 'number'){ |
| 292 | if(array_key_exists('operator', $data)){ |
| 293 | if($data['condition1']['type'] != 'blank' && $data['condition2']['type'] != 'notBlank'){ |
| 294 | $data['condition1']['filter'] = addslashes($data['condition1']['filter']); |
| 295 | $data['condition2']['filter'] = addslashes($data['condition2']['filter']); |
| 296 | |
| 297 | if($data['condition1']['type'] == 'inRange'){ |
| 298 | $data['condition1']['filterTo'] = addslashes($data['condition1']['filterTo']); |
| 299 | $inRange = str_replace("[value1]", $data['condition1']['filter'], $filterType['inRange']); |
| 300 | $val1 = str_replace("[value2]", $data['condition1']['filterTo'], $inRange); |
| 301 | }else{ |
| 302 | $val1 = str_replace("[value]", $data['condition1']['filter'], $filterType[$data['condition1']['type']]); |
| 303 | } |
| 304 | |
| 305 | if($data['condition2']['type'] == 'inRange'){ |
| 306 | $data['condition2']['filterTo'] = addslashes($data['condition2']['filterTo']); |
| 307 | $inRange = str_replace("[value1]", $data['condition2']['filter'], $filterType['inRange']); |
| 308 | $val2 = str_replace("[value2]", $data['condition2']['filterTo'], $inRange); |
| 309 | }else{ |
| 310 | $val2 = str_replace("[value]", $data['condition2']['filter'], $filterType[$data['condition2']['type']]); |
| 311 | } |
| 312 | |
| 313 | }else{ |
| 314 | $val1 = $filterType[$data['condition1']['type']]; |
| 315 | $val2 = $filterType[$data['condition2']['type']]; |
| 316 | } |
| 317 | |
| 318 | $where .= " AND a.{$key} {$val1} {$data['operator']} a.{$key} {$val2} "; |
| 319 | }else{ |
| 320 | if($data['type'] != 'blank' && $data['type'] != 'notBlank'){ |
| 321 | $data['filter'] = addslashes($data['filter']); |
| 322 | |
| 323 | if($data['type'] == 'inRange'){ |
| 324 | $data['filterTo'] = addslashes($data['filterTo']); |
| 325 | $inRange = str_replace("[value1]", $data['filter'], $filterType['inRange']); |
| 326 | $val = str_replace("[value2]", $data['filterTo'], $inRange); |
| 327 | }else{ |
| 328 | $val = str_replace("[value]", $data['filter'], $filterType[$data['type']]); |
| 329 | } |
| 330 | }else{ |
| 331 | $val = $filterType[$data['type']]; |
| 332 | } |
| 333 | |
| 334 | $where .= " AND a.{$key} {$val} "; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if($data['filterType'] == 'text'){ |
| 339 | if(array_key_exists('operator', $data)){ |
| 340 | if($data['condition1']['type'] != 'blank' && $data['condition2']['type'] != 'notBlank'){ |
| 341 | $data['condition1']['filter'] = addslashes($data['condition1']['filter']); |
| 342 | $val1 = str_replace("[value]", $data['condition1']['filter'], $filterType[$data['condition1']['type']]); |
| 343 | } |
| 344 | |
| 345 | if($data['condition2']['type'] != 'blank' && $data['condition2']['type'] != 'notBlank'){ |
| 346 | $data['condition2']['filter'] = addslashes($data['condition2']['filter']); |
| 347 | $val2 = str_replace("[value]", $data['condition2']['filter'], $filterType[$data['condition2']['type']]); |
| 348 | } |
| 349 | |
| 350 | $where .= " AND {$key} {$val1} {$data['operator']} {$key} {$val2} "; |
| 351 | }else{ |
| 352 | if($data['type'] != 'blank' && $data['type'] != 'notBlank'){ |
| 353 | $data['filter'] = addslashes($data['filter']); |
| 354 | $val = str_replace("[value]", $data['filter'], $filterType[$data['type']]); |
| 355 | }else{ |
| 356 | $val = $filterType[$data['type']]; |
| 357 | } |
| 358 | |
| 359 | $where .= " AND {$key} {$val} "; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if($data['filterType'] == 'set'){ |
| 364 | $statusName = $key; |
| 365 | |
| 366 | if($key == "updated_by"){ |
| 367 | $statusName = "a.updated_by"; |
| 368 | }elseif($key == "created_by"){ |
| 369 | $statusName = "a.created_by"; |
| 370 | }elseif($key == "company_name"){ |
| 371 | $statusName = "b.name"; |
| 372 | }elseif($key == "region"){ |
| 373 | $statusName = "a.region"; |
| 374 | }elseif($key == "is_booked"){ |
| 375 | $statusName = "a.is_booked"; |
| 376 | if($data['values']){ |
| 377 | foreach ($data['values'] as $k => $v) { |
| 378 | if($v == "No"){ |
| 379 | $data['values'][$k] = 0; |
| 380 | }elseif($v == null){ |
| 381 | $data['values'][$k] = null; |
| 382 | }else{ |
| 383 | $data['values'][$k] = 1; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | $val = implode("','", $data['values']); |
| 390 | |
| 391 | if(in_array(null, $data['values'], true)){ |
| 392 | $where .= " AND ({$statusName} IN ('{$val}') OR {$statusName} IS NULL) "; |
| 393 | }else{ |
| 394 | $where .= " AND {$statusName} IN ('{$val}') "; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | $offset = $start; |
| 401 | $limit = $end - $start; |
| 402 | |
| 403 | $subquery = ",(SELECT can_write FROM tbl_company_users WHERE company_id = a.company_id AND user_id = {$userId}) can_write"; |
| 404 | |
| 405 | $query = "SELECT |
| 406 | a.id, |
| 407 | a.region, |
| 408 | a.company_id, |
| 409 | b.name company_name, |
| 410 | a.registration_date, |
| 411 | a.brand, |
| 412 | a.model, |
| 413 | a.vehicle_type, |
| 414 | a.license_plate, |
| 415 | a.mileage, |
| 416 | a.last_itv_date, |
| 417 | a.next_itv_date, |
| 418 | a.mileage_threshold, |
| 419 | a.driver, |
| 420 | a.responsible_name, |
| 421 | a.responsible_email, |
| 422 | a.created_by, |
| 423 | a.created_at, |
| 424 | a.updated_by, |
| 425 | a.updated_at, |
| 426 | a.is_due, |
| 427 | a.is_due_appointment, |
| 428 | CASE |
| 429 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 0 THEN 0 |
| 430 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 1 THEN 1 |
| 431 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 15 THEN 15 |
| 432 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 30 THEN 30 |
| 433 | ELSE NULL |
| 434 | END AS next_itv_date_due, |
| 435 | CASE |
| 436 | WHEN DATEDIFF(a.appointment_date, NOW()) <= 0 THEN 0 |
| 437 | WHEN DATEDIFF(a.appointment_date, NOW()) <= 1 THEN 1 |
| 438 | WHEN DATEDIFF(a.appointment_date, NOW()) <= 7 THEN 7 |
| 439 | ELSE NULL |
| 440 | END AS appointment_date_due, |
| 441 | a.comments, |
| 442 | a.location, |
| 443 | a.is_booked, |
| 444 | a.appointment_time, |
| 445 | a.appointment_date, |
| 446 | DATE_FORMAT(a.registration_date, '%m/%Y') registration_date_translate, |
| 447 | DATE_FORMAT(a.last_itv_date, '%d/%m/%Y') last_itv_date_translate, |
| 448 | DATE_FORMAT(a.next_itv_date, '%m/%Y') next_itv_date_translate, |
| 449 | DATE_FORMAT(a.created_at, '%d/%m/%Y') created_at_translate, |
| 450 | DATE_FORMAT(a.updated_at, '%d/%m/%Y') updated_at_translate, |
| 451 | DATE_FORMAT(a.appointment_date, '%d/%m/%Y') appointment_date_translate |
| 452 | {$matchScoreCol} |
| 453 | {$subquery} |
| 454 | FROM |
| 455 | tbl_itv a |
| 456 | LEFT JOIN tbl_companies b ON a.company_id = b.company_id |
| 457 | WHERE a.id > 0 {$where} |
| 458 | GROUP BY a.id |
| 459 | {$orderBy} |
| 460 | LIMIT {$offset}, {$limit} |
| 461 | "; |
| 462 | // return $query; |
| 463 | $value = Cache::get(base64_encode($query)); |
| 464 | |
| 465 | if(!$value){ |
| 466 | $result = DB::select($query); |
| 467 | |
| 468 | Cache::put(base64_encode($query), $result, 600); |
| 469 | }else{ |
| 470 | $result = $value; |
| 471 | } |
| 472 | |
| 473 | $totalQuery = "SELECT |
| 474 | COUNT(a.id) totalRowCount |
| 475 | FROM |
| 476 | tbl_itv a |
| 477 | LEFT JOIN tbl_companies b ON a.company_id = b.company_id |
| 478 | WHERE a.id > 0 |
| 479 | {$where}"; |
| 480 | |
| 481 | $value = Cache::get(base64_encode($totalQuery)); |
| 482 | |
| 483 | if(!$value){ |
| 484 | $countQuery = DB::select($totalQuery); |
| 485 | |
| 486 | Cache::put(base64_encode($totalQuery), $countQuery, 600); |
| 487 | }else{ |
| 488 | $countQuery = $value; |
| 489 | } |
| 490 | |
| 491 | return response([ |
| 492 | 'message' => 'OK', |
| 493 | 'data' => $result, |
| 494 | 'totalRowCount' => $countQuery[0]->totalRowCount |
| 495 | ]); |
| 496 | |
| 497 | } catch (\Exception $e) { |
| 498 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 499 | } |
| 500 | |
| 501 | } |
| 502 | |
| 503 | public function get_dates(){ |
| 504 | |
| 505 | try { |
| 506 | |
| 507 | $query = "SELECT |
| 508 | DATE_FORMAT(a.last_itv_date, '%d/%m/%Y') last_itv_date_translate, |
| 509 | DATE_FORMAT(a.next_itv_date, '%d/%m/%Y') next_itv_date_translate, |
| 510 | DATE_FORMAT(a.created_at, '%d/%m/%Y') created_at_translate, |
| 511 | DATE_FORMAT(a.updated_at, '%d/%m/%Y') updated_at_translate |
| 512 | FROM tbl_itv a"; |
| 513 | |
| 514 | $result = DB::select($query); |
| 515 | |
| 516 | return response([ |
| 517 | 'message' => 'OK', |
| 518 | 'data' => $result |
| 519 | ]); |
| 520 | |
| 521 | } catch (\Exception $e) { |
| 522 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 523 | } |
| 524 | |
| 525 | } |
| 526 | |
| 527 | function update_itv(Request $request, $id){ |
| 528 | |
| 529 | try { |
| 530 | |
| 531 | $id = addslashes($id); |
| 532 | $data = $request->all(); |
| 533 | |
| 534 | $files = $request->file('files'); |
| 535 | unset($data['files']); |
| 536 | |
| 537 | $itv = TblItv::where('id', $id)->first(); |
| 538 | |
| 539 | $isSendItv = 0; |
| 540 | $daysDifference = 0; |
| 541 | $duration = 0; |
| 542 | $isBooked = $data['is_booked']; |
| 543 | |
| 544 | if($data['is_booked'] == 0 || $data['is_booked'] == null){ |
| 545 | if(isset($data['next_itv_date'])){ |
| 546 | $incommingDate = strtotime($data['next_itv_date']); |
| 547 | $currentDate = strtotime(date('Y-m-d')); |
| 548 | $secondsDifference = abs($incommingDate - $currentDate); |
| 549 | $daysDifference = floor($secondsDifference / (60 * 60 * 24)); |
| 550 | |
| 551 | $duration = 99999999; |
| 552 | |
| 553 | if($daysDifference >= 15 && $daysDifference <= 30) { |
| 554 | $duration = 30; |
| 555 | }elseif($daysDifference > 1 && $daysDifference <= 15){ |
| 556 | $duration = 15; |
| 557 | }elseif($daysDifference == 1){ |
| 558 | $duration = 1; |
| 559 | } |
| 560 | |
| 561 | if($daysDifference <= 30){ |
| 562 | if($itv->is_due != $duration){ |
| 563 | $isSendItv = 1; |
| 564 | } |
| 565 | }else{ |
| 566 | $data['is_due'] = null; |
| 567 | } |
| 568 | } |
| 569 | }elseif($data['is_booked'] == 1){ |
| 570 | if(isset($data['appointment_date'])){ |
| 571 | $incommingDate = strtotime($data['appointment_date']); |
| 572 | $currentDate = strtotime(date('Y-m-d')); |
| 573 | $secondsDifference = abs($incommingDate - $currentDate); |
| 574 | $daysDifference = floor($secondsDifference / (60 * 60 * 24)); |
| 575 | |
| 576 | $duration = 99999999; |
| 577 | |
| 578 | if($daysDifference > 1 && $daysDifference <= 7){ |
| 579 | $duration = 7; |
| 580 | }elseif($daysDifference == 1){ |
| 581 | $duration = 1; |
| 582 | } |
| 583 | |
| 584 | if($daysDifference <= 7){ |
| 585 | if($itv->is_due_appointment != $duration){ |
| 586 | $isSendItv = 1; |
| 587 | } |
| 588 | }else{ |
| 589 | $data['is_due_appointment'] = null; |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | $isSendItvMileage = 0; |
| 595 | $mileageDuration = 0; |
| 596 | if(isset($data['mileage']) && isset($data['mileage_threshold'])){ |
| 597 | if(($data['mileage'] > 0 && $data['mileage'] != null) && ($data['mileage_threshold'] > 0 && $data['mileage_threshold'] != null)){ |
| 598 | $mileage = floor($data['mileage']); |
| 599 | $mileageThreshold = floor($data['mileage_threshold']); |
| 600 | |
| 601 | if($mileage < $mileageThreshold){ |
| 602 | $mileageDuration = abs($mileage - $mileageThreshold); |
| 603 | |
| 604 | if($mileageDuration <= 3000){ |
| 605 | if($itv->is_due_mileage == null){ |
| 606 | $isSendItvMileage = 1; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | $fileCount = TblItvFiles::where('itv_id', $id)->count(); |
| 614 | |
| 615 | if($files){ |
| 616 | $totalFileCount = $fileCount + count($files); |
| 617 | if($totalFileCount > 3){ |
| 618 | return response(['message' => 'KO', 'error' => __('language.file_count_exceeded')]); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 623 | TblItv::where('id', $id)->update($data); |
| 624 | |
| 625 | if($isSendItv == 1){ |
| 626 | $this->email_reminders($duration, $id, $isBooked); |
| 627 | } |
| 628 | |
| 629 | if($isSendItvMileage == 1){ |
| 630 | $this->email_reminders_mileage($mileageDuration, $id); |
| 631 | } |
| 632 | |
| 633 | if($files){ |
| 634 | |
| 635 | $directory = 'public/uploads'; |
| 636 | $uploadedFiles = []; |
| 637 | $i = 0; |
| 638 | |
| 639 | $combinedFilesSize = 0; |
| 640 | foreach ($files as $file) { |
| 641 | $i++; |
| 642 | $origFilename = str_replace(" ", "", $file->getClientOriginalName()); |
| 643 | $filename = $id . '-ITV' . time() . '-' . $origFilename; |
| 644 | |
| 645 | $combinedFilesSize = $combinedFilesSize + $file->getSize(); |
| 646 | |
| 647 | if($combinedFilesSize > 25000000){ |
| 648 | return response(['message' => 'KO', 'error' => __('language.file_size_exceeded')]); |
| 649 | } |
| 650 | |
| 651 | Storage::putFileAs($directory, $file, $filename); |
| 652 | Storage::disk('google')->put($filename, file_get_contents(storage_path() .'/app/public/uploads/'. $filename)); |
| 653 | |
| 654 | if(in_array($origFilename, $uploadedFiles)){ |
| 655 | $origFilename = $origFilename . $i; |
| 656 | } |
| 657 | |
| 658 | TblItvFiles::create( |
| 659 | array( |
| 660 | 'itv_id' => $id, |
| 661 | 'original_name' => $origFilename, |
| 662 | 'filename' => $filename, |
| 663 | 'uploaded_by' => $data['updated_by'] |
| 664 | ) |
| 665 | ); |
| 666 | |
| 667 | $uploadedFiles[] = $file->getClientOriginalName(); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | Cache::flush(); |
| 672 | |
| 673 | return response([ |
| 674 | 'message' => 'OK', $daysDifference, $itv->is_due, $duration |
| 675 | ]); |
| 676 | |
| 677 | } catch (\Exception $e) { |
| 678 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 679 | } |
| 680 | |
| 681 | } |
| 682 | |
| 683 | function create_itv(Request $request){ |
| 684 | |
| 685 | try { |
| 686 | |
| 687 | $data = $request->all(); |
| 688 | |
| 689 | $files = $request->file('files'); |
| 690 | unset($data['files']); |
| 691 | |
| 692 | $result = TblItv::create($data); |
| 693 | |
| 694 | $id = $result->id; |
| 695 | |
| 696 | $isBooked = $data['is_booked']; |
| 697 | |
| 698 | if($data['is_booked'] == 0 || $data['is_booked'] == null){ |
| 699 | if(isset($data['next_itv_date'])){ |
| 700 | $incommingDate = strtotime($data['next_itv_date']); |
| 701 | $currentDate = strtotime(date('Y-m-d')); |
| 702 | $secondsDifference = abs($incommingDate - $currentDate); |
| 703 | $daysDifference = floor($secondsDifference / (60 * 60 * 24)); |
| 704 | |
| 705 | $duration = 99999999; |
| 706 | |
| 707 | if($daysDifference >= 15 && $daysDifference <= 30) { |
| 708 | $duration = 30; |
| 709 | }elseif($daysDifference > 1 && $daysDifference <= 15){ |
| 710 | $duration = 15; |
| 711 | }elseif($daysDifference == 1){ |
| 712 | $duration = 1; |
| 713 | } |
| 714 | |
| 715 | if($daysDifference <= 30){ |
| 716 | $this->email_reminders($duration, $id, $isBooked); |
| 717 | } |
| 718 | } |
| 719 | }elseif($data['is_booked'] == 1){ |
| 720 | if(isset($data['appointment_date'])){ |
| 721 | $incommingDate = strtotime($data['appointment_date']); |
| 722 | $currentDate = strtotime(date('Y-m-d')); |
| 723 | $secondsDifference = abs($incommingDate - $currentDate); |
| 724 | $daysDifference = floor($secondsDifference / (60 * 60 * 24)); |
| 725 | |
| 726 | $duration = 99999999; |
| 727 | |
| 728 | if($daysDifference > 1 && $daysDifference <= 7){ |
| 729 | $duration = 7; |
| 730 | }elseif($daysDifference == 1){ |
| 731 | $duration = 1; |
| 732 | } |
| 733 | |
| 734 | if($daysDifference <= 7){ |
| 735 | if($itv->is_due_appointment != $duration){ |
| 736 | $this->email_reminders($duration, $id, $isBooked); |
| 737 | } |
| 738 | }else{ |
| 739 | $data['is_due_appointment'] = null; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | $mileageDuration = 0; |
| 745 | if(isset($data['mileage']) && isset($data['mileage_threshold'])){ |
| 746 | if(($data['mileage'] > 0 && $data['mileage'] != null) && ($data['mileage_threshold'] > 0 && $data['mileage_threshold'] != null)){ |
| 747 | $mileage = floor($data['mileage']); |
| 748 | $mileageThreshold = floor($data['mileage_threshold']); |
| 749 | |
| 750 | $mileageDuration = abs($mileage - $mileageThreshold); |
| 751 | |
| 752 | if($mileageDuration <= 3000){ |
| 753 | $this->email_reminders_mileage($mileageDuration, $id); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | if($files){ |
| 759 | |
| 760 | $directory = 'public/uploads'; |
| 761 | $uploadedFiles = []; |
| 762 | $i = 0; |
| 763 | |
| 764 | $combinedFilesSize = 0; |
| 765 | foreach ($files as $file) { |
| 766 | $i++; |
| 767 | $origFilename = str_replace(" ", "", $file->getClientOriginalName()); |
| 768 | $filename = $id . '-ITV' . time() . '-' . $origFilename; |
| 769 | |
| 770 | $combinedFilesSize = $combinedFilesSize + $file->getSize(); |
| 771 | |
| 772 | if($combinedFilesSize > 25000000){ |
| 773 | return response(['message' => 'KO', 'error' => __('language.file_size_exceeded')]); |
| 774 | } |
| 775 | |
| 776 | Storage::putFileAs($directory, $file, $filename); |
| 777 | Storage::disk('google')->put($filename, file_get_contents(storage_path() .'/app/public/uploads/'. $filename)); |
| 778 | |
| 779 | if(in_array($origFilename, $uploadedFiles)){ |
| 780 | $origFilename = $origFilename . $i; |
| 781 | } |
| 782 | |
| 783 | TblItvFiles::create( |
| 784 | array( |
| 785 | 'itv_id' => $id, |
| 786 | 'original_name' => $origFilename, |
| 787 | 'filename' => $filename, |
| 788 | 'uploaded_by' => $data['created_by'] |
| 789 | ) |
| 790 | ); |
| 791 | |
| 792 | $uploadedFiles[] = $file->getClientOriginalName(); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | Cache::flush(); |
| 797 | |
| 798 | return response([ |
| 799 | 'message' => 'OK' |
| 800 | ]); |
| 801 | |
| 802 | } catch (\Exception $e) { |
| 803 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 804 | } |
| 805 | |
| 806 | } |
| 807 | |
| 808 | public function delete_itv(Request $request){ |
| 809 | |
| 810 | try { |
| 811 | |
| 812 | $data = $request->all(); |
| 813 | $result = array(); |
| 814 | |
| 815 | $r = new Request([ |
| 816 | 'filterModel' => $data['filterModel'], |
| 817 | 'sortModel' => $data['sortModel'], |
| 818 | 'start' => 0, |
| 819 | 'end' => 999999999, |
| 820 | 'company_id' => $data['company_id'], |
| 821 | 'user_id' => $data['user_id'], |
| 822 | 'ids' => $data['ids'], |
| 823 | 'searchText' => $data['searchText'], |
| 824 | 'ids_not_in' => $data['ids_not_in'] |
| 825 | ]); |
| 826 | |
| 827 | $result = $this->list_itv($r); |
| 828 | $result = $result->original['data']; |
| 829 | |
| 830 | $outputArray = array(); |
| 831 | |
| 832 | foreach ($result as $item) { |
| 833 | if (isset($item->id)) { |
| 834 | $outputArray[] = $item->id; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | TblItv::whereIn('id', $outputArray)->delete(); |
| 839 | |
| 840 | Cache::flush(); |
| 841 | return response(['message' => 'OK', 'data' => $result]); |
| 842 | |
| 843 | } catch (\Exception $e) { |
| 844 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 845 | } |
| 846 | |
| 847 | } |
| 848 | |
| 849 | function get_itv($id){ |
| 850 | |
| 851 | try { |
| 852 | |
| 853 | $id = addslashes($id); |
| 854 | |
| 855 | $query = "SELECT |
| 856 | a.id, |
| 857 | a.region, |
| 858 | a.company_id, |
| 859 | b.name company_name, |
| 860 | a.registration_date, |
| 861 | a.brand, |
| 862 | a.model, |
| 863 | a.vehicle_type, |
| 864 | a.license_plate, |
| 865 | a.mileage, |
| 866 | a.last_itv_date, |
| 867 | a.next_itv_date, |
| 868 | a.mileage_threshold, |
| 869 | a.driver, |
| 870 | a.responsible_name, |
| 871 | a.responsible_email, |
| 872 | a.created_by, |
| 873 | a.created_at, |
| 874 | a.updated_by, |
| 875 | a.updated_at, |
| 876 | a.is_due, |
| 877 | a.is_due_appointment, |
| 878 | a.location, |
| 879 | a.is_booked, |
| 880 | a.appointment_time, |
| 881 | a.appointment_date, |
| 882 | DATE_FORMAT(a.registration_date, '%m/%Y') registration_date_translate, |
| 883 | DATE_FORMAT(a.last_itv_date, '%d/%m/%Y') last_itv_date_translate, |
| 884 | DATE_FORMAT(a.next_itv_date, '%m/%Y') next_itv_date_translate, |
| 885 | DATE_FORMAT(a.created_at, '%d/%m/%Y') created_at_translate, |
| 886 | DATE_FORMAT(a.updated_at, '%d/%m/%Y') updated_at_translate, |
| 887 | DATE_FORMAT(a.appointment_date, '%d/%m/%Y') appointment_date_translate, |
| 888 | CASE |
| 889 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 0 THEN 0 |
| 890 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 1 THEN 1 |
| 891 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 15 THEN 15 |
| 892 | WHEN DATEDIFF(a.next_itv_date, NOW()) <= 30 THEN 30 |
| 893 | ELSE NULL |
| 894 | END AS next_itv_date_due, |
| 895 | CASE |
| 896 | WHEN DATEDIFF(a.appointment_date, NOW()) <= 0 THEN 0 |
| 897 | WHEN DATEDIFF(a.appointment_date, NOW()) <= 1 THEN 1 |
| 898 | WHEN DATEDIFF(a.appointment_date, NOW()) <= 7 THEN 7 |
| 899 | ELSE NULL |
| 900 | END AS appointment_date_due, |
| 901 | a.comments |
| 902 | FROM |
| 903 | tbl_itv a |
| 904 | LEFT JOIN tbl_companies b ON a.company_id = b.company_id |
| 905 | WHERE a.id = {$id}"; |
| 906 | |
| 907 | $result = DB::select($query); |
| 908 | |
| 909 | Cache::flush(); |
| 910 | return response(['message' => 'OK', 'data' => $result]); |
| 911 | |
| 912 | } catch (\Exception $e) { |
| 913 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | function human_filesize($bytes, $decimals = 2) { |
| 918 | $size = ['B', 'KB', 'MB']; |
| 919 | |
| 920 | $factor = floor((strlen($bytes) - 1) / 3); |
| 921 | return number_format($bytes / pow(1024, $factor), 2, ',', '.') . $size[$factor]; |
| 922 | } |
| 923 | |
| 924 | |
| 925 | function get_itv_files($id){ |
| 926 | |
| 927 | try { |
| 928 | |
| 929 | $id = addslashes($id); |
| 930 | |
| 931 | $result = TblItvFiles::where('itv_id', $id)->get(); |
| 932 | |
| 933 | foreach ($result as $key => $value) { |
| 934 | $path = storage_path('app/public/uploads/' . $result[$key]->filename); |
| 935 | |
| 936 | if (\File::exists($path)) { |
| 937 | $fileSizeBytes = \File::size($path); |
| 938 | $result[$key]->filesize = $this->human_filesize($fileSizeBytes); |
| 939 | $result[$key]->original_name = $result[$key]->original_name . " ({$result[$key]->filesize})"; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | $emailLogs = TblItvEmailLogs::where('itv_id', $id)->get(); |
| 944 | |
| 945 | return response(['message' => 'OK', 'data' => $result, 'itvEmailLogs' => $emailLogs]); |
| 946 | |
| 947 | } catch (\Exception $e) { |
| 948 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | function delete_itv_file($fileId){ |
| 953 | |
| 954 | try { |
| 955 | |
| 956 | $fileId = addslashes($fileId); |
| 957 | $file = TblItvFiles::where('file_id', $fileId)->first(); |
| 958 | $result = TblItvFiles::where('file_id', $fileId)->first(); |
| 959 | |
| 960 | if($result){ |
| 961 | TblItvFiles::where('file_id', $fileId)->delete(); |
| 962 | $path = storage_path('app/public/uploads/' . $result->filename); |
| 963 | Storage::disk('public')->delete('uploads/' . $result->filename); |
| 964 | } |
| 965 | |
| 966 | return response(['message' => 'OK']); |
| 967 | |
| 968 | } catch (\Exception $e) { |
| 969 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | function download_itv_file($fileId){ |
| 974 | |
| 975 | try { |
| 976 | |
| 977 | $fileId = addslashes($fileId); |
| 978 | |
| 979 | $result = TblItvFiles::where('file_id', $fileId)->first(); |
| 980 | |
| 981 | if($result){ |
| 982 | $path = storage_path('app/public/uploads/' . $result->filename); |
| 983 | |
| 984 | if (!Storage::disk('public')->exists('uploads/' . $result->filename)) { |
| 985 | return response(['message' => 'KO']); |
| 986 | } |
| 987 | |
| 988 | return response()->download($path); |
| 989 | } |
| 990 | |
| 991 | return response(['message' => 'KO']); |
| 992 | |
| 993 | } catch (\Exception $e) { |
| 994 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 995 | } |
| 996 | |
| 997 | } |
| 998 | |
| 999 | function isEmailValid($email) { |
| 1000 | // Regular expression pattern for email validation |
| 1001 | $pattern = '/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/'; |
| 1002 | |
| 1003 | // Check if the email matches the pattern |
| 1004 | if (preg_match($pattern, $email)) { |
| 1005 | return true; // Valid email |
| 1006 | } else { |
| 1007 | return false; // Invalid email |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | function email_reminders($duration, $id, $isBooked = 0){ |
| 1012 | |
| 1013 | $item = TblItv::where('id', $id)->first(); |
| 1014 | |
| 1015 | $due = ""; |
| 1016 | $subject = __('language.itv_reminders.subject'); |
| 1017 | $body = ""; |
| 1018 | $footer = __('language.itv_reminders.body_message_footer'); |
| 1019 | |
| 1020 | if($isBooked == 0){ |
| 1021 | if($duration == 30){ |
| 1022 | $due = __('language.itv_reminders.in_one_month'); |
| 1023 | }elseif($duration == 15){ |
| 1024 | $due = __('language.itv_reminders.in_15_days'); |
| 1025 | }elseif($duration == 1){ |
| 1026 | $due = __('language.itv_reminders.tomorrow'); |
| 1027 | $subject = __('language.itv_reminders.subject_urgent'); |
| 1028 | $footer = __('language.itv_reminders.body_message_footer_urgent'); |
| 1029 | }else{ |
| 1030 | return false; |
| 1031 | } |
| 1032 | }elseif($isBooked == 1){ |
| 1033 | if($duration == 7){ |
| 1034 | $due = __('language.itv_reminders.app_7_days'); |
| 1035 | $subject = __('language.itv_reminders.subject_7_days'); |
| 1036 | $footer = __('language.itv_reminders.body_message_footer_appointment'); |
| 1037 | }elseif($duration == 1){ |
| 1038 | $due = __('language.itv_reminders.app_tomorrow'); |
| 1039 | $subject = __('language.itv_reminders.subject_tomorrow'); |
| 1040 | $footer = __('language.itv_reminders.body_message_footer_appointment_urgent'); |
| 1041 | }else{ |
| 1042 | return false; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | $email = new \SendGrid\Mail\Mail(); |
| 1047 | |
| 1048 | $user = TblUsers::where('id', $this->userId)->first(); |
| 1049 | $userName = $user->name; |
| 1050 | |
| 1051 | $addTo = array(); |
| 1052 | |
| 1053 | if(env('SENDGRID_STAGING')){ |
| 1054 | $email->addTo($user->email); |
| 1055 | array_push($addTo, $user->email); |
| 1056 | }else{ |
| 1057 | |
| 1058 | $user = TblUsers::where('name', $item->created_by)->first(); |
| 1059 | $email->addTo($user->email); |
| 1060 | array_push($addTo, $user->email); |
| 1061 | |
| 1062 | if($item->created_by != $item->responsible_name){ |
| 1063 | $isValid = $this->isEmailValid($item->responsible_email); |
| 1064 | if($isValid){ |
| 1065 | $email->addTo($item->responsible_email); |
| 1066 | array_push($addTo, $item->responsible_email); |
| 1067 | }else{ |
| 1068 | TblItvEmailLogs::create( |
| 1069 | array( |
| 1070 | 'itv_id' => $item->id, |
| 1071 | 'email' => $item->responsible_email, |
| 1072 | 'sent_by' => $userName, |
| 1073 | 'status' => 'Invalid email' |
| 1074 | ) |
| 1075 | ); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | $subject = str_replace('{{brand}}', $item->brand, $subject); |
| 1081 | $subject = str_replace('{{license_plate}}', $item->license_plate, $subject); |
| 1082 | |
| 1083 | $email->setFrom("fire@fire.es", "Fire Service Titan"); |
| 1084 | $email->setSubject($subject); |
| 1085 | |
| 1086 | $imgpath = \File::get('fireservicetitan.png'); |
| 1087 | |
| 1088 | $email->addAttachment( |
| 1089 | $imgpath, |
| 1090 | "image/png", |
| 1091 | "fireservicetitan.png", |
| 1092 | "inline", |
| 1093 | "fireservicetitan" |
| 1094 | ); |
| 1095 | |
| 1096 | $url = env('URL') . "itv/{$item->id}"; |
| 1097 | |
| 1098 | $bodyHeaders = __('language.itv_reminders.body_message_header'); |
| 1099 | |
| 1100 | if($isBooked == 1){ |
| 1101 | $bodyHeaders = __('language.itv_reminders.body_message_header_appointment'); |
| 1102 | } |
| 1103 | |
| 1104 | $body .= __('language.itv_reminders.body_hello'); |
| 1105 | $body = str_replace('{{responsible_name}}', $item->responsible_name, $body); |
| 1106 | |
| 1107 | $body .= $bodyHeaders; |
| 1108 | $body = str_replace('{{duration}}', $due, $body); |
| 1109 | $body = str_replace('{{click}}', $url, $body); |
| 1110 | |
| 1111 | $body .= __('language.itv_reminders.body_vehicle'); |
| 1112 | $body = str_replace('{{vehicle}}', $item->brand, $body); |
| 1113 | |
| 1114 | $body .= __('language.itv_reminders.body_license_plate'); |
| 1115 | $body = str_replace('{{license_plate}}', $item->license_plate, $body); |
| 1116 | |
| 1117 | $body .= __('language.itv_reminders.body_vehicle_type'); |
| 1118 | $body = str_replace('{{vehicle_type}}', $item->vehicle_type, $body); |
| 1119 | |
| 1120 | if($isBooked == 1){ |
| 1121 | $body .= __('language.itv_reminders.body_appointment_date'); |
| 1122 | $item->appointment_date_translate = date("d/m/Y", strtotime($item->appointment_date)); |
| 1123 | $body = str_replace('{{appointment_date}}', $item->appointment_date_translate, $body); |
| 1124 | |
| 1125 | $body .= __('language.itv_reminders.body_appointment_time'); |
| 1126 | $formattedTime = date("g:i A", strtotime($item->appointment_time)); |
| 1127 | $body = str_replace('{{appointment_time}}', $formattedTime, $body); |
| 1128 | |
| 1129 | $body .= __('language.itv_reminders.body_location'); |
| 1130 | $body = str_replace('{{location}}', $item->location, $body); |
| 1131 | }else{ |
| 1132 | $body .= __('language.itv_reminders.body_next_itv_date'); |
| 1133 | $item->next_itv_date_translate = date("m/Y", strtotime($item->next_itv_date)); |
| 1134 | $body = str_replace('{{next_itv_date}}', $item->next_itv_date_translate, $body); |
| 1135 | |
| 1136 | $body .= __('language.itv_reminders.body_driver'); |
| 1137 | $body = str_replace('{{driver}}', $item->driver, $body); |
| 1138 | } |
| 1139 | |
| 1140 | $body .= $footer; |
| 1141 | |
| 1142 | $body .= "<p>Fire Service Titan</p>"; |
| 1143 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 1144 | |
| 1145 | $html = '<!DOCTYPE html>'; |
| 1146 | $html .= '<html>'; |
| 1147 | $html .= '<head>'; |
| 1148 | $html .= '<meta charset="UTF-8">'; |
| 1149 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 1150 | $html .= '</head>'; |
| 1151 | $html .= '<body>'; |
| 1152 | $html .= $body; |
| 1153 | $html .= '</body>'; |
| 1154 | $html .= '</html>'; |
| 1155 | |
| 1156 | $email->addContent("text/html", $html); |
| 1157 | |
| 1158 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 1159 | |
| 1160 | $response = $sendgrid->send($email); |
| 1161 | if ($response->statusCode() == 202) { |
| 1162 | |
| 1163 | foreach ($addTo as $addToEmail) { |
| 1164 | TblItvEmailLogs::create( |
| 1165 | array( |
| 1166 | 'itv_id' => $item->id, |
| 1167 | 'email' => $addToEmail, |
| 1168 | 'sent_by' => $userName, |
| 1169 | 'status' => 'OK' |
| 1170 | ) |
| 1171 | ); |
| 1172 | } |
| 1173 | |
| 1174 | $toUpdate = array('is_due' => $duration, 'updated_by' => $userName, 'updated_at' => date('Y-m-d H:i:s')); |
| 1175 | |
| 1176 | if($isBooked == 1){ |
| 1177 | $toUpdate = array('is_due_appointment' => $duration, 'updated_by' => $userName, 'updated_at' => date('Y-m-d H:i:s')); |
| 1178 | } |
| 1179 | |
| 1180 | TblItv::where('id', $item->id)->update($toUpdate); |
| 1181 | }else{ |
| 1182 | foreach ($addTo as $addToEmail) { |
| 1183 | TblItvEmailLogs::create( |
| 1184 | array( |
| 1185 | 'itv_id' => $item->id, |
| 1186 | 'email' => $addToEmail, |
| 1187 | 'sent_by' => $userName, |
| 1188 | 'status' => $response->body() |
| 1189 | ) |
| 1190 | ); |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | function email_reminders_mileage($duration, $id){ |
| 1196 | |
| 1197 | $item = TblItv::where('id', $id)->first(); |
| 1198 | |
| 1199 | $body = ""; |
| 1200 | $subject = ""; |
| 1201 | |
| 1202 | $email = new \SendGrid\Mail\Mail(); |
| 1203 | |
| 1204 | $user = TblUsers::where('id', $this->userId)->first(); |
| 1205 | $userName = $user->name; |
| 1206 | |
| 1207 | $addTo = array(); |
| 1208 | |
| 1209 | if(env('SENDGRID_STAGING')){ |
| 1210 | $email->addTo($user->email); |
| 1211 | array_push($addTo, $user->email); |
| 1212 | }else{ |
| 1213 | |
| 1214 | $user = TblUsers::where('name', $item->created_by)->first(); |
| 1215 | $email->addTo($user->email); |
| 1216 | array_push($addTo, $user->email); |
| 1217 | |
| 1218 | if($item->created_by != $item->responsible_name){ |
| 1219 | $isValid = $this->isEmailValid($item->responsible_email); |
| 1220 | if($isValid){ |
| 1221 | $email->addTo($item->responsible_email); |
| 1222 | array_push($addTo, $item->responsible_email); |
| 1223 | }else{ |
| 1224 | TblItvEmailLogs::create( |
| 1225 | array( |
| 1226 | 'itv_id' => $item->id, |
| 1227 | 'email' => $item->responsible_email, |
| 1228 | 'sent_by' => $userName, |
| 1229 | 'status' => 'Invalid email' |
| 1230 | ) |
| 1231 | ); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | $subject = __('language.itv_reminders_km.subject'); |
| 1237 | $subject = str_replace('{{brand}}', $item->brand, $subject); |
| 1238 | $subject = str_replace('{{license_plate}}', $item->license_plate, $subject); |
| 1239 | |
| 1240 | $email->setFrom("fire@fire.es", "Fire Service Titan"); |
| 1241 | $email->setSubject($subject); |
| 1242 | |
| 1243 | $imgpath = \File::get('fireservicetitan.png'); |
| 1244 | |
| 1245 | $email->addAttachment( |
| 1246 | $imgpath, |
| 1247 | "image/png", |
| 1248 | "fireservicetitan.png", |
| 1249 | "inline", |
| 1250 | "fireservicetitan" |
| 1251 | ); |
| 1252 | |
| 1253 | $url = env('URL') . "itv/{$item->id}"; |
| 1254 | |
| 1255 | $body .= __('language.itv_reminders_km.body_hello'); |
| 1256 | $body = str_replace('{{responsible_name}}', $item->responsible_name, $body); |
| 1257 | |
| 1258 | $body .= __('language.itv_reminders_km.body_message_header'); |
| 1259 | $body = str_replace('{{click}}', $url, $body); |
| 1260 | |
| 1261 | $body .= __('language.itv_reminders_km.body_vehicle'); |
| 1262 | $body = str_replace('{{vehicle}}', $item->brand, $body); |
| 1263 | |
| 1264 | $body .= __('language.itv_reminders_km.body_license_plate'); |
| 1265 | $body = str_replace('{{license_plate}}', $item->license_plate, $body); |
| 1266 | |
| 1267 | $body .= __('language.itv_reminders.body_vehicle_type'); |
| 1268 | $body = str_replace('{{vehicle_type}}', $item->vehicle_type, $body); |
| 1269 | |
| 1270 | $body .= __('language.itv_reminders_km.body_mileage'); |
| 1271 | $body = str_replace('{{mileage}}', number_format($item->mileage, 2, ',', '.'), $body); |
| 1272 | |
| 1273 | $body .= __('language.itv_reminders_km.body_mileage_threshold'); |
| 1274 | $body = str_replace('{{mileage_threshold}}', number_format($item->mileage_threshold, 2, ',', '.'), $body); |
| 1275 | |
| 1276 | $body .= __('language.itv_reminders_km.body_message_footer'); |
| 1277 | |
| 1278 | $body .= "<p>Fire Service Titan</p>"; |
| 1279 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 1280 | |
| 1281 | $html = '<!DOCTYPE html>'; |
| 1282 | $html .= '<html>'; |
| 1283 | $html .= '<head>'; |
| 1284 | $html .= '<meta charset="UTF-8">'; |
| 1285 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 1286 | $html .= '</head>'; |
| 1287 | $html .= '<body>'; |
| 1288 | $html .= $body; |
| 1289 | $html .= '</body>'; |
| 1290 | $html .= '</html>'; |
| 1291 | |
| 1292 | $email->addContent("text/html", $html); |
| 1293 | |
| 1294 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 1295 | |
| 1296 | $response = $sendgrid->send($email); |
| 1297 | if ($response->statusCode() == 202) { |
| 1298 | |
| 1299 | foreach ($addTo as $addToEmail) { |
| 1300 | TblItvEmailLogs::create( |
| 1301 | array( |
| 1302 | 'itv_id' => $item->id, |
| 1303 | 'email' => $addToEmail, |
| 1304 | 'sent_by' => $userName, |
| 1305 | 'status' => 'OK' |
| 1306 | ) |
| 1307 | ); |
| 1308 | } |
| 1309 | |
| 1310 | TblItv::where('id', $item->id)->update(array('is_due_mileage' => $duration, 'updated_by' => $userName, 'updated_at' => date('Y-m-d H:i:s'))); |
| 1311 | }else{ |
| 1312 | foreach ($addTo as $addToEmail) { |
| 1313 | TblItvEmailLogs::create( |
| 1314 | array( |
| 1315 | 'itv_id' => $item->id, |
| 1316 | 'email' => $addToEmail, |
| 1317 | 'sent_by' => $userName, |
| 1318 | 'status' => $response->body() |
| 1319 | ) |
| 1320 | ); |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | function get_distincts(){ |
| 1327 | |
| 1328 | try { |
| 1329 | |
| 1330 | $query = "SELECT DISTINCT region FROM tbl_itv ORDER BY region ASC"; |
| 1331 | $regions = DB::select($query); |
| 1332 | |
| 1333 | $query = "SELECT DISTINCT brand FROM tbl_itv ORDER BY brand ASC"; |
| 1334 | $brands = DB::select($query); |
| 1335 | |
| 1336 | $query = "SELECT |
| 1337 | DISTINCT b.name |
| 1338 | FROM tbl_itv a |
| 1339 | LEFT JOIN tbl_companies b |
| 1340 | ON a.company_id = b.company_id |
| 1341 | ORDER BY b.name ASC"; |
| 1342 | |
| 1343 | $companies = DB::select($query); |
| 1344 | |
| 1345 | $query = "SELECT DISTINCT responsible_name FROM tbl_itv ORDER BY responsible_name ASC"; |
| 1346 | $responsibleNames = DB::select($query); |
| 1347 | |
| 1348 | $query = "SELECT DISTINCT driver FROM tbl_itv ORDER BY driver ASC"; |
| 1349 | $drivers = DB::select($query); |
| 1350 | |
| 1351 | return response([ |
| 1352 | 'message' => 'OK', |
| 1353 | 'regions' => $regions, |
| 1354 | 'brands' => $brands, |
| 1355 | 'companies' => $companies, |
| 1356 | 'responsibleNames' => $responsibleNames, |
| 1357 | 'drivers' => $drivers |
| 1358 | ]); |
| 1359 | |
| 1360 | } catch (\Exception $e) { |
| 1361 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1362 | } |
| 1363 | |
| 1364 | } |
| 1365 | |
| 1366 | function get_all_users($companyId){ |
| 1367 | |
| 1368 | try { |
| 1369 | |
| 1370 | $companyId = addslashes($companyId); |
| 1371 | |
| 1372 | $where = ""; |
| 1373 | |
| 1374 | if($companyId != 0){ |
| 1375 | $where = "WHERE company_id = {$companyId} "; |
| 1376 | }else{ |
| 1377 | $where = "WHERE company_id IN ({$this->companyId}) "; |
| 1378 | } |
| 1379 | |
| 1380 | $query = "SELECT |
| 1381 | DISTINCT responsible_name |
| 1382 | FROM |
| 1383 | tbl_itv |
| 1384 | {$where} |
| 1385 | ORDER BY |
| 1386 | responsible_name ASC"; |
| 1387 | |
| 1388 | $responsibleNames = DB::select($query); |
| 1389 | |
| 1390 | return response([ |
| 1391 | 'message' => 'OK', |
| 1392 | 'responsibleNames' => $responsibleNames |
| 1393 | ]); |
| 1394 | |
| 1395 | } catch (\Exception $e) { |
| 1396 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1397 | } |
| 1398 | } |
| 1399 | } |