From 0bffe6af3198232db3aadf75e61987c390e41d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=20=D0=9F=D0=B5=D1=80=D0=BC=D1=8F?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2?= Date: Mon, 9 Dec 2024 14:17:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=203?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormGeneral.Designer.cs | 12 +- .../RealEstateTransactions/FormGeneral.cs | 12 ++ .../Forms/FormChartReport.Designer.cs | 108 ++++++++++++++++ .../Forms/FormChartReport.cs | 66 ++++++++++ .../Forms/FormChartReport.resx | 120 ++++++++++++++++++ .../Reports/ChartReport.cs | 45 +++++++ .../Reports/PdfBuilder.cs | 80 ++++++++++++ 7 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.Designer.cs create mode 100644 RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.cs create mode 100644 RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.resx create mode 100644 RealEstateTransactions/RealEstateTransactions/Reports/ChartReport.cs create mode 100644 RealEstateTransactions/RealEstateTransactions/Reports/PdfBuilder.cs diff --git a/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs b/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs index ef7a25a..b785034 100644 --- a/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs +++ b/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs @@ -39,6 +39,7 @@ отчётToolStripMenuItem = new ToolStripMenuItem(); DirectoryReportToolStripMenuItem = new ToolStripMenuItem(); PriceReportToolStripMenuItem = new ToolStripMenuItem(); + ChartReportToolStripMenuItem1 = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); SuspendLayout(); // @@ -103,7 +104,7 @@ // // отчётToolStripMenuItem // - отчётToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { DirectoryReportToolStripMenuItem, PriceReportToolStripMenuItem }); + отчётToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { DirectoryReportToolStripMenuItem, PriceReportToolStripMenuItem, ChartReportToolStripMenuItem1 }); отчётToolStripMenuItem.Name = "отчётToolStripMenuItem"; отчётToolStripMenuItem.Size = new Size(73, 24); отчётToolStripMenuItem.Text = "Отчёты"; @@ -124,6 +125,14 @@ PriceReportToolStripMenuItem.Text = "Документ по доходам"; PriceReportToolStripMenuItem.Click += PriceReportToolStripMenuItem_Click; // + // ChartReportToolStripMenuItem1 + // + ChartReportToolStripMenuItem1.Name = "ChartReportToolStripMenuItem1"; + ChartReportToolStripMenuItem1.ShortcutKeys = Keys.Control | Keys.P; + ChartReportToolStripMenuItem1.Size = new Size(343, 26); + ChartReportToolStripMenuItem1.Text = "Документ по сделкам"; + ChartReportToolStripMenuItem1.Click += ChartReportToolStripMenuItem1_Click; + // // FormGeneral // AutoScaleDimensions = new SizeF(8F, 20F); @@ -156,5 +165,6 @@ private ToolStripMenuItem сделкиToolStripMenuItem; private ToolStripMenuItem DirectoryReportToolStripMenuItem; private ToolStripMenuItem PriceReportToolStripMenuItem; + private ToolStripMenuItem ChartReportToolStripMenuItem1; } } diff --git a/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs b/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs index 96bd90a..e0e8e92 100644 --- a/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs +++ b/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs @@ -97,5 +97,17 @@ namespace RealEstateTransactions MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + private void ChartReportToolStripMenuItem1_Click(object sender, EventArgs e) + { + try + { + _container.Resolve().ShowDialog(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } } \ No newline at end of file diff --git a/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.Designer.cs b/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.Designer.cs new file mode 100644 index 0000000..38bb67d --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.Designer.cs @@ -0,0 +1,108 @@ +namespace RealEstateTransactions.Forms +{ + partial class FormChartReport + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + buttonSelectFilePath = new Button(); + labelFilePath = new Label(); + labelDate = new Label(); + dateTimePicker = new DateTimePicker(); + buttonMakeReport = new Button(); + SuspendLayout(); + // + // buttonSelectFilePath + // + buttonSelectFilePath.Location = new Point(12, 12); + buttonSelectFilePath.Name = "buttonSelectFilePath"; + buttonSelectFilePath.Size = new Size(94, 29); + buttonSelectFilePath.TabIndex = 0; + buttonSelectFilePath.Text = "Выбрать"; + buttonSelectFilePath.UseVisualStyleBackColor = true; + buttonSelectFilePath.Click += ButtonSelectFilePath_Click; + // + // labelFilePath + // + labelFilePath.AutoSize = true; + labelFilePath.Location = new Point(123, 16); + labelFilePath.Name = "labelFilePath"; + labelFilePath.Size = new Size(52, 20); + labelFilePath.TabIndex = 1; + labelFilePath.Text = "Файл: "; + // + // labelDate + // + labelDate.AutoSize = true; + labelDate.Location = new Point(12, 67); + labelDate.Name = "labelDate"; + labelDate.Size = new Size(48, 20); + labelDate.TabIndex = 2; + labelDate.Text = "Дата: "; + // + // dateTimePicker + // + dateTimePicker.Location = new Point(86, 62); + dateTimePicker.Name = "dateTimePicker"; + dateTimePicker.Size = new Size(214, 27); + dateTimePicker.TabIndex = 1; + // + // buttonMakeReport + // + buttonMakeReport.Location = new Point(92, 113); + buttonMakeReport.Name = "buttonMakeReport"; + buttonMakeReport.Size = new Size(136, 29); + buttonMakeReport.TabIndex = 2; + buttonMakeReport.Text = "сформировать"; + buttonMakeReport.UseVisualStyleBackColor = true; + buttonMakeReport.Click += ButtonMakeReport_Click; + // + // FormChartReport + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(328, 164); + Controls.Add(buttonMakeReport); + Controls.Add(dateTimePicker); + Controls.Add(labelDate); + Controls.Add(labelFilePath); + Controls.Add(buttonSelectFilePath); + Name = "FormChartReport"; + StartPosition = FormStartPosition.CenterParent; + Text = "Отчёт о доходе"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonSelectFilePath; + private Label labelFilePath; + private Label labelDate; + private DateTimePicker dateTimePicker; + private Button buttonMakeReport; + } +} \ No newline at end of file diff --git a/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.cs b/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.cs new file mode 100644 index 0000000..2ec46e7 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.cs @@ -0,0 +1,66 @@ +using RealEstateTransactions.Reports; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Unity; + +namespace RealEstateTransactions.Forms +{ + public partial class FormChartReport : Form + { + private string _fileName = string.Empty; + + private readonly IUnityContainer _container; + + public FormChartReport(IUnityContainer container) + { + InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); + } + + private void ButtonSelectFilePath_Click(object sender, EventArgs e) + { + var sfd = new SaveFileDialog() + { + Filter = "Pdf Files | *.pdf" + }; + + if (sfd.ShowDialog() == DialogResult.OK) + { + _fileName = sfd.FileName; + labelFilePath.Text = Path.GetFileName(_fileName); + } + } + + private void ButtonMakeReport_Click(object sender, EventArgs e) + { + try + { + if (string.IsNullOrWhiteSpace(_fileName)) + throw new Exception("Отсутствует имя файла для отчета"); + + if (_container.Resolve().CreateChart(_fileName, dateTimePicker.Value)) + { + MessageBox.Show("Документ сформирован", "Формирование документа", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Возникли ошибки при формировании документа.Подробности в логах", + "Формирование документа", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при создании очета", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.resx b/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Forms/FormChartReport.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/RealEstateTransactions/RealEstateTransactions/Reports/ChartReport.cs b/RealEstateTransactions/RealEstateTransactions/Reports/ChartReport.cs new file mode 100644 index 0000000..fa41d02 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Reports/ChartReport.cs @@ -0,0 +1,45 @@ +using Microsoft.Extensions.Logging; +using RealEstateTransactions.Repositories; + +namespace RealEstateTransactions.Reports +{ + public class ChartReport + { + private readonly IDealRepository _dealRepository; + + private readonly ILogger _logger; + + public ChartReport(IDealRepository dealRepository, ILogger logger) + { + _dealRepository = dealRepository ?? throw new + ArgumentNullException(nameof(dealRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + public bool CreateChart(string filePath, DateTime dateTime) + { + try + { + new PdfBuilder(filePath) + .AddHeader("Доход с продажи квартир") + .AddPieChart("Проданные квартиры", GetData(dateTime)) + .Build(); + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при формировании документа"); + return false; + } + } + + private List<(string Caption, double Value)> GetData(DateTime dateTime) + { + return _dealRepository + .ReadDeals() + .Where(x => x.Deal_date.Date == dateTime.Date) + .Select(x => (x.Apartment_ID.ToString(), (double)x.Deal_price)) + .ToList(); + } + } +} \ No newline at end of file diff --git a/RealEstateTransactions/RealEstateTransactions/Reports/PdfBuilder.cs b/RealEstateTransactions/RealEstateTransactions/Reports/PdfBuilder.cs new file mode 100644 index 0000000..b65b244 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Reports/PdfBuilder.cs @@ -0,0 +1,80 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Shapes.Charts; +using MigraDoc.Rendering; +using System.Text; + +namespace RealEstateTransactions.Reports +{ + public class PdfBuilder + { + private readonly string _filePath; + + private readonly Document _document; + + public PdfBuilder(string filePath) + { + if (string.IsNullOrWhiteSpace(filePath)) + throw new ArgumentNullException(nameof(filePath)); + + if (File.Exists(filePath)) File.Delete(filePath); + + _filePath = filePath; + _document = new Document(); + DefineStyles(); + } + + public PdfBuilder AddHeader(string header) + { + _document.AddSection().AddParagraph(header, "NormalBold"); + + return this; + } + + public PdfBuilder AddPieChart(string title, List<(string Caption, double + Value)> data) + { + if (data == null || data.Count == 0) return this; + + var chart = new Chart(ChartType.Pie2D); + var series = chart.SeriesCollection.AddSeries(); + series.Add(data.Select(x => x.Value).ToArray()); + var xseries = chart.XValues.AddXSeries(); + xseries.Add(data.Select(x => x.Caption).ToArray()); + chart.DataLabel.Type = DataLabelType.Percent; + chart.DataLabel.Position = DataLabelPosition.OutsideEnd; + chart.Width = Unit.FromCentimeter(16); + chart.Height = Unit.FromCentimeter(12); + chart.TopArea.AddParagraph(title); + chart.XAxis.MajorTickMark = TickMarkType.Outside; + chart.YAxis.MajorTickMark = TickMarkType.Outside; + chart.YAxis.HasMajorGridlines = true; + chart.PlotArea.LineFormat.Width = 1; + chart.PlotArea.LineFormat.Visible = true; + chart.TopArea.AddLegend(); + _document.LastSection.Add(chart); + + return this; + } + + public void Build() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + + renderer.RenderDocument(); + renderer.PdfDocument.Save(_filePath); + } + + private void DefineStyles() + { + var headerStyle = _document.Styles.AddStyle("Normal Bold", "Normal"); + headerStyle.Font.Bold = true; + headerStyle.Font.Size = 14; + } + + } +} \ No newline at end of file