diff --git a/Contracts/Reportes/ITraficosClientesService.cs b/Contracts/Reportes/ITraficosClientesService.cs new file mode 100644 index 0000000..7427694 --- /dev/null +++ b/Contracts/Reportes/ITraficosClientesService.cs @@ -0,0 +1,7 @@ +using CORRESPONSALBackend.DTO.Corresponsales; + +namespace CORRESPONSALBackend.Contracts.Reportes{ + public interface ITraficosClientesService{ + public Task GetExcel(IEnumerable traficos); + } +} \ No newline at end of file diff --git a/Controllers/ClientesController.cs b/Controllers/ClientesController.cs index dfa7e14..39d2d4d 100644 --- a/Controllers/ClientesController.cs +++ b/Controllers/ClientesController.cs @@ -1,4 +1,5 @@ using CORRESPONSALBackend.Contracts; +using CORRESPONSALBackend.Contracts.Reportes; using CORRESPONSALBackend.DTO.Cliente; using CORRESPONSALBackend.DTO.Corresponsales; using CORRESPONSALBackend.DTO.Reportes; @@ -14,10 +15,12 @@ namespace CORRESPONSALBackend.Controllers public class ClientesController : Controller { private readonly IClientesRepository _clientesRepo; + private readonly ITraficosClientesService _traficosClientesService; - public ClientesController(IClientesRepository clientesRepo) + public ClientesController(IClientesRepository clientesRepo, ITraficosClientesService traficosClientesService) { _clientesRepo = clientesRepo; + _traficosClientesService = traficosClientesService; } [Route("getAllClientes")] @@ -105,5 +108,21 @@ namespace CORRESPONSALBackend.Controllers var entrada = await _clientesRepo.GetTraficos(data); return entrada; } + + [HttpGet("Excel")] + public async Task GetExcel([FromQuery] DTOFiltrosTraficosClientes data){ + try{ + var traficos = await _clientesRepo.GetTraficos(data); + var stream = await _traficosClientesService.GetExcel(traficos); + if(stream.Length > 0){ + stream.Position = 0; + return File(stream,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", $"Reporte_Operaciones.xlsx"); + }else{ + throw new Exception("Ocurrio un error al generar el reporte, intentelo de nuevo"); + } + }catch(Exception ex){ + return BadRequest(ex.Message); + } + } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 7ba5390..1304b84 100644 --- a/Program.cs +++ b/Program.cs @@ -50,6 +50,8 @@ using CORRESPONSALBackend.Services.Reportes.Alen; using CORRESPONSALBackend.Contracts.Reportes.CuentasCerradas; using CORRESPONSALBackend.Repository.Reportes.CuentasCerradas; using CORRESPONSALBackend.Services.Reportes.CuentasCerradas; +using CORRESPONSALBackend.Contracts.Reportes; +using CORRESPONSALBackend.Services.Reportes; var builder = WebApplication.CreateBuilder(args); @@ -110,6 +112,7 @@ builder.Services.AddScoped( builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); //Utilerias builder.Services.AddScoped(); diff --git a/Services/Reportes/TraficosClientesService.cs b/Services/Reportes/TraficosClientesService.cs new file mode 100644 index 0000000..3995100 --- /dev/null +++ b/Services/Reportes/TraficosClientesService.cs @@ -0,0 +1,63 @@ +using CORRESPONSALBackend.Contracts.Reportes; +using CORRESPONSALBackend.DTO.Corresponsales; +using OfficeOpenXml; +using OfficeOpenXml.Table; + +namespace CORRESPONSALBackend.Services.Reportes{ + public class TraficosClientesService : ITraficosClientesService{ + public TraficosClientesService(){ + + } + + public async Task GetExcel(IEnumerable traficos) + { + try{ + ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + using (ExcelPackage excelPackage = new ExcelPackage()){ + var worksheet = excelPackage.Workbook.Worksheets.Add("sheet1"); + worksheet.Cells["A1"].Value="Estado"; + worksheet.Cells["B1"].Value="Trafico"; + worksheet.Cells["C1"].Value="Fecha Registro"; + worksheet.Cells["D1"].Value="Usuario"; + worksheet.Cells["E1"].Value="Cliente"; + worksheet.Cells["F1"].Value="Tipo Operación"; + worksheet.Cells["G1"].Value="Estado Operación"; + worksheet.Cells["H1"].Value="Patente"; + worksheet.Cells["I1"].Value="Aduana"; + worksheet.Cells["J1"].Value="Pedimento"; + worksheet.Cells["K1"].Value="Fecha Pago"; + worksheet.Cells["L1"].Value="Fecha Desaduanado"; + worksheet.Cells["M1"].Value="Fecha Ultima Actualización"; + int currentRow=2; + foreach(var trafico in traficos){ + worksheet.Cells[$"A{currentRow}"].Value = trafico.Proceso == 1 ? "Por Terminar" : trafico.Proceso == 2 || trafico.Proceso == 3 ? "Despachado sin Facturar" : "Despachado y Facturado"; + worksheet.Cells[$"B{currentRow}"].Value = trafico.Trafico; + worksheet.Cells[$"C{currentRow}"].Value = trafico.FechaRegistro; + worksheet.Cells[$"D{currentRow}"].Value = trafico.sUsuario; + worksheet.Cells[$"E{currentRow}"].Value = trafico.sCliente; + worksheet.Cells[$"F{currentRow}"].Value = trafico.sTipoOperacion; + worksheet.Cells[$"G{currentRow}"].Value = trafico.sEstatus; + worksheet.Cells[$"H{currentRow}"].Value = trafico.Patente; + worksheet.Cells[$"I{currentRow}"].Value = trafico.Aduana; + worksheet.Cells[$"J{currentRow}"].Value = trafico.Pedimento; + worksheet.Cells[$"K{currentRow}"].Value = trafico.FechaPago; + worksheet.Cells[$"L{currentRow}"].Value = trafico.FechaDesaduanamiento; + worksheet.Cells[$"M{currentRow}"].Value = trafico.UltimaActualizacion; + currentRow++; + } + ExcelRange range = worksheet.Cells[1, 1,traficos.Count() + 1, 13]; + ExcelTable tab = worksheet.Tables.Add(range, "Table1"); + tab.TableStyle = TableStyles.Light1; + worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); + excelPackage.Workbook.Calculate(); + var stream = new MemoryStream(); + excelPackage.SaveAs(stream); + stream.Position = 0; + return stream; + } + }catch(Exception ex){ + throw new Exception(ex.Message); + } + } + } +} \ No newline at end of file