154 lines
4.3 KiB
PHP
154 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\ClientCategoryRequest;
|
|
use App\Http\Resources\Client\ClientCategoryResource;
|
|
use App\Http\Resources\ClientResource;
|
|
use App\Models\ClientCategory;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
|
|
class ClientCategoryController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(): AnonymousResourceCollection
|
|
{
|
|
$query = ClientCategory::query();
|
|
$categories = $query->get();
|
|
|
|
return ClientCategoryResource::collection($categories);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(ClientCategoryRequest $request): ClientCategoryResource
|
|
{
|
|
$category = ClientCategory::create($request->validated());
|
|
|
|
return new ClientCategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(ClientCategory $clientCategory): ClientCategoryResource
|
|
{
|
|
return new ClientCategoryResource($clientCategory);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource by slug.
|
|
*/
|
|
public function showBySlug(string $slug): ClientCategoryResource
|
|
{
|
|
$category = ClientCategory::where('slug', $slug)->firstOrFail();
|
|
|
|
return new ClientCategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(ClientCategoryRequest $request, ClientCategory $clientCategory): ClientCategoryResource
|
|
{
|
|
$clientCategory->update($request->validated());
|
|
|
|
return new ClientCategoryResource($clientCategory->fresh());
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(ClientCategory $clientCategory): JsonResponse
|
|
{
|
|
// Check if category has clients
|
|
if ($clientCategory->clients()->exists()) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Cannot delete category that has clients assigned. Please reassign clients first.'
|
|
], 422);
|
|
}
|
|
|
|
$clientCategory->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Client category deleted successfully.'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Toggle active status of the category.
|
|
*/
|
|
public function toggleStatus(ClientCategory $clientCategory, Request $request): ClientCategoryResource
|
|
{
|
|
$request->validate([
|
|
'is_active' => 'required|boolean',
|
|
]);
|
|
|
|
$clientCategory->update([
|
|
'is_active' => $request->boolean('is_active'),
|
|
]);
|
|
|
|
return new ClientCategoryResource($clientCategory->fresh());
|
|
}
|
|
|
|
/**
|
|
* Get clients for a specific category.
|
|
*/
|
|
public function clients(ClientCategory $clientCategory, Request $request): AnonymousResourceCollection
|
|
{
|
|
$query = $clientCategory->clients();
|
|
|
|
// Active status filter
|
|
if ($request->has('is_active')) {
|
|
$query->where('is_active', $request->boolean('is_active'));
|
|
}
|
|
|
|
// Pagination
|
|
$perPage = $request->get('per_page', 15);
|
|
$clients = $query->paginate($perPage);
|
|
|
|
return ClientResource::collection($clients);
|
|
}
|
|
|
|
/**
|
|
* Reorder categories.
|
|
*/
|
|
public function reorder(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'order' => 'required|array',
|
|
'order.*' => 'integer|exists:client_categories,id',
|
|
]);
|
|
|
|
foreach ($request->order as $index => $categoryId) {
|
|
ClientCategory::where('id', $categoryId)->update(['sort_order' => $index]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Categories reordered successfully.'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get all active categories for dropdowns.
|
|
*/
|
|
public function active(): AnonymousResourceCollection
|
|
{
|
|
$categories = ClientCategory::where('is_active', true)
|
|
->orderBy('sort_order', 'asc')
|
|
->orderBy('name', 'asc')
|
|
->get();
|
|
|
|
return ClientCategoryResource::collection($categories);
|
|
}
|
|
}
|