validate([ 'from_date' => ['nullable', 'date_format:Y-m-d'], 'to_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:from_date', 'required_with:from_date'], ], [ 'from_date.date_format' => 'La fecha inicial debe tener el formato Y-m-d (ejemplo: 2025-01-01).', 'to_date.date_format' => 'La fecha final debe tener el formato Y-m-d (ejemplo: 2025-01-31).', 'to_date.after_or_equal' => 'La fecha final debe ser igual o posterior a la fecha inicial.', 'to_date.required_with' => 'La fecha final es obligatoria cuando se proporciona fecha inicial.', ]); try { $product = $this->reportService->getTopSellingProduct( fromDate: $request->input('from_date'), toDate: $request->input('to_date') ); if ($product === null) { return ApiResponse::OK->response([ 'product' => null, 'message' => 'No hay datos de ventas para el período especificado.' ]); } return ApiResponse::OK->response([ 'product' => $product, 'message' => 'Producto más vendido obtenido exitosamente.' ]); } catch (\Exception $e) { return ApiResponse::INTERNAL_ERROR->response([ 'message' => 'Error al generar el reporte: ' . $e->getMessage() ]); } } /** * Obtener productos sin movimiento * * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function productsWithoutMovement(Request $request) { $request->validate([ 'days_threshold' => ['nullable', 'integer', 'min:1'], 'include_stock_value' => ['nullable', 'boolean'], ], [ 'days_threshold.integer' => 'El umbral de días debe ser un número entero.', 'days_threshold.min' => 'El umbral de días debe ser al menos 1.', 'include_stock_value.boolean' => 'El parámetro de valor de stock debe ser verdadero o falso.', ]); try { $products = $this->reportService->getProductsWithoutMovement( daysThreshold: (int)($request->input('days_threshold', 30)), includeStockValue: (bool)($request->input('include_stock_value', true)) ); return ApiResponse::OK->response([ 'products' => $products, 'total_products' => count($products), 'message' => 'Reporte de productos sin movimiento generado exitosamente.' ]); } catch (\Exception $e) { return ApiResponse::INTERNAL_ERROR->response([ 'message' => 'Error al generar el reporte: ' . $e->getMessage() ]); } } }