diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs index fb5f831..655e733 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs @@ -6,6 +6,9 @@ using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; +using MigraDoc.DocumentObjectModel; +using PdfSharp.Pdf; +using System.Collections.Generic; namespace ElectronicsShopBusinessLogic.BusinessLogic { @@ -14,20 +17,49 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic private readonly IPaymeantStorage _paymeantstorage; private readonly IProductStorage _productstorage; private readonly IOrderStorage _orderStorage; + private readonly ICostItemStorage _costItemStorage; private readonly AbstractSaveToExcelClient _saveToExcel; private readonly AbstractSaveToWordClient _saveToWord; + private readonly AbstractSaveToPdfClient _saveToPdf; public ReportClientLogic(AbstractSaveToExcelClient abstractSaveToExcelClient, AbstractSaveToWordClient abstractSaveToWordClient, - IPaymeantStorage paymeantStorage, IProductStorage productStorage, IOrderStorage orderStorage) { + IPaymeantStorage paymeantStorage, IProductStorage productStorage, IOrderStorage orderStorage, + AbstractSaveToPdfClient abstractSaveToPdfClient, ICostItemStorage costItemStorage) { _saveToExcel = abstractSaveToExcelClient; _saveToWord= abstractSaveToWordClient; _paymeantstorage = paymeantStorage; _productstorage = productStorage; _orderStorage = orderStorage; + _costItemStorage = costItemStorage; + _saveToPdf = abstractSaveToPdfClient; + } + + // Получение списка оплаченных товаров за период + public List? GetProducts (ReportBindingModel model) { + var paymeants = _paymeantstorage.GetFillteredList(new PaymeantSearchModel { + DateFrom = model.DateFrom, + DateTo = model.DateTo, + }); + + List? products = new(); + + foreach (var paymeant in paymeants) { + var order = _orderStorage.GetElement(new OrderSearchModel { ID = paymeant.OrderID }); + foreach (var product in order.ProductList) { + products.Add(new ReportProductsViewModel { + ID = product.Value.Item1.ID, + ProductName = product.Value.Item1.ProductName, + Price = product.Value.Item1.Price, + CostItemName = _costItemStorage.GetElement(new CostItemSearchModel { ID = product.Value.Item1.CostItemID }).Name + }); + } + } + + return products; } // получение списка оплат за период - public List GetPaymeants(ReportBindingModel model) + public List? GetPaymeants(ReportBindingModel model) { return _paymeantstorage.GetFillteredList(new PaymeantSearchModel { DateFrom = model.DateFrom, @@ -73,7 +105,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return list; } - public byte[] SavePaymeantToExcelFile(int _clientID) + public byte[]? SavePaymeantToExcelFile(int _clientID) { var document = _saveToExcel.CreateReport(new ExcelInfoClient { @@ -83,13 +115,24 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return document; } - public byte[] SavePaymeantToWordFile(int _clientID) + public byte[]? SavePaymeantToWordFile(int _clientID) { var document = _saveToWord.CreateDoc(new WordInfoClient { - Title = "Список оплат", + Title = "Список оплат и товаров", ListPaymeant = _paymeantstorage.GetFillteredList(new PaymeantSearchModel { ClientID = _clientID }), }); return document; } + + public PdfDocument SaveProductToPdfFile(ReportBindingModel model) { + var document = _saveToPdf.CreteDoc(new PdfInfoClient { + Title = "Список оплаченных товаров", + FileName = "Report", + DateFrom = model.DateFrom, + DateTo = model.DateTo, + Products = GetProducts(model) + }); + return document; + } } } \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj b/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj index c5144c7..338f66b 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj @@ -12,6 +12,7 @@ + diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/MailWorker/MailKitWorker.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/MailWorker/MailKitWorker.cs index 47760c5..9df1536 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/MailWorker/MailKitWorker.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/MailWorker/MailKitWorker.cs @@ -10,12 +10,14 @@ using System.Threading.Tasks; using ElectronicsShopContracts.BindingModels; using MailKit.Net.Pop3; using MailKit.Security; +using MigraDoc.DocumentObjectModel; namespace ElectronicsShopBusinessLogic.MailWorker { public class MailKitWorker : AbstractMailWorker { - public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) : base(logger, messageInfoLogic, clientLogic) { } + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, + IClientLogic clientLogic) : base(logger, messageInfoLogic, clientLogic) { } protected override async Task SendMailAsync(MailSendInfoBindingModel info) { @@ -27,6 +29,12 @@ namespace ElectronicsShopBusinessLogic.MailWorker objMailMessage.To.Add(new MailAddress(info.MailAddress)); objMailMessage.Subject = info.Subject; objMailMessage.Body = info.Text; + + Attachment attachment = new Attachment(new MemoryStream(info.document), name: "Report.pdf"); + if (attachment != null) { + objMailMessage.Attachments.Add(attachment); + } + objMailMessage.SubjectEncoding = Encoding.UTF8; objMailMessage.BodyEncoding = Encoding.UTF8; diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToPdfClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToPdfClient.cs new file mode 100644 index 0000000..4c141e6 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToPdfClient.cs @@ -0,0 +1,65 @@ +using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; +using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using PdfSharp.Pdf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToPdfClient { + public PdfDocument CreteDoc (PdfInfoClient info) { + CretePdf(info); + + CreateParagraph(new PdfParagraph { + Text = info.Title, + Style = "NormalTitle", + alignmentType = PdfParagraphAlignmentType.Center, + }); + CreateParagraph(new PdfParagraph { + Text = $"c {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", + Style = "Normal", + alignmentType = PdfParagraphAlignmentType.Right + }); + + CreateTable(new List { "2cm", "6cm", "4cm", "6cm"}); + + CreateRow(new PdfRowParameters { + Text = new List { "Номер", "Товар", "Цена", "Статья затрат" }, + Style = "NormalTittle", + alignmentType = PdfParagraphAlignmentType.Center, + }); + + foreach (var products in info.Products) { + CreateRow(new PdfRowParameters { + Text = new List { products.ID.ToString(), products.ProductName.ToString(), products.Price.ToString(), + products.CostItemName.ToString()}, + Style = "Normal", + alignmentType = PdfParagraphAlignmentType.Left, + }); + } + CreateParagraph(new PdfParagraph { + Text = $"Итого: {info.Products.Sum(x => x.Price)}\t", + Style = "Normal", + alignmentType = PdfParagraphAlignmentType.Right + }); + + var document = SavePdf(info); + return document; + } + + // Создание doc-файла + protected abstract void CretePdf (PdfInfoClient info); + // Создание параграфа с текстом + protected abstract void CreateParagraph(PdfParagraph paragraph); + // Создание таблицы + protected abstract void CreateTable(List columns); + // Создание и заполнение строки + protected abstract void CreateRow(PdfRowParameters rowParameters); + // Сохранение файла + protected abstract PdfDocument SavePdf(PdfInfoClient info); + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..3d9afd4 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperEnums/PdfParagraphAlignmentType.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.OfficePackage.HelperEnums { + public enum PdfParagraphAlignmentType { + Center, + Left, + Right, + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoClient.cs index 43dc843..2eeec49 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoClient.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoClient.cs @@ -4,7 +4,6 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.HelperModels { public class ExcelInfoClient { - public string FileName { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; public List PaymeantProducts { get; set; } = new(); } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfInfoClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfInfoClient.cs new file mode 100644 index 0000000..93f4dbe --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfInfoClient.cs @@ -0,0 +1,16 @@ +using ElectronicsShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.OfficePackage.HelperModels { + public class PdfInfoClient { + 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? Products { get; set; } = new(); + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs new file mode 100644 index 0000000..4276c90 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfParagraph.cs @@ -0,0 +1,14 @@ +using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.OfficePackage.HelperModels { + public class PdfParagraph { + public string Text { get; set; } = string.Empty; + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType alignmentType { get; set; } + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs new file mode 100644 index 0000000..59034cf --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/PdfRowParameters.cs @@ -0,0 +1,14 @@ +using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.OfficePackage.HelperModels { + public class PdfRowParameters { + public List Text { get; set; } = new(); + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType alignmentType { get; set; } + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/WordInfoClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/WordInfoClient.cs index d1d2430..7900d66 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/WordInfoClient.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/WordInfoClient.cs @@ -5,10 +5,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.HelperModels { public class WordInfoClient { - public string FileName { get; set; } = string.Empty; - public string Title { get; set; } = string.Empty; - public List ListPaymeant { get; set; } = new(); } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs index d00382c..6fb5e1c 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs @@ -286,4 +286,4 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements return _mem.ToArray(); } } -} +} \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToPdfClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToPdfClient.cs new file mode 100644 index 0000000..e98c2e0 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToPdfClient.cs @@ -0,0 +1,98 @@ +using DocumentFormat.OpenXml.Bibliography; +using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; +using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using PdfSharp.Pdf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopBusinessLogic.OfficePackage.Implements { + public class SaveToPdfClient : AbstractSaveToPdfClient { + + 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.alignmentType); + paragraph.Style = pdfParagraph.Style; + } + + protected override void CreateRow(PdfRowParameters rowParameters) { + if (_table == null) { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Text.Count; ++i) { + // заполнение ячейки (добавление параграфа в ячейку) + row.Cells[i].AddParagraph(rowParameters.Text[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.alignmentType); + 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 CretePdf(PdfInfoClient info) { + _document = new Document(); + DefineStyles(_document); + // Ссылка нп первую секцию + _section = _document.AddSection(); + } + + protected override PdfDocument SavePdf(PdfInfoClient info) { + var renderer = new PdfDocumentRenderer(true) { + Document = _document, + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + return renderer.PdfDocument; + } + } +} diff --git a/ElectronicsShop/ElectronicsShopContracts/BindingModels/MailSendInfoBindingModel.cs b/ElectronicsShop/ElectronicsShopContracts/BindingModels/MailSendInfoBindingModel.cs index 16cc1da..049f94e 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BindingModels/MailSendInfoBindingModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using MigraDoc.DocumentObjectModel; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,5 +12,6 @@ namespace ElectronicsShopContracts.BindingModels public string MailAddress { get; set; } = string.Empty; public string Subject { get; set; } = string.Empty; public string Text { get; set; } = string.Empty; + public byte[]? document { get; set; } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BindingModels/ReportBindingModel.cs b/ElectronicsShop/ElectronicsShopContracts/BindingModels/ReportBindingModel.cs index 23d818d..e47daa5 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BindingModels/ReportBindingModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BindingModels/ReportBindingModel.cs @@ -10,8 +10,8 @@ namespace ElectronicsShopContracts.BindingModels { public class ReportBindingModel { - public string FileName { get; set; } = string.Empty; - public DateTime? DateFrom { get; set; } - public DateTime? DateTo { get; set; } + public string ClientEmail { get; set; } = string.Empty; + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs index b3b8662..f307e90 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs @@ -1,6 +1,7 @@ -using DocumentFormat.OpenXml.Packaging; -using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.ViewModels; +using MigraDoc.DocumentObjectModel; +using PdfSharp.Pdf; using System; using System.Collections.Generic; using System.Linq; @@ -12,12 +13,17 @@ namespace ElectronicsShopContracts.BusinessLogicContracts public interface IReportClientLogic { // получение списка товаров с указанием, в какие оплаты товар входит - List GetPaymeantProducts(int _clientID); + List? GetPaymeantProducts(int _clientID); // получения списка оплат - List GetPaymeants(ReportBindingModel model); - byte[] SavePaymeantToWordFile(int _clientID); + List? GetPaymeants(ReportBindingModel model); - // Сохранение компонент с указанием отчета в .excel - byte[] SavePaymeantToExcelFile(int _clientID); + // Сохранение отчета оплат в .word + byte[]? SavePaymeantToWordFile(int _clientID); + + // Сохранение отчета оплат с товарами в .excel + byte[]? SavePaymeantToExcelFile(int _clientID); + + // Отчет оплаченных товаров в .pdf + PdfDocument SaveProductToPdfFile(ReportBindingModel model); } } diff --git a/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj b/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj index 40b8cdc..5c52396 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj +++ b/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj @@ -8,6 +8,7 @@ + diff --git a/ElectronicsShop/ElectronicsShopContracts/ViewModels/ReportProductsViewModel.cs b/ElectronicsShop/ElectronicsShopContracts/ViewModels/ReportProductsViewModel.cs index ddb61c8..1d05b7b 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ViewModels/ReportProductsViewModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/ViewModels/ReportProductsViewModel.cs @@ -9,7 +9,9 @@ namespace ElectronicsShopContracts.ViewModels { public class ReportProductsViewModel { + public int ID { get; set; } public string ProductName { get; set; } = string.Empty; - public List Products { get; set; } = new(); + public double Price { get; set; } + public string CostItemName { get; set; } = string.Empty; } } diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs index 8bce23e..34349e0 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs @@ -1,10 +1,14 @@ using DocumentFormat.OpenXml.Drawing.Diagrams; using DocumentFormat.OpenXml.Packaging; +using ElectronicsShopBusinessLogic.BusinessLogic; +using ElectronicsShopBusinessLogic.MailWorker; using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataBaseImplement.Models; using Microsoft.AspNetCore.Mvc; +using MigraDoc.Rendering; namespace ElectronicsShopRestAPI.Controllers { @@ -17,12 +21,15 @@ namespace ElectronicsShopRestAPI.Controllers { private readonly IClientLogic _logic; private readonly IPaymeantLogic _payLogic; private readonly IReportClientLogic _reportLogic; + private readonly AbstractMailWorker _mailWorker; - public ClientController(ILogger logger, IClientLogic logic, IPaymeantLogic payLogic, IReportClientLogic reportlogic) { + public ClientController(ILogger logger, IClientLogic logic, IPaymeantLogic payLogic, IReportClientLogic reportlogic, + AbstractMailWorker mailWorker) { _logger = logger; _logic = logic; _payLogic = payLogic; _reportLogic = reportlogic; + _mailWorker = mailWorker; } [HttpGet] @@ -81,11 +88,11 @@ namespace ElectronicsShopRestAPI.Controllers { } } [HttpGet] - public List? GetReport(DateTime _start, DateTime _end) { + public List? GetReport(string _start,string _end) { try { var dataSource = _reportLogic.GetPaymeants(new ReportBindingModel { - DateFrom = _start, - DateTo = _end + DateFrom = DateTime.Parse(_start), + DateTo = DateTime.Parse(_end) }); return dataSource; } @@ -118,5 +125,31 @@ namespace ElectronicsShopRestAPI.Controllers { throw; } } + + [HttpPost] + public void SendReportMail (ReportBindingModel model) { + try { + + var doc = _reportLogic.SaveProductToPdfFile(model); + + MemoryStream stream = new MemoryStream(); + doc.Save(stream, true); + byte[] data = stream.ToArray(); + + + _mailWorker.MailSendAsync(new() { + MailAddress = model.ClientEmail, + Subject = "Отчет", + Text = $"Отчет оплаченных товаров с {model.DateFrom} по {model.DateTo}", + document = data + }); + + } + catch (Exception ex) { + _logger.LogError(ex, $"Ошибка создания файла"); + throw; + } + } + } } diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Program.cs b/ElectronicsShop/ElectronicsShopRestAPI/Program.cs index 76cd9f5..f62b45e 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Program.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Program.cs @@ -29,6 +29,7 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Report b/ElectronicsShop/ElectronicsShopRestAPI/Report new file mode 100644 index 0000000..358bb8b Binary files /dev/null and b/ElectronicsShop/ElectronicsShopRestAPI/Report differ diff --git a/ElectronicsShop/ElectronicsShopRestAPI/report.xlsx b/ElectronicsShop/ElectronicsShopRestAPI/report.xlsx deleted file mode 100644 index a86d406..0000000 Binary files a/ElectronicsShop/ElectronicsShopRestAPI/report.xlsx and /dev/null differ diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs index 2e8edd4..b5b6fd7 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs @@ -9,6 +9,7 @@ using ElectronicsShopUserApp.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System.Diagnostics; +using System.Globalization; using System.Net.Mime; using System.Reflection; using System.Runtime.Serialization; @@ -18,7 +19,7 @@ using System.Xml.Serialization; namespace ElectronicsShopUserApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; - private Dictionary _productList; + private Dictionary _productList; public int Id; public HomeController(ILogger logger) { @@ -28,10 +29,10 @@ namespace ElectronicsShopUserApp.Controllers { [HttpGet] public IActionResult Index() { - if (APIClient.Client == null) { + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); - } - return View(APIClient.GetRequset>($"api/client/getpaymeants?_clientid={APIClient.Client.ID}")); + } + return View(APIClient.GetRequset>($"api/client/getpaymeants?_clientid={APIClient.Client.ID}")); } [HttpGet] @@ -63,7 +64,7 @@ namespace ElectronicsShopUserApp.Controllers { Response.Redirect("Index"); } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } @@ -73,7 +74,7 @@ namespace ElectronicsShopUserApp.Controllers { return View(); } - [HttpPost] + [HttpPost] public void Enter(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception(" "); @@ -110,7 +111,7 @@ namespace ElectronicsShopUserApp.Controllers { var view = APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}"); - return View(view); + return View(view); } [HttpGet] @@ -123,45 +124,45 @@ namespace ElectronicsShopUserApp.Controllers { DateCreate = DateTime.Now, }); return RedirectToAction("OrderView"); - } + } [HttpGet] public IActionResult DeleteOrder(int id) { - APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id }); + APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id }); return RedirectToAction("Orders"); - } + } [HttpGet] public IActionResult EditOrder(int id) { - if (APIClient.Client == null) { - return Redirect("~/Home/Enter"); - } + if (APIClient.Client == null) { + return Redirect("~/Home/Enter"); + } if (id == 0) { return RedirectToAction("Orders"); } - var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={id}"); + var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={id}"); - foreach (var pr in products) { - var product = JsonConvert.DeserializeObject(pr[0]); - int count = JsonConvert.DeserializeObject(pr[1]); - _productList.Add(product.ID, (product, count)); - } + foreach (var pr in products) { + var product = JsonConvert.DeserializeObject(pr[0]); + int count = JsonConvert.DeserializeObject(pr[1]); + _productList.Add(product.ID, (product, count)); + } - (int, Dictionary) tuple = (id, _productList); - return View(tuple); - } + (int, Dictionary) tuple = (id, _productList); + return View(tuple); + } [HttpPost] - public void EditOrder(double sum, int id) { - if (sum <= 0) { - APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id }); - } + public void EditOrder(double sum, int id) { + if (sum <= 0) { + APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id }); + } - Response.Redirect("Orders"); - } + Response.Redirect("Orders"); + } - [HttpGet] + [HttpGet] public IActionResult OrderView() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); @@ -174,10 +175,10 @@ namespace ElectronicsShopUserApp.Controllers { var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={view?.ID}"); foreach (var pr in products) { - var product = JsonConvert.DeserializeObject(pr[0]); + var product = JsonConvert.DeserializeObject(pr[0]); int count = JsonConvert.DeserializeObject(pr[1]); _productList.Add(product.ID, (product, count)); - } + } (int, Dictionary) tuple = (Id, _productList); return View(tuple); @@ -186,71 +187,66 @@ namespace ElectronicsShopUserApp.Controllers { [HttpPost] public void OrderView(double sum, int id) { if (sum <= 0) { - APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id}); + APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id }); } Response.Redirect("Orders"); } [HttpGet] public IActionResult DeleteProductOrder(int id) { - var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); - APIClient.PostRequestStr($"api/main/deleteproductorder", view.ID, id); + var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); + APIClient.PostRequestStr($"api/main/deleteproductorder", view.ID, id); - return RedirectToAction("OrderView"); + return RedirectToAction("OrderView"); } - [HttpGet] + [HttpGet] public IActionResult AddProduct() { ViewBag.Products = APIClient.GetRequset>($"api/main/getproducts"); - return View(); + return View(); } - [HttpPost] + [HttpPost] public void AddProduct(int product, int count) { var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); var _order = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); - APIClient.ListPostRequest($"api/main/addproduct", _product, count, _order.ID); - Response.Redirect("OrderView"); - } + APIClient.ListPostRequest($"api/main/addproduct", _product, count, _order.ID); + Response.Redirect("OrderView"); + } [HttpGet] - public IActionResult Message() - { + public IActionResult Message() { //ViewBag.Reports = APIClient.GetRequset>($"api/main/getproducts"); return View(); } [HttpGet] - public IActionResult Payment(int id) - { - if (APIClient.Client == null) - { - return Redirect("~/Home/Enter"); - } + public IActionResult Payment(int id) { + if (APIClient.Client == null) { + return Redirect("~/Home/Enter"); + } - if (id == 0) - { + if (id == 0) { return Redirect("~/Home/Index"); - } - var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={id}"); + } + var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={id}"); - foreach (var pr in products) - { - var product = JsonConvert.DeserializeObject(pr[0]); - int count = JsonConvert.DeserializeObject(pr[1]); - _productList.Add(product.ID, (product, count)); - } + foreach (var pr in products) { + var product = JsonConvert.DeserializeObject(pr[0]); + int count = JsonConvert.DeserializeObject(pr[1]); + _productList.Add(product.ID, (product, count)); + } - (int, Dictionary) tuple = (id, _productList); - return View(tuple); - } + (int, Dictionary) tuple = (id, _productList); + return View(tuple); + } [HttpPost] public void Payment(double sum, double paysum, int id) { - if (APIClient.Client == null) { - throw new Exception(" "); - } + if (APIClient.Client == null) { + throw new Exception(" "); + } if (paysum <= 0) { throw new Exception(" 0"); } @@ -265,7 +261,7 @@ namespace ElectronicsShopUserApp.Controllers { DatePaymeant = DateTime.Now, }); Response.Redirect("Index"); - } + } [HttpPost] public PaymeantOption PayOptionCalc(double sum, double paysum) { @@ -280,10 +276,10 @@ namespace ElectronicsShopUserApp.Controllers { } } [HttpPost] - public double Calc(int count, int product) { - var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); - return count * (_product?.Price ?? 1); - } + public double Calc(int count, int product) { + var _product = APIClient.GetRequset($"api/main/getproduct?_productid={product}"); + return count * (_product?.Price ?? 1); + } [HttpGet] public IActionResult Report() { @@ -295,31 +291,32 @@ namespace ElectronicsShopUserApp.Controllers { } [HttpPost] - public void Report(DateTime DateFrom, DateTime DateTo) { + public void Report(DateTime DateFrom, DateTime DateTo){ if (DateTo == DateTime.MinValue || DateFrom > DateTo) { throw new Exception(" "); } - Response.Redirect($"ReportSearch?_datefrom={DateFrom}&_dateTo={DateTo}"); + Response.Redirect($"ReportSearch?_datefrom={DateFrom}&_dateto={DateTo}"); } [HttpGet] - public IActionResult ReportSearch(DateTime _datefrom, DateTime _dateto) { + public IActionResult ReportSearch(string _datefrom, string _dateto) { + var reports = APIClient.GetRequset>($"api/client/getreport?_start={_datefrom}&_end={_dateto}"); - (DateTime, DateTime, List) tuple = (_datefrom, _dateto, reports); + (DateTime, DateTime, List?) tuple = (DateTime.Parse(_datefrom), DateTime.Parse(_dateto), reports); return View(tuple); } [HttpGet] public IActionResult CreateExcelReport() { - var fileMemStream = APIClient.GetRequset($"api/Client/CreateXlsxReport?_clientID={APIClient.Client.ID}"); + var fileMemStream = APIClient.GetRequset($"api/Client/CreateXlsxReport?_clientID={APIClient.Client.ID}"); if (fileMemStream == null) { throw new Exception(" "); } return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx"); - } + } [HttpGet] public IActionResult CreateWordReport() { @@ -331,5 +328,15 @@ namespace ElectronicsShopUserApp.Controllers { return File(fileMemStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx"); } + + [HttpGet] + public IActionResult CreatePdfReport(string DateFrom, string DateTo) { + APIClient.PostRequest("api/client/SendReportMail", new ReportBindingModel { + ClientEmail = APIClient.Client.Email, + DateFrom = DateTime.Parse(DateFrom), + DateTo = DateTime.Parse(DateTo) + }); + return View("Report"); + } } } \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/ReportSearch.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/ReportSearch.cshtml index 2d5f000..b1d18d7 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/ReportSearch.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/ReportSearch.cshtml @@ -27,7 +27,8 @@