From 146deadf89004031c09e8e8d8dae2699bbc19a8b Mon Sep 17 00:00:00 2001 From: the Date: Tue, 25 Jun 2024 21:12:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B9=D1=81-=D0=BB=D0=B8?= =?UTF-8?q?=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BusinessLogic/BusinessLogic/ReportLogic.cs | 14 ++- .../AbstractSaveToPdfPricelist.cs | 51 ++++++++ .../OfficePackage/HelperModels/PdfInfo.cs | 1 + .../Implements/SaveToPdfPricelist.cs | 115 ++++++++++++++++++ .../BusinessLogicContracts/IReportLogic.cs | 1 + WinFormsApp/FormProducts.Designer.cs | 29 +++-- WinFormsApp/FormProducts.cs | 61 +++++++--- WinFormsApp/Program.cs | 1 + 8 files changed, 247 insertions(+), 26 deletions(-) create mode 100644 BusinessLogic/OfficePackage/AbstractSaveToPdfPricelist.cs create mode 100644 BusinessLogic/OfficePackage/Implements/SaveToPdfPricelist.cs diff --git a/BusinessLogic/BusinessLogic/ReportLogic.cs b/BusinessLogic/BusinessLogic/ReportLogic.cs index 7eec058..b508974 100644 --- a/BusinessLogic/BusinessLogic/ReportLogic.cs +++ b/BusinessLogic/BusinessLogic/ReportLogic.cs @@ -20,13 +20,15 @@ namespace BusinessLogic.BusinessLogic private readonly IMediaFileStorage _mediaFileStorage; private readonly AbstractSaveToPdf _saveToPdf; private readonly AbstractSaveToPdfCheque _saveToCheque; - public ReportLogic(IProductStorage productStorage, ISupplyStorage supplyStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToPdfCheque saveToPdfCheque, IMediaFileStorage mediaFileStorage) + private readonly AbstractSaveToPdfPricelist _savePricelist; + public ReportLogic(IProductStorage productStorage, ISupplyStorage supplyStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToPdfCheque saveToPdfCheque, IMediaFileStorage mediaFileStorage, AbstractSaveToPdfPricelist savePricelist) { _productStorage = productStorage; _supplyStorage = supplyStorage; _saveToPdf = saveToPdf; _saveToCheque = saveToPdfCheque; _mediaFileStorage = mediaFileStorage; + _savePricelist = savePricelist; } /// /// Получение списка компонент с указанием, в каких изделиях используются @@ -88,5 +90,15 @@ namespace BusinessLogic.BusinessLogic //MediaFiles = _mediaFileStorage.GetFilteredList(new MediaFileSearchModel() { ProductId = model.ProductId }) }); } + public void SavePriceListToPdfFile(ReportBindingModel model) + { + _savePricelist.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Прайс-лист", + Date = model.Date, + Products = _productStorage.GetFullList() + }); + } } } diff --git a/BusinessLogic/OfficePackage/AbstractSaveToPdfPricelist.cs b/BusinessLogic/OfficePackage/AbstractSaveToPdfPricelist.cs new file mode 100644 index 0000000..4c76d5e --- /dev/null +++ b/BusinessLogic/OfficePackage/AbstractSaveToPdfPricelist.cs @@ -0,0 +1,51 @@ +using BusinessLogic.OfficePackage.HelperEnums; +using BusinessLogic.OfficePackage.HelperModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BusinessLogic.OfficePackage +{ + public abstract class AbstractSaveToPdfPricelist + { + public void CreateDoc(PdfInfo info) + { + CreatePdf(info); + CreateParagraph(new PdfParagraph + { + Text = $"{info.Title}\nОт {info.Date}", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateParagraph(new PdfParagraph + { + Text = "Товары", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateTable(new List { "5cm", "5cm", "3cm", "2cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "Id", "Название", "Цена", "Кол-во на складе" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var product in info.Products) + { + CreateRow(new PdfRowParameters + { + Texts = new List { product.Id.ToString(), product.Name, product.Price.ToString(), product.Amount.ToString() }, + }); + } + SavePdf(info); + } + 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 CreateImage(string path); + protected abstract void SavePdf(PdfInfo info); + } +} diff --git a/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs b/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs index d91c211..8ba2d37 100644 --- a/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs +++ b/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -15,5 +15,6 @@ namespace BusinessLogic.OfficePackage.HelperModels public SupplyViewModel? Supply { get; set; } = new(); public ProductViewModel? Product { get; set; } = new(); public List? MediaFiles { get; set; } = new(); + public List? Products { get; set; } = new(); } } diff --git a/BusinessLogic/OfficePackage/Implements/SaveToPdfPricelist.cs b/BusinessLogic/OfficePackage/Implements/SaveToPdfPricelist.cs new file mode 100644 index 0000000..735fed4 --- /dev/null +++ b/BusinessLogic/OfficePackage/Implements/SaveToPdfPricelist.cs @@ -0,0 +1,115 @@ +using BusinessLogic.OfficePackage.HelperEnums; +using BusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Shapes; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BusinessLogic.OfficePackage.Implements +{ + public class SaveToPdfPricelist : AbstractSaveToPdfPricelist + { + private Document? _document; + private Section? _section; + private Table? _table; + private Image _image; + 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 CreateImage(string path) + { + if (_document == null) + { + return; + } + _document.LastSection.AddImage(path); + } + protected override void SavePdf(PdfInfo info) + { + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(info.FileName); + } + } +} diff --git a/Contracts/BusinessLogicContracts/IReportLogic.cs b/Contracts/BusinessLogicContracts/IReportLogic.cs index e4bd760..e9ef8fd 100644 --- a/Contracts/BusinessLogicContracts/IReportLogic.cs +++ b/Contracts/BusinessLogicContracts/IReportLogic.cs @@ -21,5 +21,6 @@ namespace Contracts.BusinessLogicContracts /// void SaveSuppliesToPdfFile(ReportBindingModel model); void SaveProductToPdfFile(ReportBindingModel model); + void SavePriceListToPdfFile(ReportBindingModel model); } } diff --git a/WinFormsApp/FormProducts.Designer.cs b/WinFormsApp/FormProducts.Designer.cs index e8cc40c..e84674b 100644 --- a/WinFormsApp/FormProducts.Designer.cs +++ b/WinFormsApp/FormProducts.Designer.cs @@ -31,6 +31,7 @@ dataGridView = new DataGridView(); buttonCreateProduct = new Button(); groupBoxControls = new GroupBox(); + buttonPrintCheque = new Button(); buttonReadBarCode = new Button(); pictureBox1 = new PictureBox(); buttonGenerateBarCode = new Button(); @@ -45,7 +46,7 @@ textBoxName = new TextBox(); menuStrip1 = new MenuStrip(); медиаФайлыToolStripMenuItem = new ToolStripMenuItem(); - buttonPrintCheque = new Button(); + прайслистToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); groupBoxControls.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); @@ -95,6 +96,16 @@ groupBoxControls.TabStop = false; groupBoxControls.Text = "Действия"; // + // buttonPrintCheque + // + buttonPrintCheque.Location = new Point(80, 425); + buttonPrintCheque.Name = "buttonPrintCheque"; + buttonPrintCheque.Size = new Size(108, 45); + buttonPrintCheque.TabIndex = 7; + buttonPrintCheque.Text = "Распечатать чек"; + buttonPrintCheque.UseVisualStyleBackColor = true; + buttonPrintCheque.Click += buttonPrintCheque_Click; + // // buttonReadBarCode // buttonReadBarCode.Location = new Point(69, 335); @@ -216,7 +227,7 @@ // // menuStrip1 // - menuStrip1.Items.AddRange(new ToolStripItem[] { медиаФайлыToolStripMenuItem }); + menuStrip1.Items.AddRange(new ToolStripItem[] { медиаФайлыToolStripMenuItem, прайслистToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; menuStrip1.Size = new Size(925, 24); @@ -230,15 +241,12 @@ медиаФайлыToolStripMenuItem.Text = "Медиа файлы"; медиаФайлыToolStripMenuItem.Click += медиаФайлыToolStripMenuItem_Click; // - // buttonPrintCheque + // прайслистToolStripMenuItem // - buttonPrintCheque.Location = new Point(80, 425); - buttonPrintCheque.Name = "buttonPrintCheque"; - buttonPrintCheque.Size = new Size(108, 45); - buttonPrintCheque.TabIndex = 7; - buttonPrintCheque.Text = "Распечатать чек"; - buttonPrintCheque.UseVisualStyleBackColor = true; - buttonPrintCheque.Click += buttonPrintCheque_Click; + прайслистToolStripMenuItem.Name = "прайслистToolStripMenuItem"; + прайслистToolStripMenuItem.Size = new Size(84, 20); + прайслистToolStripMenuItem.Text = "Прайс-лист"; + прайслистToolStripMenuItem.Click += прайслистToolStripMenuItem_Click; // // FormProducts // @@ -286,5 +294,6 @@ private MenuStrip menuStrip1; private ToolStripMenuItem медиаФайлыToolStripMenuItem; private Button buttonPrintCheque; + private ToolStripMenuItem прайслистToolStripMenuItem; } } \ No newline at end of file diff --git a/WinFormsApp/FormProducts.cs b/WinFormsApp/FormProducts.cs index fdc9f32..8f5892d 100644 --- a/WinFormsApp/FormProducts.cs +++ b/WinFormsApp/FormProducts.cs @@ -281,21 +281,52 @@ namespace WinFormsApp if (service is FormPreviewPDF form) { var src = $"productcheque{dataGridView.SelectedRows[0].Cells["Id"].Value}.pdf"; - try - { - _reportLogic.SaveProductToPdfFile(new ReportBindingModel - { - FileName = $"productcheque{dataGridView.SelectedRows[0].Cells["Id"].Value}.pdf", - ProductId = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value, - }); - _logger.LogInformation("Сохранение чека о товаре"); - MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка сохранения чека"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + try + { + _reportLogic.SaveProductToPdfFile(new ReportBindingModel + { + FileName = $"productcheque{dataGridView.SelectedRows[0].Cells["Id"].Value}.pdf", + ProductId = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value, + }); + _logger.LogInformation("Сохранение чека о товаре"); + MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения чека"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + form.src = System.IO.Path.GetFullPath(src); + if (form.ShowDialog() == DialogResult.OK) + { + IronPrint.Printer.PrintAsync(src); + } + } + } + + private void прайслистToolStripMenuItem_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count != 1) return; + var service = Program.ServiceProvider?.GetService(typeof(FormPreviewPDF)); + if (service is FormPreviewPDF form) + { + var date = DateTime.Now; + var src = $"pricelist{date.Year}{date.Month}{date.Day}.pdf"; + try + { + _reportLogic.SavePriceListToPdfFile(new ReportBindingModel + { + FileName = $"pricelist{date.Year}{date.Month}{date.Day}.pdf", + Date = date, + }); + _logger.LogInformation("Сохранение прайс-листа"); + MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения прайс-листа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } form.src = System.IO.Path.GetFullPath(src); if (form.ShowDialog() == DialogResult.OK) { diff --git a/WinFormsApp/Program.cs b/WinFormsApp/Program.cs index 12e8171..c5da0f4 100644 --- a/WinFormsApp/Program.cs +++ b/WinFormsApp/Program.cs @@ -53,6 +53,7 @@ namespace WinFormsApp services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient();