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