From 5678ff27d5529f780c328b14ac21a5d26afea47a Mon Sep 17 00:00:00 2001 From: the Date: Tue, 25 Jun 2024 20:24:29 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B5=D0=BA=20=D0=BD=D0=B0=20=D0=BE?= =?UTF-8?q?=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BusinessLogic/BusinessLogic/ReportLogic.cs | 20 ++- .../OfficePackage/AbstractSaveToPdfCheque.cs | 57 +++++++++ .../OfficePackage/HelperModels/PdfInfo.cs | 6 +- .../Implements/SaveToPdfCheque.cs | 119 ++++++++++++++++++ Contracts/BindingModels/ReportBindingModel.cs | 3 +- .../BusinessLogicContracts/IReportLogic.cs | 1 + WinFormsApp/FormProducts.Designer.cs | 19 ++- WinFormsApp/FormProducts.cs | 36 +++++- WinFormsApp/FormStatistics.cs | 9 +- WinFormsApp/Program.cs | 1 + 10 files changed, 259 insertions(+), 12 deletions(-) create mode 100644 BusinessLogic/OfficePackage/AbstractSaveToPdfCheque.cs create mode 100644 BusinessLogic/OfficePackage/Implements/SaveToPdfCheque.cs diff --git a/BusinessLogic/BusinessLogic/ReportLogic.cs b/BusinessLogic/BusinessLogic/ReportLogic.cs index 619fefe..7eec058 100644 --- a/BusinessLogic/BusinessLogic/ReportLogic.cs +++ b/BusinessLogic/BusinessLogic/ReportLogic.cs @@ -17,12 +17,16 @@ namespace BusinessLogic.BusinessLogic { private readonly IProductStorage _productStorage; private readonly ISupplyStorage _supplyStorage; + private readonly IMediaFileStorage _mediaFileStorage; private readonly AbstractSaveToPdf _saveToPdf; - public ReportLogic(IProductStorage productStorage, ISupplyStorage supplyStorage, AbstractSaveToPdf saveToPdf) + private readonly AbstractSaveToPdfCheque _saveToCheque; + public ReportLogic(IProductStorage productStorage, ISupplyStorage supplyStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToPdfCheque saveToPdfCheque, IMediaFileStorage mediaFileStorage) { _productStorage = productStorage; _supplyStorage = supplyStorage; _saveToPdf = saveToPdf; + _saveToCheque = saveToPdfCheque; + _mediaFileStorage = mediaFileStorage; } /// /// Получение списка компонент с указанием, в каких изделиях используются @@ -70,5 +74,19 @@ namespace BusinessLogic.BusinessLogic }) }); } + + public void SaveProductToPdfFile(ReportBindingModel model) + { + _saveToCheque.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Чек на товар", + Product = _productStorage.GetElement(new ProductSearchModel() + { + Id = model.ProductId + }), + //MediaFiles = _mediaFileStorage.GetFilteredList(new MediaFileSearchModel() { ProductId = model.ProductId }) + }); + } } } diff --git a/BusinessLogic/OfficePackage/AbstractSaveToPdfCheque.cs b/BusinessLogic/OfficePackage/AbstractSaveToPdfCheque.cs new file mode 100644 index 0000000..c978819 --- /dev/null +++ b/BusinessLogic/OfficePackage/AbstractSaveToPdfCheque.cs @@ -0,0 +1,57 @@ +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 AbstractSaveToPdfCheque + { + public void CreateDoc(PdfInfo info) + { + CreatePdf(info); + CreateParagraph(new PdfParagraph + { + Text = $"{info.Title}\nНа товар {info.Product.Name}", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + CreateParagraph(new PdfParagraph + { + Text = $"Id: {info.Product.Id}", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + //CreateParagraph(new PdfParagraph + //{ + // Text = "Список прилагаемых файлов", + // Style = "NormalTitle", + // ParagraphAlignment = PdfParagraphAlignmentType.Center + //}); + //CreateTable(new List { "10cm", "5cm" }); + //CreateRow(new PdfRowParameters + //{ + // Texts = new List { "Название", "расширение" }, + // Style = "NormalTitle", + // ParagraphAlignment = PdfParagraphAlignmentType.Center + //}); + CreateImage($"product{info.Product.Id}.png"); + CreateParagraph(new PdfParagraph + { + Text = $"Цена: {info.Product.Price}", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + 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 d283468..d91c211 100644 --- a/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs +++ b/BusinessLogic/OfficePackage/HelperModels/PdfInfo.cs @@ -11,7 +11,9 @@ namespace BusinessLogic.OfficePackage.HelperModels { public string FileName { get; set; } = string.Empty; public string Title { get; set; } = string.Empty; - public DateTime Date { get; set; } - public SupplyViewModel Supply { get; set; } = new(); + public DateTime? Date { get; set; } + public SupplyViewModel? Supply { get; set; } = new(); + public ProductViewModel? Product { get; set; } = new(); + public List? MediaFiles { get; set; } = new(); } } diff --git a/BusinessLogic/OfficePackage/Implements/SaveToPdfCheque.cs b/BusinessLogic/OfficePackage/Implements/SaveToPdfCheque.cs new file mode 100644 index 0000000..fca1912 --- /dev/null +++ b/BusinessLogic/OfficePackage/Implements/SaveToPdfCheque.cs @@ -0,0 +1,119 @@ +using BusinessLogic.OfficePackage.HelperEnums; +using BusinessLogic.OfficePackage.HelperModels; +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Shapes; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using PdfSharp.Drawing; +using PdfSharp.Pdf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BusinessLogic.OfficePackage.Implements +{ + public class SaveToPdfCheque : AbstractSaveToPdfCheque + { + private Document? _document; + private Section? _section; + private PdfPage? _page; + 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; + } + XImage image = XImage.FromFile(path); + _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/BindingModels/ReportBindingModel.cs b/Contracts/BindingModels/ReportBindingModel.cs index 0a77572..406e0c5 100644 --- a/Contracts/BindingModels/ReportBindingModel.cs +++ b/Contracts/BindingModels/ReportBindingModel.cs @@ -10,6 +10,7 @@ namespace Contracts.BindingModels { public string FileName { get; set; } = string.Empty; public DateTime? Date { get; set; } - public Guid SupplyId { get; set; } + public Guid? SupplyId { get; set; } + public Guid? ProductId { get; set; } } } diff --git a/Contracts/BusinessLogicContracts/IReportLogic.cs b/Contracts/BusinessLogicContracts/IReportLogic.cs index c0707be..e4bd760 100644 --- a/Contracts/BusinessLogicContracts/IReportLogic.cs +++ b/Contracts/BusinessLogicContracts/IReportLogic.cs @@ -20,5 +20,6 @@ namespace Contracts.BusinessLogicContracts /// /// void SaveSuppliesToPdfFile(ReportBindingModel model); + void SaveProductToPdfFile(ReportBindingModel model); } } diff --git a/WinFormsApp/FormProducts.Designer.cs b/WinFormsApp/FormProducts.Designer.cs index a0e71a4..e8cc40c 100644 --- a/WinFormsApp/FormProducts.Designer.cs +++ b/WinFormsApp/FormProducts.Designer.cs @@ -45,6 +45,7 @@ textBoxName = new TextBox(); menuStrip1 = new MenuStrip(); медиаФайлыToolStripMenuItem = new ToolStripMenuItem(); + buttonPrintCheque = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); groupBoxControls.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); @@ -79,6 +80,7 @@ // groupBoxControls // groupBoxControls.BackColor = Color.Transparent; + groupBoxControls.Controls.Add(buttonPrintCheque); groupBoxControls.Controls.Add(buttonReadBarCode); groupBoxControls.Controls.Add(pictureBox1); groupBoxControls.Controls.Add(buttonGenerateBarCode); @@ -95,7 +97,7 @@ // // buttonReadBarCode // - buttonReadBarCode.Location = new Point(69, 379); + buttonReadBarCode.Location = new Point(69, 335); buttonReadBarCode.Name = "buttonReadBarCode"; buttonReadBarCode.Size = new Size(139, 52); buttonReadBarCode.TabIndex = 6; @@ -105,9 +107,9 @@ // // pictureBox1 // - pictureBox1.Location = new Point(69, 235); + pictureBox1.Location = new Point(69, 210); pictureBox1.Name = "pictureBox1"; - pictureBox1.Size = new Size(139, 112); + pictureBox1.Size = new Size(139, 119); pictureBox1.TabIndex = 5; pictureBox1.TabStop = false; // @@ -228,6 +230,16 @@ медиаФайлыToolStripMenuItem.Text = "Медиа файлы"; медиаФайлыToolStripMenuItem.Click += медиаФайлыToolStripMenuItem_Click; // + // 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; + // // FormProducts // AutoScaleDimensions = new SizeF(7F, 15F); @@ -273,5 +285,6 @@ private Button buttonReadBarCode; private MenuStrip menuStrip1; private ToolStripMenuItem медиаФайлыToolStripMenuItem; + private Button buttonPrintCheque; } } \ No newline at end of file diff --git a/WinFormsApp/FormProducts.cs b/WinFormsApp/FormProducts.cs index 9d62565..fdc9f32 100644 --- a/WinFormsApp/FormProducts.cs +++ b/WinFormsApp/FormProducts.cs @@ -22,10 +22,11 @@ namespace WinFormsApp private Guid? _id; private readonly ILogger _logger; private readonly IProductLogic _productLogic; + private readonly IReportLogic _reportLogic; private readonly BarcodeLogic _barcodeLogic; private BarcodeResults? _barcode; List _mediaFiles; - public FormProducts(ILogger logger, IProductLogic productLogic) + public FormProducts(ILogger logger, IProductLogic productLogic, IReportLogic reportLogic) { InitializeComponent(); _productLogic = productLogic; @@ -33,6 +34,7 @@ namespace WinFormsApp _barcodeLogic = new BarcodeLogic(); _barcode = null; _mediaFiles = new List(); + _reportLogic = reportLogic; } private void FormProducts_Load(object sender, EventArgs e) @@ -108,7 +110,7 @@ namespace WinFormsApp Id = lastProductCreated.Id, Name = lastProductCreated.Name, }, true); - + } _id = null; if (!operationResult) @@ -271,5 +273,35 @@ namespace WinFormsApp } } } + + private void buttonPrintCheque_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count != 1) return; + var service = Program.ServiceProvider?.GetService(typeof(FormPreviewPDF)); + 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); + } + form.src = System.IO.Path.GetFullPath(src); + if (form.ShowDialog() == DialogResult.OK) + { + IronPrint.Printer.PrintAsync(src); + } + } + } } } diff --git a/WinFormsApp/FormStatistics.cs b/WinFormsApp/FormStatistics.cs index 90a6b2f..d83a602 100644 --- a/WinFormsApp/FormStatistics.cs +++ b/WinFormsApp/FormStatistics.cs @@ -42,11 +42,12 @@ namespace WinFormsApp { var date = dateTimePickerTo.Value; chart.Series.Add("Pending"); - for (int i = 0; i < diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++) + for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++) { var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Pending).ToList().Count; DataPoint dataPoint = new DataPoint(); dataPoint.XValue = i; + dataPoint.IsVisibleInLegend = false; dataPoint.Label = $"{date.Year}.{date.Month}"; dataPoint.SetValueY(count); chart.Series["Pending"].Points.Add(dataPoint); @@ -57,11 +58,12 @@ namespace WinFormsApp { var date = dateTimePickerTo.Value; chart.Series.Add("Arriving"); - for (int i = 0; i < diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++) + for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++) { var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Arriving).ToList().Count; DataPoint dataPoint = new DataPoint(); dataPoint.XValue = i; + dataPoint.IsVisibleInLegend = false; dataPoint.Label = $"{date.Year}.{date.Month}"; dataPoint.SetValueY(count); chart.Series["Arriving"].Points.Add(dataPoint); @@ -72,11 +74,12 @@ namespace WinFormsApp { var date = dateTimePickerTo.Value; chart.Series.Add("Completed"); - for (int i = 0; i < diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++) + for (int i = 0; i <= diff_month(dateTimePickerFrom.Value, dateTimePickerTo.Value); i++) { var count = list.Where(x => x.Date.Month == date.Month && x.Status == SupplyStatus.Completed).ToList().Count; DataPoint dataPoint = new DataPoint(); dataPoint.XValue = i; + dataPoint.IsVisibleInLegend = false; dataPoint.Label = $"{date.Year}.{date.Month}"; dataPoint.SetValueY(count); chart.Series["Completed"].Points.Add(dataPoint); diff --git a/WinFormsApp/Program.cs b/WinFormsApp/Program.cs index 753ca38..12e8171 100644 --- a/WinFormsApp/Program.cs +++ b/WinFormsApp/Program.cs @@ -52,6 +52,7 @@ namespace WinFormsApp services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient();