diff --git a/Contracts/Corresponsalias/ICorresponsaliasTraficosRepository.cs b/Contracts/Corresponsalias/ICorresponsaliasTraficosRepository.cs index 78ada49..b85c54c 100644 --- a/Contracts/Corresponsalias/ICorresponsaliasTraficosRepository.cs +++ b/Contracts/Corresponsalias/ICorresponsaliasTraficosRepository.cs @@ -10,6 +10,7 @@ namespace CORRESPONSALBackend.Contracts.Corresponsalias { public Task Get(int id); public Task GetByPedimento(int pedimento); + public Task GetByReferencia(string referencia); public Task GetAll(int Mode); public Task> GetRectificaciones(int id); public Task AddRectificacion(int id); diff --git a/Contracts/Corresponsalias/Services/ICorresponsaliasTraficosService.cs b/Contracts/Corresponsalias/Services/ICorresponsaliasTraficosService.cs new file mode 100644 index 0000000..a6be5fe --- /dev/null +++ b/Contracts/Corresponsalias/Services/ICorresponsaliasTraficosService.cs @@ -0,0 +1,5 @@ +namespace CORRESPONSALBackend.Contracts.Corresponsalias.Services{ + public interface ICorresponsaliasTraficosService{ + public Task> ReadTxtFacturasAlen(int fileId); + } +} diff --git a/Contracts/Reportes/Alen/IReportesAlenService.cs b/Contracts/Reportes/Alen/IReportesAlenService.cs new file mode 100644 index 0000000..2fd51bd --- /dev/null +++ b/Contracts/Reportes/Alen/IReportesAlenService.cs @@ -0,0 +1,7 @@ +using CORRESPONSALBackend.Models.Corresponsales; + +namespace CORRESPONSALBackend.Contracts.Reportes.Alen{ + public interface IReportesAlenService{ + public Task GetRelacionFacturas(ITrafico trafico, IEnumerable facturas, IEnumerable contenedores); + } +} \ No newline at end of file diff --git a/Controllers/Corresponsalias/TraficosController.cs b/Controllers/Corresponsalias/TraficosController.cs index d42cfef..a982daf 100644 --- a/Controllers/Corresponsalias/TraficosController.cs +++ b/Controllers/Corresponsalias/TraficosController.cs @@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Mvc; using CORRESPONSALBackend.Services.C1896; using CORRESPONSALBackend.Repository.Corresponsalias; using CORRESPONSALBackend.DTO.Reportes; +using CORRESPONSALBackend.Contracts.Corresponsalias.Services; +using CORRESPONSALBackend.Contracts.Catalogos; namespace CORRESPONSALBackend.Controllers.Corresponsalias { @@ -19,14 +21,24 @@ namespace CORRESPONSALBackend.Controllers.Corresponsalias private readonly ICorresponsaliasTraficosRepository _Repo; private readonly ICorresponsaliasFacturasRepository _RepoFacturas; private readonly ICorresponsaliasContenedoresRepository _RepoContenedores; + private readonly ICorresponsaliasTraficosService _traficosService; + private readonly IProveedoresRepository _proveedoresRepository; + private readonly ICorresponsaliasFacturasRepository _facturasRepository; + private readonly ICorresponsaliasContenedoresRepository _contenedoresRepository; private readonly IConfiguration _config; - public TraficosController(ICorresponsaliasTraficosRepository Repo, ICorresponsaliasFacturasRepository RepoFacturas, ICorresponsaliasContenedoresRepository RepoContenedores, IConfiguration config) + public TraficosController(ICorresponsaliasTraficosRepository Repo, ICorresponsaliasFacturasRepository RepoFacturas, + ICorresponsaliasContenedoresRepository RepoContenedores, IConfiguration config, ICorresponsaliasTraficosService traficosService, + IProveedoresRepository proveedoresRepository, ICorresponsaliasFacturasRepository facturasRepository, ICorresponsaliasContenedoresRepository contenedoresRepository) { _config = config; _Repo = Repo; _RepoFacturas = RepoFacturas; _RepoContenedores = RepoContenedores; + _traficosService = traficosService; + _proveedoresRepository = proveedoresRepository; + _facturasRepository = facturasRepository; + _contenedoresRepository = contenedoresRepository; } /// Corresponsales: Traficos @@ -144,5 +156,60 @@ namespace CORRESPONSALBackend.Controllers.Corresponsalias var referencia = await _Repo.ValidaTraficoDuplicado(aduana, patente, pedimento, idTrafico); return referencia; } + + [HttpPost("Append/LayoutAlen")] + public async Task AppendFacturasContenedoresAlen([FromQuery] int idTrafico, [FromQuery] int fileId){ + try{ + var trafico = await _Repo.Get(idTrafico); + var proveedores = await _proveedoresRepository.GetAll(0); + proveedores.ToList(); + var facturas = await _traficosService.ReadTxtFacturasAlen(fileId); + { + trafico.Pedimento = int.Parse(facturas[0][6]); + trafico.FechaPago = DateTime.Parse(facturas[0][7]).ToString("yyyy/MM/dd"); + trafico.Clave = facturas[0][8]; + trafico.TipoCambio = double.Parse(facturas[0][15]); + trafico.CantidadFracciones = int.Parse(facturas[0][21]); + trafico.DescripcionMercancia = facturas[0][14]; + trafico.Observaciones = facturas[0][48]; + await _Repo.Append(trafico); + } + foreach(var factura in facturas){ + //Crear modelo de contenedor y asignar los datos en la factura actual + CorresponsalesContenedores nuevoContenedor = new CorresponsalesContenedores(){ + id = 0, + IdTrafico = idTrafico, + Contenedor = factura[32], + Semaforo = (byte?)(factura[45] == "VERDE" ? 2 : 1), + FSemaforo = factura[22] + }; + var contenedorGuardado = await _contenedoresRepository.Append(nuevoContenedor); + //Crear modelo de factura y asignar los datos en la factura actual + var proveedorFactura = proveedores.FirstOrDefault(p => p.Nombre == factura[10]); + CorresponsalFacturas nuevaFactura = new CorresponsalFacturas(){ + id = 0, + Factura = factura[13], + IdTrafico = idTrafico, + ValorFacturaDls = double.Parse(factura[16]), + Proveedor = proveedorFactura.id, + Pedido = factura[29], + FechaFactura = factura[7], + Activo = true, + Code = 0, + FolioGEMCO = "", + COVE = factura[50], + Entrega = factura[52], + Transporte = factura[51], + UUID = factura[33], + IdContenedor = contenedorGuardado.id + }; + await _facturasRepository.Append(nuevaFactura); + + } + return new OkObjectResult(new {trafico, facturas = await _facturasRepository.GetAll(idTrafico), contenedores = await _contenedoresRepository.GetAll(idTrafico)}); + }catch(Exception ex){ + return BadRequest(ex.Message); + } + } } } \ No newline at end of file diff --git a/Controllers/Reportes/ReportesAlenController.cs b/Controllers/Reportes/ReportesAlenController.cs new file mode 100644 index 0000000..1506ce3 --- /dev/null +++ b/Controllers/Reportes/ReportesAlenController.cs @@ -0,0 +1,42 @@ +using CORRESPONSALBackend.Contracts.Corresponsalias; +using CORRESPONSALBackend.Contracts.Reportes.Alen; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace CORRESPONSALBackend.Controllers.Reportes{ + [Authorize] + [Route("api/[controller]")] + [ApiController] + public class ReportesAlenController : ControllerBase{ + + private readonly ICorresponsaliasTraficosRepository _traficosRepository; + private readonly ICorresponsaliasFacturasRepository _facturasRepository; + private readonly ICorresponsaliasContenedoresRepository _contenedoresRepository; + private readonly IReportesAlenService _reportesAlenService; + public ReportesAlenController(ICorresponsaliasTraficosRepository traficosRepository, ICorresponsaliasFacturasRepository facturasRepository, + IReportesAlenService reportesAlenService, ICorresponsaliasContenedoresRepository contenedoresRepository){ + _traficosRepository = traficosRepository; + _facturasRepository = facturasRepository; + _contenedoresRepository = contenedoresRepository; + _reportesAlenService = reportesAlenService; + } + + [HttpGet("RelacionFacturas")] + public async Task GetRelacionFacturas([FromQuery] string referencia){ + try{ + var trafico = await _traficosRepository.GetByReferencia(referencia); + var facturas = await _facturasRepository.GetAll(trafico.id); + var contenedores = await _contenedoresRepository.GetAll(trafico.id); + var stream = await _reportesAlenService.GetRelacionFacturas(trafico, facturas, contenedores); + if(stream.Length > 0){ + stream.Position = 0; + return File(stream,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", $"RelacionFacturas.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/Models/Corresponsales/CorresponsalFacturas.cs b/Models/Corresponsales/CorresponsalFacturas.cs index 662e334..4f3b7e6 100644 --- a/Models/Corresponsales/CorresponsalFacturas.cs +++ b/Models/Corresponsales/CorresponsalFacturas.cs @@ -12,5 +12,10 @@ public Boolean? Activo { get; set; } = true; public int? Code { get; set; } = 0; public string? FolioGEMCO { get; set; } = null!; + public string? COVE {get;set;} = null!; + public string? Entrega {get;set;}=null!; + public string? Transporte {get;set;}=null!; + public string? UUID {get;set;}= null!; + public int? IdContenedor {get;set;} = 0; } } diff --git a/Program.cs b/Program.cs index 3d9f1d5..f1540be 100644 --- a/Program.cs +++ b/Program.cs @@ -44,6 +44,9 @@ using CORRESPONSALBackend.Contracts.Reportes.Newell.Semanal; using CORRESPONSALBackend.Services.Reportes.Newell.Semanal; using CORRESPONSALBackend.Contracts.Corresponsalias.Services; using CORRESPONSALBackend.Services.Corresponsalias.Facturas; +using CORRESPONSALBackend.Services.Corresponsalias.Traficos; +using CORRESPONSALBackend.Contracts.Reportes.Alen; +using CORRESPONSALBackend.Services.Reportes.Alen; var builder = WebApplication.CreateBuilder(args); @@ -65,6 +68,8 @@ builder.Services.AddScoped( builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); // Corresponsalias builder.Services.AddScoped(); diff --git a/Repository/Corresponsalias/CorresponsaliasFacturasRepository.cs b/Repository/Corresponsalias/CorresponsaliasFacturasRepository.cs index ca471b7..52f5dc9 100644 --- a/Repository/Corresponsalias/CorresponsaliasFacturasRepository.cs +++ b/Repository/Corresponsalias/CorresponsaliasFacturasRepository.cs @@ -36,7 +36,12 @@ namespace CORRESPONSALBackend.Repository.Corresponsalias data.Proveedor, data.ValorFacturaDls, data.Pedido, - data.FechaFactura + data.FechaFactura, + data.UUID, + data.COVE, + data.Entrega, + data.Transporte, + data.IdContenedor }, commandType: CommandType.StoredProcedure); return entrada.First(); diff --git a/Repository/Corresponsalias/CorresponsaliasTraficosRepository.cs b/Repository/Corresponsalias/CorresponsaliasTraficosRepository.cs index 8ffd61c..aca37c0 100644 --- a/Repository/Corresponsalias/CorresponsaliasTraficosRepository.cs +++ b/Repository/Corresponsalias/CorresponsaliasTraficosRepository.cs @@ -51,6 +51,18 @@ namespace CORRESPONSALBackend.Repository.Corresponsalias return entrada.FirstOrDefault(new DTOReporteSemanalNewell { }); } + public async Task GetByReferencia(string referencia) + { + var query = "[Corresponsales.Trafico.GetByReferencia]"; + using var connection = _context.CreateConnection(); + var entrada = await connection.QueryAsync(query, new + { + @referencia=referencia + }, + commandType: CommandType.StoredProcedure); + return entrada.FirstOrDefault(new ITrafico { }); + } + public async Task> GetRectificaciones(int id) { var query = "[Corresponsales.Trafico.Rectificacion.GetAll]"; diff --git a/Services/Corresponsalias/Traficos/CorresponsaliasTraficosService.cs b/Services/Corresponsalias/Traficos/CorresponsaliasTraficosService.cs new file mode 100644 index 0000000..24689c2 --- /dev/null +++ b/Services/Corresponsalias/Traficos/CorresponsaliasTraficosService.cs @@ -0,0 +1,43 @@ +using CORRESPONSALBackend.Contracts.Corresponsalias.Services; +using CORRESPONSALBackend.Contracts.Utils; +using CORRESPONSALBackend.Models.Utils; + +namespace CORRESPONSALBackend.Services.Corresponsalias.Traficos{ + public class CorresponsaliasTraficosService : ICorresponsaliasTraficosService + { + private readonly IFileManagerRepository _fileManagerRepo; + private readonly IFilePaths4ProcessRepository _RepoRelativePath; + private readonly IConfiguration _config; + private readonly string RootPathCorresponsales; + + public CorresponsaliasTraficosService(IFileManagerRepository fileManagerRepo, IFilePaths4ProcessRepository RepoRelativePath, IConfiguration config){ + _fileManagerRepo = fileManagerRepo; + _RepoRelativePath = RepoRelativePath; + _config = config; + RootPathCorresponsales = _config.GetValue("AllFiles"); + } + public async Task> ReadTxtFacturasAlen(int fileId) + { + FileManager recFound = await _fileManagerRepo.getFileById(fileId); + FilePaths4Process RelativePath = await _RepoRelativePath.getPaths4ProcessById(recFound.Proceso); + string filePath = RootPathCorresponsales + RelativePath.Path + recFound.NombreArchivo; + if(System.IO.File.Exists(filePath)){ + try{ + var fileContent = File.ReadAllText(filePath); + string[] GCs = fileContent.Split("\r\n"); + List facturas = new List(); + foreach(var gc in GCs){ + facturas.Add(gc.Split('|')); + } + File.Delete(filePath); + return facturas; + }catch(Exception ex){ + File.Delete(filePath); + throw new Exception(ex.Message); + } + }else{ + return new List(); + } + } + } +} \ No newline at end of file diff --git a/Services/Reportes/Alen/ReportesAlenService.cs b/Services/Reportes/Alen/ReportesAlenService.cs new file mode 100644 index 0000000..136103e --- /dev/null +++ b/Services/Reportes/Alen/ReportesAlenService.cs @@ -0,0 +1,68 @@ +using CORRESPONSALBackend.Contracts.Reportes.Alen; +using CORRESPONSALBackend.Models.Corresponsales; +using OfficeOpenXml; +using OfficeOpenXml.Table; + +namespace CORRESPONSALBackend.Services.Reportes.Alen{ + public class ReportesAlenService : IReportesAlenService + { + public async Task GetRelacionFacturas(ITrafico trafico, IEnumerable facturas, IEnumerable contenedores) + { + try{ + ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + using (ExcelPackage excelPackage = new ExcelPackage()){ + var worksheet = excelPackage.Workbook.Worksheets.Add("sheet1"); + worksheet.Cells["A1"].Value = "Numero de Factura"; + worksheet.Cells["B1"].Value = "Folio Fiscal"; + worksheet.Cells["C1"].Value = "Fecha de Factura"; + worksheet.Cells["D1"].Value = "Numero de COVE"; + worksheet.Cells["E1"].Value = "Valor Factura"; + worksheet.Cells["F1"].Value = "Entrega"; + worksheet.Cells["G1"].Value = "Transporte"; + worksheet.Cells["H1"].Value = "Pedimento"; + worksheet.Cells["I1"].Value = "Fecha-Hora Cruce"; + worksheet.Cells["J1"].Value = "Fecha Cuenta Corresponsal"; + worksheet.Cells["K1"].Value = "Honorarios"; + worksheet.Cells["L1"].Value = "Maniobras"; + worksheet.Cells["M1"].Value = "Validacion"; + worksheet.Cells["N1"].Value = "Prevalidacion"; + worksheet.Cells["O1"].Value = "IVA"; + worksheet.Cells["P1"].Value = "Total M.N. por Operacion"; + int currentRow = 2; + foreach(var factura in facturas){ + var fechaCruce = factura.IdContenedor != null && factura.IdContenedor != 0 ? contenedores.FirstOrDefault(x => x.id == factura.IdContenedor).FSemaforo : ""; + worksheet.Cells[$"A{currentRow}"].Value = factura.Factura; + worksheet.Cells[$"B{currentRow}"].Value = factura.UUID; + worksheet.Cells[$"C{currentRow}"].Value = factura.FechaFactura; + worksheet.Cells[$"D{currentRow}"].Value = factura.COVE; + worksheet.Cells[$"E{currentRow}"].Value = factura.ValorFacturaDls; + worksheet.Cells[$"F{currentRow}"].Value = factura.Entrega; + worksheet.Cells[$"G{currentRow}"].Value = factura.Transporte; + worksheet.Cells[$"H{currentRow}"].Value = trafico.Pedimento; + worksheet.Cells[$"I{currentRow}"].Value = fechaCruce; + worksheet.Cells[$"J{currentRow}"].Value = trafico.FechaCuenta; + worksheet.Cells[$"K{currentRow}"].Value = "$0.00"; + worksheet.Cells[$"L{currentRow}"].Value = "$0.00"; + worksheet.Cells[$"M{currentRow}"].Value = "$0.00"; + worksheet.Cells[$"N{currentRow}"].Value = "$0.00"; + worksheet.Cells[$"O{currentRow}"].Value = "$0.00"; + worksheet.Cells[$"P{currentRow}"].Value = "$0.00"; + currentRow++; + } + //Se crea la tabla en el rango de celdas donde se mostrara la inforamcion. + ExcelRange range = worksheet.Cells[1, 1,facturas.Count() + 1, 16]; + 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