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