find($this->moduleVmwareId); if (!$vm) { Log::error('SendVmCredentialsEmailJob: ModuleVmware not found', [ 'module_vmware_id' => $this->moduleVmwareId, ]); return; } $customer = $vm->customer; if (!$customer?->email) { Log::error('SendVmCredentialsEmailJob: customer or email missing', [ 'module_vmware_id' => $this->moduleVmwareId, ]); return; } $setting = Setting::where('key', 'setting')->first(); $settingValue = $setting ? json_decode((string) $setting->value) : null; // Resolve billing info from order items $orderItem = $vm->order?->orderItems?->first(); $months = (int) ($orderItem?->months ?? 0); $emailData = [ 'company_name' => $settingValue->company ?? 'Thế Giới Số', 'company_logo' => $settingValue->logo ?? '', 'client_name' => trim($customer->firstname . ' ' . $customer->lastname), 'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS', 'product_billing' => $this->billingLabel($months), 'product_price' => $orderItem ? number_format((float) $orderItem->price, 0, ',', '.') : 'N/A', 'service_host' => $vm->ip ?? 'N/A', 'service_username' => $vm->user, 'service_port' => $vm->port, 'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)', 'os_name' => $vm->os ? (($vm->os->osGroup?->name ?? '') . ' ' . $vm->os->version) : 'Unknown OS', 'service_due_exp' => $vm->next_due_date ? date('d/m/Y', (int) $vm->next_due_date) : 'N/A', ]; // Generate invoice PDF attachment (synchronous, no queue) $invoicePdf = null; $invoicePdfFileName = null; $invoice = Invoice::where('order_id', $vm->order_id) ->with(['invoiceItems.product', 'customer', 'order.orderItems']) ->first(); if ($invoice) { try { $pdfService = app(InvoicePdfService::class); $invoicePdf = $pdfService->generate($invoice); $invoicePdfFileName = $pdfService->generateFileName($invoice); } catch (\Throwable $e) { Log::error('SendVmCredentialsEmailJob: failed to generate invoice PDF', [ 'module_vmware_id' => $this->moduleVmwareId, 'invoice_id' => $invoice->id, 'error' => $e->getMessage(), ]); } } SendInformationVirtualMachine::dispatch($emailData, $customer, $invoicePdf, $invoicePdfFileName); Log::info('SendVmCredentialsEmailJob: email dispatched', [ 'module_vmware_id' => $this->moduleVmwareId, 'customer_email' => $customer->email, 'ip' => $vm->ip, 'invoice_attached' => $invoicePdf !== null, ]); // Notify admin with VM credentials after successful provisioning $order = $vm->order; AdminOrderNotificationJob::dispatch([ 'customer_name' => trim($customer->firstname . ' ' . $customer->lastname), 'customer_email' => $customer->email, 'client_ip' => null, 'order_num' => $order?->order_num ?? $order?->id ?? 'N/A', 'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS', 'product_type' => 'Cloud VPS', 'billing_cycle' => $this->billingLabel($months), 'total' => $orderItem?->price ?? 0, 'created_at' => $order?->created_at?->format('d/m/Y H:i:s') ?? now()->format('d/m/Y H:i:s'), 'ip' => $vm->ip ?? '', 'username' => $vm->user ?? '', 'password' => $vm->plain_password ?? '', ]); Log::info('SendVmCredentialsEmailJob: admin notification dispatched', [ 'module_vmware_id' => $this->moduleVmwareId, ]); } public function failed(\Throwable $e): void { Log::error('SendVmCredentialsEmailJob: failed', [ 'module_vmware_id' => $this->moduleVmwareId, 'error' => $e->getMessage(), ]); } // ------------------------------------------------------------------------- private function billingLabel(?int $months): string { return match ($months) { 1 => 'Hàng tháng', 3 => '3 Tháng', 6 => '6 Tháng', 12 => 'Hàng năm', 24 => '2 Năm', default => ($months ?? 0) . ' Tháng', }; } }