diff --git a/Controllers/LinesController.cs b/Controllers/LinesController.cs index ed8ce63..d9269d1 100644 --- a/Controllers/LinesController.cs +++ b/Controllers/LinesController.cs @@ -3,6 +3,7 @@ using line_gestao_api.Data; using line_gestao_api.Dtos; using line_gestao_api.Models; using line_gestao_api.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -503,6 +504,7 @@ namespace line_gestao_api.Controllers // ✅ 8. IMPORT EXCEL // ========================================================== [HttpPost("import-excel")] + [Authorize] [Consumes("multipart/form-data")] [RequestSizeLimit(50_000_000)] public async Task> ImportExcel([FromForm] ImportExcelForm form) diff --git a/Dtos/ParcelamentosDtos.cs b/Dtos/ParcelamentosDtos.cs index 53d73b6..a1238fa 100644 --- a/Dtos/ParcelamentosDtos.cs +++ b/Dtos/ParcelamentosDtos.cs @@ -1,6 +1,6 @@ namespace line_gestao_api.Dtos; -public sealed class ParcelamentoListDto +public class ParcelamentoListDto { public Guid Id { get; set; } public int? AnoRef { get; set; } diff --git a/Services/ParcelamentosImportService.cs b/Services/ParcelamentosImportService.cs index c1a5671..e1e7b13 100644 --- a/Services/ParcelamentosImportService.cs +++ b/Services/ParcelamentosImportService.cs @@ -57,18 +57,20 @@ public sealed class ParcelamentosImportService }; } - var yearRowIndex = headerRowIndex - 1; + var yearRowIndex = FindYearRowIndex(ws, headerRowIndex); var headerRow = ws.Row(headerRowIndex); var map = BuildHeaderMap(headerRow); var colLinha = GetCol(map, "LINHA"); + var colAnoRef = GetColAny(map, "ANO REF", "ANOREF", "ANO REF.", "ANO REFERENCIA", "ANO REFERÊNCIA"); + var colItem = GetCol(map, "ITEM"); var colCliente = GetCol(map, "CLIENTE"); var colQtParcelas = GetColAny(map, "QT PARCELAS", "QT. PARCELAS", "QT PARCELAS (NN/TT)", "QTDE PARCELAS"); var colValorCheio = GetColAny(map, "VALOR CHEIO"); var colDesconto = GetColAny(map, "DESCONTO"); var colValorComDesconto = GetColAny(map, "VALOR C/ DESCONTO", "VALOR COM DESCONTO"); - if (colLinha == 0 || colValorComDesconto == 0) + if (colLinha == 0 || colValorComDesconto == 0 || colAnoRef == 0 || colItem == 0) { return new ParcelamentosImportSummaryDto { @@ -77,7 +79,7 @@ public sealed class ParcelamentosImportService new ParcelamentosImportErrorDto { LinhaExcel = headerRowIndex, - Motivo = "Colunas obrigatórias não encontradas (LINHA / VALOR C/ DESCONTO)." + Motivo = "Colunas obrigatórias não encontradas (LINHA / ANO REF / ITEM / VALOR C/ DESCONTO)." } } }; @@ -99,15 +101,15 @@ public sealed class ParcelamentosImportService for (int row = headerRowIndex + 1; row <= lastRow; row++) { var linhaValue = GetCellString(ws, row, colLinha); - var itemStr = GetCellString(ws, row, 4); + var itemStr = GetCellString(ws, row, colItem); if (string.IsNullOrWhiteSpace(itemStr) && string.IsNullOrWhiteSpace(linhaValue)) { - break; + continue; } summary.Lidos++; - var anoRef = TryNullableInt(GetCellString(ws, row, 3)); + var anoRef = TryNullableInt(GetCellString(ws, row, colAnoRef)); var item = TryNullableInt(itemStr); if (!item.HasValue) @@ -127,7 +129,7 @@ public sealed class ParcelamentosImportService { LinhaExcel = row, Motivo = "AnoRef inválido ou vazio.", - Valor = GetCellString(ws, row, 3) + Valor = GetCellString(ws, row, colAnoRef) }); continue; } @@ -201,7 +203,12 @@ public sealed class ParcelamentosImportService private static IXLWorksheet? FindWorksheet(XLWorkbook wb) { return wb.Worksheets.FirstOrDefault(w => NormalizeHeader(w.Name) == NormalizeHeader("PARCELAMENTOS DE APARELHOS")) - ?? wb.Worksheets.FirstOrDefault(w => NormalizeHeader(w.Name) == NormalizeHeader("PARCELAMENTOS")); + ?? wb.Worksheets.FirstOrDefault(w => NormalizeHeader(w.Name) == NormalizeHeader("PARCELAMENTOS")) + ?? wb.Worksheets.FirstOrDefault(w => + { + var normalized = NormalizeHeader(w.Name); + return normalized.Contains("PARCELAMENTO") || normalized.Contains("PARCELAMENTOS") || normalized.Contains("PARECALEMENTO"); + }); } private static int FindHeaderRow(IXLWorksheet ws) @@ -260,6 +267,23 @@ public sealed class ParcelamentosImportService return yearMap; } + private static int FindYearRowIndex(IXLWorksheet ws, int headerRowIndex) + { + for (int row = headerRowIndex - 1; row >= Math.Max(1, headerRowIndex - 3); row--) + { + var rowCells = ws.Row(row).CellsUsed(); + foreach (var cell in rowCells) + { + if (int.TryParse(OnlyDigits(cell.GetString()), out var year) && year >= 2000 && year <= 2100) + { + return row; + } + } + } + + return 0; + } + private static List BuildMonthColumns(IXLRow headerRow, int startCol) { var lastCol = headerRow.LastCellUsed()?.Address.ColumnNumber ?? startCol;