Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 86 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SendEmailInvoiceNewCreditDays | |
0.00% |
0 / 86 |
|
0.00% |
0 / 4 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
380 | |||
| send_email | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
6 | |||
| isEmailValid | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Services\GestionaService; |
| 6 | use App\Models\TblCustomerChangeComms; |
| 7 | use Illuminate\Console\Command; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | use Illuminate\Support\Facades\File; |
| 11 | use SendGrid\Mail\Mail; |
| 12 | |
| 13 | class SendEmailInvoiceNewCreditDays extends Command |
| 14 | { |
| 15 | /** |
| 16 | * The name and signature of the console command. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $signature = 'send:invoice-new-credit-days {invoiceDate?}'; |
| 21 | |
| 22 | /** |
| 23 | * The console command description. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $description = 'Send email invoice new credit days'; |
| 28 | |
| 29 | protected $gestionaService; |
| 30 | |
| 31 | /** |
| 32 | * Create a new command instance. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function __construct(GestionaService $gestionaService) |
| 37 | { |
| 38 | parent::__construct(); |
| 39 | $this->gestionaService = $gestionaService; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Execute the console command. |
| 44 | * |
| 45 | * @return int |
| 46 | */ |
| 47 | public function handle() |
| 48 | { |
| 49 | try { |
| 50 | |
| 51 | $invoiceDate = $this->argument('invoiceDate') ?? null; |
| 52 | |
| 53 | if ($invoiceDate != null) { |
| 54 | $d = \DateTime::createFromFormat('Y-m-d', $invoiceDate); |
| 55 | |
| 56 | if (!$d || $d->format('Y-m-d') !== $invoiceDate) { |
| 57 | throw new \InvalidArgumentException('Invalid date format. Expected Y-m-d.'); |
| 58 | } |
| 59 | } else { |
| 60 | $invoiceDate = date('Y-m-d', strtotime('-1 day')); |
| 61 | } |
| 62 | |
| 63 | $email = new \SendGrid\Mail\Mail; |
| 64 | $logo = File::get(public_path('fireservicetitan.png')); |
| 65 | |
| 66 | $regions = array( |
| 67 | 'Catalunya', |
| 68 | 'Almeria', |
| 69 | 'Madrid', |
| 70 | 'Valencia' |
| 71 | ); |
| 72 | |
| 73 | foreach ($regions as $region) { |
| 74 | $yesterdayInvoices = $this->gestionaService->request('get', 'factura/vence/'. $invoiceDate, $region, []); |
| 75 | |
| 76 | if (!empty($yesterdayInvoices) && !empty($yesterdayInvoices['facturas'])) { |
| 77 | |
| 78 | for ($i = 0; $i < count($yesterdayInvoices['facturas']); $i++) { |
| 79 | |
| 80 | $invoiceId = $yesterdayInvoices['facturas'][$i]['ID']; |
| 81 | $dataInvoice = $this->gestionaService->request('get', 'factura/'. $invoiceId, $region, []); |
| 82 | |
| 83 | if (!empty($dataInvoice) && !empty($dataInvoice['factura'])) { |
| 84 | |
| 85 | if(isset($dataInvoice['factura']['cod_cliente'])){ |
| 86 | |
| 87 | $clientCode = $dataInvoice['factura']['cod_cliente']; |
| 88 | $result = TblCustomerChangeComms::where('client_code', $clientCode)->first(); |
| 89 | |
| 90 | if($result != null){ |
| 91 | |
| 92 | $clientInvoiceResponse = $this->gestionaService->request('get', 'cliente/'. $result->client_code, $result->region, []); |
| 93 | |
| 94 | if (!empty($clientInvoiceResponse) && !empty($clientInvoiceResponse['cliente'])) { |
| 95 | |
| 96 | if (env('SENDGRID_STAGING')) { |
| 97 | $toEmail = array("christian@ibventur.es"); |
| 98 | } else { |
| 99 | $toEmail = explode(';', $toEmail); |
| 100 | $toEmail = array_map('trim', $toEmail); |
| 101 | } |
| 102 | |
| 103 | $invalidEmailCount = 0; |
| 104 | foreach ($toEmail as $clientEmail) { |
| 105 | $isValid = $this->isEmailValid($clientEmail); |
| 106 | if ($isValid) { |
| 107 | $email->addTo($clientEmail); |
| 108 | } else { |
| 109 | $invalidEmailCount++; |
| 110 | Log::channel('cron_send_email_invoice_new_credit_days')->error("[$result->client_code]: Invalid email address $clientEmail"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if($invalidEmailCount != count($toEmail)){ |
| 115 | $this->send_email($result->id, $result->client_code, $result->client_name, $result->credit_days, $logo, $toEmail, $email); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } catch (\Exception $e) { |
| 126 | Log::channel('cron_send_email_invoice_new_credit_days')->error($e->getMessage()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | function send_email($id, $clientCode, $clientName, $creditDays, $logo, $toEmail, $email){ |
| 131 | |
| 132 | $body = "<p>Estimado {$clientName},</p></br>"; |
| 133 | $body .= "<p>Le informamos de que, como parte de un proceso de revisión y mejora de nuestros procedimientos administrativos y de facturación, a partir del 01/03/2026 el plazo de vencimiento de las facturas emitidas a su nombre pasará a ser de {$creditDays} días, en sustitución del plazo actual.</p></br>"; |
| 134 | $body .= "<p>Esta modificación tiene como objetivo homogeneizar y optimizar la gestión administrativa, sin que suponga ningún otro cambio al servicio que le ofrecemos habitualmente.</p></br>"; |
| 135 | $body .= "<p>Muchas gracias.</p></br>"; |
| 136 | $body .= '<p>Fire Service Titan</p>'; |
| 137 | $body .= "<img src='cid:fireservicetitan' style='height: 45px;' />"; |
| 138 | |
| 139 | $html = '<!DOCTYPE html>'; |
| 140 | $html .= '<html>'; |
| 141 | $html .= '<head>'; |
| 142 | $html .= '<meta charset="UTF-8">'; |
| 143 | $html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
| 144 | $html .= '</head>'; |
| 145 | $html .= '<body>'; |
| 146 | $html .= $body; |
| 147 | $html .= '</body>'; |
| 148 | $html .= '</html>'; |
| 149 | |
| 150 | $email->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 151 | $email->setSubject('Change in Payment Terms'); |
| 152 | $email->addContent('text/html', $html); |
| 153 | |
| 154 | $email->addAttachment( |
| 155 | $logo, |
| 156 | 'image/png', |
| 157 | 'fireservicetitan.png', |
| 158 | 'inline', |
| 159 | 'fireservicetitan' |
| 160 | ); |
| 161 | |
| 162 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY')); |
| 163 | $response = $sendgrid->send($email); |
| 164 | |
| 165 | if ($response->statusCode() == 202) { |
| 166 | $toEmail = json_encode($toEmail); |
| 167 | TblCustomerChangeComms::where('id', $id) |
| 168 | ->update(array( |
| 169 | 'email_sent_at' => date('Y-m-d H:i:s') |
| 170 | ) |
| 171 | ); |
| 172 | Log::channel('cron_send_email_invoice_new_credit_days')->info("[SENT-OK] ID:$clientCode - $toEmail"); |
| 173 | } else { |
| 174 | $error = $response->body(); |
| 175 | Log::channel('cron_send_email_invoice_new_credit_days')->error("[ERROR-SG] ID:$clientCode - $error"); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public function isEmailValid($email) |
| 180 | { |
| 181 | $pattern = '/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/'; |
| 182 | |
| 183 | if (preg_match($pattern, $email)) { |
| 184 | return true; |
| 185 | } else { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | } |