From 343788051b6aa85113323952e68e444b42a41e47 Mon Sep 17 00:00:00 2001 From: the Date: Wed, 24 May 2023 20:22:06 +0400 Subject: [PATCH] pdf 1 --- .../BusinessLogics/ReportLogic.cs | 32 +++++-- .../ComputerShopBusinessLogic.csproj | 1 + .../OfficePackage/AbstractSaveToPdf.cs | 44 ++++++++- .../HelperModels/PdfInfoProvider.cs | 18 ++++ .../HelperModels/PdfParagraph.cs | 16 ++++ .../HelperModels/PdfRowParameters.cs | 16 ++++ .../OfficePackage/Implements/SaveToPdf.cs | 94 ++++++++++++++++++- .../ReportPurchaseSupplyViewModel.cs | 7 +- 8 files changed, 211 insertions(+), 17 deletions(-) create mode 100644 ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfInfoProvider.cs create mode 100644 ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs create mode 100644 ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs index c3ff752..0f83ae5 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ReportLogic.cs @@ -55,7 +55,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics var receivings = new List(); - foreach(var supply in supplies) + foreach (var supply in supplies) { if (supply.ReceivingId.HasValue) { @@ -73,7 +73,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics { receivingnames.Add($"Id: {receiving.Id}. Status: {Enum.GetName(typeof(EquipmentReceivingStatus), receiving.Status)}."); } - if(receivingnames.Count > 0 && component != null) + if (receivingnames.Count > 0 && component != null) { result.Add(new() { @@ -82,7 +82,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics }); } } - + return result; } @@ -90,12 +90,12 @@ namespace ComputerShopBusinessLogic.BusinessLogics public void SaveReceivingComponentsToWordFile(ReportBindingModel model) { if (model.Ids != null) - _saveToWord.CreateDoc(new WordInfoProvider - { - FileName = model.FileName, - Title = "Список получений по компонентам", - ComponentReceivings = GetComponentReceivings(model.Ids) - }); + _saveToWord.CreateDoc(new WordInfoProvider + { + FileName = model.FileName, + Title = "Список получений по компонентам", + ComponentReceivings = GetComponentReceivings(model.Ids) + }); } public void SaveReceivingComponentsToXmlFile(ReportBindingModel model) @@ -109,6 +109,20 @@ namespace ComputerShopBusinessLogic.BusinessLogics }); } + public void SavePurchaseSuppliesToPdfFile(ReportBindingModel model) + { + if (model.DateFrom == null) + { + throw new ArgumentException("Дата начала не задана"); + } + + if (model.DateTo == null) + { + throw new ArgumentException("Дата окончания не задана"); + } + + } + public List GetPurchaseReceiving() { throw new NotImplementedException(); diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj b/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj index 0d03bb4..2c48cf0 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj +++ b/ComputerShopProvider/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj @@ -16,6 +16,7 @@ + diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index fb0a0ee..2d7eb0d 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -1,4 +1,6 @@ -using System; +using ComputerShopBusinessLogic.OfficePackage.HelperEnums; +using ComputerShopBusinessLogic.OfficePackage.HelperModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,45 @@ using System.Threading.Tasks; namespace ComputerShopBusinessLogic.OfficePackage { - public class AbstractSaveToPdf + public abstract class AbstractSaveToPdf { + public void CreateDoc(PdfInfoProvider 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 { "4cm", "5cm", "3cm", "4cm", "2cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "ID закупки", "Название компонента", "Дата создания закупки", "Дата создания поставки", "Статус поставки" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var supplyPurchase in info.SupplyPurchases) + { + CreateRow(new PdfRowParameters + { + Texts = new List { supplyPurchase.Purchase.Id.ToString(), supplyPurchase.Purchase.ComponentName, supplyPurchase.Purchase.DateCreate.ToString(), supplyPurchase.Supply.DateCreate.ToString(), supplyPurchase.Supply.Status.ToString()}, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + SavePdf(info); + } + protected abstract void CreatePdf(PdfInfoProvider info); + protected abstract void CreateParagraph(PdfParagraph paragraph); + protected abstract void CreateTable(List columns); + protected abstract void CreateRow(PdfRowParameters rowParameters); + protected abstract void SavePdf(PdfInfoProvider info); } } diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfInfoProvider.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfInfoProvider.cs new file mode 100644 index 0000000..3d7cd56 --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfInfoProvider.cs @@ -0,0 +1,18 @@ +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.OfficePackage.HelperModels +{ + public class PdfInfoProvider + { + public string FileName { get; set; } = "F:\\ReportsCourseWork\\pdffile.pdf"; + public string Title { get; set; } = string.Empty; + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + public List SupplyPurchases { get; set; } = new(); + } +} diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs new file mode 100644 index 0000000..ad0aea4 --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs @@ -0,0 +1,16 @@ +using ComputerShopBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.OfficePackage.HelperModels +{ + public class PdfParagraph + { + public string Text { get; set; } = string.Empty; + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs new file mode 100644 index 0000000..f302562 --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs @@ -0,0 +1,16 @@ +using ComputerShopBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.OfficePackage.HelperModels +{ + public class PdfRowParameters + { + public List Texts { get; set; } = new(); + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs index c4ed5b5..79b9135 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -1,4 +1,9 @@ -using System; +using ComputerShopBusinessLogic.OfficePackage.HelperEnums; +using ComputerShopBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +13,92 @@ namespace ComputerShopBusinessLogic.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 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 CreatePdf(PdfInfoProvider info) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + } + + 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 CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + + protected override void SavePdf(PdfInfoProvider info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } } } diff --git a/ComputerShopProvider/ComputerShopContracts/ViewModels/ReportPurchaseSupplyViewModel.cs b/ComputerShopProvider/ComputerShopContracts/ViewModels/ReportPurchaseSupplyViewModel.cs index efc842b..d7f26e3 100644 --- a/ComputerShopProvider/ComputerShopContracts/ViewModels/ReportPurchaseSupplyViewModel.cs +++ b/ComputerShopProvider/ComputerShopContracts/ViewModels/ReportPurchaseSupplyViewModel.cs @@ -8,10 +8,7 @@ namespace ComputerShopContracts.ViewModels { public class ReportPurchaseSupplyViewModel { - public int UserId { get; set; } - public int ComponentId { get; set; } - public string ComponentName { get; } = String.Empty; - public DateTime PurchaseDateCreating { get; set; } - public DateTime SupplyDateCreating { get; set; } + public PurchaseViewModel Purchase { get; set; } + public SupplyViewModel Supply { get; set; } } }