diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs index 1281df7..19548c8 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ReportStorekeeperLogic.cs @@ -14,22 +14,22 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper private readonly IGoodStorage _goodStorage; - private readonly IUserStorage _userStorage; - private readonly AbstractSaveToExcel _saveToExcel; private readonly AbstractSaveToWord _saveToWord; + private readonly AbstractSaveToPdf _saveToPdf; + private readonly AbstractMailWorker _mailWorker; - public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage, AbstractMailWorker abstractMailWorker, IUserStorage userStorage) + public ReportStorekeeperLogic(IComponentStorage componentStorage, AbstractSaveToExcel abstractSaveToExcel, AbstractSaveToWord abstractSaveToWord, IGoodStorage goodStorage, AbstractMailWorker abstractMailWorker, AbstractSaveToPdf saveToPdf) { _componentStorage = componentStorage; _saveToExcel = abstractSaveToExcel; _saveToWord = abstractSaveToWord; _goodStorage = goodStorage; _mailWorker = abstractMailWorker; - _userStorage = userStorage; + _saveToPdf = saveToPdf; } public List GetBuildGood(List goods) { @@ -119,15 +119,24 @@ namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper public bool SendReportOnMail(ReportBindingModel model) { - var user = _userStorage.GetElement(new() { Id = model.UserId }); - if (user == null) return false; + model.FileName = "temp.pdf"; + _saveToPdf.CreateComponentsReport(new PdfInfo + { + FileName = model.FileName, + Title = "Отчет по комплектующим", + DateFrom = model.DateFrom, + DateTo = model.DateTo, + ReportComponents = GetComponents(model) + }); + byte[] file = File.ReadAllBytes(model.FileName); + File.Delete(model.FileName); _mailWorker.MailSendAsync(new() { - MailAddress = user.Email, + MailAddress = model.UserEmail, Subject = "Отчет по комплектующим", Text = $"Отчет по полученным вами комлектующим за период с {model.DateFrom} по {model.DateTo} в формате Pdf.", - //File = file + File = file }); return true; } diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs index 9bcadf7..87bada3 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs @@ -152,7 +152,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts /// public void SendByMailPurchaseReport(ReportBindingModel model) { - byte[] file = _saveToPdf.GetPurchaseReportFile(new() + _saveToPdf.GetPurchaseReportFile(new() { FileName = model.FileName, Title = "Отчет по покупкам", @@ -160,12 +160,15 @@ namespace HardwareShopContracts.BusinessLogicsContracts DateTo = model.DateTo, ReportPurchases = GetPurchase(model) }); + byte[] file = File.ReadAllBytes(model.FileName); + File.Delete(model.FileName); + _mailKitWorker.MailSendAsync(new() { MailAddress = model.UserEmail, Subject = "Отчет по покупкам", Text = $"За период с {model.DateFrom.ToShortDateString()} " + - $"по {model.DateTo.ToShortDateString()}.", + $"по {model.DateTo.ToShortDateString()}.", File = file }); } diff --git a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj index caa0fe4..b67de25 100644 --- a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj +++ b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj @@ -11,6 +11,7 @@ + diff --git a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index f5fefac..6ac6744 100644 --- a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -7,7 +7,7 @@ namespace HardwareShopBusinessLogic.OfficePackage { public abstract class AbstractSaveToPdf { - public byte[] GetPurchaseReportFile(PdfInfo info) + public void GetPurchaseReportFile(PdfInfo info) { CreatePdf(info); @@ -74,9 +74,51 @@ namespace HardwareShopBusinessLogic.OfficePackage } } - return GetFile(info); + 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-файла @@ -109,11 +151,5 @@ 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 8ab2ecf..7b6a4f7 100644 --- a/HardwareShop/HardwareShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs +++ b/HardwareShop/HardwareShopBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -4,6 +4,7 @@ using MigraDoc.DocumentObjectModel; using MigraDoc.DocumentObjectModel.Tables; using MigraDoc.Rendering; + namespace HardwareShopBusinessLogic.OfficePackage.Implements { public class SaveToPdf : AbstractSaveToPdf @@ -107,16 +108,9 @@ namespace HardwareShopBusinessLogic.OfficePackage.Implements { Document = _document }; + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); 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/HardwareShopClientApp/Controllers/StorekeeperController.cs b/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs index b989862..3485bfa 100644 --- a/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs +++ b/HardwareShop/HardwareShopClientApp/Controllers/StorekeeperController.cs @@ -440,6 +440,7 @@ namespace HardwareShopStorekeeperApp.Controllers throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } reportModel.UserId = APIClient.User.Id; + reportModel.UserEmail = APIClient.User.Email; APIClient.PostRequest("api/report/componentsreportsendonmail", reportModel); } } diff --git a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs index 3ca9f8a..656316c 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/ReportController.cs @@ -67,7 +67,6 @@ namespace HardwareShopRestApi.Controllers { try { - string subject = _reportStorekeeperLogic.SendReportOnMail(model); } catch (Exception ex)