From 5b702639140e4190010e9a74c9b4879798206bc1 Mon Sep 17 00:00:00 2001 From: Felix Morales Date: Tue, 12 Dec 2023 17:43:06 -0600 Subject: [PATCH] feature, crear un excel con las facturas de un trafico --- .../ICorresponsaliasFacturasService.cs | 7 ++++ .../Corresponsalias/FacturasController.cs | 18 ++++++++- Program.cs | 3 ++ .../CorresponsaliasFacturasService.cs | 39 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 Contracts/Corresponsalias/Services/ICorresponsaliasFacturasService.cs create mode 100644 Services/Corresponsalias/Facturas/CorresponsaliasFacturasService.cs diff --git a/Contracts/Corresponsalias/Services/ICorresponsaliasFacturasService.cs b/Contracts/Corresponsalias/Services/ICorresponsaliasFacturasService.cs new file mode 100644 index 0000000..81f70aa --- /dev/null +++ b/Contracts/Corresponsalias/Services/ICorresponsaliasFacturasService.cs @@ -0,0 +1,7 @@ +using CORRESPONSALBackend.Models.Corresponsales; + +namespace CORRESPONSALBackend.Contracts.Corresponsalias.Services{ + public interface ICorresponsaliasFacturasService{ + public Task GetFacturasExcel(IEnumerable facturas, string folioGemco); + } +} \ No newline at end of file diff --git a/Controllers/Corresponsalias/FacturasController.cs b/Controllers/Corresponsalias/FacturasController.cs index f411a05..bee667d 100644 --- a/Controllers/Corresponsalias/FacturasController.cs +++ b/Controllers/Corresponsalias/FacturasController.cs @@ -1,5 +1,6 @@ using System.IO.Compression; using CORRESPONSALBackend.Contracts.Corresponsalias; +using CORRESPONSALBackend.Contracts.Corresponsalias.Services; using CORRESPONSALBackend.Models.Corresponsales; using CORRESPONSALBackend.Services.C1896; using Microsoft.AspNetCore.Authorization; @@ -13,11 +14,15 @@ namespace CORRESPONSALBackend.Controllers.Corresponsalias public class FacturasController : Controller { private readonly ICorresponsaliasFacturasRepository _Repo; + private readonly ICorresponsaliasFacturasService _Service; + private readonly ICorresponsaliasTraficosRepository _TraficosRepo; private readonly IConfiguration _config; - public FacturasController(ICorresponsaliasFacturasRepository Repo, IConfiguration config) + public FacturasController(ICorresponsaliasFacturasRepository Repo, IConfiguration config, ICorresponsaliasFacturasService Service, ICorresponsaliasTraficosRepository TraficosRepo) { _config = config; + _Service = Service; _Repo = Repo; + _TraficosRepo = TraficosRepo; } /// Corresponsales: Facturas @@ -53,5 +58,16 @@ namespace CORRESPONSALBackend.Controllers.Corresponsalias return Srv.ReadDataFromFile(@"c:\data\layout_omg_alen.txt"); } */ + [HttpGet("GetExcel")] + public async Task GetFacturasExcel([FromQuery] int idTrafico){ + try{ + var facturas = await _Repo.GetAll(idTrafico); + var trafico = await _TraficosRepo.Get(idTrafico); + var stream = await _Service.GetFacturasExcel(facturas, trafico.FolioGemco); + return File(stream, "application/octet-stream", $"Facturas.xlsx"); + }catch(Exception ex){ + return BadRequest(ex.Message); + } + } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index d95f511..3d9f1d5 100644 --- a/Program.cs +++ b/Program.cs @@ -42,6 +42,8 @@ using CORRESPONSALBackend.Services.Reportes.GC50; using CORRESPONSALBackend.Services.Reportes.Embarques; using CORRESPONSALBackend.Contracts.Reportes.Newell.Semanal; using CORRESPONSALBackend.Services.Reportes.Newell.Semanal; +using CORRESPONSALBackend.Contracts.Corresponsalias.Services; +using CORRESPONSALBackend.Services.Corresponsalias.Facturas; var builder = WebApplication.CreateBuilder(args); @@ -62,6 +64,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Corresponsalias builder.Services.AddScoped(); diff --git a/Services/Corresponsalias/Facturas/CorresponsaliasFacturasService.cs b/Services/Corresponsalias/Facturas/CorresponsaliasFacturasService.cs new file mode 100644 index 0000000..3a447bb --- /dev/null +++ b/Services/Corresponsalias/Facturas/CorresponsaliasFacturasService.cs @@ -0,0 +1,39 @@ +using CORRESPONSALBackend.Contracts.Corresponsalias.Services; +using CORRESPONSALBackend.Models.Corresponsales; +using OfficeOpenXml; +using OfficeOpenXml.Table; + +namespace CORRESPONSALBackend.Services.Corresponsalias.Facturas{ + public class CorresponsaliasFactuasService : ICorresponsaliasFacturasService + { + public async Task GetFacturasExcel(IEnumerable facturas, string folioGemco) + { + try{ + ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + using (ExcelPackage excelPackage = new ExcelPackage()){ + var worksheet = excelPackage.Workbook.Worksheets.Add("sheet1"); + worksheet.Cells["A1"].Value = "Referencia"; + worksheet.Cells["B1"].Value = "Factura"; + int currentRow = 2; + foreach(var factura in facturas){ + worksheet.Cells[$"A{currentRow}"].Value = folioGemco; + worksheet.Cells[$"B{currentRow}"].Value = factura.Factura; + currentRow++; + } + //Se crea la tabla en el rango de celdas donde se mostrara la inforamcion. + ExcelRange range = worksheet.Cells[1, 1,facturas.Count() + 1, 2]; + 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