using DocumentFormat.OpenXml.EMMA; using HardwareShopBusinessLogic.OfficePackage.HelperEnums; using HardwareShopBusinessLogic.OfficePackage.HelperModels; using MigraDoc.Rendering; namespace HardwareShopBusinessLogic.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 }); } } 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); } }