find($this->moduleVmwareId); if (!$vm) { $msg = "SendVmCredentialsEmailJob: ModuleVmware #{$this->moduleVmwareId} not found"; Log::error($msg); $telegram->SendErrorMessage($msg); return; } $customer = $vm->customer; if (!$customer?->email) { $msg = "SendVmCredentialsEmailJob: customer or email missing for ModuleVmware #{$this->moduleVmwareId}"; Log::error($msg, ['module_vmware_id' => $this->moduleVmwareId]); $telegram->SendErrorMessage($msg); return; } // ── [1 & 2] Activate service + VM records ──────────────────────────── $orderItem = $vm->order?->orderItems?->first(); $service = $orderItem?->service; $this->activateService($vm, $service); // ── [3] Build email payload ─────────────────────────────────────────── $setting = Setting::where('key', 'setting')->first(); $settingValue = $setting ? json_decode((string) $setting->value) : null; $months = (int) ($orderItem?->months ?? 0); $osGroupName = $vm->os?->osGroup?->name ?? ''; $isWindows = str_contains(strtolower($osGroupName), 'windows'); $osUsername = $isWindows ? 'Administrator' : 'root'; $emailData = [ 'company_name' => $settingValue->company ?? 'Thế Giới Số', 'company_logo' => $settingValue->logo ?? '', 'client_name' => $customer->fullname ?? '', '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' => $osUsername, 'service_port' => $vm->port, 'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)', 'os_name' => $vm->os ? ($osGroupName . ' ' . $vm->os->version) : 'Unknown OS', 'service_due_exp' => $vm->next_due_date ? date('d/m/Y', (int) $vm->next_due_date) : 'N/A', ]; // PDF is generated inside the mail job — never pass binary blobs through queue payload $invoice = Invoice::where('order_id', $vm->order_id)->first(); SendInformationVirtualMachine::dispatch($emailData, $customer, $invoice?->id); Log::info('SendVmCredentialsEmailJob: email dispatched', [ 'module_vmware_id' => $this->moduleVmwareId, 'customer_email' => $customer->email, 'ip' => $vm->ip, 'invoice_id' => $invoice?->id, ]); // ── [4] Admin notification ──────────────────────────────────────────── $order = $vm->order; AdminOrderNotificationJob::dispatch([ 'customer_name' => $customer->fullname ?? '', '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' => $osUsername, '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(), ]); try { app(Telegram::class)->SendErrorMessage([ 'job' => 'SendVmCredentialsEmailJob', 'module_vmware_id' => $this->moduleVmwareId, 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), ]); } catch (\Throwable $te) { Log::error('SendVmCredentialsEmailJob: could not send Telegram notification: ' . $te->getMessage()); } } // ───────────────────────────────────────────────────────────────────────── /** * Activate the Service record and link it bidirectionally with ModuleVmware. * * services: status → 'active', vm_id → module_vmware.id * module_vmware: status → 1, service_id → services.id */ private function activateService(ModuleVmware $vm, ?Service $service): void { DB::transaction(function () use ($vm, $service) { if ($service) { $service->update([ 'status' => 'active', 'vm_id' => $vm->id, 'server_name' => $service->server_name ?: ($vm->hostname ?? $vm->name), ]); $vm->update([ 'status' => 1, 'service_id' => $service->id, ]); Log::info('SendVmCredentialsEmailJob: service activated', [ 'module_vmware_id' => $vm->id, 'service_id' => $service->id, 'ip' => $vm->ip, ]); } else { // Service record not found — still activate the VM so it is not stuck at pending $vm->update(['status' => 1]); Log::warning('SendVmCredentialsEmailJob: no Service record found, activated module_vmware only', [ 'module_vmware_id' => $vm->id, ]); } }); } // ───────────────────────────────────────────────────────────────────────── 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', }; } }