134 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			134 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | using GestionIntegral.Api.Data.Repositories; // Para ITipoPagoRepository | ||
|  | using GestionIntegral.Api.Dtos.Contables; | ||
|  | using GestionIntegral.Api.Models.Contables; // Para TipoPago | ||
|  | using GestionIntegral.Api.Services.Contables; | ||
|  | using System.Collections.Generic; | ||
|  | using System.Linq; | ||
|  | using System.Threading.Tasks; | ||
|  | 
 | ||
|  | namespace GestionIntegral.Api.Services.Contables | ||
|  | { | ||
|  |     public class TipoPagoService : ITipoPagoService | ||
|  |     { | ||
|  |         private readonly ITipoPagoRepository _tipoPagoRepository; | ||
|  |         private readonly ILogger<TipoPagoService> _logger; | ||
|  | 
 | ||
|  |         public TipoPagoService(ITipoPagoRepository tipoPagoRepository, ILogger<TipoPagoService> logger) | ||
|  |         { | ||
|  |             _tipoPagoRepository = tipoPagoRepository; | ||
|  |             _logger = logger; | ||
|  |         } | ||
|  | 
 | ||
|  |         public async Task<IEnumerable<TipoPagoDto>> ObtenerTodosAsync(string? nombreFilter) | ||
|  |         { | ||
|  |             var tiposPago = await _tipoPagoRepository.GetAllAsync(nombreFilter); | ||
|  |             // Mapeo de Entidad a DTO | ||
|  |             return tiposPago.Select(tp => new TipoPagoDto | ||
|  |             { | ||
|  |                 IdTipoPago = tp.IdTipoPago, | ||
|  |                 Nombre = tp.Nombre, | ||
|  |                 Detalle = tp.Detalle | ||
|  |             }); | ||
|  |         } | ||
|  | 
 | ||
|  |         public async Task<TipoPagoDto?> ObtenerPorIdAsync(int id) | ||
|  |         { | ||
|  |             var tipoPago = await _tipoPagoRepository.GetByIdAsync(id); | ||
|  |             if (tipoPago == null) return null; | ||
|  | 
 | ||
|  |             // Mapeo de Entidad a DTO | ||
|  |             return new TipoPagoDto | ||
|  |             { | ||
|  |                 IdTipoPago = tipoPago.IdTipoPago, | ||
|  |                 Nombre = tipoPago.Nombre, | ||
|  |                 Detalle = tipoPago.Detalle | ||
|  |             }; | ||
|  |         } | ||
|  | 
 | ||
|  |         public async Task<(TipoPagoDto? TipoPago, string? Error)> CrearAsync(CreateTipoPagoDto createDto, int idUsuario) | ||
|  |         { | ||
|  |             // Validación: Nombre no puede estar duplicado | ||
|  |             if (await _tipoPagoRepository.ExistsByNameAsync(createDto.Nombre)) | ||
|  |             { | ||
|  |                 return (null, "El nombre del tipo de pago ya existe."); | ||
|  |             } | ||
|  | 
 | ||
|  |             var nuevoTipoPago = new TipoPago | ||
|  |             { | ||
|  |                 Nombre = createDto.Nombre, | ||
|  |                 Detalle = createDto.Detalle | ||
|  |             }; | ||
|  | 
 | ||
|  |             var tipoPagoCreado = await _tipoPagoRepository.CreateAsync(nuevoTipoPago, idUsuario); | ||
|  | 
 | ||
|  |             if (tipoPagoCreado == null) | ||
|  |             { | ||
|  |                 _logger.LogError("Falló la creación del Tipo de Pago en el repositorio para el nombre: {Nombre}", createDto.Nombre); | ||
|  |                 return (null, "Error al crear el tipo de pago en la base de datos."); | ||
|  |             } | ||
|  | 
 | ||
|  |             // Mapeo de Entidad creada (con ID) a DTO | ||
|  |             var tipoPagoDto = new TipoPagoDto | ||
|  |             { | ||
|  |                 IdTipoPago = tipoPagoCreado.IdTipoPago, | ||
|  |                 Nombre = tipoPagoCreado.Nombre, | ||
|  |                 Detalle = tipoPagoCreado.Detalle | ||
|  |             }; | ||
|  | 
 | ||
|  |             return (tipoPagoDto, null); // Éxito | ||
|  |         } | ||
|  | 
 | ||
|  |         public async Task<(bool Exito, string? Error)> ActualizarAsync(int id, UpdateTipoPagoDto updateDto, int idUsuario) | ||
|  |         { | ||
|  |             // Verificar si el tipo de pago existe | ||
|  |             var tipoPagoExistente = await _tipoPagoRepository.GetByIdAsync(id); | ||
|  |             if (tipoPagoExistente == null) | ||
|  |             { | ||
|  |                 return (false, "Tipo de pago no encontrado."); | ||
|  |             } | ||
|  | 
 | ||
|  |             // Validación: Nombre no puede estar duplicado (excluyendo el ID actual) | ||
|  |             if (await _tipoPagoRepository.ExistsByNameAsync(updateDto.Nombre, id)) | ||
|  |             { | ||
|  |                 return (false, "El nombre del tipo de pago ya existe para otro registro."); | ||
|  |             } | ||
|  | 
 | ||
|  |             // Mapeo de DTO a Entidad para actualizar | ||
|  |             tipoPagoExistente.Nombre = updateDto.Nombre; | ||
|  |             tipoPagoExistente.Detalle = updateDto.Detalle; | ||
|  | 
 | ||
|  |             var actualizado = await _tipoPagoRepository.UpdateAsync(tipoPagoExistente, idUsuario); | ||
|  | 
 | ||
|  |             if (!actualizado) | ||
|  |             { | ||
|  |                  _logger.LogError("Falló la actualización del Tipo de Pago en el repositorio para el ID: {Id}", id); | ||
|  |                  return (false, "Error al actualizar el tipo de pago en la base de datos."); | ||
|  |             } | ||
|  |             return (true, null); // Éxito | ||
|  |         } | ||
|  | 
 | ||
|  |         public async Task<(bool Exito, string? Error)> EliminarAsync(int id, int idUsuario) | ||
|  |         { | ||
|  |             // Verificar si el tipo de pago existe | ||
|  |             var tipoPagoExistente = await _tipoPagoRepository.GetByIdAsync(id); | ||
|  |             if (tipoPagoExistente == null) | ||
|  |             { | ||
|  |                 return (false, "Tipo de pago no encontrado."); | ||
|  |             } | ||
|  | 
 | ||
|  |             // Validación: No se puede eliminar si está en uso | ||
|  |             if (await _tipoPagoRepository.IsInUseAsync(id)) | ||
|  |             { | ||
|  |                 return (false, "No se puede eliminar. El tipo de pago está siendo utilizado en pagos registrados."); | ||
|  |             } | ||
|  | 
 | ||
|  |             var eliminado = await _tipoPagoRepository.DeleteAsync(id, idUsuario); | ||
|  |             if (!eliminado) | ||
|  |             { | ||
|  |                 _logger.LogError("Falló la eliminación del Tipo de Pago en el repositorio para el ID: {Id}", id); | ||
|  |                 return (false, "Error al eliminar el tipo de pago de la base de datos."); | ||
|  |             } | ||
|  |             return (true, null); // Éxito | ||
|  |         } | ||
|  |     } | ||
|  | } |