pdv.backend/app/Http/Controllers/App/ReportController.php
2026-01-05 15:52:08 -06:00

95 lines
3.7 KiB
PHP

<?php
namespace App\Http\Controllers\App;
use App\Http\Controllers\Controller;
use App\Services\ReportService;
use Illuminate\Http\Request;
use Notsoweb\ApiResponse\Enums\ApiResponse;
class ReportController extends Controller
{
public function __construct(
protected ReportService $reportService
) {}
/**
* Obtener el producto más vendido
*/
public function topSellingProduct(Request $request)
{
$request->validate([
'include_stock_value' => ['nullable', 'boolean'],
'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'],
], [
'include_stock_value.boolean' => 'El parámetro de valor de stock debe ser verdadero o falso.',
'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
*/
public function productsWithoutMovement(Request $request)
{
$request->validate([
'include_stock_value' => ['nullable', 'boolean'],
'from_date' => ['required', 'date_format:Y-m-d'],
'to_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:from_date'],
], [
'include_stock_value.boolean' => 'El parámetro de valor de stock debe ser verdadero o falso.',
'from_date.required' => 'La fecha inicial es obligatoria.',
'from_date.date_format' => 'La fecha inicial debe tener el formato Y-m-d (ejemplo: 2025-01-01).',
'to_date.required' => 'La fecha final es obligatoria.',
'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.',
]);
try {
$products = $this->reportService->getProductsWithoutMovement(
includeStockValue: (bool)($request->input('include_stock_value', true)),
fromDate: $request->input('from_date'),
toDate: $request->input('to_date')
);
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()
]);
}
}
}