Files
portal-api/app/Services/CustomerRenewalNotifier.php
baodev0andClaude Sonnet 5.5 62596d9063 feat(vm): hoàn tiền khi cấp VM lỗi, theo dõi job queue, uuid dịch vụ cho ticket, chuẩn hoá RAM addon
- VmProvisionException/VmProvisionRefund xử lý lỗi cấp phát VM
- QueueJobInspector + QueueMonitorController xem chi tiết job
- Thêm service uuid cho ticket, thời lượng cho activity_logs
- Migration chuẩn hoá RAM addon sang MB, trạng thái deleting cho snapshot

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 11:54:05 +07:00

72 lines
3.3 KiB
PHP

<?php
namespace App\Services;
use App\Mail\ServiceRenewedMail;
use App\Models\Invoice;
use App\Models\Notification;
use App\Models\Service;
use App\Models\Setting;
use App\Support\AutoEmailGate;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class CustomerRenewalNotifier
{
public const SCENARIO = 'admin_customer_service_renewed';
public const SOURCE_CUSTOMER = 'customer';
public const SOURCE_SYSTEM = 'system';
public function notify(Service $service, Invoice $invoice, int $months, ?CarbonInterface $previousExpiresAt, string $source): void
{
$service->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()]);
}
}
}