From 07e82fc4a38a48a4fe83e61c8fd9c88c7e84770c Mon Sep 17 00:00:00 2001 From: RozhVan Date: Sat, 21 Dec 2024 10:43:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0?= =?UTF-8?q?=D1=91=D1=82=D1=81=D1=8F=20pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GasStation/Entities/Selling.cs | 20 ++++++++--- GasStation/FormGasstation.Designer.cs | 1 + GasStation/FormGasstation.cs | 2 +- GasStation/Forms/FormProductReport.cs | 2 +- GasStation/Forms/FormSelling.cs | 3 +- ...igner.cs => FormSupplyDistRep.Designer.cs} | 8 ++--- ...ormSellDistRep.cs => FormSupplyDistRep.cs} | 4 +-- ...ellDistRep.resx => FormSupplyDistRep.resx} | 0 GasStation/Reports/ChartReport.cs | 20 +++++------ GasStation/Reports/DocReport.cs | 2 +- GasStation/Reports/ExcelBuilder.cs | 8 ++--- GasStation/Reports/PdfBuilder.cs | 3 ++ GasStation/Reports/TableReport.cs | 33 +++++++------------ .../Implementations/SellingRepository.cs | 6 ++-- 14 files changed, 57 insertions(+), 55 deletions(-) rename GasStation/Forms/{FormSellDistRep.Designer.cs => FormSupplyDistRep.Designer.cs} (95%) rename GasStation/Forms/{FormSellDistRep.cs => FormSupplyDistRep.cs} (94%) rename GasStation/Forms/{FormSellDistRep.resx => FormSupplyDistRep.resx} (100%) diff --git a/GasStation/Entities/Selling.cs b/GasStation/Entities/Selling.cs index 033282e..ce7e7fe 100644 --- a/GasStation/Entities/Selling.cs +++ b/GasStation/Entities/Selling.cs @@ -1,4 +1,6 @@ -namespace GasStation.Entities; +using System.Collections; + +namespace GasStation.Entities; public class Selling { @@ -6,21 +8,29 @@ public class Selling public int GasmanId { get; private set; } - public int Count { get; private set; } - public IEnumerable ProdutcSellings { get; private set; } = []; public DateTime SellingDateTime { get; private set; } - public static Selling CreateSelling(int id, int gasmanId, int count, IEnumerable produtcSellings) + public static Selling CreateSelling(int id, int gasmanId, IEnumerable produtcSellings) { return new Selling { Id = id, GasmanId = gasmanId, - Count = count, SellingDateTime = DateTime.Now, ProdutcSellings = produtcSellings }; } + + public static Selling CreateSelling(TempProductSelling tempProductSelling, IEnumerable produtcSellings) + { + return new Selling + { + Id = tempProductSelling.Id, + GasmanId = tempProductSelling.GasmanId, + SellingDateTime = tempProductSelling.SellingDateTime, + ProdutcSellings = produtcSellings + }; + } } diff --git a/GasStation/FormGasstation.Designer.cs b/GasStation/FormGasstation.Designer.cs index 6c8ec56..6abd09d 100644 --- a/GasStation/FormGasstation.Designer.cs +++ b/GasStation/FormGasstation.Designer.cs @@ -120,6 +120,7 @@ // ProductReportToolStripMenuItem // ProductReportToolStripMenuItem.Name = "ProductReportToolStripMenuItem"; + ProductReportToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.E; ProductReportToolStripMenuItem.Size = new Size(280, 22); ProductReportToolStripMenuItem.Text = "Движение товаров"; ProductReportToolStripMenuItem.Click += ProductReportToolStripMenuItem_Click; diff --git a/GasStation/FormGasstation.cs b/GasStation/FormGasstation.cs index dcc54a9..4fb0fd4 100644 --- a/GasStation/FormGasstation.cs +++ b/GasStation/FormGasstation.cs @@ -103,7 +103,7 @@ namespace GasStation { try { - _container.Resolve().ShowDialog(); + _container.Resolve().ShowDialog(); } catch (Exception ex) { diff --git a/GasStation/Forms/FormProductReport.cs b/GasStation/Forms/FormProductReport.cs index 8a0d927..f11f885 100644 --- a/GasStation/Forms/FormProductReport.cs +++ b/GasStation/Forms/FormProductReport.cs @@ -41,7 +41,7 @@ namespace GasStation.Forms } if (comboBoxProduct.SelectedIndex < 0) { - throw new Exception("Не выбран корм"); + throw new Exception("Не выбран товар"); } if (dateTimePickerEnd.Value <= dateTimePickerBegin.Value) { diff --git a/GasStation/Forms/FormSelling.cs b/GasStation/Forms/FormSelling.cs index 94e19a8..3ca6712 100644 --- a/GasStation/Forms/FormSelling.cs +++ b/GasStation/Forms/FormSelling.cs @@ -33,8 +33,7 @@ namespace GasStation.Forms throw new Exception("Имеются незаполненые поля"); } - _sellingRepository.CreateSelling(Selling.CreateSelling(0, (int)comboBoxGasman.SelectedValue!, - 0, CreateProductSellingsFromDataGrid())); + _sellingRepository.CreateSelling(Selling.CreateSelling(0, (int)comboBoxGasman.SelectedValue!, CreateProductSellingsFromDataGrid())); Close(); } diff --git a/GasStation/Forms/FormSellDistRep.Designer.cs b/GasStation/Forms/FormSupplyDistRep.Designer.cs similarity index 95% rename from GasStation/Forms/FormSellDistRep.Designer.cs rename to GasStation/Forms/FormSupplyDistRep.Designer.cs index 74894d5..78c6898 100644 --- a/GasStation/Forms/FormSellDistRep.Designer.cs +++ b/GasStation/Forms/FormSupplyDistRep.Designer.cs @@ -1,6 +1,6 @@ namespace GasStation.Reports { - partial class FormSellDistRep + partial class FormSupplyDistRep { /// /// Required designer variable. @@ -80,7 +80,7 @@ labelFileName.TabIndex = 4; labelFileName.Text = "Файл"; // - // FormSellDistRep + // FormSupplyDistRep // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; @@ -90,8 +90,8 @@ Controls.Add(dateTimePicker); Controls.Add(buttonCreate); Controls.Add(buttonSelectFileName); - Name = "FormSellDistRep"; - Text = "Распределение товара"; + Name = "FormSupplyDistRep"; + Text = "Поступление товара"; ResumeLayout(false); PerformLayout(); } diff --git a/GasStation/Forms/FormSellDistRep.cs b/GasStation/Forms/FormSupplyDistRep.cs similarity index 94% rename from GasStation/Forms/FormSellDistRep.cs rename to GasStation/Forms/FormSupplyDistRep.cs index 87fcf97..b291ffc 100644 --- a/GasStation/Forms/FormSellDistRep.cs +++ b/GasStation/Forms/FormSupplyDistRep.cs @@ -2,13 +2,13 @@ namespace GasStation.Reports; -public partial class FormSellDistRep : Form +public partial class FormSupplyDistRep : Form { private string _fileName = string.Empty; private readonly IUnityContainer _container; - public FormSellDistRep(IUnityContainer container) + public FormSupplyDistRep(IUnityContainer container) { InitializeComponent(); _container = container ?? throw new ArgumentNullException(nameof(container)); diff --git a/GasStation/Forms/FormSellDistRep.resx b/GasStation/Forms/FormSupplyDistRep.resx similarity index 100% rename from GasStation/Forms/FormSellDistRep.resx rename to GasStation/Forms/FormSupplyDistRep.resx diff --git a/GasStation/Reports/ChartReport.cs b/GasStation/Reports/ChartReport.cs index 0feaf83..b4ba4dd 100644 --- a/GasStation/Reports/ChartReport.cs +++ b/GasStation/Reports/ChartReport.cs @@ -5,13 +5,13 @@ namespace GasStation.Reports; internal class ChartReport { - private readonly ISellingRepository _sellingRepository; + private readonly ISupplyRepository _supplyRepository; private readonly ILogger _logger; - public ChartReport(ISellingRepository sellingRepository, ILogger logger) + public ChartReport(ISupplyRepository supplyRepository, ILogger logger) { - _sellingRepository = sellingRepository ?? + _supplyRepository = supplyRepository ?? throw new - ArgumentNullException(nameof(sellingRepository)); + ArgumentNullException(nameof(supplyRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } @@ -20,8 +20,8 @@ internal class ChartReport try { new PdfBuilder(filePath) - .AddHeader("Продажа продукта") - .AddPieChart("Проданный товар", GetData(dateTime)) + .AddHeader("Поступление продукта") + .AddPieChart("Поступивший товар", GetData(dateTime)) .Build(); return true; } @@ -33,10 +33,10 @@ internal class ChartReport } private List<(string Caption, double Value)> GetData(DateTime dateTime) { - return _sellingRepository - .ReadSelling() - .Where(x => x.SellingDateTime.Date == dateTime.Date) - .GroupBy(x => x.GasmanId, (key, group) => new { Id = key, Count = group.Sum(x => x.Count)}) + return _supplyRepository + .ReadSupply() + .Where(x => x.SupplyDate.Date == dateTime.Date) + .GroupBy(x => x.ProductID, (key, group) => new { Id = key, Count = group.Sum(x => x.Count)}) .Select(x => (x.Id.ToString(), (double)x.Count)).ToList(); } } diff --git a/GasStation/Reports/DocReport.cs b/GasStation/Reports/DocReport.cs index c28edf4..e1183f6 100644 --- a/GasStation/Reports/DocReport.cs +++ b/GasStation/Reports/DocReport.cs @@ -79,7 +79,7 @@ internal class DocReport private List GetProducts() { return [ - ["Тип корма", "Название"], + ["Стоимость товара", "Тип товара"], .. _productRepository .ReadProduct() .Select(x => new string[] { x.ProductCost.ToString(), x.ProductType.ToString() }), diff --git a/GasStation/Reports/ExcelBuilder.cs b/GasStation/Reports/ExcelBuilder.cs index 783f87f..88018ea 100644 --- a/GasStation/Reports/ExcelBuilder.cs +++ b/GasStation/Reports/ExcelBuilder.cs @@ -30,7 +30,7 @@ internal class ExcelBuilder public ExcelBuilder AddHeader(string header, int startIndex, int count) { CreateCell(startIndex, _rowIndex, header, - StyleIndex.SimpleTextWithoutBorder); + StyleIndex.BoldTextWithoutBorder); for (int i = startIndex + 1; i < startIndex + count; ++i) { CreateCell(i, _rowIndex, "", @@ -79,7 +79,7 @@ internal class ExcelBuilder for (var j = 0; j < data.First().Length; ++j) { CreateCell(j, _rowIndex, data.First()[j], - StyleIndex.SimpleTextWithoutBorder); + StyleIndex.BoldTextWithBorder); } _rowIndex++; for (var i = 1; i < data.Count - 1; ++i) @@ -87,14 +87,14 @@ internal class ExcelBuilder for (var j = 0; j < data[i].Length; ++j) { CreateCell(j, _rowIndex, data[i][j], - StyleIndex.SimpleTextWithoutBorder); + StyleIndex.SimpleTextWithBorder); } _rowIndex++; } for (var j = 0; j < data.Last().Length; ++j) { CreateCell(j, _rowIndex, data.Last()[j], - StyleIndex.SimpleTextWithoutBorder); + StyleIndex.BoldTextWithBorder); } _rowIndex++; return this; diff --git a/GasStation/Reports/PdfBuilder.cs b/GasStation/Reports/PdfBuilder.cs index 6548ccb..7689b4b 100644 --- a/GasStation/Reports/PdfBuilder.cs +++ b/GasStation/Reports/PdfBuilder.cs @@ -1,6 +1,7 @@ using MigraDoc.DocumentObjectModel; using MigraDoc.DocumentObjectModel.Shapes.Charts; using MigraDoc.Rendering; +using System.Text; namespace GasStation.Reports; @@ -9,6 +10,7 @@ internal class PdfBuilder private readonly string _filePath; private readonly Document _document; + public PdfBuilder(string filePath) { if (string.IsNullOrWhiteSpace(filePath)) @@ -58,6 +60,7 @@ internal class PdfBuilder } public void Build() { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var renderer = new PdfDocumentRenderer(true) { Document = _document diff --git a/GasStation/Reports/TableReport.cs b/GasStation/Reports/TableReport.cs index c8e5d44..d2f83e1 100644 --- a/GasStation/Reports/TableReport.cs +++ b/GasStation/Reports/TableReport.cs @@ -5,20 +5,15 @@ namespace GasStation.Reports; internal class TableReport { - private readonly ISupplyRepository _supplyRepository; private readonly ISellingRepository _sellingRepository; private readonly ILogger _logger; - internal static readonly string[] item = ["Сотрудник", "Дата", "Количество пришло", "Количество ушло"]; - public TableReport(ISupplyRepository supplyRepository, ISellingRepository sellingRepository, + internal static readonly string[] item = ["Сотрудник", "Дата", "Продажа"]; + public TableReport(ISellingRepository sellingRepository, ILogger logger) { - _supplyRepository = supplyRepository ?? - throw new - ArgumentNullException(nameof(supplyRepository)); _sellingRepository = sellingRepository ?? - throw new - ArgumentNullException(nameof(sellingRepository)); + throw new ArgumentNullException(nameof(sellingRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } @@ -27,9 +22,9 @@ internal class TableReport try { new ExcelBuilder(filePath) - .AddHeader("Сводка по движению товара", 0, 4) + .AddHeader("Сводка по движению товара", 0, 3) .AddParagraph("за период", 0) - .AddTable([10, 10, 15, 15], GetData(productId, startDate, endDate)) + .AddTable([10, 10, 15], GetData(productId, startDate, endDate)) .Build(); return true; } @@ -39,27 +34,21 @@ internal class TableReport return false; } } + private List GetData(int productId, DateTime startDate, DateTime endDate) { - var data = _supplyRepository - .ReadSupply() - .Where(x => x.SupplyDate >= startDate && x.SupplyDate <= endDate) - .Select(x => new { x.SupplierID, Date = x.SupplyDate, CountIn = (int?)x.Count, CountOut = (int?)null }) - .Union( - _sellingRepository + var data = _sellingRepository .ReadSelling() .Where(x => x.SellingDateTime >= startDate && x.SellingDateTime <= endDate && x.ProdutcSellings.Any(y => y.ProductID == productId)) - .Select(x => new {x.GasmanId, Date = x.SellingDateTime, CountIn = (int?)null, + .Select(x => new {x.GasmanId, Date = x.SellingDateTime, CountOut = x.ProdutcSellings.FirstOrDefault(y => y.ProductID == productId)?.Count }) - ) - .OrderBy(x => x.Date); + .OrderBy(x => x.Date); return new List() { item }.Union(data .Select(x => - new string[] { x.GasmanId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? - string.Empty, x.CountOut?.ToString() ?? string.Empty})) + new string[] { x.GasmanId.ToString(), x.Date.ToString(), x.CountOut?.ToString() ?? string.Empty})) .Union( - [["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]] + [["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString()]] ).ToList(); } } diff --git a/GasStation/Repositories/Implementations/SellingRepository.cs b/GasStation/Repositories/Implementations/SellingRepository.cs index 10230c0..962a677 100644 --- a/GasStation/Repositories/Implementations/SellingRepository.cs +++ b/GasStation/Repositories/Implementations/SellingRepository.cs @@ -29,8 +29,8 @@ public class SellingRepository : ISellingRepository connection.Open(); using var transaction = connection.BeginTransaction(); var queryInsert = @" -INSERT INTO selling (sellingDateTime, gasmanId, count) -VALUES (@SellingDateTime, @GasmanId, @Count); +INSERT INTO selling (sellingDateTime, gasmanId) +VALUES (@SellingDateTime, @GasmanId); SELECT MAX(Id) FROM selling"; var Id = connection.QueryFirst(queryInsert, selling, transaction); var querySubInsert = @" @@ -57,7 +57,7 @@ VALUES (@Id, @ProductID, @Count)"; using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = @" SELECT s.*, ps.ProductID, ps.Count FROM selling s -INNER JOIN ProductSelling ps ON ps.SellingId = s.Id"; +INNER JOIN Product_Selling ps ON ps.Id = s.Id"; var selling = connection.Query(querySelect); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(selling)); return selling.GroupBy(x => x.Id, y => y, (key, value) =>