loadMissing(['customer', 'product']); $customer = $service->customer; if (! $customer) { return; } $productName = $service->product?->name ?? 'Dịch vụ'; $serverName = (string) $service->server_name; $expiresAt = $service->expires_at?->format('d/m/Y') ?? 'N/A'; $auto = $source === self::SOURCE_SYSTEM; $amount = number_format((float) $invoice->total, 0, ',', '.') . ' VNĐ'; Notification::notifyAdmins([ 'type' => 'service_renewed_by_customer', 'title' => $auto ? 'Dịch vụ được tự động gia hạn' : 'Khách hàng gia hạn dịch vụ', 'body' => "{$customer->fullname} " . ($auto ? 'được tự động gia hạn' : 'đã gia hạn') . " {$productName}" . ($serverName !== '' ? " ({$serverName})" : '') . " thêm {$months} tháng ({$amount}), hạn mới {$expiresAt}.", 'link' => '/services/' . $service->id, 'data' => ['service_id' => $service->id, 'invoice_id' => $invoice->id, 'months' => $months], ]); app(ServiceRenewalNotifier::class)->notifyCustomerRenewal($service, $invoice, $months, $previousExpiresAt); $adminEmail = json_decode((string) Setting::where('key', 'setting')->value('value'), true)['admin_email'] ?? null; if (! $adminEmail || ! AutoEmailGate::allows(self::SCENARIO)) { return; } try { Mail::to($adminEmail)->queue(new ServiceRenewedMail(self::SCENARIO, [ 'customer_name' => (string) $customer->fullname, 'customer_email' => (string) $customer->email, 'product_name' => $productName, 'server_name_suffix' => $serverName !== '' ? ' (' . e($serverName) . ')' : '', 'months_label' => "{$months} tháng", 'old_expires_at' => $previousExpiresAt?->format('d/m/Y') ?? 'N/A', 'expires_at' => $expiresAt, 'invoice_number' => (string) $invoice->invoice_number, 'amount_formatted' => $amount, 'payment_method_label' => $invoice->payment_method === 'balance' ? 'Trừ số dư tài khoản' : (string) ($invoice->payment_method ?: 'N/A'), 'renewal_source_label' => $auto ? 'Tự động gia hạn (hệ thống)' : 'Khách hàng tự gia hạn', 'admin_url' => rtrim(env('ADMIN_URL', config('app.url')), '/') . '/services/' . $service->id, ])); } catch (\Throwable $e) { Log::error('CustomerRenewalNotifier: không gửi được email admin', ['service_id' => $service->id, 'error' => $e->getMessage()]); } } }