invoiceRepository->filter( $request->all(), ['payment', 'customer'], [], ['*'], true ); return response()->json([ 'error' => false, 'data' => [ 'items' => InvoiceResource::collection($query->get())->resolve(), 'total' => $query->count(), ], ]); } public function datatable(Request $request): JsonResponse { $query = $this->invoiceRepository->filter( $request->all(), ['payment', 'customer'], [], ['*'], true ); return DataTables::eloquent($query) ->addColumn('invoice_number', fn($d) => $d->invoice_number) ->addColumn('date', fn($d) => $d->date) ->addColumn('customer', fn($d) => $d->customer?->fullname) ->addColumn('payment', fn($d) => $d->payment?->name) ->addColumn('total', fn($d) => $d->total) ->addColumn('status', fn($d) => $d->status) ->addColumn('action', fn($d) => $d->id) ->rawColumns(['invoice_number', 'date', 'customer', 'payment', 'total', 'status', 'action']) ->make(true); } public function store(Request $request): JsonResponse { $validated = $request->validate([ 'customer_id' => 'required|numeric', 'payment_method' => 'required|numeric', 'sub_total' => 'required|numeric', 'total' => 'required|numeric', ]); $invoiceData = [ 'date' => time(), 'due_date' => time() + (7 * 24 * 60 * 60), 'sub_total' => $validated['sub_total'], 'tax' => $validated['sub_total'] * 0.1, 'total' => $validated['total'], 'payment_method' => $validated['payment_method'], 'customer_id' => $validated['customer_id'], 'status' => 0, ]; try { DB::beginTransaction(); $invoice = $this->invoiceRepository->create($invoiceData); DB::commit(); return response()->json([ 'error' => false, 'message' => 'Invoice created successfully', 'data' => new InvoiceResource($invoice) ], 201); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'error' => true, 'message' => $e->getMessage() ], 500); } } public function show(int $id): JsonResponse { $invoice = $this->invoiceRepository->findWithRelationships($id, ['payment', 'invoiceItem', 'customer']); if (!$invoice) { return response()->json([ 'error' => true, 'message' => "Can't find invoice by ID #" . $id ], 404); } return response()->json([ 'error' => false, 'data' => new InvoiceResource($invoice) ]); } public function update(InvoiceRequest $request, int $id): JsonResponse { $invoice = $this->invoiceRepository->find($id); if (!$invoice) { return response()->json([ 'error' => true, 'message' => "Can't find invoice by ID #" . $id ], 404); } $request->validated(); $status = match (strtolower($request->get('action'))) { "paid" => 1, "cancel" => 2, default => 0 }; try { DB::beginTransaction(); $this->invoiceRepository->update($id, [ 'status' => $status, 'date_paid' => $status === 1 ? time() : null ]); DB::commit(); return response()->json([ 'error' => false, 'message' => 'Invoice updated successfully' ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'error' => true, 'message' => $e->getMessage() ], 500); } } public function destroy(int $id): JsonResponse { $invoice = $this->invoiceRepository->find($id); if (!$invoice) { return response()->json([ 'error' => true, 'message' => "Can't find invoice by ID #" . $id ], 404); } try { DB::beginTransaction(); $this->orderRepository->updateBy('invoice_id', $id, ['invoice_id' => 0]); $this->invoiceRepository->delete($id); DB::commit(); return response()->json([ 'error' => false, 'message' => 'Invoice deleted successfully' ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'error' => true, 'message' => $e->getMessage() ], 500); } } public function markAsPaid(int $id): JsonResponse { $invoice = $this->invoiceRepository->find($id); if (!$invoice) { return response()->json([ 'error' => true, 'message' => "Can't find invoice by ID #" . $id ], 404); } try { DB::beginTransaction(); $this->invoiceRepository->update($id, [ 'status' => 1, 'date_paid' => time() ]); DB::commit(); return response()->json([ 'error' => false, 'message' => 'Invoice marked as paid' ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'error' => true, 'message' => $e->getMessage() ], 500); } } public function cancel(int $id): JsonResponse { $invoice = $this->invoiceRepository->find($id); if (!$invoice) { return response()->json([ 'error' => true, 'message' => "Can't find invoice by ID #" . $id ], 404); } try { DB::beginTransaction(); $this->invoiceRepository->update($id, [ 'status' => 2, 'date_paid' => null ]); DB::commit(); return response()->json([ 'error' => false, 'message' => 'Invoice cancelled' ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'error' => true, 'message' => $e->getMessage() ], 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}", ]); } }