feat(billing): renewals, coupons, VAT numbering, billing cycles and invoice payment tracking
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
ea9a53f742
commit
25f23d541e
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Mail\ServiceRenewedMail;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Mail as MailLog;
|
||||
use App\Models\Service;
|
||||
use App\Models\Setting;
|
||||
use App\Services\Email\EmailTemplateService;
|
||||
use App\Support\AutoEmailGate;
|
||||
use App\Support\EmailTemplateShell;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class ServiceRenewalNotifier
|
||||
{
|
||||
public const CUSTOMER_SCENARIO = 'service_renewed';
|
||||
public const ADMIN_SCENARIO = 'admin_service_renewed';
|
||||
|
||||
public function __construct(private readonly EmailTemplateService $templates) {}
|
||||
|
||||
public function notifyAdminRenewal(
|
||||
Service $service,
|
||||
Invoice $invoice,
|
||||
int $months,
|
||||
?CarbonInterface $previousExpiresAt,
|
||||
string $payment,
|
||||
string $adminName,
|
||||
?int $adminId,
|
||||
): void {
|
||||
if (! AutoEmailGate::mailEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service->loadMissing(['customer', 'product']);
|
||||
$customer = $service->customer;
|
||||
$serverName = (string) $service->server_name;
|
||||
|
||||
$data = [
|
||||
'client_name' => $customer->fullname ?? '',
|
||||
'customer_name' => $customer->fullname ?? '',
|
||||
'customer_email' => $customer->email ?? '',
|
||||
'product_name' => $service->product?->name ?? 'Dịch vụ',
|
||||
'server_name_suffix' => $serverName !== '' ? ' (' . e($serverName) . ')' : '',
|
||||
'months_label' => "{$months} tháng",
|
||||
'old_expires_at' => $previousExpiresAt?->format('d/m/Y') ?? 'N/A',
|
||||
'expires_at' => $service->expires_at?->format('d/m/Y') ?? 'N/A',
|
||||
'invoice_number' => $invoice->invoice_number,
|
||||
'amount_formatted' => number_format((float) $invoice->total, 0, ',', '.') . ' VNĐ',
|
||||
'payment_method_label' => $payment === 'balance' ? 'Trừ số dư tài khoản' : 'Đã thu ngoài hệ thống',
|
||||
'admin_name' => $adminName,
|
||||
'price_row' => EmailTemplateShell::row('Số tiền gia hạn', number_format((float) $invoice->total, 0, ',', '.') . ' VNĐ'),
|
||||
];
|
||||
|
||||
AutoEmailGate::markManual();
|
||||
|
||||
$this->safely(fn () => $this->sendToCustomer($service, $data, $adminId));
|
||||
$this->safely(fn () => $this->sendToAdmin($data));
|
||||
}
|
||||
|
||||
private function sendToCustomer(Service $service, array $data, ?int $adminId): void
|
||||
{
|
||||
$customer = $service->customer;
|
||||
if (! filled($customer?->email)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$mail = new ServiceRenewedMail(self::CUSTOMER_SCENARIO, $data);
|
||||
$template = $this->templates->loadTemplate(self::CUSTOMER_SCENARIO);
|
||||
|
||||
MailLog::create([
|
||||
'template_id' => $template->id,
|
||||
'subject' => $mail->envelope()->subject,
|
||||
'from_address' => $template->mailConfig()['from_email'] ?? config('mail.from.address'),
|
||||
'to' => $customer->email,
|
||||
'customer_id' => $customer->id,
|
||||
'sent_by' => $adminId,
|
||||
'message' => $mail->render(),
|
||||
]);
|
||||
|
||||
Mail::to($customer->email)->queue($mail);
|
||||
}
|
||||
|
||||
private function sendToAdmin(array $data): void
|
||||
{
|
||||
$adminEmail = json_decode((string) Setting::where('key', 'setting')->value('value'), true)['admin_email'] ?? null;
|
||||
if (! $adminEmail) {
|
||||
Log::warning('ServiceRenewalNotifier: admin_email not configured in settings.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Mail::to($adminEmail)->queue(new ServiceRenewedMail(self::ADMIN_SCENARIO, $data));
|
||||
}
|
||||
|
||||
private function safely(callable $send): void
|
||||
{
|
||||
try {
|
||||
$send();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('ServiceRenewalNotifier: failed to send email.', ['error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user