feat: Implement client status validation on login, refine password rules, and simplify customer resource status.
This commit is contained in:
@@ -5,14 +5,18 @@ namespace App\Http\Controllers\Api\Admin\Invoice;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\InvoiceRequest;
|
||||
use App\Http\Resources\Api\Admin\InvoiceResource;
|
||||
use App\Mail\InvoiceNotificationMail;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Payment;
|
||||
use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
||||
use App\Repositories\InvoiceAttachment\InvoiceAttachmentRepositoryInterface;
|
||||
use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
|
||||
use App\Repositories\Order\OrderRepositoryInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
@@ -21,6 +25,7 @@ class InvoiceController extends Controller
|
||||
private readonly InvoiceRepositoryInterface $invoiceRepository,
|
||||
private readonly OrderRepositoryInterface $orderRepository,
|
||||
private readonly InvoiceItemRepositoryInterface $invoiceItemRepository,
|
||||
private readonly InvoiceAttachmentRepositoryInterface $attachmentRepository,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
@@ -253,4 +258,106 @@ class InvoiceController extends Controller
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Attachments ────────────────────────────────────────────────────────────
|
||||
|
||||
public function listAttachments(int $id): JsonResponse
|
||||
{
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
if (!$invoice) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
|
||||
}
|
||||
|
||||
$attachments = $this->attachmentRepository->getByInvoice($id);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $attachments,
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadAttachment(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
if (!$invoice) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
|
||||
}
|
||||
|
||||
$request->validate([
|
||||
'file' => 'required|file|mimes:jpg,jpeg,png,gif,pdf,doc,docx,xls,xlsx|max:10240',
|
||||
]);
|
||||
|
||||
$file = $request->file('file');
|
||||
$path = $file->store("invoice-attachments/{$id}", 'public');
|
||||
$uploader = $request->user()?->username ?? $request->user()?->name ?? 'admin';
|
||||
|
||||
$attachment = $this->attachmentRepository->create([
|
||||
'invoice_id' => $id,
|
||||
'filename' => $file->getClientOriginalName(),
|
||||
'path' => $path,
|
||||
'disk' => 'public',
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'size' => $file->getSize(),
|
||||
'uploaded_by' => $uploader,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'File uploaded successfully',
|
||||
'data' => $attachment,
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function deleteAttachment(int $id, int $attachmentId): JsonResponse
|
||||
{
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
if (!$invoice) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
|
||||
}
|
||||
|
||||
$attachment = $this->attachmentRepository->find($attachmentId);
|
||||
if (!$attachment || $attachment->invoice_id !== $id) {
|
||||
return response()->json(['error' => true, 'message' => 'Attachment not found'], 404);
|
||||
}
|
||||
|
||||
$this->attachmentRepository->deleteWithFile($attachmentId);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Attachment deleted',
|
||||
]);
|
||||
}
|
||||
|
||||
// ─── Send Mail ───────────────────────────────────────────────────────────────
|
||||
|
||||
public function sendMail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'message' => 'nullable|string|max:1000',
|
||||
]);
|
||||
|
||||
$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');
|
||||
$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)
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Email đã được gửi tới {$customer->email}",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user