update
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Invoice;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\InvoiceRequest;
|
||||
use App\Http\Resources\Api\Admin\InvoiceResource;
|
||||
@@ -12,11 +13,15 @@ use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
||||
use App\Repositories\InvoiceAttachment\InvoiceAttachmentRepositoryInterface;
|
||||
use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
|
||||
use App\Repositories\Order\OrderRepositoryInterface;
|
||||
use App\Services\InvoicePaymentService;
|
||||
use App\Services\InvoicePdfService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Throwable;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
@@ -26,13 +31,15 @@ class InvoiceController extends Controller
|
||||
private readonly OrderRepositoryInterface $orderRepository,
|
||||
private readonly InvoiceItemRepositoryInterface $invoiceItemRepository,
|
||||
private readonly InvoiceAttachmentRepositoryInterface $attachmentRepository,
|
||||
private readonly InvoicePdfService $invoicePdfService,
|
||||
private readonly InvoicePaymentService $invoicePaymentService,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$query = $this->invoiceRepository->filter(
|
||||
$request->all(),
|
||||
['payment', 'customer'],
|
||||
['payment', 'customer', 'order'],
|
||||
[],
|
||||
['*'],
|
||||
true
|
||||
@@ -51,7 +58,7 @@ class InvoiceController extends Controller
|
||||
{
|
||||
$query = $this->invoiceRepository->filter(
|
||||
$request->all(),
|
||||
['payment', 'customer'],
|
||||
['payment', 'customer', 'order'],
|
||||
[],
|
||||
['*'],
|
||||
true
|
||||
@@ -138,18 +145,33 @@ class InvoiceController extends Controller
|
||||
|
||||
$request->validated();
|
||||
|
||||
$status = match (strtolower($request->get('action'))) {
|
||||
"paid" => 1,
|
||||
"cancel" => 2,
|
||||
default => 0
|
||||
$action = strtolower((string) $request->get('action'));
|
||||
$status = match ($action) {
|
||||
'paid' => InvoiceStatus::PAID->value,
|
||||
'cancel' => InvoiceStatus::CANCELLED->value,
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($status === null) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Invalid invoice action.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$this->invoiceRepository->update($id, [
|
||||
'status' => $status,
|
||||
'date_paid' => $status === 1 ? time() : null
|
||||
]);
|
||||
$invoice->load(['order', 'customer']);
|
||||
|
||||
if ($status === InvoiceStatus::PAID->value) {
|
||||
$this->invoicePaymentService->markPaidByAdmin($invoice, auth()->id());
|
||||
} else {
|
||||
$invoice->update([
|
||||
'status' => $status,
|
||||
'paid_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
@@ -208,10 +230,10 @@ class InvoiceController extends Controller
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$this->invoiceRepository->update($id, [
|
||||
'status' => 1,
|
||||
'date_paid' => time()
|
||||
]);
|
||||
|
||||
$invoice->load(['order', 'customer']);
|
||||
$this->invoicePaymentService->markPaidByAdmin($invoice, auth()->id());
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
@@ -240,9 +262,9 @@ class InvoiceController extends Controller
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$this->invoiceRepository->update($id, [
|
||||
'status' => 2,
|
||||
'date_paid' => null
|
||||
$invoice->update([
|
||||
'status' => InvoiceStatus::CANCELLED->value,
|
||||
'paid_at' => null
|
||||
]);
|
||||
DB::commit();
|
||||
|
||||
@@ -334,6 +356,7 @@ class InvoiceController extends Controller
|
||||
{
|
||||
$request->validate([
|
||||
'message' => 'nullable|string|max:1000',
|
||||
'type' => 'nullable|string|in:invoice,renewal',
|
||||
]);
|
||||
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
@@ -349,11 +372,45 @@ class InvoiceController extends Controller
|
||||
|
||||
$attachments = $this->attachmentRepository->getByInvoice($id);
|
||||
$companyName = setting('general.company_name') ?: config('app.name');
|
||||
$message = $request->get('message', "Vui lòng xem thông tin hóa đơn #{$invoice->order_num} của bạn.");
|
||||
|
||||
Mail::to($customer->email)->queue(
|
||||
new InvoiceNotificationMail($invoice, $attachments, $customer->fullname, $message, $companyName)
|
||||
$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,
|
||||
|
||||
Reference in New Issue
Block a user