fix: Mejorar la validación y asignación de tags en el controlador de Tags
This commit is contained in:
parent
75889becaf
commit
36865e7cef
@ -107,14 +107,7 @@ public function tagStore(Request $request)
|
|||||||
|
|
||||||
if (!$statusAvailable) {
|
if (!$statusAvailable) {
|
||||||
return ApiResponse::NOT_FOUND->response([
|
return ApiResponse::NOT_FOUND->response([
|
||||||
'message' => 'El tag no tiene estado disponible.',
|
'message' => 'El estado "disponible" no existe en el catálogo de estados.',
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($tag->module_id === null) {
|
|
||||||
return ApiResponse::BAD_REQUEST->response([
|
|
||||||
'message' => 'El tag no está asignado a ningún módulo. Debe asignarse primero a un módulo antes de poder usarse.',
|
|
||||||
'tag_number' => $tagNumber,
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,44 +166,33 @@ public function assignToModule(Request $request)
|
|||||||
try {
|
try {
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'module_id' => 'required|exists:modules,id',
|
'module_id' => 'required|exists:modules,id',
|
||||||
'tag_ids' => 'required_without_all:package_id,from_tag_number,to_tag_number|array|min:1',
|
'cantidad' => 'required|integer|min:1',
|
||||||
'tag_ids.*' => 'exists:tags,id',
|
|
||||||
'package_id' => 'required_with:from_tag_number,to_tag_number|exists:packages,id',
|
|
||||||
'from_tag_number' => 'required_with:package_id|string',
|
|
||||||
'to_tag_number' => 'required_with:package_id|string',
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
|
|
||||||
// Determinar qué tags asignar
|
// Buscar tags disponibles (sin módulo asignado y sin vehículo)
|
||||||
if ($request->has('tag_ids')) {
|
$tags = Tag::whereNull('module_id')
|
||||||
// Asignación por IDs específicos
|
->whereNull('vehicle_id')
|
||||||
$tags = Tag::with('status')->whereIn('id', $request->tag_ids)->get();
|
->orderBy('id', 'ASC')
|
||||||
} else {
|
->limit($request->cantidad)
|
||||||
// Asignación por rango
|
->get();
|
||||||
$tags = Tag::where('package_id', $request->package_id)
|
|
||||||
->where('tag_number', '>=', $request->from_tag_number)
|
|
||||||
->where('tag_number', '<=', $request->to_tag_number)
|
|
||||||
->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($tags->isEmpty()) {
|
if ($tags->isEmpty()) {
|
||||||
return ApiResponse::NOT_FOUND->response([
|
return ApiResponse::NOT_FOUND->response([
|
||||||
'message' => 'No se encontraron tags con los criterios especificados.',
|
'message' => 'No se encontraron tags disponibles.',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validar que no estén asignados a vehículos
|
if ($tags->count() < $request->cantidad) {
|
||||||
$unavailableTags = $tags->filter(fn($tag) => $tag->vehicle_id !== null);
|
|
||||||
|
|
||||||
if ($unavailableTags->isNotEmpty()) {
|
|
||||||
return ApiResponse::BAD_REQUEST->response([
|
return ApiResponse::BAD_REQUEST->response([
|
||||||
'message' => 'Algunos tags ya están asignados a vehículos.',
|
'message' => "Solo hay {$tags->count()} tags disponibles, pero solicitaste {$request->cantidad}.",
|
||||||
'unavailable_tags' => $unavailableTags->pluck('tag_number')->toArray(),
|
'disponibles' => $tags->count(),
|
||||||
|
'solicitados' => $request->cantidad,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asignar módulo
|
// Asignar módulo a los tags
|
||||||
$tagIds = $tags->pluck('id')->toArray();
|
$tagIds = $tags->pluck('id')->toArray();
|
||||||
Tag::whereIn('id', $tagIds)->update(['module_id' => $request->module_id]);
|
Tag::whereIn('id', $tagIds)->update(['module_id' => $request->module_id]);
|
||||||
|
|
||||||
|
|||||||
@ -72,7 +72,6 @@
|
|||||||
Route::resource('tags', TagsController::class);
|
Route::resource('tags', TagsController::class);
|
||||||
Route::post('tags/import', [TagsController::class, 'tagStore']);
|
Route::post('tags/import', [TagsController::class, 'tagStore']);
|
||||||
Route::post('tags/assign-to-module', [TagsController::class, 'assignToModule']);
|
Route::post('tags/assign-to-module', [TagsController::class, 'assignToModule']);
|
||||||
Route::get('tags/pdf-vale-entrega', [TagsController::class, 'generateValeEntregaPdf']);
|
|
||||||
|
|
||||||
//Rutas de nombres de archivos en catálogo
|
//Rutas de nombres de archivos en catálogo
|
||||||
Route::resource('catalog-name-imgs', CatalogNameImgController::class);
|
Route::resource('catalog-name-imgs', CatalogNameImgController::class);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user