95 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| // Archivo: GestionIntegral.Api/Data/Repositories/Suscripciones/FacturaRepository.cs
 | |
| 
 | |
| using Dapper;
 | |
| using GestionIntegral.Api.Models.Suscripciones;
 | |
| using System.Data;
 | |
| 
 | |
| namespace GestionIntegral.Api.Data.Repositories.Suscripciones
 | |
| {
 | |
|     public class FacturaRepository : IFacturaRepository
 | |
|     {
 | |
|         private readonly DbConnectionFactory _connectionFactory;
 | |
|         private readonly ILogger<FacturaRepository> _logger;
 | |
| 
 | |
|         public FacturaRepository(DbConnectionFactory connectionFactory, ILogger<FacturaRepository> logger)
 | |
|         {
 | |
|             _connectionFactory = connectionFactory;
 | |
|             _logger = logger;
 | |
|         }
 | |
| 
 | |
|         public async Task<Factura?> GetByIdAsync(int idFactura)
 | |
|         {
 | |
|             const string sql = "SELECT * FROM dbo.susc_Facturas WHERE IdFactura = @IdFactura;";
 | |
|             using var connection = _connectionFactory.CreateConnection();
 | |
|             return await connection.QuerySingleOrDefaultAsync<Factura>(sql, new { idFactura });
 | |
|         }
 | |
| 
 | |
|         public async Task<IEnumerable<Factura>> GetByPeriodoAsync(string periodo)
 | |
|         {
 | |
|             const string sql = "SELECT * FROM dbo.susc_Facturas WHERE Periodo = @Periodo ORDER BY IdFactura;";
 | |
|             using var connection = _connectionFactory.CreateConnection();
 | |
|             return await connection.QueryAsync<Factura>(sql, new { Periodo = periodo });
 | |
|         }
 | |
| 
 | |
|         public async Task<Factura?> GetBySuscripcionYPeriodoAsync(int idSuscripcion, string periodo, IDbTransaction transaction)
 | |
|         {
 | |
|             const string sql = "SELECT TOP 1 * FROM dbo.susc_Facturas WHERE IdSuscripcion = @IdSuscripcion AND Periodo = @Periodo;";
 | |
|              if (transaction == null || transaction.Connection == null)
 | |
|             {
 | |
|                 throw new ArgumentNullException(nameof(transaction), "La transacción o su conexión no pueden ser nulas.");
 | |
|             }
 | |
|             return await transaction.Connection.QuerySingleOrDefaultAsync<Factura>(sql, new { IdSuscripcion = idSuscripcion, Periodo = periodo }, transaction);
 | |
|         }
 | |
| 
 | |
|         public async Task<Factura?> CreateAsync(Factura nuevaFactura, IDbTransaction transaction)
 | |
|         {
 | |
|             if (transaction == null || transaction.Connection == null)
 | |
|             {
 | |
|                 throw new ArgumentNullException(nameof(transaction), "La transacción o su conexión no pueden ser nulas.");
 | |
|             }
 | |
|             const string sqlInsert = @"
 | |
|                 INSERT INTO dbo.susc_Facturas
 | |
|                     (IdSuscripcion, Periodo, FechaEmision, FechaVencimiento, ImporteBruto, 
 | |
|                      DescuentoAplicado, ImporteFinal, Estado)
 | |
|                 OUTPUT INSERTED.*
 | |
|                 VALUES
 | |
|                     (@IdSuscripcion, @Periodo, @FechaEmision, @FechaVencimiento, @ImporteBruto, 
 | |
|                      @DescuentoAplicado, @ImporteFinal, @Estado);";
 | |
|             
 | |
|             return await transaction.Connection.QuerySingleAsync<Factura>(sqlInsert, nuevaFactura, transaction);
 | |
|         }
 | |
| 
 | |
|         public async Task<bool> UpdateEstadoAsync(int idFactura, string nuevoEstado, IDbTransaction transaction)
 | |
|         {
 | |
|             if (transaction == null || transaction.Connection == null)
 | |
|             {
 | |
|                 throw new ArgumentNullException(nameof(transaction), "La transacción o su conexión no pueden ser nulas.");
 | |
|             }
 | |
|             const string sql = "UPDATE dbo.susc_Facturas SET Estado = @NuevoEstado WHERE IdFactura = @IdFactura;";
 | |
|             var rowsAffected = await transaction.Connection.ExecuteAsync(sql, new { NuevoEstado = nuevoEstado, IdFactura = idFactura }, transaction);
 | |
|             return rowsAffected == 1;
 | |
|         }
 | |
| 
 | |
|         public async Task<bool> UpdateNumeroFacturaAsync(int idFactura, string numeroFactura, IDbTransaction transaction)
 | |
|         {
 | |
|             if (transaction == null || transaction.Connection == null)
 | |
|             {
 | |
|                 throw new ArgumentNullException(nameof(transaction), "La transacción o su conexión no pueden ser nulas.");
 | |
|             }
 | |
|             const string sql = "UPDATE dbo.susc_Facturas SET NumeroFactura = @NumeroFactura, Estado = 'Pendiente de Cobro' WHERE IdFactura = @IdFactura;";
 | |
|             var rowsAffected = await transaction.Connection.ExecuteAsync(sql, new { NumeroFactura = numeroFactura, IdFactura = idFactura }, transaction);
 | |
|             return rowsAffected == 1;
 | |
|         }
 | |
|         
 | |
|         public async Task<bool> UpdateLoteDebitoAsync(IEnumerable<int> idsFacturas, int idLoteDebito, IDbTransaction transaction)
 | |
|         {
 | |
|             if (transaction == null || transaction.Connection == null)
 | |
|             {
 | |
|                 throw new ArgumentNullException(nameof(transaction), "La transacción o su conexión no pueden ser nulas.");
 | |
|             }
 | |
|             const string sql = "UPDATE dbo.susc_Facturas SET IdLoteDebito = @IdLoteDebito, Estado = 'Enviada a Débito' WHERE IdFactura IN @IdsFacturas;";
 | |
|             var rowsAffected = await transaction.Connection.ExecuteAsync(sql, new { IdLoteDebito = idLoteDebito, IdsFacturas = idsFacturas }, transaction);
 | |
|             return rowsAffected == idsFacturas.Count();
 | |
|         }
 | |
|     }
 | |
| } |