Files
GestionIntegralWeb/Backend/GestionIntegral.Api/Services/Reportes/ReportesService.cs

68 lines
2.8 KiB
C#

using GestionIntegral.Api.Data.Repositories.Reportes;
using GestionIntegral.Api.Dtos.Reportes;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GestionIntegral.Api.Services.Reportes
{
public class ReportesService : IReportesService
{
private readonly IReportesRepository _reportesRepository;
private readonly ILogger<ReportesService> _logger;
public ReportesService(IReportesRepository reportesRepository, ILogger<ReportesService> logger)
{
_reportesRepository = reportesRepository;
_logger = logger;
}
public async Task<(IEnumerable<ExistenciaPapelDto> Data, string? Error)> ObtenerExistenciaPapelAsync(
DateTime fechaDesde, DateTime fechaHasta, int? idPlanta, bool consolidado)
{
if (fechaDesde > fechaHasta)
{
return (Enumerable.Empty<ExistenciaPapelDto>(), "La fecha 'Desde' no puede ser mayor que la fecha 'Hasta'.");
}
if (!consolidado && !idPlanta.HasValue)
{
return (Enumerable.Empty<ExistenciaPapelDto>(), "Se requiere un ID de planta para reportes no consolidados.");
}
try
{
var dataFromRepo = await _reportesRepository.GetExistenciaPapelAsync(fechaDesde, fechaHasta, idPlanta, consolidado);
// Ajustar DateTimeKind a Utc aquí
var dataWithUtcDates = dataFromRepo.Select(dto =>
{
if (dto.FechaEstimacionFinStock.HasValue)
{
// Aseguramos que solo tomamos la parte de la fecha y la especificamos como UTC
// Si ya viene con hora 00:00:00 del SP, .Date no cambia nada.
// Si viniera con hora, .Date la trunca a 00:00:00.
dto.FechaEstimacionFinStock = DateTime.SpecifyKind(dto.FechaEstimacionFinStock.Value.Date, DateTimeKind.Utc);
}
return dto;
}).ToList();
return (dataWithUtcDates, null);
}
catch (ArgumentNullException ex) when (ex.ParamName == "idPlanta") // Capturar la excepción del repositorio
{
_logger.LogWarning(ex, "ArgumentNullException para idPlanta en ObtenerExistenciaPapelAsync.");
return (Enumerable.Empty<ExistenciaPapelDto>(), ex.Message); // Devolver el mensaje de error del repo
}
catch (Exception ex)
{
_logger.LogError(ex, "Error en ReportesService al obtener Existencia de Papel.");
return (Enumerable.Empty<ExistenciaPapelDto>(), "Error interno al generar el reporte.");
}
}
}
}