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', ]; SendInformationVirtualMachine::dispatch($emailData, $customer); Log::info('SendVmCredentialsEmailJob: email dispatched', [ 'module_vmware_id' => $this->moduleVmwareId, 'customer_email' => $customer->email, 'ip' => $vm->ip, ]); } 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', }; } }