From 7f1d42b0a6bf2d12647008a6e0f5157d7c6326a7 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 29 May 2024 16:45:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BE=D1=82=D1=87=D1=91=D1=82=20=D0=B2=20=D0=BF=D0=B4=D1=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerHardwareStoreBusinessLogic.csproj | 1 + .../OfficePackage/AbstractSaveToPdf.cs | 156 ++++++++++++++++++ .../HelperEnums/PdfParagraphAlignmentType.cs | 11 ++ .../OfficePackage/HelperModels/PdfInfo.cs | 19 +++ .../HelperModels/PdfParagraph.cs | 13 ++ .../HelperModels/PdfRowParameters.cs | 13 ++ .../OfficePackage/Implements/SaveToPdf.cs | 117 +++++++++++++ .../ViewModels/ReportComponentsViewModel.cs | 11 ++ .../ReportPurchaseComponentViewModel.cs | 15 ++ .../ViewModels/ReportPurchaseViewModel.cs | 18 +- .../Controllers/HomeController.cs | 2 +- 11 files changed, 367 insertions(+), 9 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportComponentsViewModel.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseComponentViewModel.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj index 4247a2b..1c5425f 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj @@ -9,6 +9,7 @@ + diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs new file mode 100644 index 0000000..ab0c216 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -0,0 +1,156 @@ +using ComputerHardwareContracts.OfficePackage.HelperModels; +using ComputerHardwareStoreBusinessLogic.OfficePackage.HelperEnums; +using ComputerHardwareStoreBusinessLogic.OfficePackage.HelperModels; +using DocumentFormat.OpenXml.EMMA; +using MigraDoc.Rendering; + +namespace ComputerHardwareContracts.OfficePackage +{ + public abstract class AbstractSaveToPdf + { + public void GetPurchaseReportFile(PdfInfo info) + { + CreatePdf(info); + + CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + CreateParagraph(new PdfParagraph + { + Text = $"за период с {info.DateFrom.ToShortDateString()} " + + $"по {info.DateTo.ToShortDateString()}", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + CreateTable(new List { "3cm", "4cm", "3cm", "4cm", "4cm" }); + + CreateRow(new PdfRowParameters + { + Texts = new List { "Покупка", "Дата покупки", "Цена", "Комментарии", "Комплектующие" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + foreach (var record in info.ReportPurchases) + { + List comments = record.Comments; + List components = record.Components; + int recordHeight = Math.Max(comments.Count + 1, components.Count + 1); + for (int i = 0; i < recordHeight; i++) + { + List cellsData = new() { "", "", "", "", "" }; + if (i == 0) + { + cellsData[0] = record.Id.ToString(); + cellsData[1] = record.PurchaseDate.ToShortDateString(); + cellsData[2] = record.PurchaseSum.ToString("0.00") + " р."; + CreateRow(new PdfRowParameters + { + Texts = cellsData, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + continue; + } + int k = i - 1; + if (k < comments.Count) + { + cellsData[3] = comments[k]; + } + if (k < components.Count) + { + cellsData[4] = components[k]; + } + CreateRow(new PdfRowParameters + { + Texts = cellsData, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + } + CreateParagraph(new PdfParagraph { Text = $"Итого: {info.ReportPurchases.Sum(x => x.PurchaseSum)}\t", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Left }); + SavePdf(info); + } + + public void CreateComponentsReport(PdfInfo info) + { + CreatePdf(info); + CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); + CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); + + CreateTable(new List { "5cm", "5cm", "3cm" }); + + CreateRow(new PdfRowParameters + { + Texts = new List { "Комплектующее", "Товар/Сборка", "Количество" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + + foreach (var record in info.ReportComponents) + { + CreateRow(new PdfRowParameters + { + Texts = new List { record.ComponentName, "", "" }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + foreach (var goodOrBuild in record.GoodOrBuilds) + { + CreateRow(new PdfRowParameters + { + Texts = new List { "", goodOrBuild.Item1, goodOrBuild.Item2.ToString() }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + CreateRow(new PdfRowParameters + { + Texts = new List { "Итого", "", record.TotalCount.ToString() }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + + SavePdf(info); + } + + /// + /// Создание doc-файла + /// + /// + protected abstract void CreatePdf(PdfInfo info); + + /// + /// Создание параграфа с текстом + /// + /// + /// + protected abstract void CreateParagraph(PdfParagraph paragraph); + + /// + /// Создание таблицы + /// + /// + /// + protected abstract void CreateTable(List columns); + + /// + /// Создание и заполнение строки + /// + /// + protected abstract void CreateRow(PdfRowParameters rowParameters); + + /// + /// Сохранение файла + /// + /// + protected abstract void SavePdf(PdfInfo info); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..f9df197 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs @@ -0,0 +1,11 @@ +namespace ComputerHardwareStoreBusinessLogic.OfficePackage.HelperEnums +{ + public enum PdfParagraphAlignmentType + { + Center, + + Left, + + Rigth + } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs new file mode 100644 index 0000000..b385ea2 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -0,0 +1,19 @@ +using ComputerHardwareContracts.ViewModels; + +namespace ComputerHardwareContracts.OfficePackage.HelperModels +{ + public class PdfInfo + { + 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 List ReportComponents { get; set; } = new(); + + public List ReportPurchases { get; set; } = new(); + } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs new file mode 100644 index 0000000..cd097f2 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs @@ -0,0 +1,13 @@ +using ComputerHardwareStoreBusinessLogic.OfficePackage.HelperEnums; + +namespace ComputerHardwareStoreBusinessLogic.OfficePackage.HelperModels +{ + public class PdfParagraph + { + public string Text { get; set; } = string.Empty; + + public string Style { get; set; } = string.Empty; + + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs new file mode 100644 index 0000000..a592f82 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs @@ -0,0 +1,13 @@ +using ComputerHardwareStoreBusinessLogic.OfficePackage.HelperEnums; + +namespace ComputerHardwareStoreBusinessLogic.OfficePackage.HelperModels +{ + public class PdfRowParameters + { + public List Texts { get; set; } = new(); + + public string Style { get; set; } = string.Empty; + + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs new file mode 100644 index 0000000..26b86e4 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -0,0 +1,117 @@ +using ComputerHardwareContracts.OfficePackage; +using ComputerHardwareContracts.OfficePackage.HelperModels; +using ComputerHardwareStoreBusinessLogic.OfficePackage.HelperEnums; +using ComputerHardwareStoreBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; + +namespace ComputerHardwareStoreBusinessLogic.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.Rigth => 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 + }; + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportComponentsViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportComponentsViewModel.cs new file mode 100644 index 0000000..f994689 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportComponentsViewModel.cs @@ -0,0 +1,11 @@ +namespace ComputerHardwareContracts.ViewModels +{ + public class ReportComponentsViewModel + { + public string ComponentName { get; set; } = string.Empty; + + public int TotalCount { get; set; } + + public List> GoodOrBuilds { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseComponentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseComponentViewModel.cs new file mode 100644 index 0000000..badd778 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseComponentViewModel.cs @@ -0,0 +1,15 @@ +namespace HardwareShopContracts.ViewModels +{ + public class ReportPurchaseComponentViewModel + { + public int Id { get; set; } + + public List<(string Build, int count, List<(string Component, int count)>)> Builds { get; set; } = new(); + + public int TotalCount { get; set; } + + public double TotalCost { get; set; } + + + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseViewModel.cs index 43ee4b8..78b2df0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ReportPurchaseViewModel.cs @@ -1,13 +1,15 @@ -namespace ComputerHardwareStoreContracts.ViewModels +namespace ComputerHardwareContracts.ViewModels { - public class ReportPurchaseViewModel - { - public int Id { get; set; } + public class ReportPurchaseViewModel + { + public int Id { get; set; } - public List<(string Build, int count, List<(string Component, int count)>)> Builds { get; set; } = new(); + public DateTime PurchaseDate { get; set; } - public int TotalCount { get; set; } + public double PurchaseSum { get; set; } - public double TotalCost { get; set; } - } + public List Comments { get; set; } = new(); + + public List Components { get; set; } = new(); + } } diff --git a/ComputerHardwareStore/StoreKeeperClient/Controllers/HomeController.cs b/ComputerHardwareStore/StoreKeeperClient/Controllers/HomeController.cs index e05eafc..70b20c8 100644 --- a/ComputerHardwareStore/StoreKeeperClient/Controllers/HomeController.cs +++ b/ComputerHardwareStore/StoreKeeperClient/Controllers/HomeController.cs @@ -235,7 +235,7 @@ namespace StoreKeeperClient.Controllers public IActionResult Mails() { - if (APIClient.StoreKeeper == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); }