diff --git a/src/Armory.Api/Controllers/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/WarMaterialAndSpecialEquipmentAssignmentFormatsController.cs b/src/Armory.Api/Controllers/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/WarMaterialAndSpecialEquipmentAssignmentFormatsController.cs index a07fd1cd..97ebf811 100644 --- a/src/Armory.Api/Controllers/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/WarMaterialAndSpecialEquipmentAssignmentFormatsController.cs +++ b/src/Armory.Api/Controllers/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/WarMaterialAndSpecialEquipmentAssignmentFormatsController.cs @@ -44,5 +44,15 @@ public async Task> RegisterWarMaterialAndSpecialEquipme return BadRequest(); } } + + [HttpGet] + public async Task> Get() + { + var stream = + await _mediator.Send(new GenerateWarMaterialAndSpecialEquipmentAssignmentFormatQuery(1)); + + stream.Position = 0; + return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "format.xlsx"); + } } } diff --git a/src/Armory/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/Application/Generate/WarMaterialAndSpecialEquipmentAssignmentFormatGenerator.cs b/src/Armory/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/Application/Generate/WarMaterialAndSpecialEquipmentAssignmentFormatGenerator.cs index fdaab105..1d16d1a2 100644 --- a/src/Armory/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/Application/Generate/WarMaterialAndSpecialEquipmentAssignmentFormatGenerator.cs +++ b/src/Armory/Formats/WarMaterialAndSpecialEquipmentAssignmentFormats/Application/Generate/WarMaterialAndSpecialEquipmentAssignmentFormatGenerator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Armory.Formats.Shared.Domain; using Armory.Formats.WarMaterialAndSpecialEquipmentAssignmentFormats.Domain; using Armory.Shared.Domain.ClosedXML; @@ -13,6 +14,7 @@ public class WarMaterialAndSpecialEquipmentAssignmentFormatGenerator { private const string FormatName = "FORMATO ASIGNACIÓN MATERIAL DE GUERRA Y EQUIPO ESPECIAL"; private const string FormatTitle = "FUERZA AÉREA COLOMBIANA"; + private static readonly XLColor HeaderColor = XLColor.FromHtml("#FFFF99"); private static readonly List WeaponsAndAmmunitionHeader = new() { @@ -20,6 +22,16 @@ public class WarMaterialAndSpecialEquipmentAssignmentFormatGenerator "TIPO DE MUNICIÓN", "CALIBRE", "MARCA", "LOTE", "CANTIDAD DE MUNICIÓN" }; + private static readonly List SpecialEquipmentsHeader = new() + { + "ÍTEM", "TIPO", "MODELO", "SERIE", "CANTIDAD" + }; + + private static readonly List ExplosivesHeader = new() + { + "TIPO DE MUNICIÓN", "CALIBRE", "MARCA", "LOTE", "No. SERIE", "CANTIDAD" + }; + private readonly IHostingEnvironment _environment; private readonly IWorksheetManager _worksheetManager; @@ -91,7 +103,7 @@ private void MakeWorksheetMainInfo(IXLWorksheet worksheet, private void MakeWeaponsAndAmmunitionHeader(IXLWorksheet worksheet) { _worksheetManager.SetCommonRangeStyles(worksheet.Range("A19:M20")); - _worksheetManager.SetRangeFillBackgroundColor(worksheet.Range("A19:M20"), XLColor.FromHtml("#FFFF99")); + _worksheetManager.SetRangeFillBackgroundColor(worksheet.Range("A19:M20"), HeaderColor); worksheet.Row(20).Height = 25; _worksheetManager.MergeRangeAndSetValue(worksheet.Range("A19:A20"), "ÍTEM"); @@ -108,9 +120,141 @@ private void MakeWeaponsAndAmmunitionHeader(IXLWorksheet worksheet) worksheet.Cell("H20").Style.Alignment.WrapText = true; } - private void MakeWeaponsAndAmmunitionInfo(IXLWorksheet worksheet, + private int MakeWeaponsAndAmmunitionInfo(IXLWorksheet worksheet, WarMaterialAndSpecialEquipmentAssignmentFormat format) { + const int start = 21; + for (var i = 0; i < format.Weapons.Count; i++) + { + var weapon = format.Weapons.ElementAt(i); + + _worksheetManager.SetRangeValues(worksheet.Range($"A{start + i}:H{start + i}"), + new List + { + (i + 1).ToString(), + weapon.Type, + weapon.Mark, + weapon.Model, + weapon.Caliber, + weapon.Code, + weapon.NumberOfProviders.ToString(), + weapon.ProviderCapacity.ToString() + }); + } + + for (var i = 0; i < format.Ammunition.Count; i++) + { + var ammunition = format.Weapons.ElementAt(i); + var formatAmmunition = + format.WarMaterialAndSpecialEquipmentAssignmentFormatAmmunition.First(x => + x.AmmunitionCode == ammunition.Code); + + _worksheetManager.SetRangeValues(worksheet.Range($"I{start + i}:M{start + i}"), + new List + { + ammunition.Type, + ammunition.Caliber, + ammunition.Mark, + ammunition.Lot, + formatAmmunition.Quantity.ToString() + }); + } + + var maxNumOfElements = Math.Max(format.Ammunition.Count, format.Weapons.Count); + var workedRange = worksheet.Range($"A21:M{start + maxNumOfElements - 1}"); + _worksheetManager.SetRangeBorders(workedRange, XLBorderStyleValues.Hair); + _worksheetManager.SetRangeFontName(workedRange, "Arial"); + _worksheetManager.SetRangeAlignment(workedRange, XLAlignmentHorizontalValues.Center, + XLAlignmentVerticalValues.Center); + + return start + maxNumOfElements; + } + + private void MakeSpecialEquipmentHeader(IXLWorksheet worksheet, int start) + { + var headerRange = worksheet.Range($"A{start}:E{start}"); + _worksheetManager.SetCommonRangeStyles(headerRange); + _worksheetManager.SetRangeFillBackgroundColor(headerRange, HeaderColor); + _worksheetManager.MergeRangeAndSetValue(headerRange, "EQUIPO ESPECIAL Y ACCESORIOS"); + + headerRange = worksheet.Range($"A{start + 1}:E{start + 1}"); + worksheet.Row(start + 1).Height = 25; + _worksheetManager.SetCommonRangeStyles(headerRange); + _worksheetManager.SetRangeFontSize(headerRange, 9); + _worksheetManager.SetRangeFillBackgroundColor(headerRange, HeaderColor); + _worksheetManager.SetRangeValues(headerRange, SpecialEquipmentsHeader); + } + + private void MakeExplosivesHeader(IXLWorksheet worksheet, int start) + { + var headerRange = worksheet.Range($"H{start}:M{start}"); + _worksheetManager.SetCommonRangeStyles(headerRange); + _worksheetManager.SetRangeFillBackgroundColor(headerRange, HeaderColor); + _worksheetManager.MergeRangeAndSetValue(headerRange, "GRANADAS Y EXPLOSIVOS"); + + headerRange = worksheet.Range($"H{start + 1}:M{start + 1}"); + worksheet.Row(start + 1).Height = 25; + _worksheetManager.SetCommonRangeStyles(headerRange); + _worksheetManager.SetRangeFontSize(headerRange, 9); + _worksheetManager.SetRangeFillBackgroundColor(headerRange, HeaderColor); + _worksheetManager.SetRangeValues(headerRange, ExplosivesHeader); + } + + private int MakeSpecialEquipmentAndExplosivesInfo(IXLWorksheet worksheet, + WarMaterialAndSpecialEquipmentAssignmentFormat format, int previousEnd) + { + var start = previousEnd; + for (var i = 0; i < format.Equipments.Count; i++) + { + var equipment = format.Equipments.ElementAt(i); + var equipmentFormat = + format.WarMaterialAndSpecialEquipmentAssignmentFormatEquipments.First(x => + x.EquipmentCode == equipment.Code); + + _worksheetManager.SetRangeValues(worksheet.Range($"A{start + i}:E{start + i}"), + new List + { + (i + 1).ToString(), + equipment.Type, + equipment.Model, + equipment.Series, + equipmentFormat.Quantity.ToString() + }); + } + + for (var i = 0; i < format.Explosives.Count; i++) + { + var explosive = format.Explosives.ElementAt(i); + var explosiveFormat = + format.WarMaterialAndSpecialEquipmentAssignmentFormatExplosives.First(x => + x.ExplosiveCode == explosive.Code); + + _worksheetManager.SetRangeValues(worksheet.Range($"H{start + i}:M{start + i}"), + new List + { + explosive.Type, + explosive.Caliber, + explosive.Mark, + explosive.Lot, + explosive.Series, + explosiveFormat.Quantity.ToString() + }); + } + + var maxNumOfElements = Math.Max(format.Explosives.Count, format.Equipments.Count); + var workedRange = worksheet.Range($"A{start}:E{start + maxNumOfElements - 1}"); + _worksheetManager.SetRangeBorders(workedRange, XLBorderStyleValues.Hair); + _worksheetManager.SetRangeFontName(workedRange, "Arial"); + _worksheetManager.SetRangeAlignment(workedRange, XLAlignmentHorizontalValues.Center, + XLAlignmentVerticalValues.Center); + + workedRange = worksheet.Range($"H{start}:M{start + maxNumOfElements - 1}"); + _worksheetManager.SetRangeBorders(workedRange, XLBorderStyleValues.Hair); + _worksheetManager.SetRangeFontName(workedRange, "Arial"); + _worksheetManager.SetRangeAlignment(workedRange, XLAlignmentHorizontalValues.Center, + XLAlignmentVerticalValues.Center); + + return start + maxNumOfElements; } public MemoryStream Generate(WarMaterialAndSpecialEquipmentAssignmentFormat format) @@ -121,7 +265,11 @@ public MemoryStream Generate(WarMaterialAndSpecialEquipmentAssignmentFormat form MakeWorksheetHeader(workSheet, format); MakeWorksheetMainInfo(workSheet, format); MakeWeaponsAndAmmunitionHeader(workSheet); - MakeWeaponsAndAmmunitionInfo(workSheet, format); + + var weaponAndAmmunitionEnd = MakeWeaponsAndAmmunitionInfo(workSheet, format); + MakeSpecialEquipmentHeader(workSheet, weaponAndAmmunitionEnd + 1); + MakeExplosivesHeader(workSheet, weaponAndAmmunitionEnd + 1); + MakeSpecialEquipmentAndExplosivesInfo(workSheet, format, weaponAndAmmunitionEnd + 3); var memoryStream = new MemoryStream(); workBook.SaveAs(memoryStream); diff --git a/src/Shared/Domain/ClosedXML/IWorksheetManager.cs b/src/Shared/Domain/ClosedXML/IWorksheetManager.cs index c114425d..0cf266d9 100644 --- a/src/Shared/Domain/ClosedXML/IWorksheetManager.cs +++ b/src/Shared/Domain/ClosedXML/IWorksheetManager.cs @@ -8,11 +8,18 @@ public interface IWorksheetManager void SetRowsHeight(IXLRows rows, float height); void SetColumnsWidth(IXLColumns columns, float width); void SetCommonRangeStyles(IXLRange range); - void MergeRange(IXLRange range); + + void SetRangeAlignment(IXLRange range, XLAlignmentHorizontalValues horizontal, + XLAlignmentVerticalValues vertical); + + void SetRangeOutsideBorder(IXLRange range, XLBorderStyleValues borderStyleValues); + void SetRangeInsideBorder(IXLRange range, XLBorderStyleValues borderStyleValues); + void SetRangeBorders(IXLRange range, XLBorderStyleValues borderStyleValues); void MergeRangeAndSetValue(IXLRange range, T value); void SetRangeFontBold(IXLRange range, bool bold); void SetRangeFontSize(IXLRange range, double fontSize); + void SetRangeFontName(IXLRange range, string fontName); void SetRangeFillBackgroundColor(IXLRange range, XLColor color); void SetRangeValues(IXLRange range, List values); } diff --git a/src/Shared/Infrastructure/ClosedXML/WorksheetManager.cs b/src/Shared/Infrastructure/ClosedXML/WorksheetManager.cs index 7f2fe4ea..4711d5f3 100644 --- a/src/Shared/Infrastructure/ClosedXML/WorksheetManager.cs +++ b/src/Shared/Infrastructure/ClosedXML/WorksheetManager.cs @@ -20,18 +20,34 @@ public void SetColumnsWidth(IXLColumns columns, float width) public void SetCommonRangeStyles(IXLRange range) { - range.Style.Border.OutsideBorder = XLBorderStyleValues.Hair; - range.Style.Border.InsideBorder = XLBorderStyleValues.Hair; - range.Style.Font.FontSize = 12; - range.Style.Font.FontName = "Arial"; + SetRangeBorders(range, XLBorderStyleValues.Hair); + SetRangeFontSize(range, 12); + SetRangeFontName(range, "Arial"); SetRangeFontBold(range, true); - range.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; - range.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; + SetRangeAlignment(range, XLAlignmentHorizontalValues.Center, XLAlignmentVerticalValues.Center); } - public void MergeRange(IXLRange range) + public void SetRangeAlignment(IXLRange range, XLAlignmentHorizontalValues horizontal, + XLAlignmentVerticalValues vertical) { - range.Merge(); + range.Style.Alignment.Horizontal = horizontal; + range.Style.Alignment.Vertical = vertical; + } + + public void SetRangeOutsideBorder(IXLRange range, XLBorderStyleValues borderStyleValues) + { + range.Style.Border.OutsideBorder = borderStyleValues; + } + + public void SetRangeInsideBorder(IXLRange range, XLBorderStyleValues borderStyleValues) + { + range.Style.Border.InsideBorder = borderStyleValues; + } + + public void SetRangeBorders(IXLRange range, XLBorderStyleValues borderStyleValues) + { + SetRangeOutsideBorder(range, borderStyleValues); + SetRangeInsideBorder(range, borderStyleValues); } public void MergeRangeAndSetValue(IXLRange range, T value) @@ -50,6 +66,11 @@ public void SetRangeFontSize(IXLRange range, double fontSize) range.Style.Font.FontSize = fontSize; } + public void SetRangeFontName(IXLRange range, string fontName) + { + range.Style.Font.FontName = fontName; + } + public void SetRangeFillBackgroundColor(IXLRange range, XLColor color) { range.Style.Fill.BackgroundColor = color; @@ -68,5 +89,10 @@ public void SetRangeValues(IXLRange range, List values) ++index; } } + + private static void MergeRange(IXLRangeBase range) + { + range.Merge(); + } } }