diff --git a/BusinessLogic/BusinessLogic.csproj b/BusinessLogic/BusinessLogic.csproj index 832c28f..c4585ce 100644 --- a/BusinessLogic/BusinessLogic.csproj +++ b/BusinessLogic/BusinessLogic.csproj @@ -14,6 +14,7 @@ + diff --git a/BusinessLogic/BusinessLogic/ReportLogic.cs b/BusinessLogic/BusinessLogic/ReportLogic.cs new file mode 100644 index 0000000..652fb2f --- /dev/null +++ b/BusinessLogic/BusinessLogic/ReportLogic.cs @@ -0,0 +1,74 @@ +using BusinessLogic.OfficePackage.HelperModels; +using BusinessLogic.OfficePackage; +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using Contracts.SearchModels; + +namespace BusinessLogic.BusinessLogic +{ + public class ReportLogic : IReportLogic + { + private readonly IProductStorage _productStorage; + private readonly ISupplyStorage _supplyStorage; + private readonly AbstractSaveToPdf _saveToPdf; + public ReportLogic(IProductStorage productStorage, ISupplyStorage supplyStorage, AbstractSaveToPdf saveToPdf) + { + _productStorage = productStorage; + _supplyStorage = supplyStorage; + _saveToPdf = saveToPdf; + } + /// + /// Получение списка компонент с указанием, в каких изделиях используются + /// + /// + public List GetSupplyProduct() + { + var products = _productStorage.GetFullList(); + var supplies = _supplyStorage.GetFullList(); + var list = new List(); + foreach (var supply in supplies) + { + var record = new ReportSupplyProductViewModel + { + SupplyName = supply.Name, + Products = new List>(), + TotalCount = 0 + }; + foreach (var product in products) + { + if (supply.Products.ContainsKey(product.Id)) + { + record.Products.Add(new Tuple(product.Name, supply.Products[product.Id].Item2)); + record.TotalCount += supply.Products[product.Id].Item2; + } + } + list.Add(record); + } + return list; + } + /// + /// Сохранение заказов в файл-Pdf + /// + /// + public void SaveSuppliesToPdfFile(ReportBindingModel model) + { + _saveToPdf.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Отчёт о поставке", + Date = model.Date!.Value, + Supply = _supplyStorage.GetElement(new SupplySearchModel() + { + Id = model.SupplyId, + }) + }); + } + } +} diff --git a/BusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/BusinessLogic/OfficePackage/AbstractSaveToPdf.cs index 4be3403..b634b0e 100644 --- a/BusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/BusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -1,4 +1,5 @@ -using BusinessLogic.OfficePackage.HelperEnums; +using BusinessLogic.BusinessLogic; +using BusinessLogic.OfficePackage.HelperEnums; using BusinessLogic.OfficePackage.HelperModels; using System; using System.Collections.Generic; @@ -10,6 +11,7 @@ namespace BusinessLogic.OfficePackage { public abstract class AbstractSaveToPdf { + public BarcodeLogic BarcodeLogic { get; set; } = new BarcodeLogic(); public void CreateDoc(PdfInfo info) { CreatePdf(info); @@ -19,12 +21,6 @@ namespace BusinessLogic.OfficePackage Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - CreateParagraph(new PdfParagraph - { - Text = $"с{info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", - Style = "Normal", - ParagraphAlignment = PdfParagraphAlignmentType.Center - }); CreateTable(new List { "2cm", "3cm", "6cm", "4cm" }); CreateRow(new PdfRowParameters { diff --git a/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs index 2ef7778..d283468 100644 --- a/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs +++ b/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -11,8 +11,7 @@ namespace BusinessLogic.OfficePackage.HelperModels { public string FileName { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; - public DateTime DateFrom { get; set; } - public DateTime DateTo { get; set; } + public DateTime Date { get; set; } public SupplyViewModel Supply { get; set; } = new(); } } diff --git a/BusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/BusinessLogic/OfficePackage/Implements/SaveToPdf.cs new file mode 100644 index 0000000..ff2e318 --- /dev/null +++ b/BusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -0,0 +1,101 @@ +using BusinessLogic.OfficePackage.HelperEnums; +using BusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; + +namespace BusinessLogic.OfficePackage.Implements +{ + public class SaveToPdf : AbstractSaveToPdf + { + private Document? _document; + private Section? _section; + private Table? _table; + private static ParagraphAlignment + GetParagraphAlignment(PdfParagraphAlignmentType type) + { + return type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Right => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + /// + /// Создание стилей для документа + /// + /// + private static void DefineStyles(Document document) + { + var style = document.Styles["Normal"]; + style.Font.Name = "Times New Roman"; + style.Font.Size = 14; + style = document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + } + protected override void CreatePdf(PdfInfo info) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + } + protected override void CreateParagraph(PdfParagraph pdfParagraph) + { + if (_section == null) + { + return; + } + var paragraph = _section.AddParagraph(pdfParagraph.Text); + paragraph.Format.SpaceAfter = "1cm"; + paragraph.Format.Alignment = + GetParagraphAlignment(pdfParagraph.ParagraphAlignment); + paragraph.Style = pdfParagraph.Style; + } + protected override void CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + protected override void CreateRow(PdfRowParameters rowParameters) + { + if (_table == null) + { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Texts.Count; ++i) + { + row.Cells[i].AddParagraph(rowParameters.Texts[i]); + if (!string.IsNullOrEmpty(rowParameters.Style)) + { + row.Cells[i].Style = rowParameters.Style; + } + Unit borderWidth = 0.5; + row.Cells[i].Borders.Left.Width = borderWidth; + row.Cells[i].Borders.Right.Width = borderWidth; + row.Cells[i].Borders.Top.Width = borderWidth; + row.Cells[i].Borders.Bottom.Width = borderWidth; + row.Cells[i].Format.Alignment = + GetParagraphAlignment(rowParameters.ParagraphAlignment); + row.Cells[i].VerticalAlignment = VerticalAlignment.Center; + } + } + protected override void SavePdf(PdfInfo info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } +} diff --git a/Contracts/BindingModels/ReportBindingModel.cs b/Contracts/BindingModels/ReportBindingModel.cs new file mode 100644 index 0000000..0a77572 --- /dev/null +++ b/Contracts/BindingModels/ReportBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class ReportBindingModel + { + public string FileName { get; set; } = string.Empty; + public DateTime? Date { get; set; } + public Guid SupplyId { get; set; } + } +} diff --git a/Contracts/BusinessLogicContracts/IReportLogic.cs b/Contracts/BusinessLogicContracts/IReportLogic.cs new file mode 100644 index 0000000..c0707be --- /dev/null +++ b/Contracts/BusinessLogicContracts/IReportLogic.cs @@ -0,0 +1,24 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface IReportLogic + { + /// + /// Получение списка компонент с указанием, в каких изделиях используются + /// + /// + List GetSupplyProduct(); + /// + /// Сохранение заказов в файл-Pdf + /// + /// + void SaveSuppliesToPdfFile(ReportBindingModel model); + } +} diff --git a/Contracts/ViewModels/ReportSupplyProductViewModel.cs b/Contracts/ViewModels/ReportSupplyProductViewModel.cs new file mode 100644 index 0000000..fcf1ba2 --- /dev/null +++ b/Contracts/ViewModels/ReportSupplyProductViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class ReportSupplyProductViewModel + { + public string SupplyName { get; set; } = string.Empty; + public int TotalCount { get; set; } + public List> Products { get; set; } = new(); + } +} diff --git a/Contracts/ViewModels/ReportSupplyViewModel.cs b/Contracts/ViewModels/ReportSupplyViewModel.cs new file mode 100644 index 0000000..b85c166 --- /dev/null +++ b/Contracts/ViewModels/ReportSupplyViewModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class ReportSupplyViewModel + { + public int Id { get; set; } + public DateTime Date { get; set; } + public string Name { get; set; } = string.Empty; + public double Price { get; set; } + public String Status { get; set; } = string.Empty; + } +} diff --git a/WinFormsApp/Program.cs b/WinFormsApp/Program.cs index 2cf562f..d830c81 100644 --- a/WinFormsApp/Program.cs +++ b/WinFormsApp/Program.cs @@ -6,6 +6,8 @@ using Contracts.StorageContracts; using DatabaseImplement.Implements; using Contracts.BusinessLogicContracts; using BusinessLogic.BusinessLogic; +using BusinessLogic.OfficePackage.Implements; +using BusinessLogic.OfficePackage; namespace WinFormsApp { @@ -45,6 +47,8 @@ namespace WinFormsApp services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient();