Files
portal-api/app/Jobs/Mail/SendInformationVirtualMachine.php
baodev0 520f21cb11 feat: add GeneratesUniqueSlug class for unique slug generation
feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
2026-05-25 15:39:42 +07:00

106 lines
3.8 KiB
PHP

<?php
namespace App\Jobs\Mail;
use App\Http\Services\Telegram;
use App\Mail\CreateVirtualMachine;
use App\Mail\VmCredentialsMail;
use App\Models\EmailNotificationTemplate;
use App\Models\Invoice;
use App\Services\EmailNotificationTemplateRenderer;
use App\Services\InvoicePdfService;
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 SendInformationVirtualMachine implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $data = [];
public $customer;
public ?int $invoiceId;
public function __construct($data = [], $customer = [], ?int $invoiceId = null)
{
$this->data = $data;
$this->customer = $customer;
$this->invoiceId = $invoiceId;
}
public function handle(): void
{
try {
// Generate PDF inside the job — never pass binary blobs through queue payload
$invoice = null;
$invoicePdf = null;
$invoicePdfFileName = null;
if ($this->invoiceId) {
try {
$invoice = Invoice::with(['invoiceItems.product', 'customer', 'order.orderItems'])
->find($this->invoiceId);
if ($invoice) {
$pdfService = app(InvoicePdfService::class);
$invoicePdf = $pdfService->generate($invoice);
$invoicePdfFileName = $pdfService->generateFileName($invoice);
}
} catch (\Throwable $e) {
Log::warning('SendInformationVirtualMachine: PDF generation failed, sending without attachment', [
'invoice_id' => $this->invoiceId,
'error' => $e->getMessage(),
]);
}
}
// Try block-based DB template first
$dbTemplate = EmailNotificationTemplate::where('type', 'vm_credentials')
->where('is_active', true)
->first();
if ($dbTemplate) {
$email = new VmCredentialsMail($dbTemplate, $this->data, $invoice, $invoicePdf, $invoicePdfFileName);
} else {
// Fallback: legacy Template model (ID=2)
$email = new CreateVirtualMachine(2, $this->data, $invoicePdf, $invoicePdfFileName);
}
Mail::to($this->customer->email)->send($email);
\App\Models\Mail::create([
'template_id' => 2,
'subject' => $email->envelope()->subject,
'to' => $this->customer->email,
'customer_id' => $this->customer->id,
'message' => $email->render(),
]);
} catch (\Throwable $exception) {
throw new \Exception($exception->getMessage(), 0, $exception);
}
}
public function failed(\Throwable $exception): void
{
Log::channel('Custom')->error("SendInformationVirtualMachine failed at " . Carbon::now() . ": " . $exception->getMessage());
try {
app(Telegram::class)->SendErrorMessage([
'job' => 'SendInformationVirtualMachine',
'email' => $this->customer->email ?? 'unknown',
'error' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
]);
} catch (\Throwable $e) {
Log::error('SendInformationVirtualMachine: could not send Telegram notification: ' . $e->getMessage());
}
}
}