Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 978 |
|
0.00% |
0 / 45 |
CRAP | |
0.00% |
0 / 1 |
| Notifications | |
0.00% |
0 / 978 |
|
0.00% |
0 / 45 |
32580 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| list_notifications | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| update_notifications | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| send_follow_up_notification | |
0.00% |
0 / 151 |
|
0.00% |
0 / 1 |
272 | |||
| send_request | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| send_g3w_warning | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| send_request_notification | |
0.00% |
0 / 137 |
|
0.00% |
0 / 1 |
272 | |||
| get_cc_bcc | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| delete_cc_bcc | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update_cc_bcc | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| create_cc_bcc | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| isEmailValid | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| delete_notifications | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| list_notification_logs | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| delete_notification_logs | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| get_cc_g3w | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| delete_cc_g3w | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update_cc_g3w | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| create_cc_g3w | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| get_cc_bcc_request | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| delete_cc_bcc_request | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update_cc_bcc_request | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| create_cc_bcc_request | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| send_executive | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
56 | |||
| send_executive_report | |
0.00% |
0 / 186 |
|
0.00% |
0 / 1 |
240 | |||
| get_executive_reports | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
30 | |||
| currency | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_cc_bcc_executive | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| delete_cc_bcc_executive | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update_cc_bcc_executive | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| create_cc_bcc_executive | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| update_notification_settings | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| get_notification_settings | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| send_report_logs | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
12 | |||
| get_emails_acceptance_notifications | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| create_to_email_acceptance | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| delete_to_email_acceptance | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update_to_email_acceptance | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| create_cc_email_acceptance | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| delete_cc_email_acceptance | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| update_cc_email_acceptance | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| list_last_follow_up_date | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| update_last_follow_up_date_working_days | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| update_last_follow_up_date | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
30 | |||
| delete_last_follow_up_date | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\TblNotifications; |
| 6 | use App\Models\TblCompanies; |
| 7 | use App\Models\TblCompanyUsers; |
| 8 | use App\Models\TblUsers; |
| 9 | use App\Models\TblCcBccNotifications; |
| 10 | use App\Models\TblNotificationLogs; |
| 11 | use App\Models\TblCcBccRequestNotifications; |
| 12 | use App\Models\TblCcG3WEmailReminders; |
| 13 | use App\Models\TblCcBccExecutiveNotifications; |
| 14 | use App\Models\TblNotificationSettings; |
| 15 | use App\Models\TblToAcceptanceNotifications; |
| 16 | use App\Models\TblCcAcceptanceNotifications; |
| 17 | use App\Models\TblLastFollowUpDate; |
| 18 | use Illuminate\Http\Request; |
| 19 | use Illuminate\Support\Facades\DB; |
| 20 | use Illuminate\Support\Facades\App; |
| 21 | use Illuminate\Support\Facades\Storage; |
| 22 | use Illuminate\Support\Facades\Log; |
| 23 | use Illuminate\Support\Facades\Artisan; |
| 24 | use SendGrid\Mail\Mail; |
| 25 | |
| 26 | class Notifications extends Controller |
| 27 | { |
| 28 | public function __construct(){ |
| 29 | $this->locale = @getallheaders()['Locale-ID']; |
| 30 | $this->userId = @getallheaders()['User-ID']; |
| 31 | $this->region = @getallheaders()['Region']; |
| 32 | |
| 33 | App::setLocale($this->locale); |
| 34 | |
| 35 | $this->companyIds = array(); |
| 36 | |
| 37 | if($this->region != null && $this->region != "" && $this->region != "All"){ |
| 38 | $this->region = urldecode($this->region); |
| 39 | |
| 40 | $query = "SELECT |
| 41 | b.company_id |
| 42 | FROM |
| 43 | tbl_company_users a |
| 44 | LEFT JOIN tbl_companies b ON a.company_id = b.company_id |
| 45 | WHERE |
| 46 | a.user_id = {$this->userId} |
| 47 | AND b.region = '{$this->region}'"; |
| 48 | |
| 49 | $this->companyIds = DB::select($query); |
| 50 | |
| 51 | $this->companyIds = collect($this->companyIds)->pluck('company_id')->toArray(); |
| 52 | }else{ |
| 53 | $this->companyIds = TblCompanyUsers::where('user_id', $this->userId)->pluck('company_id')->all(); |
| 54 | } |
| 55 | |
| 56 | $this->companyId = implode(',', $this->companyIds); |
| 57 | } |
| 58 | |
| 59 | function list_notifications(Request $request){ |
| 60 | |
| 61 | try { |
| 62 | |
| 63 | $data = $request->all(); |
| 64 | $userId = addslashes($data['user_id']); |
| 65 | |
| 66 | $query = "SELECT |
| 67 | notification_id, |
| 68 | user_id, |
| 69 | REPLACE(content, '<br>', '') content, |
| 70 | link, |
| 71 | is_open, |
| 72 | created_by, |
| 73 | created_at, |
| 74 | updated_by, |
| 75 | updated_at, |
| 76 | CASE |
| 77 | WHEN TIMESTAMPDIFF(SECOND, created_at, NOW()) < 60 THEN |
| 78 | CASE |
| 79 | WHEN TIMESTAMPDIFF(SECOND, created_at, NOW()) = 1 THEN '1 second ago' |
| 80 | ELSE CONCAT(TIMESTAMPDIFF(SECOND, created_at, NOW()), ' seconds ago') |
| 81 | END |
| 82 | WHEN TIMESTAMPDIFF(MINUTE, created_at, NOW()) < 60 THEN |
| 83 | CASE |
| 84 | WHEN TIMESTAMPDIFF(MINUTE, created_at, NOW()) = 1 THEN '1 minute ago' |
| 85 | ELSE CONCAT(TIMESTAMPDIFF(MINUTE, created_at, NOW()), ' minutes ago') |
| 86 | END |
| 87 | WHEN TIMESTAMPDIFF(HOUR, created_at, NOW()) < 24 THEN |
| 88 | CASE |
| 89 | WHEN TIMESTAMPDIFF(HOUR, created_at, NOW()) = 1 THEN '1 hour ago' |
| 90 | ELSE CONCAT(TIMESTAMPDIFF(HOUR, created_at, NOW()), ' hours ago') |
| 91 | END |
| 92 | WHEN TIMESTAMPDIFF(DAY, created_at, NOW()) < 7 THEN |
| 93 | CASE |
| 94 | WHEN TIMESTAMPDIFF(DAY, created_at, NOW()) = 1 THEN '1 day ago' |
| 95 | ELSE CONCAT(TIMESTAMPDIFF(DAY, created_at, NOW()), ' days ago') |
| 96 | END |
| 97 | WHEN TIMESTAMPDIFF(WEEK, created_at, NOW()) < 4 THEN |
| 98 | CASE |
| 99 | WHEN TIMESTAMPDIFF(WEEK, created_at, NOW()) = 1 THEN '1 week ago' |
| 100 | ELSE CONCAT(TIMESTAMPDIFF(WEEK, created_at, NOW()), ' weeks ago') |
| 101 | END |
| 102 | WHEN TIMESTAMPDIFF(MONTH, created_at, NOW()) < 12 THEN |
| 103 | CASE |
| 104 | WHEN TIMESTAMPDIFF(MONTH, created_at, NOW()) = 1 THEN '1 month ago' |
| 105 | ELSE CONCAT(TIMESTAMPDIFF(MONTH, created_at, NOW()), ' months ago') |
| 106 | END |
| 107 | ELSE |
| 108 | CASE |
| 109 | WHEN TIMESTAMPDIFF(YEAR, created_at, NOW()) = 1 THEN '1 year ago' |
| 110 | ELSE CONCAT(TIMESTAMPDIFF(YEAR, created_at, NOW()), ' years ago') |
| 111 | END |
| 112 | END AS formatted_created_at |
| 113 | FROM |
| 114 | tbl_notifications |
| 115 | WHERE user_id = {$userId} |
| 116 | ORDER BY |
| 117 | created_at DESC"; |
| 118 | |
| 119 | $result = DB::select($query); |
| 120 | |
| 121 | $totalOpenNotification = 0; |
| 122 | foreach ($result as $item) { |
| 123 | if($item->is_open > 0){ |
| 124 | $totalOpenNotification++; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return response([ |
| 129 | 'message' => 'OK', |
| 130 | 'data' => $result, |
| 131 | 'total' => $totalOpenNotification |
| 132 | ]); |
| 133 | |
| 134 | } catch (\Exception $e) { |
| 135 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | } |
| 140 | |
| 141 | function update_notifications(Request $request, $notificationId){ |
| 142 | |
| 143 | try { |
| 144 | |
| 145 | $data = $request->all(); |
| 146 | |
| 147 | if($notificationId == 999999999){ |
| 148 | TblNotifications::where('user_id', $data['user_id'])->update(array( |
| 149 | 'is_open' => null |
| 150 | )); |
| 151 | }else{ |
| 152 | TblNotifications::where('notification_id', $notificationId)->update(array( |
| 153 | 'is_open' => null, |
| 154 | 'updated_at' => date('Y-m-d H:i:s'), |
| 155 | 'updated_by' => $data['updated_by'] |
| 156 | )); |
| 157 | } |
| 158 | |
| 159 | $r = new Request([ |
| 160 | 'user_id' => $data['user_id'] |
| 161 | ]); |
| 162 | |
| 163 | $result = $this->list_notifications($r); |
| 164 | |
| 165 | return $result; |
| 166 | |
| 167 | } catch (\Exception $e) { |
| 168 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | function send_follow_up_notification($userId = null){ |
| 173 | |
| 174 | try { |
| 175 | |
| 176 | if($userId != null){ |
| 177 | $this->userId = $userId; |
| 178 | $this->locale = 'es'; |
| 179 | App::setLocale($this->locale); |
| 180 | } |
| 181 | |
| 182 | $users = TblUsers::get(); |
| 183 | $body = ""; |
| 184 | $allTotalFollowUps = 0; |
| 185 | $totalOrders = 0; |
| 186 | $totalCompanies = 0; |
| 187 | foreach ($users as $user) { |
| 188 | $userCompanies = TblCompanyUsers::where('user_id', $user->id)->get(); |
| 189 | |
| 190 | $body .= __('language.send_follow_up_notification.body_hello'); |
| 191 | $body = str_replace('{{username}}', $user->name, $body); |
| 192 | |
| 193 | $body .= __('language.send_follow_up_notification.body_message'); |
| 194 | |
| 195 | $hasFollowUp = false; |
| 196 | $companyCcIds = array(); |
| 197 | foreach ($userCompanies as $userCompany) { |
| 198 | |
| 199 | $companyId = $userCompany->company_id; |
| 200 | $company = TblCompanies::where('company_id', $companyId)->first(); |
| 201 | |
| 202 | if($company->is_send_follow_up > 0){ |
| 203 | $toEmail = $user->email; |
| 204 | |
| 205 | $query = "SELECT |
| 206 | id, |
| 207 | quote_id, |
| 208 | amount, |
| 209 | DATE_FORMAT(last_follow_up_date, '%d/%m/%Y') last_follow_up_date, |
| 210 | commercial |
| 211 | FROM |
| 212 | tbl_quotations |
| 213 | WHERE |
| 214 | last_follow_up_date < NOW() |
| 215 | AND email IS NOT NULL |
| 216 | AND email <> '' |
| 217 | AND NOT EXISTS ( |
| 218 | SELECT |
| 219 | 1 |
| 220 | FROM |
| 221 | tbl_blocked_domains bd |
| 222 | WHERE |
| 223 | email LIKE CONCAT('%', bd.domain, '%') |
| 224 | AND bd.company_id = company_id |
| 225 | ) |
| 226 | AND last_follow_up_date IS NOT NULL |
| 227 | AND reason_for_not_following_up_id IS NULL |
| 228 | AND last_follow_up_date > 0 |
| 229 | AND last_follow_up_date < NOW() |
| 230 | AND total_sent < 3 |
| 231 | AND for_add = 0 |
| 232 | AND company_id = {$companyId} |
| 233 | AND commercial IS NOT NULL |
| 234 | AND commercial = '{$user->name}' |
| 235 | AND budget_status_id = 2 |
| 236 | ORDER BY last_follow_up_date ASC, CAST(amount AS DOUBLE) DESC"; |
| 237 | |
| 238 | $result = DB::select($query); |
| 239 | |
| 240 | $totalFolloUps = count($result); |
| 241 | |
| 242 | if($totalFolloUps > 0){ |
| 243 | $allTotalFollowUps = $allTotalFollowUps + $totalFolloUps; |
| 244 | array_push($companyCcIds, $companyId); |
| 245 | |
| 246 | $hasFollowUp = true; |
| 247 | $body .= __('language.send_follow_up_notification.body_company_message'); |
| 248 | $body = str_replace('{{total}}', $totalFolloUps, $body); |
| 249 | $body = str_replace('{{company}}', $company->name, $body); |
| 250 | |
| 251 | $clickhRef = env('URL') . "orders?commercial={$user->name}&status=Enviado&last_follow_up_date=1&company_id={$companyId}"; |
| 252 | $body = str_replace('{{click}}', $clickhRef, $body); |
| 253 | |
| 254 | $ul = "<ul>"; |
| 255 | |
| 256 | $list = ""; |
| 257 | $totalAmount = 0; |
| 258 | for ($i = 0; $i < $totalFolloUps; $i++) { |
| 259 | $li = __('language.send_follow_up_notification.body_due'); |
| 260 | |
| 261 | $id = $result[$i]->id; |
| 262 | $date = $result[$i]->last_follow_up_date; |
| 263 | $quoteId = $result[$i]->quote_id; |
| 264 | $totalAmount = $totalAmount + $result[$i]->amount; |
| 265 | $amount = $this->currency($result[$i]->amount, 1); |
| 266 | |
| 267 | $url = env('URL') . "orders/{$id}?company_id={$companyId}"; |
| 268 | $href = "<a href='{$url}'>{$quoteId}</a>"; |
| 269 | |
| 270 | $li = str_replace('{{quote_id}}', $href, $li); |
| 271 | $li = str_replace('{{amount}}', $amount, $li); |
| 272 | $list .= str_replace('{{date}}', $date, $li); |
| 273 | } |
| 274 | $ul .= $list; |
| 275 | $ul .= "</ul>"; |
| 276 | |
| 277 | $totalAmount = $this->currency($totalAmount, 1); |
| 278 | $body = str_replace('{{amount}}', $totalAmount, $body); |
| 279 | |
| 280 | $content = $body . $ul; |
| 281 | |
| 282 | $body .= $ul; |
| 283 | |
| 284 | }else{ |
| 285 | continue; |
| 286 | } |
| 287 | }else{ |
| 288 | continue; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if($hasFollowUp){ |
| 293 | |
| 294 | $body = str_replace('{{totalOrders}}', $allTotalFollowUps, $body); |
| 295 | $body = str_replace('{{totalCompanies}}', count($companyCcIds), $body); |
| 296 | |
| 297 | $clickhRef = env('URL') . "orders?commercial={$user->name}&status=Enviado&last_follow_up_date=1&company_id=0"; |
| 298 | $body = str_replace('{{clickAll}}', $clickhRef, $body); |
| 299 | |
| 300 | $imgpathS = \File::get(public_path('sendfollowups.png')); |
| 301 | $img = "<br><img src='cid:sendfollowups' />"; |
| 302 | $body .= $img; |
| 303 | |
| 304 | $imgpath = \File::get(public_path('fireservicetitan.png')); |
| 305 | $body .= "<br><p>Fire Service Titan</p>"; |
| 306 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 307 | |
| 308 | $html = '<!DOCTYPE html>'; |
| 309 | $html .= '<html>'; |
| 310 | $html .= '<head>'; |
| 311 | $html .= '<meta charset="UTF-8">'; |
| 312 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 313 | $html .= '</head>'; |
| 314 | $html .= '<body>'; |
| 315 | $html .= $body; |
| 316 | $html .= '</body>'; |
| 317 | $html .= '</html>'; |
| 318 | |
| 319 | $dateNow = date_create(date('Y-m-d')); |
| 320 | $dateNow = date_format($dateNow,"d/m/Y"); |
| 321 | |
| 322 | $subject = __('language.send_follow_up_notification.subject'); |
| 323 | $subject = str_replace('{{dateNow}}', $dateNow, $subject); |
| 324 | |
| 325 | if($toEmail != null){ |
| 326 | $email = new \SendGrid\Mail\Mail(); |
| 327 | $sentBy = 'System'; |
| 328 | |
| 329 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 330 | $email->setSubject($subject); |
| 331 | |
| 332 | $userAdmin = TblUsers::where('id', $this->userId)->first(); |
| 333 | $sentBy = $userAdmin->name; |
| 334 | |
| 335 | if(env('SENDGRID_STAGING')){ |
| 336 | $toEmail = $userAdmin->email; |
| 337 | $sentBy = $userAdmin->name; |
| 338 | $user->id = $this->userId; |
| 339 | }else{ |
| 340 | $companyCcIds = array_values(array_unique($companyCcIds)); |
| 341 | $ccBcc = TblCcBccNotifications::whereIn('company_id', $companyCcIds)->get(); |
| 342 | |
| 343 | $inCc = array(); |
| 344 | |
| 345 | if(count($ccBcc) > 0){ |
| 346 | foreach ($ccBcc as $data) { |
| 347 | if($data->email != $toEmail && !in_array($data->email, $inCc)){ |
| 348 | $email->addCc($data->email); |
| 349 | array_push($inCc, $data->email); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | $email->addTo($toEmail); |
| 356 | $email->addContent("text/html", $html); |
| 357 | |
| 358 | $email->addAttachment( |
| 359 | $imgpath, |
| 360 | "image/png", |
| 361 | "fireservicetitan.png", |
| 362 | "inline", |
| 363 | "fireservicetitan" |
| 364 | ); |
| 365 | |
| 366 | $email->addAttachment( |
| 367 | $imgpathS, |
| 368 | "image/png", |
| 369 | "sendfollowups.png", |
| 370 | "inline", |
| 371 | "sendfollowups" |
| 372 | ); |
| 373 | |
| 374 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 375 | |
| 376 | $response = $sendgrid->send($email); |
| 377 | if ($response->statusCode() == 202) { |
| 378 | Log::channel('email_log')->info('ID: '. $toEmail .' - TO FOLLOW UP EMAIL NOTIFICATION SENT'); |
| 379 | |
| 380 | TblNotifications::create( |
| 381 | array( |
| 382 | 'user_id' => $user->id, |
| 383 | 'content' => $content, |
| 384 | 'is_open' => 1, |
| 385 | 'created_by' => 'System', |
| 386 | ) |
| 387 | ); |
| 388 | |
| 389 | TblNotificationLogs::create( |
| 390 | array( |
| 391 | 'commercial' => $user->name, |
| 392 | 'total_follow_ups' => $allTotalFollowUps, |
| 393 | 'created_by' => $sentBy |
| 394 | ) |
| 395 | ); |
| 396 | |
| 397 | } else { |
| 398 | $error = true; |
| 399 | Log::channel('email_log')->error('ID: '. $toEmail .' - ' . $response->body()); |
| 400 | } |
| 401 | |
| 402 | $body = ""; |
| 403 | $content = ""; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | $hasFollowUp = false; |
| 408 | $body = ""; |
| 409 | $content = ""; |
| 410 | $allTotalFollowUps = 0; |
| 411 | } |
| 412 | |
| 413 | return response([ |
| 414 | 'message' => 'OK' |
| 415 | ]); |
| 416 | |
| 417 | } catch (\Exception $e) { |
| 418 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | function send_request($companyId){ |
| 423 | |
| 424 | try { |
| 425 | |
| 426 | $companyId = addslashes($companyId); |
| 427 | |
| 428 | $user = array('commercial'); |
| 429 | |
| 430 | foreach ($user as $col) { |
| 431 | $this->send_request_notification($companyId, $col); |
| 432 | } |
| 433 | |
| 434 | return response([ |
| 435 | 'message' => 'OK' |
| 436 | ]); |
| 437 | |
| 438 | } catch (\Exception $e) { |
| 439 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 440 | } |
| 441 | |
| 442 | } |
| 443 | |
| 444 | function send_g3w_warning($companyId){ |
| 445 | |
| 446 | try { |
| 447 | |
| 448 | $companyId = addslashes($companyId); |
| 449 | |
| 450 | $userAdmin = TblUsers::where('id', $this->userId)->first(); |
| 451 | |
| 452 | if(env('SENDGRID_STAGING')){ |
| 453 | Artisan::call('send:g3w-email-reminders', [ |
| 454 | 'sent_by' => $userAdmin->name, |
| 455 | 'email' => $userAdmin->email, |
| 456 | 'company_id' => $companyId |
| 457 | ]); |
| 458 | }else{ |
| 459 | Artisan::call('send:g3w-email-reminders', [ |
| 460 | 'sent_by' => $userAdmin->name, |
| 461 | 'email' => null, |
| 462 | 'company_id' => $companyId |
| 463 | ]); |
| 464 | } |
| 465 | |
| 466 | $output = Artisan::output(); |
| 467 | |
| 468 | return response([ |
| 469 | 'message' => 'OK' |
| 470 | ]); |
| 471 | |
| 472 | } catch (\Exception $e) { |
| 473 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 474 | } |
| 475 | |
| 476 | } |
| 477 | |
| 478 | function send_request_notification($userId = null){ |
| 479 | |
| 480 | try { |
| 481 | |
| 482 | if($userId != null){ |
| 483 | $this->userId = $userId; |
| 484 | $this->locale = 'es'; |
| 485 | App::setLocale($this->locale); |
| 486 | } |
| 487 | |
| 488 | $users = TblUsers::get(); |
| 489 | $body = ""; |
| 490 | $allTotalRequest = 0; |
| 491 | |
| 492 | foreach ($users as $user) { |
| 493 | $userCompanies = TblCompanyUsers::where('user_id', $user->id)->get(); |
| 494 | |
| 495 | $body .= __('language.send_request_notification.body_hello'); |
| 496 | $body = str_replace('{{username}}', $user->name, $body); |
| 497 | $body .= __('language.send_request_notification.body_intro'); |
| 498 | |
| 499 | $hasRequest = false; |
| 500 | $companyCcIds = array(); |
| 501 | foreach ($userCompanies as $userCompany) { |
| 502 | |
| 503 | $companyId = $userCompany->company_id; |
| 504 | $company = TblCompanies::where('company_id', $companyId)->first(); |
| 505 | |
| 506 | if($company->is_send_request > 0){ |
| 507 | $toEmail = $user->email; |
| 508 | |
| 509 | $query = "SELECT |
| 510 | id, |
| 511 | quote_id, |
| 512 | DATE_FORMAT(created_at, '%d/%m/%Y') created_at, |
| 513 | commercial, |
| 514 | created_by |
| 515 | FROM |
| 516 | tbl_quotations |
| 517 | WHERE |
| 518 | company_id = {$companyId} |
| 519 | AND budget_status_id IN (6) |
| 520 | AND commercial = '{$user->name}'"; |
| 521 | |
| 522 | $result = DB::select($query); |
| 523 | |
| 524 | $totalRequest = count($result); |
| 525 | |
| 526 | if($totalRequest > 0){ |
| 527 | $allTotalRequest = $allTotalRequest + $totalRequest; |
| 528 | $hasRequest = true; |
| 529 | array_push($companyCcIds, $companyId); |
| 530 | |
| 531 | $body .= __('language.send_request_notification.body_message_commercial'); |
| 532 | $body = str_replace('{{total}}', $totalRequest, $body); |
| 533 | $body = str_replace('{{company}}', $company->name, $body); |
| 534 | |
| 535 | $clickhRef = env('URL') . "orders?commercial={$user->name}&status=Solicitud&company_id={$companyId}"; |
| 536 | $body = str_replace('{{click}}', $clickhRef, $body); |
| 537 | |
| 538 | $ul = "<ul>"; |
| 539 | |
| 540 | $list = ""; |
| 541 | $totalAmount = 0; |
| 542 | for ($i = 0; $i < $totalRequest; $i++) { |
| 543 | $li = __('language.send_request_notification.body_due'); |
| 544 | |
| 545 | $id = $result[$i]->id; |
| 546 | $date = $result[$i]->created_at; |
| 547 | $quoteId = $result[$i]->quote_id; |
| 548 | |
| 549 | $url = env('URL') . "orders/{$id}?company_id={$companyId}"; |
| 550 | $href = "<a href='{$url}'>{$quoteId}</a>"; |
| 551 | |
| 552 | $li = str_replace('{{quote_id}}', $href, $li); |
| 553 | $list .= str_replace('{{date}}', $date, $li); |
| 554 | } |
| 555 | $ul .= $list; |
| 556 | $ul .= "</ul>"; |
| 557 | |
| 558 | $totalAmount = $this->currency($totalAmount, 1); |
| 559 | $body = str_replace('{{amount}}', $totalAmount, $body); |
| 560 | |
| 561 | $content = $body . $ul; |
| 562 | |
| 563 | $body .= $ul; |
| 564 | |
| 565 | }else{ |
| 566 | continue; |
| 567 | } |
| 568 | }else{ |
| 569 | continue; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if($hasRequest){ |
| 574 | |
| 575 | $body = str_replace('{{totalOrders}}', $allTotalRequest, $body); |
| 576 | $body = str_replace('{{totalCompanies}}', count($companyCcIds), $body); |
| 577 | |
| 578 | $clickhRef = env('URL') . "orders?commercial={$user->name}&status=Solicitud&company_id=0"; |
| 579 | $body = str_replace('{{clickAll}}', $clickhRef, $body); |
| 580 | |
| 581 | $imgpath = \File::get(public_path('fireservicetitan.png')); |
| 582 | |
| 583 | $body .= "<br><p>Fire Service Titan</p>"; |
| 584 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 585 | |
| 586 | $html = '<!DOCTYPE html>'; |
| 587 | $html .= '<html>'; |
| 588 | $html .= '<head>'; |
| 589 | $html .= '<meta charset="UTF-8">'; |
| 590 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 591 | $html .= '</head>'; |
| 592 | $html .= '<body>'; |
| 593 | $html .= $body; |
| 594 | $html .= '</body>'; |
| 595 | $html .= '</html>'; |
| 596 | |
| 597 | $dateNow = date_create(date('Y-m-d')); |
| 598 | $dateNow = date_format($dateNow,"d/m/Y"); |
| 599 | |
| 600 | $subject = __('language.send_request_notification.subject'); |
| 601 | $subject = str_replace('{{dateNow}}', $dateNow, $subject); |
| 602 | $subject = str_replace('{{total}}', $allTotalRequest, $subject); |
| 603 | |
| 604 | if($toEmail != null){ |
| 605 | $email = new \SendGrid\Mail\Mail(); |
| 606 | $sentBy = 'System'; |
| 607 | |
| 608 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 609 | $email->setSubject($subject); |
| 610 | |
| 611 | $userAdmin = TblUsers::where('id', $this->userId)->first(); |
| 612 | $sentBy = $userAdmin->name; |
| 613 | |
| 614 | if(env('SENDGRID_STAGING')){ |
| 615 | $toEmail = $userAdmin->email; |
| 616 | $sentBy = $userAdmin->name; |
| 617 | $user->id = $this->userId; |
| 618 | }else{ |
| 619 | $companyCcIds = array_values(array_unique($companyCcIds)); |
| 620 | $ccBcc = TblCcBccRequestNotifications::whereIn('company_id', $companyCcIds)->get(); |
| 621 | |
| 622 | $inCc = array(); |
| 623 | |
| 624 | if(count($ccBcc) > 0){ |
| 625 | foreach ($ccBcc as $data) { |
| 626 | if($data->email != $toEmail && !in_array($data->email, $inCc)){ |
| 627 | $email->addCc($data->email); |
| 628 | array_push($inCc, $data->email); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | $email->addTo($toEmail); |
| 635 | $email->addContent("text/html", $html); |
| 636 | |
| 637 | $email->addAttachment( |
| 638 | $imgpath, |
| 639 | "image/png", |
| 640 | "fireservicetitan.png", |
| 641 | "inline", |
| 642 | "fireservicetitan" |
| 643 | ); |
| 644 | |
| 645 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 646 | |
| 647 | $response = $sendgrid->send($email); |
| 648 | if ($response->statusCode() == 202) { |
| 649 | Log::channel('email_log')->info('ID:' . $toEmail .' - REQUEST EMAIL NOTIFICATION SENT'); |
| 650 | |
| 651 | TblNotifications::create( |
| 652 | array( |
| 653 | 'user_id' => $user->id, |
| 654 | 'content' => $content, |
| 655 | 'is_open' => 1, |
| 656 | 'created_by' => 'System', |
| 657 | ) |
| 658 | ); |
| 659 | |
| 660 | TblNotificationLogs::create( |
| 661 | array( |
| 662 | 'company_id' => $companyId, |
| 663 | 'commercial' => $user->name, |
| 664 | 'total_request' => $allTotalRequest, |
| 665 | 'created_by' => $sentBy |
| 666 | ) |
| 667 | ); |
| 668 | |
| 669 | } else { |
| 670 | $error = true; |
| 671 | Log::channel('email_log')->error('ID:' . $toEmail .' - ' . $response->body()); |
| 672 | } |
| 673 | |
| 674 | $body = ""; |
| 675 | $content = ""; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | $hasRequest = false; |
| 680 | $body = ""; |
| 681 | $content = ""; |
| 682 | $allTotalRequest = 0; |
| 683 | } |
| 684 | |
| 685 | return response([ |
| 686 | 'message' => 'OK' |
| 687 | ]); |
| 688 | |
| 689 | |
| 690 | } catch (\Exception $e) { |
| 691 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | function get_cc_bcc($id){ |
| 696 | |
| 697 | try { |
| 698 | |
| 699 | $companyId = addslashes($id); |
| 700 | |
| 701 | $result = TblCcBccNotifications::where('company_id', $companyId)->get(); |
| 702 | |
| 703 | return response(['message' => 'OK', 'data' => $result]); |
| 704 | |
| 705 | } catch (\Exception $e) { |
| 706 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | function delete_cc_bcc($id){ |
| 711 | |
| 712 | try { |
| 713 | |
| 714 | $id = addslashes($id); |
| 715 | |
| 716 | TblCcBccNotifications::where('id', $id)->delete(); |
| 717 | |
| 718 | return response(['message' => 'OK']); |
| 719 | |
| 720 | } catch (\Exception $e) { |
| 721 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | function update_cc_bcc(Request $request, $id){ |
| 726 | |
| 727 | try { |
| 728 | |
| 729 | $data = $request->all(); |
| 730 | $id = addslashes($id); |
| 731 | |
| 732 | $x = $this->isEmailValid($data['email']); |
| 733 | |
| 734 | if($x){ |
| 735 | $result = TblCcBccNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 736 | |
| 737 | if($result > 0){ |
| 738 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 739 | }else{ |
| 740 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 741 | TblCcBccNotifications::where('id', $id)->update($data); |
| 742 | } |
| 743 | }else{ |
| 744 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 745 | } |
| 746 | |
| 747 | return response(['message' => 'OK']); |
| 748 | |
| 749 | } catch (\Exception $e) { |
| 750 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | function create_cc_bcc(Request $request){ |
| 755 | |
| 756 | try { |
| 757 | |
| 758 | $data = $request->all(); |
| 759 | |
| 760 | $x = $this->isEmailValid($data['email']); |
| 761 | |
| 762 | if($x){ |
| 763 | $result = TblCcBccNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 764 | |
| 765 | if($result > 0){ |
| 766 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 767 | }else{ |
| 768 | TblCcBccNotifications::create($data); |
| 769 | } |
| 770 | }else{ |
| 771 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 772 | } |
| 773 | |
| 774 | return response(['message' => 'OK']); |
| 775 | |
| 776 | } catch (\Exception $e) { |
| 777 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | function isEmailValid($email) { |
| 782 | // Regular expression pattern for email validation |
| 783 | $pattern = '/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/'; |
| 784 | |
| 785 | // Check if the email matches the pattern |
| 786 | if (preg_match($pattern, $email)) { |
| 787 | return true; // Valid email |
| 788 | } else { |
| 789 | return false; // Invalid email |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | function delete_notifications($userId){ |
| 794 | |
| 795 | try { |
| 796 | |
| 797 | $userId = addslashes($userId); |
| 798 | |
| 799 | TblNotifications::where('user_id', $userId)->delete(); |
| 800 | |
| 801 | $r = new Request([ |
| 802 | 'user_id' => $userId |
| 803 | ]); |
| 804 | |
| 805 | $result = $this->list_notifications($r); |
| 806 | |
| 807 | return $result; |
| 808 | |
| 809 | } catch (\Exception $e) { |
| 810 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 811 | } |
| 812 | |
| 813 | } |
| 814 | |
| 815 | function list_notification_logs($companyId){ |
| 816 | |
| 817 | try { |
| 818 | |
| 819 | $companyId = addslashes($companyId); |
| 820 | |
| 821 | if($companyId != 0){ |
| 822 | $where = " company_id = {$companyId} "; |
| 823 | }else{ |
| 824 | $where = " company_id IN ({$this->companyId}) "; |
| 825 | } |
| 826 | |
| 827 | $query = "SELECT |
| 828 | id, |
| 829 | commercial, |
| 830 | total_follow_ups, |
| 831 | total_request, |
| 832 | total_executive_report, |
| 833 | total_g3w_warning, |
| 834 | CASE |
| 835 | WHEN total_follow_ups IS NOT NULL THEN total_follow_ups |
| 836 | WHEN total_request IS NOT NULL THEN total_request |
| 837 | WHEN total_executive_report IS NOT NULL THEN total_executive_report |
| 838 | WHEN total_g3w_warning IS NOT NULL THEN total_g3w_warning |
| 839 | END totals, |
| 840 | created_by, |
| 841 | created_at |
| 842 | FROM `tbl_notification_logs` |
| 843 | WHERE {$where} |
| 844 | OR company_id IS NULL |
| 845 | ORDER BY created_at DESC"; |
| 846 | |
| 847 | $result = DB::select($query); |
| 848 | |
| 849 | return response(['message' => 'OK', 'data' => $result]); |
| 850 | |
| 851 | } catch (\Exception $e) { |
| 852 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 853 | } |
| 854 | |
| 855 | } |
| 856 | |
| 857 | function delete_notification_logs($companyId){ |
| 858 | |
| 859 | try { |
| 860 | |
| 861 | $companyId = addslashes($companyId); |
| 862 | |
| 863 | TblNotificationLogs::where('company_id', $companyId)->orWhere('company_id', null)->delete(); |
| 864 | |
| 865 | return response(['message' => 'OK']); |
| 866 | |
| 867 | } catch (\Exception $e) { |
| 868 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | function get_cc_g3w($id){ |
| 873 | |
| 874 | try { |
| 875 | |
| 876 | $companyId = addslashes($id); |
| 877 | |
| 878 | $result = TblCcG3WEmailReminders::where('company_id', $companyId)->get(); |
| 879 | |
| 880 | return response(['message' => 'OK', 'data' => $result]); |
| 881 | |
| 882 | } catch (\Exception $e) { |
| 883 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | function delete_cc_g3w($id){ |
| 888 | |
| 889 | try { |
| 890 | |
| 891 | $id = addslashes($id); |
| 892 | |
| 893 | TblCcG3WEmailReminders::where('id', $id)->delete(); |
| 894 | |
| 895 | return response(['message' => 'OK']); |
| 896 | |
| 897 | } catch (\Exception $e) { |
| 898 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | function update_cc_g3w(Request $request, $id){ |
| 903 | |
| 904 | try { |
| 905 | |
| 906 | $data = $request->all(); |
| 907 | $id = addslashes($id); |
| 908 | |
| 909 | $x = $this->isEmailValid($data['email']); |
| 910 | |
| 911 | if($x){ |
| 912 | $result = TblCcG3WEmailReminders::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 913 | |
| 914 | if($result > 0){ |
| 915 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 916 | }else{ |
| 917 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 918 | TblCcG3WEmailReminders::where('id', $id)->update($data); |
| 919 | } |
| 920 | }else{ |
| 921 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 922 | } |
| 923 | |
| 924 | return response(['message' => 'OK']); |
| 925 | |
| 926 | } catch (\Exception $e) { |
| 927 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | function create_cc_g3w(Request $request){ |
| 932 | |
| 933 | try { |
| 934 | |
| 935 | $data = $request->all(); |
| 936 | |
| 937 | $x = $this->isEmailValid($data['email']); |
| 938 | |
| 939 | if($x){ |
| 940 | $result = TblCcG3WEmailReminders::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 941 | |
| 942 | if($result > 0){ |
| 943 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 944 | }else{ |
| 945 | TblCcG3WEmailReminders::create($data); |
| 946 | } |
| 947 | }else{ |
| 948 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 949 | } |
| 950 | |
| 951 | return response(['message' => 'OK']); |
| 952 | |
| 953 | } catch (\Exception $e) { |
| 954 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | function get_cc_bcc_request($id){ |
| 959 | |
| 960 | try { |
| 961 | |
| 962 | $companyId = addslashes($id); |
| 963 | |
| 964 | $result = TblCcBccRequestNotifications::where('company_id', $companyId)->get(); |
| 965 | |
| 966 | return response(['message' => 'OK', 'data' => $result]); |
| 967 | |
| 968 | } catch (\Exception $e) { |
| 969 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | function delete_cc_bcc_request($id){ |
| 974 | |
| 975 | try { |
| 976 | |
| 977 | $id = addslashes($id); |
| 978 | |
| 979 | TblCcBccRequestNotifications::where('id', $id)->delete(); |
| 980 | |
| 981 | return response(['message' => 'OK']); |
| 982 | |
| 983 | } catch (\Exception $e) { |
| 984 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | function update_cc_bcc_request(Request $request, $id){ |
| 989 | |
| 990 | try { |
| 991 | |
| 992 | $data = $request->all(); |
| 993 | $id = addslashes($id); |
| 994 | |
| 995 | $x = $this->isEmailValid($data['email']); |
| 996 | |
| 997 | if($x){ |
| 998 | $result = TblCcBccRequestNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 999 | |
| 1000 | if($result > 0){ |
| 1001 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1002 | }else{ |
| 1003 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 1004 | TblCcBccRequestNotifications::where('id', $id)->update($data); |
| 1005 | } |
| 1006 | }else{ |
| 1007 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1008 | } |
| 1009 | |
| 1010 | return response(['message' => 'OK']); |
| 1011 | |
| 1012 | } catch (\Exception $e) { |
| 1013 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | function create_cc_bcc_request(Request $request){ |
| 1018 | |
| 1019 | try { |
| 1020 | |
| 1021 | $data = $request->all(); |
| 1022 | |
| 1023 | $x = $this->isEmailValid($data['email']); |
| 1024 | |
| 1025 | if($x){ |
| 1026 | $result = TblCcBccRequestNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1027 | |
| 1028 | if($result > 0){ |
| 1029 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1030 | }else{ |
| 1031 | TblCcBccRequestNotifications::create($data); |
| 1032 | } |
| 1033 | }else{ |
| 1034 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1035 | } |
| 1036 | |
| 1037 | return response(['message' => 'OK']); |
| 1038 | |
| 1039 | } catch (\Exception $e) { |
| 1040 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | function send_executive(Request $request){ |
| 1045 | |
| 1046 | try { |
| 1047 | |
| 1048 | $data = $request->all(); |
| 1049 | |
| 1050 | $executiveEmails = TblCcBccExecutiveNotifications::get(); |
| 1051 | |
| 1052 | if(isset($data['User-ID'])){ |
| 1053 | $this->userId = $data['User-ID']; |
| 1054 | } |
| 1055 | |
| 1056 | if(isset($data['self']) && $data['self'] == 1){ |
| 1057 | $user = TblUsers::where('id', $this->userId)->first(); |
| 1058 | $this->send_executive_report($user->email, $user->name, $user->id, $data['processed_by']); |
| 1059 | }else{ |
| 1060 | foreach ($executiveEmails as $item) { |
| 1061 | $user = TblUsers::where('email', $item->email)->first(); |
| 1062 | if($user){ |
| 1063 | $this->send_executive_report($item->email, $user->name, $user->id, $data['processed_by']); |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | return response([ |
| 1069 | 'message' => 'OK' |
| 1070 | ]); |
| 1071 | |
| 1072 | } catch (\Exception $e) { |
| 1073 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1074 | } |
| 1075 | |
| 1076 | } |
| 1077 | |
| 1078 | function send_executive_report($toEmail, $toName, $toUserId, $sentBy = 'System'){ |
| 1079 | |
| 1080 | try{ |
| 1081 | |
| 1082 | $result = TblNotificationSettings::where('id', 1)->first(); |
| 1083 | |
| 1084 | $currentYear = date('Y'); |
| 1085 | $previousYear = date('Y', strtotime('-1 year', strtotime(date('Y-m-d')))); |
| 1086 | |
| 1087 | if($toEmail != null && $result->is_auto_send_executive == 1){ |
| 1088 | |
| 1089 | if(env('SENDGRID_STAGING')){ |
| 1090 | $user = TblUsers::where('id', $this->userId)->first(); |
| 1091 | $toEmail = $user->email; |
| 1092 | } |
| 1093 | |
| 1094 | $result = (object) $this->get_executive_reports(); |
| 1095 | |
| 1096 | $weekNumber = $result->weekNumber; |
| 1097 | $monday = $result->monday; |
| 1098 | $totalBudgets = number_format($result->totalBudgets, 0, ',', '.'); |
| 1099 | $totalAmountApproved = $result->totalAmountApproved; |
| 1100 | $totalAmount = $result->totalAmount; |
| 1101 | $dividendMaintenance = $result->dividendMaintenance; |
| 1102 | $dividendCorrectives = $result->dividendCorrectives; |
| 1103 | $percentageMoreThanLastWeek = $result->percentageMoreThanLastWeek; |
| 1104 | $approvalRate = $result->approvalRate; |
| 1105 | |
| 1106 | $subject = "FST executive report week $weekNumber (starting on monday - $monday) - Automated"; |
| 1107 | |
| 1108 | $body = "<p>Hello {$toName},</p>"; |
| 1109 | |
| 1110 | $url = env('URL') . "analytics?week={$weekNumber}"; |
| 1111 | $body .= "<p>Find a summary of the commercial activity across <b>Grupo Fire</b> (see the <a href='{$url}'>report on FST here</a>) for week {$weekNumber} (starting on monday - {$monday})</p>"; |
| 1112 | $body .= "<p><b>{$totalBudgets} budget(s)</b> ({$percentageMoreThanLastWeek}% compared to last week) created for a total of <b>{$totalAmount}</b> ({$dividendMaintenance}% “Mantenimiento”, {$dividendCorrectives}% “Correctivos”) - "; |
| 1113 | $body .= "this week across all companies - <i>we have an approval rate of <b>{$approvalRate}%</b> for the month <b>(<span style='color: blue;'>{$totalAmountApproved} approved</span>)</b></i></p>"; |
| 1114 | |
| 1115 | $query = "SELECT |
| 1116 | a.company_id, |
| 1117 | a.name, |
| 1118 | ( |
| 1119 | SELECT |
| 1120 | COUNT(created_at) |
| 1121 | FROM |
| 1122 | tbl_quotations |
| 1123 | WHERE |
| 1124 | company_id = a.company_id |
| 1125 | AND for_add = 0 |
| 1126 | AND created_at IS NOT NULL |
| 1127 | AND issue_date IS NOT NULL |
| 1128 | AND YEAR(created_at) = YEAR(issue_date) |
| 1129 | ) total |
| 1130 | FROM |
| 1131 | tbl_companies a |
| 1132 | WHERE is_send_executive = 1 |
| 1133 | -- AND company_id IN ({$this->companyId}) |
| 1134 | ORDER BY |
| 1135 | 3 DESC |
| 1136 | "; |
| 1137 | |
| 1138 | $companies = DB::select($query); |
| 1139 | |
| 1140 | $li = array(); |
| 1141 | |
| 1142 | foreach ($companies as $company) { |
| 1143 | $result = (object) $this->get_executive_reports($company->company_id); |
| 1144 | $result->name = $company->name; |
| 1145 | array_push($li, $result); |
| 1146 | } |
| 1147 | |
| 1148 | usort($li, function ($a, $b) { |
| 1149 | return $b->totalBudgets - $a->totalBudgets; |
| 1150 | }); |
| 1151 | |
| 1152 | $body .= "<ul>"; |
| 1153 | foreach ($li as $result) { |
| 1154 | $totalBudgets = number_format($result->totalBudgets, 0, ',', '.'); |
| 1155 | $totalAmount = $result->totalAmount; |
| 1156 | $dividendMaintenance = $result->dividendMaintenance; |
| 1157 | $dividendCorrectives = $result->dividendCorrectives; |
| 1158 | $percentageMoreThanLastWeek = $result->percentageMoreThanLastWeek; |
| 1159 | $approvalRate = $result->approvalRate; |
| 1160 | |
| 1161 | $body .= "<li>{$totalBudgets} order(s) in “<b>{$result->name}</b>” ({$dividendMaintenance}% “Mantenimiento”, {$dividendCorrectives}% “Correctivos”) for total of <b>{$totalAmount}</b></li>"; |
| 1162 | } |
| 1163 | $body .= "</ul>"; |
| 1164 | |
| 1165 | |
| 1166 | $result = (object) $this->get_executive_reports(null, true, $currentYear); |
| 1167 | |
| 1168 | $totalBudgets = number_format($result->totalBudgets, 0, ',', '.'); |
| 1169 | $totalAmountApproved = $result->totalAmountApproved; |
| 1170 | $totalAmount = $result->totalAmount; |
| 1171 | $dividendMaintenance = $result->dividendMaintenance; |
| 1172 | $dividendCorrectives = $result->dividendCorrectives; |
| 1173 | $percentageMoreThanLastWeek = $result->percentageMoreThanLastWeek; |
| 1174 | $approvalRate = $result->approvalRate; |
| 1175 | $totalDaysFromStartOfYear = date('z') + 1; |
| 1176 | $totalDaysOfCurrentYear = date('z', mktime(0, 0, 0, 12, 31,date('Y'))) + 1; |
| 1177 | $expected = 0; |
| 1178 | |
| 1179 | $totalGoalForTheYear = 0; |
| 1180 | if($result->totalGoalForTheYear > 0){ |
| 1181 | $totalGoalForTheYear = number_format(($result->totalBudgets / $result->totalGoalForTheYear) * 100, 0, ',', '.'); |
| 1182 | $expected = number_format(($totalDaysFromStartOfYear / $totalDaysOfCurrentYear) * 100, 0, ',', '.'); |
| 1183 | } |
| 1184 | |
| 1185 | $body .= "<p><b>For {$currentYear}</b> as a whole, <b><span style='color: blue;'>{$totalBudgets}</span> budgets (<span style='color: blue;'>{$totalGoalForTheYear}% of the goal for the year accomplished</span> vs {$expected}% expected) for a total of <span style='color: blue;'>{$totalAmount}</span></b> ({$dividendMaintenance}% “Mantenimientos”, {$dividendCorrectives}% “Correctivos”) "; |
| 1186 | $body .= "were created across all companies - <i>we have an approval rate of <b>{$approvalRate}%</b> for the year <b>(<span style='color: blue;'>{$totalAmountApproved} approved</span>)</b></i></p>"; |
| 1187 | |
| 1188 | $query = "SELECT |
| 1189 | a.company_id, |
| 1190 | a.name, |
| 1191 | ( |
| 1192 | SELECT |
| 1193 | COUNT(created_at) |
| 1194 | FROM |
| 1195 | tbl_quotations |
| 1196 | WHERE |
| 1197 | company_id = a.company_id |
| 1198 | AND for_add != 1 |
| 1199 | AND amount REGEXP '^[0-9]+\\.?[0-9]*$' = 1 |
| 1200 | AND created_at IS NOT NULL |
| 1201 | AND YEAR(created_at) = {$currentYear} |
| 1202 | ) total |
| 1203 | FROM |
| 1204 | tbl_companies a |
| 1205 | WHERE is_send_executive = 1 |
| 1206 | -- AND company_id IN ({$this->companyId}) |
| 1207 | ORDER BY |
| 1208 | 3 DESC |
| 1209 | "; |
| 1210 | |
| 1211 | $companies = DB::select($query); |
| 1212 | |
| 1213 | $li = array(); |
| 1214 | |
| 1215 | foreach ($companies as $company) { |
| 1216 | $result = (object) $this->get_executive_reports($company->company_id, true, $currentYear); |
| 1217 | $result->name = $company->name; |
| 1218 | array_push($li, $result); |
| 1219 | } |
| 1220 | |
| 1221 | usort($li, function ($a, $b) { |
| 1222 | return $b->totalBudgets - $a->totalBudgets; |
| 1223 | }); |
| 1224 | |
| 1225 | $body .= "<ul>"; |
| 1226 | foreach ($li as $result) { |
| 1227 | |
| 1228 | $totalBudgets = number_format($result->totalBudgets, 0, ',', '.'); |
| 1229 | $totalAmountApproved = $result->totalAmountApproved; |
| 1230 | $totalAmount = $result->totalAmount; |
| 1231 | $dividendMaintenance = $result->dividendMaintenance; |
| 1232 | $dividendCorrectives = $result->dividendCorrectives; |
| 1233 | $percentageMoreThanLastWeek = $result->percentageMoreThanLastWeek; |
| 1234 | $approvalRate = $result->approvalRate; |
| 1235 | $goalForTheYear = number_format($result->totalGoalForTheYear, 0, ',', '.'); |
| 1236 | $totalDaysFromStartOfYear = date('z') + 1; |
| 1237 | $totalDaysOfCurrentYear = date('z', mktime(0, 0, 0, 12, 31,date('Y'))) + 1; |
| 1238 | $expected = 0; |
| 1239 | |
| 1240 | $totalGoalForTheYear = 0; |
| 1241 | $style = "style='color: green'"; |
| 1242 | if($result->totalGoalForTheYear > 0){ |
| 1243 | $totalGoalForTheYear = number_format(($result->totalBudgets / $result->totalGoalForTheYear) * 100, 0, ',', '.'); |
| 1244 | $expected = number_format(($totalDaysFromStartOfYear / $totalDaysOfCurrentYear) * 100, 0, ',', '.'); |
| 1245 | |
| 1246 | if($totalGoalForTheYear < $expected){ |
| 1247 | $style = "style='color: red'"; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | $body .= "<li>{$totalBudgets} order(s) in “<b>{$result->name}</b>” ({$dividendMaintenance}% “Mantenimiento”, {$dividendCorrectives}% “Correctivos”) for total of <b>{$totalAmount}</b></li>"; |
| 1252 | $body .= "<ul>"; |
| 1253 | $body .= "<li><i><b>{$goalForTheYear} goal</b> for the year. <b {$style}>{$totalGoalForTheYear}%</b> accomplished to date vs <b>{$expected}% expected</b></i></li>"; |
| 1254 | $body .= "<li><i>Approval rate of <b>{$approvalRate}%</b> (<span style='color: blue;'>{$totalAmountApproved} approved</span>)</i></li>"; |
| 1255 | $body .= "</ul>"; |
| 1256 | } |
| 1257 | |
| 1258 | $body .= "</ul>"; |
| 1259 | |
| 1260 | |
| 1261 | $result = (object) $this->get_executive_reports(null, true, $previousYear); |
| 1262 | |
| 1263 | $totalBudgets = number_format($result->totalBudgets, 0, ',', '.'); |
| 1264 | $totalAmount = $result->totalAmount; |
| 1265 | $dividendMaintenance = $result->dividendMaintenance; |
| 1266 | $dividendCorrectives = $result->dividendCorrectives; |
| 1267 | $percentageMoreThanLastWeek = $result->percentageMoreThanLastWeek; |
| 1268 | $approvalRate = $result->approvalRate; |
| 1269 | |
| 1270 | $body .= "<p>For your reference, in <b>{$previousYear}</b>, <b>{$totalBudgets} budgets for a total of {$totalAmount}</b> ({$dividendMaintenance}% “Mantenimientos”, {$dividendCorrectives}% “Correctivos”) "; |
| 1271 | $body .= "were created across all companies - <i>we have an approval rate of <b>{$approvalRate}%</b> for the year</i></p>"; |
| 1272 | |
| 1273 | $query = "SELECT |
| 1274 | a.company_id, |
| 1275 | a.name, |
| 1276 | ( |
| 1277 | SELECT |
| 1278 | COUNT(created_at) |
| 1279 | FROM |
| 1280 | tbl_quotations |
| 1281 | WHERE |
| 1282 | company_id = a.company_id |
| 1283 | AND for_add != 1 |
| 1284 | AND amount REGEXP '^[0-9]+\\.?[0-9]*$' = 1 |
| 1285 | AND created_at IS NOT NULL |
| 1286 | AND YEAR(created_at) = {$currentYear} |
| 1287 | ) total |
| 1288 | FROM |
| 1289 | tbl_companies a |
| 1290 | WHERE is_send_executive = 1 |
| 1291 | -- AND company_id IN ({$this->companyId}) |
| 1292 | ORDER BY |
| 1293 | 3 DESC |
| 1294 | "; |
| 1295 | |
| 1296 | $companies = DB::select($query); |
| 1297 | |
| 1298 | $li = array(); |
| 1299 | |
| 1300 | foreach ($companies as $company) { |
| 1301 | $result = (object) $this->get_executive_reports($company->company_id, true, $previousYear); |
| 1302 | $result->name = $company->name; |
| 1303 | array_push($li, $result); |
| 1304 | } |
| 1305 | |
| 1306 | usort($li, function ($a, $b) { |
| 1307 | return $b->totalBudgets - $a->totalBudgets; |
| 1308 | }); |
| 1309 | |
| 1310 | $body .= "<ul>"; |
| 1311 | |
| 1312 | foreach ($li as $result) { |
| 1313 | $totalBudgets = number_format($result->totalBudgets, 0, ',', '.'); |
| 1314 | $totalAmount = $result->totalAmount; |
| 1315 | $dividendMaintenance = $result->dividendMaintenance; |
| 1316 | $dividendCorrectives = $result->dividendCorrectives; |
| 1317 | $percentageMoreThanLastWeek = $result->percentageMoreThanLastWeek; |
| 1318 | $approvalRate = $result->approvalRate; |
| 1319 | |
| 1320 | $body .= "<li>{$totalBudgets} order(s) in “<b>{$result->name}</b>” ({$dividendMaintenance}% “Mantenimiento”, {$dividendCorrectives}% “Correctivos”) for total of <b>{$totalAmount}</b> <i> - approval rate of <b>{$approvalRate}%</b></i></li>"; |
| 1321 | } |
| 1322 | |
| 1323 | $body .= "</ul><br><br>"; |
| 1324 | |
| 1325 | $imgpath = \File::get(public_path('fireservicetitan.png')); |
| 1326 | |
| 1327 | $content = $body; |
| 1328 | |
| 1329 | $body .= "<br><p>Fire Service Titan</p>"; |
| 1330 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 1331 | |
| 1332 | $html = '<!DOCTYPE html>'; |
| 1333 | $html .= '<html>'; |
| 1334 | $html .= '<head>'; |
| 1335 | $html .= '<meta charset="UTF-8">'; |
| 1336 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 1337 | $html .= '</head>'; |
| 1338 | $html .= '<body>'; |
| 1339 | $html .= $body; |
| 1340 | $html .= '</body>'; |
| 1341 | $html .= '</html>'; |
| 1342 | |
| 1343 | $email = new \SendGrid\Mail\Mail(); |
| 1344 | |
| 1345 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 1346 | $email->setSubject($subject); |
| 1347 | |
| 1348 | $email->addTo($toEmail); |
| 1349 | $email->addContent("text/html", $html); |
| 1350 | |
| 1351 | $email->addAttachment( |
| 1352 | $imgpath, |
| 1353 | "image/png", |
| 1354 | "fireservicetitan.png", |
| 1355 | "inline", |
| 1356 | "fireservicetitan" |
| 1357 | ); |
| 1358 | |
| 1359 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 1360 | |
| 1361 | $response = $sendgrid->send($email); |
| 1362 | if ($response->statusCode() == 202) { |
| 1363 | Log::channel('email_log')->info('ID:' . $toEmail .' - EXECUTIVE REPORT EMAIL NOTIFICATION SENT'); |
| 1364 | |
| 1365 | TblNotifications::create( |
| 1366 | array( |
| 1367 | 'user_id' => $toUserId, |
| 1368 | 'content' => $content, |
| 1369 | 'is_open' => 1, |
| 1370 | 'created_by' => 'System', |
| 1371 | ) |
| 1372 | ); |
| 1373 | |
| 1374 | TblNotificationLogs::create( |
| 1375 | array( |
| 1376 | 'commercial' => $toName, |
| 1377 | 'total_executive_report' => 1, |
| 1378 | 'created_by' => $sentBy |
| 1379 | ) |
| 1380 | ); |
| 1381 | } else { |
| 1382 | $error = true; |
| 1383 | Log::channel('email_log')->error('ID:' . $toEmail .' - ' . $response->body()); |
| 1384 | } |
| 1385 | |
| 1386 | $body = ""; |
| 1387 | |
| 1388 | return response([ |
| 1389 | 'message' => 'OK' |
| 1390 | ]); |
| 1391 | } |
| 1392 | |
| 1393 | } catch (\Exception $e) { |
| 1394 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1395 | } |
| 1396 | |
| 1397 | } |
| 1398 | |
| 1399 | function get_executive_reports($companyId = null, $wholeYear = false, $year = null){ |
| 1400 | |
| 1401 | $where = ""; |
| 1402 | $whereYear = ""; |
| 1403 | $whereMonth = "DATE_FORMAT(q.created_at, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') "; |
| 1404 | |
| 1405 | if($companyId != null){ |
| 1406 | $where = " AND company_id = {$companyId} "; |
| 1407 | } |
| 1408 | |
| 1409 | $now = date('Y-m-d'); |
| 1410 | $weekNumber = date('W', strtotime($now)); |
| 1411 | $monday = date('F j, Y', strtotime($now . " - " . (date('N', strtotime($now)) - 1) . " days")); |
| 1412 | |
| 1413 | $mondayDate = date('Y-m-d', strtotime($now . " - " . (date('N', strtotime($now)) - 1) . " days")); |
| 1414 | $sundayDate = date('Y-m-d', strtotime($mondayDate . " + 6 days")); |
| 1415 | |
| 1416 | $mondayLastWeekDate = date('Y-m-d', strtotime($mondayDate . " - 1 week")); |
| 1417 | $sundayLastWeekDate = date('Y-m-d', strtotime($mondayLastWeekDate . " + 6 days")); |
| 1418 | |
| 1419 | $whereAccMonth = ""; |
| 1420 | if($wholeYear == false){ |
| 1421 | $whereYear = " AND DATE_FORMAT(q.created_at, '%Y-%m-%d') BETWEEN '{$mondayDate}' AND '{$sundayDate}' "; |
| 1422 | $whereMonth = " AND DATE_FORMAT(q.created_at, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') "; |
| 1423 | $whereAccMonth = " AND DATE_FORMAT(q.acceptance_date, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') "; |
| 1424 | }else{ |
| 1425 | $whereYear = " AND YEAR(q.created_at) = {$year} "; |
| 1426 | $whereMonth = " AND YEAR(q.created_at) = {$year} "; |
| 1427 | $whereAccMonth = " AND YEAR(q.acceptance_date) = {$year} "; |
| 1428 | } |
| 1429 | |
| 1430 | $query = "SELECT |
| 1431 | COUNT(q.created_at) totalBudgets, |
| 1432 | SUM(q.amount) totalAmount, |
| 1433 | COALESCE( |
| 1434 | SUM(CASE WHEN bt.budget_type_group_id = 3 THEN 1 END) |
| 1435 | , 0 |
| 1436 | ) / |
| 1437 | COUNT(q.created_at) * 100 dividendMaintenance, |
| 1438 | COALESCE( |
| 1439 | SUM(CASE WHEN bt.budget_type_group_id = 5 THEN 1 END) |
| 1440 | , 0 |
| 1441 | ) / |
| 1442 | COUNT(q.created_at) * 100 dividendCorrectives |
| 1443 | FROM |
| 1444 | tbl_quotations q |
| 1445 | LEFT JOIN tbl_budget_types bt ON q.budget_type_id = bt.budget_type_id |
| 1446 | WHERE |
| 1447 | q.for_add != 1 |
| 1448 | AND q.amount REGEXP '^[0-9]+\\.?[0-9]*$' = 1 |
| 1449 | AND q.created_at IS NOT NULL |
| 1450 | AND q.company_id IN (SELECT company_id FROM tbl_companies WHERE is_send_executive = 1 {$where}) |
| 1451 | AND q.budget_type_id != 7 |
| 1452 | AND YEAR(q.created_at) = YEAR(q.issue_date) |
| 1453 | {$whereYear} |
| 1454 | "; |
| 1455 | |
| 1456 | $result = DB::select($query)[0]; |
| 1457 | |
| 1458 | $totalBudgets = $result->totalBudgets; |
| 1459 | $totalAmount = $this->currency($result->totalAmount, 1, 0); |
| 1460 | $totalAmountC = $result->totalAmount; |
| 1461 | $dividendMaintenance = number_format($result->dividendMaintenance, 0); |
| 1462 | $dividendCorrectives = number_format($result->dividendCorrectives, 0); |
| 1463 | |
| 1464 | $query = "SELECT |
| 1465 | COUNT(q.created_at) totalBudgetLastWeek |
| 1466 | FROM tbl_quotations q WHERE |
| 1467 | DATE_FORMAT(q.created_at, '%Y-%m-%d') BETWEEN '{$mondayLastWeekDate}' |
| 1468 | AND '{$sundayLastWeekDate}' |
| 1469 | AND q.for_add != 1 |
| 1470 | AND q.amount REGEXP '^[0-9]+\\.?[0-9]*$' = 1 |
| 1471 | AND q.created_at IS NOT NULL |
| 1472 | AND q.budget_type_id != 7 |
| 1473 | AND q.budget_type_id IS NOT NULL |
| 1474 | AND YEAR(q.created_at) = YEAR(q.issue_date) |
| 1475 | AND q.company_id IN (SELECT company_id FROM tbl_companies WHERE is_send_executive = 1 {$where})"; |
| 1476 | |
| 1477 | $result = DB::select($query)[0]; |
| 1478 | |
| 1479 | $percentageMoreThanLastWeek = 0; |
| 1480 | |
| 1481 | if($result->totalBudgetLastWeek > 0){ |
| 1482 | $percentageMoreThanLastWeek = number_format($totalBudgets / $result->totalBudgetLastWeek * 100, 2); |
| 1483 | } |
| 1484 | |
| 1485 | $query = "SELECT |
| 1486 | SUM(q.amount) totalAmount |
| 1487 | FROM tbl_quotations q WHERE |
| 1488 | q.for_add != 1 |
| 1489 | AND q.amount REGEXP '^[0-9]+\\.?[0-9]*$' = 1 |
| 1490 | AND q.created_at IS NOT NULL |
| 1491 | AND q.budget_type_id != 7 |
| 1492 | AND q.budget_type_id IS NOT NULL |
| 1493 | AND YEAR(q.created_at) = YEAR(q.issue_date) |
| 1494 | AND q.company_id IN (SELECT company_id FROM tbl_companies WHERE is_send_executive = 1 {$where}) |
| 1495 | {$whereMonth}"; |
| 1496 | |
| 1497 | $result = DB::select($query)[0]; |
| 1498 | |
| 1499 | $totalAmountCC = $result->totalAmount; |
| 1500 | |
| 1501 | $query = "SELECT |
| 1502 | COUNT(CASE WHEN q.created_at IS NOT NULL THEN 1 END) totalBudgets, |
| 1503 | COUNT(CASE WHEN q.acceptance_date IS NOT NULL AND q.budget_status_id = 3 THEN 1 END) totalAcceptance, |
| 1504 | SUM(CASE WHEN q.acceptance_date IS NOT NULL AND q.budget_status_id = 3 THEN q.amount END) totalAmountApproved |
| 1505 | FROM tbl_quotations q WHERE |
| 1506 | q.for_add != 1 |
| 1507 | AND q.amount REGEXP '^[0-9]+\\.?[0-9]*$' = 1 |
| 1508 | AND q.created_at IS NOT NULL |
| 1509 | AND q.budget_type_id != 7 |
| 1510 | AND q.budget_type_id IS NOT NULL |
| 1511 | AND YEAR(q.created_at) = YEAR(q.issue_date) |
| 1512 | AND q.company_id IN (SELECT company_id FROM tbl_companies WHERE is_send_executive = 1 {$where}) |
| 1513 | {$whereAccMonth}"; |
| 1514 | |
| 1515 | $result = DB::select($query)[0]; |
| 1516 | |
| 1517 | $approvalRate = 0; |
| 1518 | $totalAmountApproved = $result->totalAmountApproved; |
| 1519 | if($totalAmountCC > 0){ |
| 1520 | $approvalRate = number_format(($totalAmountApproved / $totalAmountCC) * 100, 0); |
| 1521 | } |
| 1522 | |
| 1523 | $query = "SELECT SUM(goal_for_the_year) goalForTheYear FROM tbl_companies WHERE is_send_executive = 1 {$where}"; |
| 1524 | $result = DB::select($query)[0]; |
| 1525 | |
| 1526 | return array( |
| 1527 | 'weekNumber' => $weekNumber, |
| 1528 | 'monday' => $monday, |
| 1529 | 'totalBudgets' => $totalBudgets, |
| 1530 | 'totalAmount' => $totalAmount, |
| 1531 | 'totalAmountApproved' => $this->currency($totalAmountApproved, 1, 0), |
| 1532 | 'dividendMaintenance' => $dividendMaintenance, |
| 1533 | 'dividendCorrectives' => $dividendCorrectives, |
| 1534 | 'percentageMoreThanLastWeek' => $percentageMoreThanLastWeek, |
| 1535 | 'approvalRate' => $approvalRate, |
| 1536 | 'totalGoalForTheYear' => $result->goalForTheYear |
| 1537 | ); |
| 1538 | |
| 1539 | } |
| 1540 | |
| 1541 | function currency($amount, $withEuro = null, $n = 2){ |
| 1542 | |
| 1543 | if($withEuro != null){ |
| 1544 | $withEuro = ' €'; |
| 1545 | } |
| 1546 | |
| 1547 | return number_format($amount, $n, ',', '.') . $withEuro; |
| 1548 | } |
| 1549 | |
| 1550 | function get_cc_bcc_executive(){ |
| 1551 | |
| 1552 | try { |
| 1553 | |
| 1554 | $result = TblCcBccExecutiveNotifications::get(); |
| 1555 | |
| 1556 | return response(['message' => 'OK', 'data' => $result]); |
| 1557 | |
| 1558 | } catch (\Exception $e) { |
| 1559 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | function delete_cc_bcc_executive($id){ |
| 1564 | |
| 1565 | try { |
| 1566 | |
| 1567 | $id = addslashes($id); |
| 1568 | |
| 1569 | TblCcBccExecutiveNotifications::where('id', $id)->delete(); |
| 1570 | |
| 1571 | return response(['message' => 'OK']); |
| 1572 | |
| 1573 | } catch (\Exception $e) { |
| 1574 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | function update_cc_bcc_executive(Request $request, $id){ |
| 1579 | |
| 1580 | try { |
| 1581 | |
| 1582 | $data = $request->all(); |
| 1583 | $id = addslashes($id); |
| 1584 | |
| 1585 | $x = $this->isEmailValid($data['email']); |
| 1586 | |
| 1587 | if($x){ |
| 1588 | $result = TblCcBccExecutiveNotifications::where('email', strtolower($data['email']))->count(); |
| 1589 | |
| 1590 | if($result > 0){ |
| 1591 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1592 | }else{ |
| 1593 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 1594 | TblCcBccExecutiveNotifications::where('id', $id)->update($data); |
| 1595 | } |
| 1596 | }else{ |
| 1597 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1598 | } |
| 1599 | |
| 1600 | return response(['message' => 'OK']); |
| 1601 | |
| 1602 | } catch (\Exception $e) { |
| 1603 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | function create_cc_bcc_executive(Request $request){ |
| 1608 | |
| 1609 | try { |
| 1610 | |
| 1611 | $data = $request->all(); |
| 1612 | |
| 1613 | $x = $this->isEmailValid($data['email']); |
| 1614 | |
| 1615 | if($x){ |
| 1616 | $result = TblCcBccExecutiveNotifications::where('email', strtolower($data['email']))->count(); |
| 1617 | |
| 1618 | if($result > 0){ |
| 1619 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1620 | }else{ |
| 1621 | TblCcBccExecutiveNotifications::create($data); |
| 1622 | } |
| 1623 | }else{ |
| 1624 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1625 | } |
| 1626 | |
| 1627 | return response(['message' => 'OK']); |
| 1628 | |
| 1629 | } catch (\Exception $e) { |
| 1630 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | function update_notification_settings(Request $request){ |
| 1635 | |
| 1636 | try { |
| 1637 | |
| 1638 | $data = $request->all(); |
| 1639 | |
| 1640 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 1641 | TblNotificationSettings::where('id', 1)->update($data); |
| 1642 | |
| 1643 | return response(['message' => 'OK']); |
| 1644 | |
| 1645 | } catch (\Exception $e) { |
| 1646 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1647 | } |
| 1648 | |
| 1649 | } |
| 1650 | |
| 1651 | function get_notification_settings(){ |
| 1652 | |
| 1653 | try { |
| 1654 | |
| 1655 | $result = TblNotificationSettings::where('id', 1)->first(); |
| 1656 | |
| 1657 | return response(['message' => 'OK', 'data' => $result]); |
| 1658 | |
| 1659 | } catch (\Exception $e) { |
| 1660 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | function send_report_logs(Request $request){ |
| 1665 | |
| 1666 | try { |
| 1667 | |
| 1668 | $data = $request->all(); |
| 1669 | |
| 1670 | $toEmail = env('SENDGRID_TO_EMAIL'); |
| 1671 | |
| 1672 | $imgpath = \File::get('fireservicetitan.png'); |
| 1673 | |
| 1674 | $body = $data['logs']; |
| 1675 | |
| 1676 | $body .= "<br><br><p>Fire Service Titan</p>"; |
| 1677 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 1678 | |
| 1679 | $html = '<!DOCTYPE html>'; |
| 1680 | $html .= '<html>'; |
| 1681 | $html .= '<head>'; |
| 1682 | $html .= '<meta charset="UTF-8">'; |
| 1683 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 1684 | $html .= '</head>'; |
| 1685 | $html .= '<body>'; |
| 1686 | $html .= $body; |
| 1687 | $html .= '</body>'; |
| 1688 | $html .= '</html>'; |
| 1689 | |
| 1690 | $email = new \SendGrid\Mail\Mail(); |
| 1691 | $subject = "FST Error report " . date('Y-m-d H:i:s'); |
| 1692 | |
| 1693 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 1694 | $email->setSubject($subject); |
| 1695 | |
| 1696 | $email->addTo($toEmail); |
| 1697 | $email->addContent("text/html", $html); |
| 1698 | |
| 1699 | $email->addAttachment( |
| 1700 | $imgpath, |
| 1701 | "image/png", |
| 1702 | "fireservicetitan.png", |
| 1703 | "inline", |
| 1704 | "fireservicetitan" |
| 1705 | ); |
| 1706 | |
| 1707 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 1708 | |
| 1709 | $response = $sendgrid->send($email); |
| 1710 | if ($response->statusCode() == 202) { |
| 1711 | Log::channel('email_log')->info('ID:' . $toEmail .' - REPORT LOG EMAIL SENT'); |
| 1712 | } |
| 1713 | |
| 1714 | return response(['message' => 'OK']); |
| 1715 | |
| 1716 | } catch (\Exception $e) { |
| 1717 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | function get_emails_acceptance_notifications($companyId){ |
| 1722 | |
| 1723 | try { |
| 1724 | |
| 1725 | $companyId = addslashes($companyId); |
| 1726 | |
| 1727 | $cc = TblCcAcceptanceNotifications::where('company_id', $companyId)->get(); |
| 1728 | $to = TblToAcceptanceNotifications::where('company_id', $companyId)->get(); |
| 1729 | |
| 1730 | return response(['message' => 'OK', 'cc' => $cc, 'to' => $to]); |
| 1731 | |
| 1732 | } catch (\Exception $e) { |
| 1733 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | function create_to_email_acceptance(Request $request){ |
| 1738 | |
| 1739 | try { |
| 1740 | |
| 1741 | $data = $request->all(); |
| 1742 | |
| 1743 | $x = $this->isEmailValid($data['email']); |
| 1744 | |
| 1745 | if($x){ |
| 1746 | $result = TblToAcceptanceNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1747 | |
| 1748 | if($result > 0){ |
| 1749 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1750 | }else{ |
| 1751 | TblToAcceptanceNotifications::create($data); |
| 1752 | } |
| 1753 | }else{ |
| 1754 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1755 | } |
| 1756 | |
| 1757 | return response(['message' => 'OK']); |
| 1758 | |
| 1759 | } catch (\Exception $e) { |
| 1760 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | function delete_to_email_acceptance($id){ |
| 1765 | |
| 1766 | try { |
| 1767 | |
| 1768 | $id = addslashes($id); |
| 1769 | |
| 1770 | TblToAcceptanceNotifications::where('id', $id)->delete(); |
| 1771 | |
| 1772 | return response(['message' => 'OK']); |
| 1773 | |
| 1774 | } catch (\Exception $e) { |
| 1775 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | function update_to_email_acceptance(Request $request, $id){ |
| 1780 | |
| 1781 | try { |
| 1782 | |
| 1783 | $data = $request->all(); |
| 1784 | $id = addslashes($id); |
| 1785 | |
| 1786 | $x = $this->isEmailValid($data['email']); |
| 1787 | |
| 1788 | if($x){ |
| 1789 | $to = TblToAcceptanceNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1790 | $cc = TblCcAcceptanceNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1791 | |
| 1792 | if($to > 0 || $cc > 0){ |
| 1793 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1794 | }else{ |
| 1795 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 1796 | TblToAcceptanceNotifications::where('id', $id)->update($data); |
| 1797 | } |
| 1798 | }else{ |
| 1799 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1800 | } |
| 1801 | |
| 1802 | return response(['message' => 'OK']); |
| 1803 | |
| 1804 | } catch (\Exception $e) { |
| 1805 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | function create_cc_email_acceptance(Request $request){ |
| 1810 | |
| 1811 | try { |
| 1812 | |
| 1813 | $data = $request->all(); |
| 1814 | |
| 1815 | $x = $this->isEmailValid($data['email']); |
| 1816 | |
| 1817 | if($x){ |
| 1818 | $result = TblCcAcceptanceNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1819 | |
| 1820 | if($result > 0){ |
| 1821 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1822 | }else{ |
| 1823 | TblCcAcceptanceNotifications::create($data); |
| 1824 | } |
| 1825 | }else{ |
| 1826 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1827 | } |
| 1828 | |
| 1829 | return response(['message' => 'OK']); |
| 1830 | |
| 1831 | } catch (\Exception $e) { |
| 1832 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | function delete_cc_email_acceptance($id){ |
| 1837 | |
| 1838 | try { |
| 1839 | |
| 1840 | $id = addslashes($id); |
| 1841 | |
| 1842 | TblCcAcceptanceNotifications::where('id', $id)->delete(); |
| 1843 | |
| 1844 | return response(['message' => 'OK']); |
| 1845 | |
| 1846 | } catch (\Exception $e) { |
| 1847 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | function update_cc_email_acceptance(Request $request, $id){ |
| 1852 | |
| 1853 | try { |
| 1854 | |
| 1855 | $data = $request->all(); |
| 1856 | $id = addslashes($id); |
| 1857 | |
| 1858 | $x = $this->isEmailValid($data['email']); |
| 1859 | |
| 1860 | if($x){ |
| 1861 | $cc = TblCcAcceptanceNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1862 | $to = TblToAcceptanceNotifications::where('company_id', $data['company_id'])->where('email', strtolower($data['email']))->count(); |
| 1863 | |
| 1864 | if($cc > 0 || $to > 0){ |
| 1865 | return response(['message' => 'KO', 'error' => __('language.email_exists')]); |
| 1866 | }else{ |
| 1867 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 1868 | TblCcAcceptanceNotifications::where('id', $id)->update($data); |
| 1869 | } |
| 1870 | }else{ |
| 1871 | return response(['message' => 'KO', 'error' => __('language.email_invalid')]); |
| 1872 | } |
| 1873 | |
| 1874 | return response(['message' => 'OK']); |
| 1875 | |
| 1876 | } catch (\Exception $e) { |
| 1877 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | function list_last_follow_up_date($companyId){ |
| 1882 | |
| 1883 | try { |
| 1884 | |
| 1885 | $companyId = addslashes($companyId); |
| 1886 | |
| 1887 | $result = TblLastFollowUpDate::where('company_id', $companyId)->orderBy('row_id', 'ASC')->get(); |
| 1888 | |
| 1889 | $data = array(); |
| 1890 | |
| 1891 | for ($i = 0; $i < count($result); $i++) { |
| 1892 | $data[$result[$i]->row_id]['row_id'] = $result[$i]->row_id; |
| 1893 | $data[$result[$i]->row_id]['last_follow_up_date'] = $result[$i]->last_follow_up_date; |
| 1894 | if($result[$i]->budget_type_id != null){ |
| 1895 | $data[$result[$i]->row_id]['budget_types'][] = $result[$i]->budget_type_id; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | return response(['message' => 'OK', 'data' => array_values($data)]); |
| 1900 | |
| 1901 | } catch (\Exception $e) { |
| 1902 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1903 | } |
| 1904 | |
| 1905 | } |
| 1906 | |
| 1907 | function update_last_follow_up_date_working_days(Request $request){ |
| 1908 | |
| 1909 | try { |
| 1910 | |
| 1911 | $data = $request->all(); |
| 1912 | $companyId = addslashes($data['company_id']); |
| 1913 | $rowId = addslashes($data['row_id']); |
| 1914 | |
| 1915 | unset($data['company_id']); |
| 1916 | unset($data['row_id']); |
| 1917 | |
| 1918 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 1919 | |
| 1920 | TblLastFollowUpDate::where('company_id', $companyId)->where('row_id', $rowId)->update($data); |
| 1921 | |
| 1922 | return $this->list_last_follow_up_date($companyId); |
| 1923 | |
| 1924 | } catch (\Exception $e) { |
| 1925 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1926 | } |
| 1927 | |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | function update_last_follow_up_date(Request $request){ |
| 1932 | |
| 1933 | try { |
| 1934 | |
| 1935 | $data = $request->all(); |
| 1936 | $companyId = addslashes($data['company_id']); |
| 1937 | $rowId = addslashes($data['row_id']); |
| 1938 | unset($data['company_id']); |
| 1939 | unset($data['row_id']); |
| 1940 | |
| 1941 | $previousData = TblLastFollowUpDate::where('company_id', $companyId)->where('row_id', $rowId)->first(); |
| 1942 | |
| 1943 | $lastBudgetTypeId = end($data['budget_type_id']); |
| 1944 | $lastFollowUpDate = TblLastFollowUpDate::where('company_id', $companyId)->where('budget_type_id', $lastBudgetTypeId)->first(); |
| 1945 | |
| 1946 | TblLastFollowUpDate::where('company_id', $companyId)->whereIn('budget_type_id', $data['budget_type_id'])->delete(); |
| 1947 | TblLastFollowUpDate::where('company_id', $companyId)->where('row_id', $rowId)->delete(); |
| 1948 | |
| 1949 | foreach ($data['budget_type_id'] as $budgetTypeId) { |
| 1950 | TblLastFollowUpDate::create( |
| 1951 | array( |
| 1952 | 'company_id' => $companyId, |
| 1953 | 'row_id' => $rowId, |
| 1954 | 'budget_type_id' => $budgetTypeId, |
| 1955 | 'last_follow_up_date' => $previousData->last_follow_up_date, |
| 1956 | 'updated_by' => $data['updated_by'], |
| 1957 | 'updated_at' => date('Y-m-d H:i:s') |
| 1958 | ) |
| 1959 | ); |
| 1960 | } |
| 1961 | |
| 1962 | if($lastFollowUpDate != null){ |
| 1963 | if($lastFollowUpDate->budget_type_id != null){ |
| 1964 | TblLastFollowUpDate::create( |
| 1965 | array( |
| 1966 | 'company_id' => $companyId, |
| 1967 | 'row_id' => $lastFollowUpDate->row_id, |
| 1968 | 'budget_type_id' => null, |
| 1969 | 'last_follow_up_date' => $lastFollowUpDate->last_follow_up_date, |
| 1970 | 'created_by' => $data['updated_by'] |
| 1971 | ) |
| 1972 | ); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | return $this->list_last_follow_up_date($companyId); |
| 1977 | |
| 1978 | } catch (\Exception $e) { |
| 1979 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 1980 | } |
| 1981 | |
| 1982 | } |
| 1983 | |
| 1984 | function delete_last_follow_up_date(Request $request){ |
| 1985 | |
| 1986 | try { |
| 1987 | |
| 1988 | $data = $request->all(); |
| 1989 | $companyId = addslashes($data['company_id']); |
| 1990 | $rowId = addslashes($data['row_id']); |
| 1991 | unset($data['company_id']); |
| 1992 | unset($data['row_id']); |
| 1993 | |
| 1994 | $count = TblLastFollowUpDate::where('company_id', $companyId)->where('row_id', $rowId)->count(); |
| 1995 | |
| 1996 | if($count > 1){ |
| 1997 | TblLastFollowUpDate::where('company_id', $companyId)->where('budget_type_id', $data['budget_type_id'])->delete(); |
| 1998 | }else{ |
| 1999 | TblLastFollowUpDate::where('company_id', $companyId)->where('row_id', $rowId)->update(array('budget_type_id' => null)); |
| 2000 | } |
| 2001 | |
| 2002 | return $this->list_last_follow_up_date($companyId); |
| 2003 | |
| 2004 | } catch (\Exception $e) { |
| 2005 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 2006 | } |
| 2007 | |
| 2008 | } |
| 2009 | } |