supplier = model(Supplier::class); } public function index() { $page = max(1, (int) ($this->request->getGet('page') ?? 1)); $perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20))); $keyword = trim((string) ($this->request->getGet('q') ?? '')); $query = $this->supplier->newQuery()->orderBy('name', 'ASC'); if ($keyword !== '') { $query->where(function ($q) use ($keyword) { $q->where('name', 'LIKE', "%{$keyword}%") ->orWhere('email', 'LIKE', "%{$keyword}%") ->orWhere('phone', 'LIKE', "%{$keyword}%"); }); } $result = $this->paginate($query, $page, $perPage); return $this->success($result, 'Suppliers retrieved successfully'); } public function show($id = null) { $supplier = $this->supplier->find($id); if (!$supplier) { return $this->respondError('Supplier not found', Response::HTTP_NOT_FOUND); } return $this->success($supplier, 'Supplier retrieved successfully'); } public function store() { $data = $this->payloadData(); if (empty($data)) { return $this->respondError('Invalid request data', Response::HTTP_BAD_REQUEST); } $rules = [ 'name' => 'required|max:255', 'email' => 'nullable|email', 'phone' => 'nullable|max:50', ]; $validator = Validator::make($data, $rules); if ($validator->fails()) { return $this->respondValidationError($validator->errors()->toArray()); } try { $supplier = $this->supplier->create([ 'name' => $data['name'], 'email' => $data['email'] ?? null, 'phone' => $data['phone'] ?? null, 'address' => $data['address'] ?? null, 'notes' => $data['notes'] ?? null, ]); return $this->success($supplier->toArray(), 'Supplier created successfully', Response::HTTP_CREATED); } catch (\Throwable $e) { Log::error('Supplier creation error: ' . $e->getMessage()); return $this->respondError('Failed to create supplier', Response::HTTP_INTERNAL_SERVER_ERROR); } } public function update($id = null) { $supplier = $this->supplier->find($id); if (!$supplier) { return $this->respondError('Supplier not found', Response::HTTP_NOT_FOUND); } $data = $this->payloadData(); if (empty($data)) { return $this->respondError('Invalid request data', Response::HTTP_BAD_REQUEST); } $allowed = ['name', 'email', 'phone', 'address', 'notes']; $updateData = []; foreach ($allowed as $field) { if (array_key_exists($field, $data)) { $updateData[$field] = $data[$field]; } } if (empty($updateData)) { return $this->respondError('No valid fields to update', Response::HTTP_BAD_REQUEST); } try { $this->supplier->where('id', $id)->update($updateData); $updated = $this->supplier->find($id); return $this->success($updated, 'Supplier updated successfully'); } catch (\Throwable $e) { Log::error('Supplier update error: ' . $e->getMessage()); return $this->respondError('Failed to update supplier', Response::HTTP_INTERNAL_SERVER_ERROR); } } public function destroy($id = null) { $supplier = $this->supplier->find($id); if (!$supplier) { return $this->respondError('Supplier not found', Response::HTTP_NOT_FOUND); } try { $this->supplier->delete($id); return $this->respondDeleted(null, 'Supplier deleted successfully'); } catch (\Throwable $e) { Log::error('Supplier deletion error: ' . $e->getMessage()); return $this->respondError('Failed to delete supplier', Response::HTTP_INTERNAL_SERVER_ERROR); } } }