Files
portal-api/app/Services/AdminPaymentNotifier.php

51 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Jobs\Mail\Admin\AdminPaymentReceivedJob;
use App\Models\Invoice;
use App\Models\Notification;
class AdminPaymentNotifier
{
public function notify(Invoice $invoice, int $amount, string $paymentType): void
{
$invoice->loadMissing(['customer', 'order']);
$outstanding = InvoicePaymentService::outstanding($invoice);
$reference = $invoice->order?->order_num ?? $invoice->invoice_number;
$detailLink = $invoice->order
? '/order-invoice/' . $invoice->order->order_num
: '/order-invoice';
$label = $paymentType === 'order' ? 'đơn hàng' : 'hóa đơn';
$amountFormatted = number_format($amount, 0, ',', '.') . ' VNĐ';
Notification::notifyAdmins([
'type' => 'payment_received',
'title' => 'Khách hàng thanh toán ' . $label,
'body' => "{$invoice->customer->fullname} đã thanh toán {$amountFormatted} cho {$label} #{$reference}.",
'link' => $detailLink,
'data' => [
'invoice_id' => $invoice->id,
'order_id' => $invoice->order_id,
'order_num' => $invoice->order?->order_num,
'invoice_number' => $invoice->invoice_number,
'amount' => $amount,
'outstanding' => $outstanding,
],
]);
AdminPaymentReceivedJob::dispatch([
'payment_type' => $paymentType === 'order' ? 'Đơn hàng' : 'Hóa đơn',
'reference' => $reference,
'invoice_number' => $invoice->invoice_number,
'customer_name' => (string) $invoice->customer->fullname,
'customer_email' => (string) $invoice->customer->email,
'amount' => $amount,
'outstanding' => $outstanding,
'created_at' => now()->format('d/m/Y H:i:s'),
])->afterCommit();
}
}