Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 145 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SendG3WEmailReminders | |
0.00% |
0 / 145 |
|
0.00% |
0 / 2 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 144 |
|
0.00% |
0 / 1 |
552 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Http\Controllers\Quotations; |
| 6 | use App\Http\Controllers\Users; |
| 7 | use App\Http\Controllers\Companies; |
| 8 | use App\Models\TblEmailConfiguration; |
| 9 | use App\Models\TblCcG3WEmailReminders; |
| 10 | use App\Models\TblNotificationLogs; |
| 11 | use Illuminate\Console\Command; |
| 12 | use Illuminate\Support\Facades\Log; |
| 13 | use Illuminate\Support\Facades\DB; |
| 14 | use Illuminate\Http\Request; |
| 15 | |
| 16 | class SendG3WEmailReminders extends Command |
| 17 | { |
| 18 | /** |
| 19 | * The name and signature of the console command. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $signature = 'send:g3w-email-reminders {sent_by?} {email?} {company_id?}'; |
| 24 | |
| 25 | /** |
| 26 | * The console command description. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $description = 'Send g3w warning email reminders'; |
| 31 | |
| 32 | /** |
| 33 | * Create a new command instance. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function __construct() |
| 38 | { |
| 39 | parent::__construct(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Execute the console command. |
| 44 | * |
| 45 | * @return int |
| 46 | */ |
| 47 | public function handle() |
| 48 | { |
| 49 | |
| 50 | try { |
| 51 | |
| 52 | $sentByArg = $this->argument('sent_by') ?? "System"; |
| 53 | $toEmailArg = $this->argument('email') ?? null; |
| 54 | $companyIdArg = $this->argument('company_id') ?? 0; |
| 55 | |
| 56 | $where = ""; |
| 57 | |
| 58 | if($companyIdArg != 0){ |
| 59 | $where = " AND a.company_id = {$companyIdArg} "; |
| 60 | } |
| 61 | |
| 62 | $query = "SELECT |
| 63 | b.id, |
| 64 | COUNT(a.commercial) total, |
| 65 | a.commercial, |
| 66 | b.email, |
| 67 | CONCAT('[', GROUP_CONCAT(JSON_OBJECT('id', a.id, 'quote_id', a.quote_id, 'company_id', a.company_id)), ']') json_ids_no_budget_types |
| 68 | FROM tbl_quotations a |
| 69 | LEFT JOIN tbl_users b ON a.commercial = b.name |
| 70 | WHERE a.sync_import = 1 |
| 71 | AND (a.budget_type_id IS NULL |
| 72 | OR a.budget_type_id=16) |
| 73 | AND a.commercial IS NOT NULL |
| 74 | AND b.email IS NOT NULL |
| 75 | {$where} |
| 76 | GROUP BY 1 |
| 77 | ORDER BY a.commercial ASC"; |
| 78 | |
| 79 | $json_ids_no_budget_types = DB::select($query); |
| 80 | |
| 81 | $query = "SELECT |
| 82 | b.id, |
| 83 | COUNT(a.commercial) total, |
| 84 | a.commercial, |
| 85 | b.email, |
| 86 | CONCAT('[', GROUP_CONCAT(JSON_OBJECT('id', a.id, 'quote_id', a.quote_id, 'company_id', a.company_id)), ']') json_ids_incomplete_data |
| 87 | FROM tbl_quotations a |
| 88 | LEFT JOIN tbl_users b ON a.commercial = b.name |
| 89 | WHERE a.sync_import = 1 |
| 90 | AND ((a.client IS NULL OR TRIM(a.client) = '') |
| 91 | OR (a.email IS NULL |
| 92 | OR TRIM(a.email) = '')) |
| 93 | AND a.commercial IS NOT NULL |
| 94 | AND b.email IS NOT NULL |
| 95 | {$where} |
| 96 | GROUP BY 1 |
| 97 | ORDER BY a.commercial ASC"; |
| 98 | |
| 99 | $json_ids_incomplete_data = DB::select($query); |
| 100 | |
| 101 | $combined = []; |
| 102 | |
| 103 | foreach ($json_ids_no_budget_types as $item) { |
| 104 | $email = $item->email; |
| 105 | |
| 106 | $combined[$email] = (object) [ |
| 107 | 'id' => $item->id, |
| 108 | 'commercial' => $item->commercial, |
| 109 | 'email' => $email, |
| 110 | 'total' => $item->total, |
| 111 | 'json_ids_no_budget_types' => json_decode($item->json_ids_no_budget_types), |
| 112 | 'json_ids_incomplete_data' => [], |
| 113 | ]; |
| 114 | } |
| 115 | |
| 116 | foreach ($json_ids_incomplete_data as $item) { |
| 117 | $email = $item->email; |
| 118 | |
| 119 | if (!isset($combined[$email])) { |
| 120 | $combined[$email] = (object) [ |
| 121 | 'id' => $item->id, |
| 122 | 'commercial' => $item->commercial, |
| 123 | 'email' => $email, |
| 124 | 'total' => $item->total, |
| 125 | 'json_ids_no_budget_types' => [], |
| 126 | 'json_ids_incomplete_data' => json_decode($item->json_ids_incomplete_data), |
| 127 | ]; |
| 128 | } else { |
| 129 | $combined[$email]->total += $item->total; |
| 130 | $combined[$email]->json_ids_incomplete_data = json_decode($item->json_ids_incomplete_data); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | $result = array_values($combined); |
| 135 | |
| 136 | if(count($result) > 0){ |
| 137 | |
| 138 | $today = date('d/m/Y'); |
| 139 | |
| 140 | for ($i = 0; $i < count($result); $i++) { |
| 141 | $body = ""; |
| 142 | $companyCcIds = array(); |
| 143 | $jsonIdsNoBudgetTypes = $result[$i]->json_ids_no_budget_types; |
| 144 | $jsonIdsIncompleteData = $result[$i]->json_ids_incomplete_data; |
| 145 | $commercial = $result[$i]->commercial; |
| 146 | |
| 147 | $subject = "Revisión de warnings en G3W – Acción necesaria en presupuestos {$today} for {$commercial}"; |
| 148 | |
| 149 | if($toEmailArg != null){ |
| 150 | $toEmail = $toEmailArg; |
| 151 | }else{ |
| 152 | $toEmail = $result[$i]->email; |
| 153 | } |
| 154 | |
| 155 | $body .= "<p>Hola <b>{$commercial}</b>,</p>"; |
| 156 | $body .= "<p>Te escribo porque he estado revisando tus <i>warnings</i> en G3W desde la integración con TITAN, y he detectado algunos puntos que necesitan tu atención:</p>"; |
| 157 | |
| 158 | if($result[$i]->total > 0){ |
| 159 | |
| 160 | if($jsonIdsNoBudgetTypes){ |
| 161 | $totalIdsNoBudgetTypes = count($jsonIdsNoBudgetTypes); |
| 162 | $urlIdsNoBudgetTypes = env('URL') . "orders?commercial={$commercial}&g3w_warning=si&g3w_warning_fields=Tipo&company_id={$companyIdArg}"; |
| 163 | $body .= "<p><b>PRESUPUESTOS SIN TIPO DE PRESUPUESTO</b><p>"; |
| 164 | $body .= "<p>Los siguientes presupuestos en G3W no tienen el tipo de cliente asignado en TITAN, por lo tanto <b>no se contabilizan en los datos:</b></p>"; |
| 165 | $body .= "<p><b>#<a href='{$urlIdsNoBudgetTypes}'>{$totalIdsNoBudgetTypes}</a> IDs EN G3W:</b></p>"; |
| 166 | $body .= "<ul>"; |
| 167 | foreach ($jsonIdsNoBudgetTypes as $item) { |
| 168 | $url = env('URL') . "orders/{$item->id}?company_id={$item->company_id}"; |
| 169 | $href = "<li><a href='{$url}'>{$item->quote_id}</a></li>"; |
| 170 | $body .= $href; |
| 171 | |
| 172 | if(!in_array($item->company_id, $companyCcIds)){ |
| 173 | array_push($companyCcIds, $item->company_id); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | $body .= "</ul>"; |
| 178 | |
| 179 | $body .= "<p>Para solucionarlo:</p>"; |
| 180 | $body .= "<ol>"; |
| 181 | $body .= "<li>Accede a G3W</li>"; |
| 182 | $body .= "<li>Revisa cada presupuesto indicado.</li>"; |
| 183 | $body .= '<li>Añade el campo <b>"ORIGEN DE PRESUPUESTO"</b>.</li>'; |
| 184 | $body .= "</ol>"; |
| 185 | |
| 186 | $body .= "<p>Si tienes dudas sobre qué origen asignar, puedes consultarlo en el siguiente enlace:</p>"; |
| 187 | $url = env('URL') . "normalize-g3w"; |
| 188 | $body .= "<p><a href='{$url}'><b>Apartado de normalización</b></a></p>"; |
| 189 | } |
| 190 | |
| 191 | if($jsonIdsIncompleteData){ |
| 192 | $totalIdsIncompleteData = count($jsonIdsIncompleteData); |
| 193 | $urlIdsIncompleteData = env('URL') . "orders?commercial={$commercial}&g3w_warning=si&g3w_warning_fields=Email,Datos cliente&company_id={$companyIdArg}"; |
| 194 | $body .= "<p><b>DATOS DE CLIENTE INCOMPLETOS</b><p>"; |
| 195 | $body .= "<p>En algunos casos, durante el volcado desde G3W, los datos del cliente pueden no haberse trasladado correctamente (por ser un cliente nuevo o por falta de información en G3W).</p>"; |
| 196 | $body .= "<p>Por favor, <b>rellena los datos manualmente EN TITAN</b> para los siguientes presupuestos:</p>"; |
| 197 | $body .= "<p><b>#<a href='{$urlIdsIncompleteData}'>{$totalIdsIncompleteData}</a> de orden interno:</b></p>"; |
| 198 | $body .= "<ul>"; |
| 199 | foreach ($jsonIdsIncompleteData as $item) { |
| 200 | $url = env('URL') . "orders/{$item->id}?company_id={$item->company_id}"; |
| 201 | $href = "<li><a href='{$url}'>{$item->quote_id}</a></li>"; |
| 202 | $body .= $href; |
| 203 | } |
| 204 | $body .= "</ul>"; |
| 205 | |
| 206 | if(!in_array($item->company_id, $companyCcIds)){ |
| 207 | array_push($companyCcIds, $item->company_id); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | $body .= "<p>Si tienes cualquier duda sobre esta operativa o necesitas ayuda con alguno de los <i>warnings</i>, no dudes en escribirme.</p>"; |
| 212 | $body .= "<p>Gracias por tu atención y colaboración.</p>"; |
| 213 | $body .= "<p>Un saludo,</p>"; |
| 214 | |
| 215 | $imgpath = \File::get(public_path('fireservicetitan.png')); |
| 216 | $body .= "<br><p>Fire Service Titan</p>"; |
| 217 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 218 | |
| 219 | $html = '<!DOCTYPE html>'; |
| 220 | $html .= '<html>'; |
| 221 | $html .= '<head>'; |
| 222 | $html .= '<meta charset="UTF-8">'; |
| 223 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 224 | $html .= '</head>'; |
| 225 | $html .= '<body>'; |
| 226 | $html .= $body; |
| 227 | $html .= '</body>'; |
| 228 | $html .= '</html>'; |
| 229 | |
| 230 | if($toEmail != null){ |
| 231 | $email = new \SendGrid\Mail\Mail(); |
| 232 | |
| 233 | if($toEmailArg == null){ |
| 234 | $companyCcIds = array_values(array_unique($companyCcIds)); |
| 235 | $ccBcc = TblCcG3WEmailReminders::whereIn('company_id', $companyCcIds)->get(); |
| 236 | |
| 237 | $inCc = array(); |
| 238 | |
| 239 | if(count($ccBcc) > 0){ |
| 240 | foreach ($ccBcc as $data) { |
| 241 | if($data->email != $toEmail && !in_array($data->email, $inCc)){ |
| 242 | $email->addCc($data->email); |
| 243 | array_push($inCc, $data->email); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 250 | $email->setSubject($subject); |
| 251 | $email->addTo($toEmail); |
| 252 | $email->addContent("text/html", $html); |
| 253 | |
| 254 | $email->addAttachment( |
| 255 | $imgpath, |
| 256 | "image/png", |
| 257 | "fireservicetitan.png", |
| 258 | "inline", |
| 259 | "fireservicetitan" |
| 260 | ); |
| 261 | |
| 262 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY','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')); |
| 263 | |
| 264 | $response = $sendgrid->send($email); |
| 265 | if ($response->statusCode() != 202) { |
| 266 | Log::channel('cron_send_g3w_email_reminders')->error('ID: '. $toEmail .' - ' . $response->body()); |
| 267 | } else { |
| 268 | TblNotificationLogs::create( |
| 269 | array( |
| 270 | 'company_id' => $companyIdArg, |
| 271 | 'commercial' => $commercial, |
| 272 | 'total_g3w_warning' => $result[$i]->total, |
| 273 | 'created_by' => $sentByArg |
| 274 | ) |
| 275 | ); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | } catch (\Exception $e) { |
| 283 | Log::channel('cron_send_g3w_email_reminders')->error($e->getMessage()); |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | } |