update fix deadcode, security
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Invoice\Concerns;
|
||||
|
||||
use App\Mail\InvoiceNotificationMail;
|
||||
use App\Models\Customer;
|
||||
use App\Services\InvoiceReminderService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Throwable;
|
||||
|
||||
trait SendsInvoiceNotifications
|
||||
{
|
||||
/**
|
||||
* Nhắc thanh toán thủ công — dùng khi kế toán muốn thúc riêng một khách mà không chờ
|
||||
* mốc tự động trong billing.reminder_offsets.
|
||||
*/
|
||||
public function remind(int $id, InvoiceReminderService $reminders): JsonResponse
|
||||
{
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
|
||||
if (!$invoice) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => "Can't find invoice by ID #" . $id
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$reminders->isRemindable($invoice)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Chỉ nhắc được hoá đơn chưa thanh toán hoặc quá hạn.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$reminders->remind($invoice->load('customer'), force: true);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không gửi được email nhắc: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi email nhắc thanh toán tới ' . $invoice->customer?->email,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendMail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'message' => 'nullable|string|max:1000',
|
||||
'type' => 'nullable|string|in:invoice,renewal',
|
||||
]);
|
||||
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
if (!$invoice) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
|
||||
}
|
||||
|
||||
$invoice->load('customer');
|
||||
$customer = $invoice->customer;
|
||||
if (!$customer || !$customer->email) {
|
||||
return response()->json(['error' => true, 'message' => 'Customer email not found'], 422);
|
||||
}
|
||||
|
||||
$attachments = $this->attachmentRepository->getByInvoice($id);
|
||||
$companyName = setting('general.company_name') ?: config('app.name');
|
||||
$type = $request->get('type', 'invoice');
|
||||
$message = $request->get(
|
||||
'message',
|
||||
$type === 'renewal'
|
||||
? "Dịch vụ của quý khách sắp đến hạn gia hạn. Vui lòng kiểm tra hóa đơn #{$invoice->order_num} được đính kèm và hoàn tất thanh toán để dịch vụ không bị gián đoạn."
|
||||
: "Vui lòng xem thông tin hóa đơn #{$invoice->order_num} của bạn."
|
||||
);
|
||||
$paymentUrl = url('/client/invoices/' . $invoice->invoice_number . '/payment');
|
||||
try {
|
||||
$invoicePdf = $this->invoicePdfService->generate($invoice);
|
||||
$invoicePdfFileName = $this->invoicePdfService->generateFileName($invoice);
|
||||
|
||||
Mail::to($customer->email)->send(
|
||||
new InvoiceNotificationMail(
|
||||
$invoice,
|
||||
$attachments,
|
||||
$customer->fullname,
|
||||
$message,
|
||||
$companyName,
|
||||
$type,
|
||||
$invoicePdf,
|
||||
$invoicePdfFileName,
|
||||
$paymentUrl
|
||||
)
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
Log::error('InvoiceController@sendMail failed', [
|
||||
'invoice_id' => $id,
|
||||
'invoice_number' => $invoice->invoice_number ?? null,
|
||||
'customer_email' => $customer->email,
|
||||
'type' => $type,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không thể gửi email: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Email đã được gửi tới {$customer->email}",
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user