From b686555cf7de6d476e42f3bc62d1f32e3ee2ca02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Thu, 18 May 2023 18:01:28 +0400 Subject: [PATCH] pdf --- .../Worker/WorkerReportLogic.cs | 25 +++- .../OfficePackage/AbstractSaveToPdf.cs | 121 +++++++++++------- .../OfficePackage/Implements/SaveToPdf.cs | 8 ++ .../BindingModels/ReportBindingModel.cs | 4 +- .../IWorkerReportLogic.cs | 2 +- .../Controllers/ReportController.cs | 8 +- .../Controllers/HomeController.cs | 11 ++ .../Views/Home/WorkerReport.cshtml | 36 +++--- 8 files changed, 140 insertions(+), 75 deletions(-) diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs index 83c7e98..68915a3 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs @@ -23,15 +23,17 @@ namespace HardwareShopContracts.BusinessLogicsContracts private readonly AbstractSaveToWord _saveToWord; - private readonly AbstractSaveToWord _saveToPdf; + private readonly AbstractSaveToPdf _saveToPdf; + private readonly MailSender _mailSender; - public WorkerReportLogic(IPurchaseStorage purchaseStorage, IBuildStorage buildStorage, AbstractSaveToWord saveToPdf, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord) + public WorkerReportLogic(IPurchaseStorage purchaseStorage, MailSender mailSender, IBuildStorage buildStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord) { _purchaseStorage = purchaseStorage; _buildStorage = buildStorage; _saveToExcel = saveToExcel; _saveToWord = saveToWord; _saveToPdf = saveToPdf; + _mailSender = mailSender; } /// @@ -149,9 +151,24 @@ namespace HardwareShopContracts.BusinessLogicsContracts /// Сохранение отчёта по покупкам в файл-Pdf /// /// - public byte[] SendByMailPurchaseReport(ReportBindingModel model) + public void SendByMailPurchaseReport(ReportBindingModel model) { - throw new NotImplementedException(); + byte[] file = _saveToPdf.GetEquipmentReportFile(new() + { + FileName = model.FileName, + Title = "Отчет по покупкам", + DateFrom = model.DateFrom, + DateTo = model.DateTo, + ReportPurchases = GetPurchase(model) + }); + // _mailSender.SendMailAsync(new() + // { + // MailAddress = reportModel.UserEmail, + // Subject = "Отчет по покупкам", + // Text = $"За период с {reportModel.DateFrom.ToShortDateString()} " + + //$"по {reportModel.DateTo.ToShortDateString()}.", + // File = file + // }); } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index 46fc9c9..0c04751 100644 --- a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -1,65 +1,82 @@ -using HardwareShopBusinessLogic.OfficePackage.HelperEnums; +using DocumentFormat.OpenXml.EMMA; +using HardwareShopBusinessLogic.OfficePackage.HelperEnums; using HardwareShopBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.Rendering; namespace HardwareShopBusinessLogic.OfficePackage { public abstract class AbstractSaveToPdf { - //public void CreateDoc(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 }); + public byte[] GetEquipmentReportFile(PdfInfo info) + { + CreatePdf(info); - // CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "3cm" }); + CreateParagraph(new PdfParagraph + { + Text = info.Title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); - // CreateRow(new PdfRowParameters - // { - // Texts = new List { "Номер", "Дата заказа", "Блюдо", "Сумма", "Статус" }, - // Style = "NormalTitle", - // ParagraphAlignment = PdfParagraphAlignmentType.Center - // }); + CreateParagraph(new PdfParagraph + { + Text = $"за период с {info.DateFrom.ToShortDateString()} " + + $"по {info.DateTo.ToShortDateString()}", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); - // foreach (var order in info.Orders) - // { - // CreateRow(new PdfRowParameters - // { - // Texts = new List { order.Id.ToString(), order.DateCreate.ToShortDateString(), order.DishName, order.Sum.ToString(), order.OrderStatus }, - // Style = "Normal", - // ParagraphAlignment = PdfParagraphAlignmentType.Left - // }); - // } - // CreateParagraph(new PdfParagraph { Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Rigth }); + CreateTable(new List { "5cm", "5cm", "5cm" }); - // SavePdf(info); - //} + CreateRow(new PdfRowParameters + { + Texts = new List { "Комплектация", "Автомобили", "Работы" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); - //public void CreateOrdersGroupedByDateDoc(PdfInfo info) - //{ - // CreatePdf(info); - // CreateParagraph(new PdfParagraph { Text = info.Title, 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 + }); + } + } - // CreateTable(new List { "3cm", "3cm", "3cm" }); + return GetFile(info); + } - // CreateRow(new PdfRowParameters - // { - // Texts = new List { "Дата заказов", "Количество заказов", "Сумма" }, - // Style = "NormalTitle", - // ParagraphAlignment = PdfParagraphAlignmentType.Center - // }); - - // foreach (var order in info.OrdersGroupedByDate) - // { - // CreateRow(new PdfRowParameters - // { - // Texts = new List { order.DateCreate.ToShortDateString(), order.Count.ToString(), order.Sum.ToString() }, - // Style = "Normal", - // ParagraphAlignment = PdfParagraphAlignmentType.Left - // }); - // } - - // SavePdf(info); - //} /// /// Создание doc-файла @@ -92,5 +109,11 @@ namespace HardwareShopBusinessLogic.OfficePackage /// /// protected abstract void SavePdf(PdfInfo info); + + /// + /// Сохранение отправляем файл + /// + /// + protected abstract byte[] GetFile(PdfInfo info); } } diff --git a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs index 9a36ede..8ab2ecf 100644 --- a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs +++ b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -110,5 +110,13 @@ namespace HardwareShopBusinessLogic.OfficePackage.Implements renderer.RenderDocument(); renderer.PdfDocument.Save(info.FileName); } + + protected override byte[] GetFile(PdfInfo info) + { + SavePdf(info); + byte[] file = File.ReadAllBytes(info.FileName); + File.Delete(info.FileName); + return file; + } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs index 1bb0758..f501c97 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs @@ -4,9 +4,9 @@ { public string FileName { get; set; } = string.Empty; - public DateTime? DateFrom { get; set; } + public DateTime DateFrom { get; set; } - public DateTime? DateTo { get; set; } + public DateTime DateTo { get; set; } public int UserId { get; set; } } diff --git a/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IWorkerReportLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IWorkerReportLogic.cs index 4c66b07..920c70e 100644 --- a/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IWorkerReportLogic.cs +++ b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IWorkerReportLogic.cs @@ -35,6 +35,6 @@ namespace HardwareShopContracts.BusinessLogicsContracts /// Сохранение отчёта по покупкам в файл-Pdf /// /// - byte[] SendByMailPurchaseReport(ReportBindingModel model); + void SendByMailPurchaseReport(ReportBindingModel model); } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs index 37b3603..9ee95fc 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs @@ -89,6 +89,12 @@ namespace HardwareShopRestApi.Controllers throw; } } - } + + [HttpPost] + public void SendByMailPurchaseReport(ReportBindingModel reportModel) + { + _reportWorkerLogic.SendByMailPurchaseReport(reportModel); + } + } } diff --git a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs index 1578ee0..1110ed2 100644 --- a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs @@ -553,5 +553,16 @@ namespace HardwareShopWorkerApp.Controllers List? list = APIClient.PostRequestWithResult>("api/report/getpurchasereportdata", reportModel); return list; } + + [HttpPost] + public void SendByMailPurchaseReport([FromBody] ReportBindingModel reportModel) + { + if (APIClient.User == null) + { + return; + } + reportModel.UserId = APIClient.User.Id; + APIClient.PostRequest("api/report/SendByMailPurchaseReport", reportModel); + } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml index 2e803d2..1dbb85c 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml @@ -87,24 +87,24 @@ }); }); - //sendByMailButton.addEventListener("click", () => { - // const dateFrom = new Date(dateFromInput.value); - // const dateTo = new Date(dateToInput.value); - // const reportModel = { - // "DateFrom": dateFrom, - // "DateTo": dateTo - // }; - // if (!validate(reportModel)) { - // return; - // } - // $.ajax({ - // url: "/home/sendbymailepurchasereport", - // type: "POST", - // contentType: "application/json", - // data: JSON.stringify(reportModel) - // }).done(() => { - // }); - //}); + sendByMailButton.addEventListener("click", () => { + const dateFrom = new Date(dateFromInput.value); + const dateTo = new Date(dateToInput.value); + const reportModel = { + "DateFrom": dateFrom, + "DateTo": dateTo + }; + if (!validate(reportModel)) { + return; + } + $.ajax({ + url: "/home/sendbymailepurchasereport", + type: "POST", + contentType: "application/json", + data: JSON.stringify(reportModel) + }).done(() => { + }); + }); dateFromInput.addEventListener("input", () => { errorDivShell.style.gridTemplateRows = "0fr";