Files
portal-api/app/Jobs/Mail/ExpiryMail.php
T
2026-10-08 11:25:28 +07:00

96 lines
3.1 KiB
PHP

<?php
namespace App\Jobs\Mail;
use App\Http\Services\Telegram;
use App\Mail\ExpiryService;
use App\Models\ModuleHosting;
use App\Models\ModuleVmware;
use App\Models\Setting;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class ExpiryMail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected int $product;
protected int $template_id;
protected int $type;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($product = 0,$mail = 0,$type = 0)
{
$this->product = $product;
$this->template_id = $mail;
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$setting = Setting::where('key','setting')->first();
$product = null;
switch ($this->type)
{
case 3:
$product = ModuleHosting::with('Customer')->find($this->product);
break;
case 4:
$product = ModuleVmware::with('IpAddress','Customer')->find($this->product);
break;
}
$data = [
'company_name' => json_decode($setting->value)->company,
'company_logo' => json_decode($setting->value)->logo,
'client_name' => $product->Customer->firstname . " " . $product->Customer->lastname,
'product_name' => $product->GetInvoiceItem()->GetItemProduct()->name,
'service_due_exp' => Carbon::parse($product->next_due_date)->format('d/m/Y'),
'service_remaining' => (int) Carbon::now()->diffInDays(Carbon::parse($product->next_due_date), false) + 1
];
try {
$email = new ExpiryService($this->template_id,$data);
if (Mail::to($product->Customer->email)->send($email) === null) {
return;
}
\App\Models\Mail::create([
'template_id' => $this->template_id,
'subject' => $email->envelope()->subject,
'to' => $product->Customer->email,
'customer_id' => $product->Customer->id,
'message' => $email->render()
]);
} catch (\Exception $exception)
{
throw new \Exception( $exception->getMessage());
}
}
public function failed(\Throwable $exception, Telegram $telegram)
{
Log::channel('Custom')->info("=================================");
Log::channel('Custom')->info("Thông báo lỗi lúc: " .Carbon::now());
Log::channel('Custom')->info("Notification Send Expiry Product: ");
Log::channel('Custom')->info("========");
Log::channel('Custom')->info($exception->getMessage());
Log::channel('Custom')->info("=================================");
$telegram->SendErrorMessage(json_decode($exception->getMessage()));
}
}