Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 83 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ResendAcceptanceEmails | |
0.00% |
0 / 83 |
|
0.00% |
0 / 3 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 79 |
|
0.00% |
0 / 1 |
182 | |||
| currency | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Models\TblCcAcceptanceNotifications; |
| 6 | use App\Models\TblCompanies; |
| 7 | use App\Models\TblQuotations; |
| 8 | use App\Models\TblResendEmails; |
| 9 | use App\Models\TblToAcceptanceNotifications; |
| 10 | use App\Models\TblUsers; |
| 11 | use Illuminate\Console\Command; |
| 12 | use Illuminate\Support\Facades\Log; |
| 13 | |
| 14 | class ResendAcceptanceEmails extends Command |
| 15 | { |
| 16 | /** |
| 17 | * The name and signature of the console command. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $signature = 'command:resend-acceptance-emails {id?} {type?} {user_id?}'; |
| 22 | |
| 23 | /** |
| 24 | * The console command description. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $description = 'Command description'; |
| 29 | |
| 30 | /** |
| 31 | * Create a new command instance. |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function __construct() |
| 36 | { |
| 37 | parent::__construct(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Execute the console command. |
| 42 | * |
| 43 | * @return int |
| 44 | */ |
| 45 | public function handle() |
| 46 | { |
| 47 | $quotationId = $this->argument('id') ?? null; |
| 48 | $typeId = 3; |
| 49 | $userId = $this->argument('user_id') ?? 1; |
| 50 | |
| 51 | $budget = TblQuotations::where('id', $quotationId)->first(); |
| 52 | |
| 53 | $emails = []; |
| 54 | |
| 55 | if ($budget != null) { |
| 56 | $companyId = $budget->company_id; |
| 57 | $updatedBy = $budget->updated_by ?? $budget->created_by; |
| 58 | |
| 59 | $to = TblToAcceptanceNotifications::where('company_id', $companyId)->get(); |
| 60 | $cc = TblCcAcceptanceNotifications::where('company_id', $companyId)->get(); |
| 61 | |
| 62 | if (count($to) > 0 && count($cc) > 0) { |
| 63 | |
| 64 | $company = TblCompanies::where('company_id', $companyId)->first(); |
| 65 | |
| 66 | $quoteId = $budget->quote_id; |
| 67 | $amount = $this->currency($budget->amount, 1); |
| 68 | |
| 69 | $url = env('URL')."orders/{$quotationId}?company_id={$companyId}"; |
| 70 | $href = "<a href='{$url}'>{$quoteId}</a>"; |
| 71 | |
| 72 | $imgpath = \File::get(public_path('fireservicetitan.png')); |
| 73 | $base64 = 'data:image/png;base64,'.base64_encode($imgpath); |
| 74 | |
| 75 | $body = __('language.send_acceptance_notification.body_hello'); |
| 76 | $body .= __('language.send_acceptance_notification.body_message'); |
| 77 | |
| 78 | $body = str_replace('{{client}}', $budget->client, $body); |
| 79 | $body = str_replace('{{username}}', $updatedBy, $body); |
| 80 | $body = str_replace('{{company}}', $company->name, $body); |
| 81 | $body = str_replace('{{amount}}', $amount, $body); |
| 82 | $body = str_replace('{{quote_id}}', $href, $body); |
| 83 | |
| 84 | $body .= '<p>Fire Service Titan</p>'; |
| 85 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 86 | |
| 87 | $html = '<!DOCTYPE html>'; |
| 88 | $html .= '<html>'; |
| 89 | $html .= '<head>'; |
| 90 | $html .= '<meta charset="UTF-8">'; |
| 91 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 92 | $html .= '</head>'; |
| 93 | $html .= '<body>'; |
| 94 | $html .= $body; |
| 95 | $html .= '</body>'; |
| 96 | $html .= '</html>'; |
| 97 | |
| 98 | $subject = __('language.send_acceptance_notification.subject'); |
| 99 | $subject = str_replace('{{quote_id}}', $quoteId, $subject); |
| 100 | |
| 101 | $email = new \SendGrid\Mail\Mail; |
| 102 | |
| 103 | $user = TblUsers::where('id', $userId)->first(); |
| 104 | |
| 105 | if (env('SENDGRID_STAGING')) { |
| 106 | $email->addTo($user->email); |
| 107 | array_push($emails, $user->email); |
| 108 | } else { |
| 109 | |
| 110 | $user = TblUsers::where('name', $updatedBy)->first(); |
| 111 | |
| 112 | foreach ($to as $item) { |
| 113 | if (! in_array($item->email, $emails)) { |
| 114 | array_push($emails, $item->email); |
| 115 | $email->addTo($item->email); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | foreach ($cc as $item) { |
| 120 | if (! in_array($item->email, $emails)) { |
| 121 | array_push($emails, $item->email); |
| 122 | $email->addCc($item->email); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if ($user != null) { |
| 127 | $email->addCc($user->email); |
| 128 | array_push($emails, $user->email); |
| 129 | } |
| 130 | |
| 131 | $ccUser = TblUsers::where('name', $budget->commercial)->first(); |
| 132 | |
| 133 | if ($ccUser) { |
| 134 | if (! in_array($ccUser->email, $emails)) { |
| 135 | $email->addCc($ccUser->email); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 141 | $email->setSubject($subject); |
| 142 | $email->addContent('text/html', $html); |
| 143 | |
| 144 | $email->addAttachment( |
| 145 | $imgpath, |
| 146 | 'image/png', |
| 147 | 'fireservicetitan.png', |
| 148 | 'inline', |
| 149 | 'fireservicetitan' |
| 150 | ); |
| 151 | |
| 152 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY')); |
| 153 | |
| 154 | $response = $sendgrid->send($email); |
| 155 | if ($response->statusCode() == 202) { |
| 156 | TblResendEmails::where('quotation_id', $quotationId)->where('type', $typeId)->delete(); |
| 157 | |
| 158 | $emails = json_encode($emails); |
| 159 | Log::channel('resend_emails')->error("[ACC-SENT-OK] ID:{$quoteId}: {$emails}}"); |
| 160 | } else { |
| 161 | $error = true; |
| 162 | Log::channel('resend_emails')->error('[ACC-ERROR-SG] ID:'.$quoteId.' - '.$response->body()); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | public function currency($amount, $withEuro = '') |
| 172 | { |
| 173 | |
| 174 | if ($withEuro != null) { |
| 175 | $withEuro = ' €'; |
| 176 | } |
| 177 | |
| 178 | return number_format($amount, 2, ',', '.').$withEuro; |
| 179 | } |
| 180 | } |