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