diff --git a/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs b/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs index b21203d..e47545e 100644 --- a/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs +++ b/RealEstateTransactions/RealEstateTransactions/FormGeneral.Designer.cs @@ -37,6 +37,7 @@ допродажныеУслугиToolStripMenuItem = new ToolStripMenuItem(); сделкиToolStripMenuItem = new ToolStripMenuItem(); отчётToolStripMenuItem = new ToolStripMenuItem(); + DirectoryReportToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); SuspendLayout(); // @@ -60,21 +61,21 @@ // квартирыToolStripMenuItem // квартирыToolStripMenuItem.Name = "квартирыToolStripMenuItem"; - квартирыToolStripMenuItem.Size = new Size(224, 26); + квартирыToolStripMenuItem.Size = new Size(174, 26); квартирыToolStripMenuItem.Text = "Квартиры"; квартирыToolStripMenuItem.Click += КвартирыToolStripMenuItem_Click; // // покупателиToolStripMenuItem // покупателиToolStripMenuItem.Name = "покупателиToolStripMenuItem"; - покупателиToolStripMenuItem.Size = new Size(224, 26); + покупателиToolStripMenuItem.Size = new Size(174, 26); покупателиToolStripMenuItem.Text = "Покупатели"; покупателиToolStripMenuItem.Click += ПокупателиToolStripMenuItem_Click; // // услугиToolStripMenuItem // услугиToolStripMenuItem.Name = "услугиToolStripMenuItem"; - услугиToolStripMenuItem.Size = new Size(224, 26); + услугиToolStripMenuItem.Size = new Size(174, 26); услугиToolStripMenuItem.Text = "Услуги"; услугиToolStripMenuItem.Click += УслугиToolStripMenuItem_Click; // @@ -101,10 +102,19 @@ // // отчётToolStripMenuItem // + отчётToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { DirectoryReportToolStripMenuItem }); отчётToolStripMenuItem.Name = "отчётToolStripMenuItem"; отчётToolStripMenuItem.Size = new Size(73, 24); отчётToolStripMenuItem.Text = "Отчёты"; // + // DirectoryReportToolStripMenuItem + // + DirectoryReportToolStripMenuItem.Name = "DirectoryReportToolStripMenuItem"; + DirectoryReportToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.W; + DirectoryReportToolStripMenuItem.Size = new Size(343, 26); + DirectoryReportToolStripMenuItem.Text = "Документ по справочникам"; + DirectoryReportToolStripMenuItem.Click += DirectoryReportToolStripMenuItem_Click; + // // FormGeneral // AutoScaleDimensions = new SizeF(8F, 20F); @@ -135,5 +145,6 @@ private ToolStripMenuItem отчётToolStripMenuItem; private ToolStripMenuItem услугиToolStripMenuItem; private ToolStripMenuItem сделкиToolStripMenuItem; + private ToolStripMenuItem DirectoryReportToolStripMenuItem; } } diff --git a/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs b/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs index ba33777..3a92158 100644 --- a/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs +++ b/RealEstateTransactions/RealEstateTransactions/FormGeneral.cs @@ -73,5 +73,17 @@ namespace RealEstateTransactions MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + private void DirectoryReportToolStripMenuItem_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/FormDirectoryReport.Designer.cs b/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.Designer.cs new file mode 100644 index 0000000..6903d52 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.Designer.cs @@ -0,0 +1,104 @@ +namespace RealEstateTransactions.Forms +{ + partial class FormDirectoryReport + { + /// + /// 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() + { + checkBoxApartment = new CheckBox(); + checkBoxBuyer = new CheckBox(); + checkBoxServices = new CheckBox(); + buttonBuild = new Button(); + SuspendLayout(); + // + // checkBoxApartment + // + checkBoxApartment.AutoSize = true; + checkBoxApartment.Font = new Font("Segoe UI", 14F); + checkBoxApartment.Location = new Point(39, 22); + checkBoxApartment.Name = "checkBoxApartment"; + checkBoxApartment.Size = new Size(144, 36); + checkBoxApartment.TabIndex = 0; + checkBoxApartment.Text = "Квартиры"; + checkBoxApartment.UseVisualStyleBackColor = true; + // + // checkBoxBuyer + // + checkBoxBuyer.AutoSize = true; + checkBoxBuyer.Font = new Font("Segoe UI", 14F); + checkBoxBuyer.Location = new Point(39, 84); + checkBoxBuyer.Name = "checkBoxBuyer"; + checkBoxBuyer.Size = new Size(167, 36); + checkBoxBuyer.TabIndex = 1; + checkBoxBuyer.Text = "Покупатели"; + checkBoxBuyer.UseVisualStyleBackColor = true; + // + // checkBoxServices + // + checkBoxServices.AutoSize = true; + checkBoxServices.Font = new Font("Segoe UI", 14F); + checkBoxServices.Location = new Point(39, 144); + checkBoxServices.Name = "checkBoxServices"; + checkBoxServices.Size = new Size(109, 36); + checkBoxServices.TabIndex = 2; + checkBoxServices.Text = "Услуги"; + checkBoxServices.UseVisualStyleBackColor = true; + // + // buttonBuild + // + buttonBuild.Font = new Font("Segoe UI", 14F); + buttonBuild.Location = new Point(12, 204); + buttonBuild.Name = "buttonBuild"; + buttonBuild.Size = new Size(222, 72); + buttonBuild.TabIndex = 3; + buttonBuild.Text = "Сформировать"; + buttonBuild.UseVisualStyleBackColor = true; + buttonBuild.Click += ButtonBuild_Click; + // + // FormDirectoryReport + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(246, 288); + Controls.Add(buttonBuild); + Controls.Add(checkBoxServices); + Controls.Add(checkBoxBuyer); + Controls.Add(checkBoxApartment); + Name = "FormDirectoryReport"; + StartPosition = FormStartPosition.CenterParent; + Text = "Выгрузка"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private CheckBox checkBoxApartment; + private CheckBox checkBoxBuyer; + private CheckBox checkBoxServices; + private Button buttonBuild; + } +} \ No newline at end of file diff --git a/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.cs b/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.cs new file mode 100644 index 0000000..4d4f316 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.cs @@ -0,0 +1,65 @@ +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 FormDirectoryReport : Form + { + private readonly IUnityContainer _container; + + public FormDirectoryReport(IUnityContainer container) + { + InitializeComponent(); + _container = container ?? throw new + ArgumentNullException(nameof(container)); + } + + private void ButtonBuild_Click(object sender, EventArgs e) + { + try + { + if (!checkBoxApartment.Checked && + !checkBoxBuyer.Checked && !checkBoxServices.Checked) + { + throw new Exception("Не выбран ни один справочник для выгрузки"); + } + + var sfd = new SaveFileDialog() + { + Filter = "Docx Files | *.docx" + }; + + if (sfd.ShowDialog() != DialogResult.OK) + { + throw new Exception("Не выбран файла для отчета"); + } + + if (_container.Resolve().CreateDoc(sfd.FileName, checkBoxApartment.Checked, + checkBoxBuyer.Checked, checkBoxServices.Checked)) + { + 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/FormDirectoryReport.resx b/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Forms/FormDirectoryReport.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/RealEstateTransactions.csproj b/RealEstateTransactions/RealEstateTransactions/RealEstateTransactions.csproj index b94bfad..3cd7071 100644 --- a/RealEstateTransactions/RealEstateTransactions/RealEstateTransactions.csproj +++ b/RealEstateTransactions/RealEstateTransactions/RealEstateTransactions.csproj @@ -10,11 +10,13 @@ + + diff --git a/RealEstateTransactions/RealEstateTransactions/Reports/DocReport.cs b/RealEstateTransactions/RealEstateTransactions/Reports/DocReport.cs new file mode 100644 index 0000000..d915bad --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Reports/DocReport.cs @@ -0,0 +1,88 @@ +using Microsoft.Extensions.Logging; +using RealEstateTransactions.Repositories; + +namespace RealEstateTransactions.Reports +{ + public class DocReport + { + private readonly IApartmentRepository _apartmentRepository; + private readonly IBuyerRepository _buyerRepository; + private readonly IServicesRepository _servicesRepository; + private readonly ILogger _logger; + + public DocReport(IApartmentRepository apartmentRepository, IBuyerRepository buyerRepository, + IServicesRepository servicesRepository, ILogger logger) + { + _apartmentRepository = apartmentRepository ?? throw new + ArgumentNullException(nameof(apartmentRepository)); + _buyerRepository = buyerRepository ?? throw new + ArgumentNullException(nameof(buyerRepository)); + _servicesRepository = servicesRepository ?? throw new + ArgumentNullException(nameof(servicesRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + public bool CreateDoc(string filePath, bool includeApartments, bool + includeBuyers, bool includeServices) + { + try + { + var builder = new WordBuilder(filePath) + .AddHeader("Документ со справочниками"); + if (includeApartments) + { + builder.AddParagraph("Квартиры") + .AddTable([1040, 1040, 1040, 1040, 1040, 1040], + GetApartments()); + } + if (includeBuyers) + { + builder.AddParagraph("Покупатели") + .AddTable([2400, 2400, 2400], GetBuyers()); + } + if (includeServices) + { + builder.AddParagraph("Услуги") + .AddTable([3600, 3600], GetServices()); + } + builder.Build(); + + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при формировании документа"); + return false; + } + } + private List GetApartments() + { + return [ + ["Номер агенства", "Комнаты", "Площадь", "Цена за кв. метр", "Базовая стоимость", "Желаемая цена продажи"], + .. _apartmentRepository + .ReadApartments() + .Select(x => new string[] { x.Agency_ID.ToString(), x.Form_factor_ID.ToString(), + x.Area.ToString(), x.Price_per_SM.ToString(), x.Base_price.ToString(), x.Desired_price.ToString() }), + ]; + } + private List GetBuyers() + { + return [ + ["ФИО", "Серия паспорта", "Номер паспорта"], + .. _buyerRepository + .ReadBuyers() + .Select(x => new string[] { x.Full_name, x.Passport_series.ToString(), + x.Passport_number.ToString() }), + ]; + } + private List GetServices() + { + return [ + ["Название", "Цена"], + .. _servicesRepository + .ReadServices() + .Select(x => new string[] { x.Name, x.Price.ToString() }), + ]; + } + } +} \ No newline at end of file diff --git a/RealEstateTransactions/RealEstateTransactions/Reports/WordBuilder.cs b/RealEstateTransactions/RealEstateTransactions/Reports/WordBuilder.cs new file mode 100644 index 0000000..ba982bb --- /dev/null +++ b/RealEstateTransactions/RealEstateTransactions/Reports/WordBuilder.cs @@ -0,0 +1,126 @@ +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; + +namespace RealEstateTransactions.Reports +{ + public class WordBuilder + { + private readonly string _filePath; + + private readonly Document _document; + + private readonly Body _body; + + public WordBuilder(string filePath) + { + if (string.IsNullOrWhiteSpace(filePath)) + throw new ArgumentNullException(nameof(filePath)); + + if (File.Exists(filePath)) File.Delete(filePath); + + _filePath = filePath; + _document = new Document(); + _body = _document.AppendChild(new Body()); + } + + public WordBuilder AddHeader(string header) + { + var paragraph = _body.AppendChild(new Paragraph()); + var run = paragraph.AppendChild(new Run(new RunProperties(new Bold()))); + run.AppendChild(new Text(header)); + + return this; + } + + public WordBuilder AddParagraph(string text) + { + var paragraph = _body.AppendChild(new Paragraph()); + var run = paragraph.AppendChild(new Run()); + run.AppendChild(new Text(text)); + + return this; + } + + public WordBuilder AddTable(int[] widths, List data) + { + if (widths == null || widths.Length == 0) + { + throw new ArgumentNullException(nameof(widths)); + } + if (data == null || data.Count == 0) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.Any(x => x.Length != widths.Length)) + { + throw new InvalidOperationException("widths.Length != data.Length"); + } + + var table = new Table(); + table.AppendChild(new TableProperties( + new TableBorders( + new TopBorder() + { + Val = new + EnumValue(BorderValues.Single), Size = 12 + }, + new BottomBorder() + { + Val = new + EnumValue(BorderValues.Single), Size = 12 + }, + new LeftBorder() + { + Val = new + EnumValue(BorderValues.Single), Size = 12 + }, + new RightBorder() + { + Val = new + EnumValue(BorderValues.Single), Size = 12 + }, + new InsideHorizontalBorder() + { + Val = new + EnumValue(BorderValues.Single), Size = 12 + }, + new InsideVerticalBorder() + { + Val = new + EnumValue(BorderValues.Single), Size = 12 + }) + )); + + var tr = new TableRow(); + for (var j = 0; j < widths.Length; ++j) + { + tr.Append(new TableCell( + new TableCellProperties(new TableCellWidth() + { + Width = + widths[j].ToString() + }), + new Paragraph(new Run(new RunProperties(new Bold()), new + Text(data.First()[j]))))); + } + table.Append(tr); + + table.Append(data.Skip(1).Select(x => + new TableRow(x.Select(y => new TableCell(new Paragraph(new + Run(new Text(y)))))))); + + _body.Append(table); + + return this; + } + + public void Build() + { + using var wordDocument = WordprocessingDocument.Create(_filePath, + WordprocessingDocumentType.Document); + var mainPart = wordDocument.AddMainDocumentPart(); + mainPart.Document = _document; + } + } +} \ No newline at end of file