diff --git a/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs b/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs index 046a619..7fae25b 100644 --- a/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/CarService/CarServiceBusinessLogic/BusinessLogics/ReportLogic.cs @@ -16,14 +16,16 @@ namespace CarServiceBusinessLogic.BusinessLogics private readonly IWorkPaymentStorage _workPaymentStorage; private readonly AbstractSaveToWord _saveToWord; private readonly AbstractSaveToExcel _saveToExcel; + private readonly AbstractSaveToPdf _saveToPdf; - public ReportLogic(ILogger logger, IWorkStorage workStorage, IWorkPaymentStorage workPaymentStorage, AbstractSaveToWord saveToWord, AbstractSaveToExcel saveToExcel) + public ReportLogic(ILogger logger, IWorkStorage workStorage, IWorkPaymentStorage workPaymentStorage, AbstractSaveToWord saveToWord, AbstractSaveToExcel saveToExcel, AbstractSaveToPdf saveToPdf) { _logger = logger; _workStorage = workStorage; _workPaymentStorage = workPaymentStorage; _saveToWord = saveToWord; _saveToExcel = saveToExcel; + _saveToPdf = saveToPdf; } public List GetRequestsByWorks(ReportBindingModel model) { @@ -53,9 +55,16 @@ namespace CarServiceBusinessLogic.BusinessLogics WorksWithRequests = GetRequestsByWorks(model) }); } - public void SaveOrdersToPdfFile(ReportBindingModel model) + public void SavePaymentsToPdfFile(ReportBindingModel model) { - throw new NotImplementedException(); + _saveToPdf.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Список оплат", + DateFrom = model.DateFrom!.Value, + DateTo = model.DateTo!.Value, + Payments = GetPayments(model) + }); } } } diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/CarService/CarServiceBusinessLogic/OfficePackage/AbstractSaveToPdf.cs new file mode 100644 index 0000000..cf9adc5 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -0,0 +1,68 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels; + +namespace BlacksmithWorkshopBusinessLogic.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 + }); + CreateTable(new List { "3cm", "2,5cm", "2cm", "2,5cm", "2,5cm", "2,5cm", "3cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "Дата оплаты", "Клиент", "Номер заявки", "Работа", "Сумма платежа", "Всего оплачено", "Осталось оплатить" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var payment in info.Payments) + { + CreateRow(new PdfRowParameters + { + Texts = new List { payment.PaymentDate.ToShortDateString(), payment.CustomerName, payment.RepairRequestId.ToString(), payment.WorkName, payment.PaymentSum.ToString(), payment.Paid.ToString(), payment.NotPaid.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); + } +} diff --git a/CarService/CarServiceBusinessLogic/OfficePackage/Implements/SaveToPdf.cs b/CarService/CarServiceBusinessLogic/OfficePackage/Implements/SaveToPdf.cs new file mode 100644 index 0000000..b44f346 --- /dev/null +++ b/CarService/CarServiceBusinessLogic/OfficePackage/Implements/SaveToPdf.cs @@ -0,0 +1,100 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperEnums; +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; + +namespace BlacksmithWorkshopBusinessLogic.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 CreatePdf(PdfInfo info) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + } + 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 CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + 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 SavePdf(PdfInfo info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } +} \ No newline at end of file diff --git a/CarService/CarServiceContracts/BusinessLogicsContracts/IReportLogic.cs b/CarService/CarServiceContracts/BusinessLogicsContracts/IReportLogic.cs index e974eb8..4bf91e7 100644 --- a/CarService/CarServiceContracts/BusinessLogicsContracts/IReportLogic.cs +++ b/CarService/CarServiceContracts/BusinessLogicsContracts/IReportLogic.cs @@ -30,6 +30,6 @@ namespace CarServiceContracts.BusinessLogicsContracts /// Сохранение заказов в файл-Pdf /// /// - void SaveOrdersToPdfFile(ReportBindingModel model); + void SavePaymentsToPdfFile(ReportBindingModel model); } } diff --git a/CarService/CarServiceWebApp/Controllers/ReportController.cs b/CarService/CarServiceWebApp/Controllers/ReportController.cs index ddf57ec..57b9d2f 100644 --- a/CarService/CarServiceWebApp/Controllers/ReportController.cs +++ b/CarService/CarServiceWebApp/Controllers/ReportController.cs @@ -11,8 +11,10 @@ namespace CarServiceWebApp.Controllers private readonly IReportLogic _reportLogic; private readonly IWorkLogic _workLogic; private static List SelectedWorks = new(); - private static ReportBindingModel PaymentsModel = new(); - + private static ReportBindingModel PaymentsModel = new() + { + FileName = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportPdf.pdf" + }; public ReportController(IReportLogic reportLogic, IWorkLogic workLogic) { @@ -67,7 +69,8 @@ namespace CarServiceWebApp.Controllers [HttpPost] public IActionResult DateSelection(DateTime dateFrom, DateTime dateTo) { - PaymentsModel = new() { DateFrom = dateFrom, DateTo = dateTo }; + PaymentsModel.DateFrom = dateFrom; + PaymentsModel.DateTo = dateTo; return Redirect("~/Report/ReportPayments"); } public IActionResult ReportPayments() @@ -102,6 +105,20 @@ namespace CarServiceWebApp.Controllers byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); + return File(fileBytes, "application/force-download", fileName); + } + public IActionResult SaveToPdf() + { + _reportLogic.SavePaymentsToPdfFile(PaymentsModel); + return Redirect("~/Report/DownLoadPdf"); + } + public IActionResult DownLoadPdf() + { + string filePath = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportPdf.pdf"; + string fileName = "Отчет.pdf"; + + byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); + return File(fileBytes, "application/force-download", fileName); } } diff --git a/CarService/CarServiceWebApp/Files/ReportPdf.pdf b/CarService/CarServiceWebApp/Files/ReportPdf.pdf new file mode 100644 index 0000000..5e0ce0b Binary files /dev/null and b/CarService/CarServiceWebApp/Files/ReportPdf.pdf differ diff --git a/CarService/CarServiceWebApp/Program.cs b/CarService/CarServiceWebApp/Program.cs index 3ed79d6..ba0e723 100644 --- a/CarService/CarServiceWebApp/Program.cs +++ b/CarService/CarServiceWebApp/Program.cs @@ -29,6 +29,7 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); var app = builder.Build(); diff --git a/CarService/CarServiceWebApp/Views/Report/ReportPayments.cshtml b/CarService/CarServiceWebApp/Views/Report/ReportPayments.cshtml index 12e1022..c718a4f 100644 --- a/CarService/CarServiceWebApp/Views/Report/ReportPayments.cshtml +++ b/CarService/CarServiceWebApp/Views/Report/ReportPayments.cshtml @@ -57,4 +57,5 @@ {

Нет оплат за указанный период

} + \ No newline at end of file