не создаётся pdf

This commit is contained in:
RozhVan 2024-12-21 10:43:19 +04:00
parent 8a1b622b21
commit 07e82fc4a3
14 changed files with 57 additions and 55 deletions

View File

@ -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<ProductSelling> ProdutcSellings { get; private set; } = [];
public DateTime SellingDateTime { get; private set; }
public static Selling CreateSelling(int id, int gasmanId, int count, IEnumerable<ProductSelling> produtcSellings)
public static Selling CreateSelling(int id, int gasmanId, IEnumerable<ProductSelling> produtcSellings)
{
return new Selling
{
Id = id,
GasmanId = gasmanId,
Count = count,
SellingDateTime = DateTime.Now,
ProdutcSellings = produtcSellings
};
}
public static Selling CreateSelling(TempProductSelling tempProductSelling, IEnumerable<ProductSelling> produtcSellings)
{
return new Selling
{
Id = tempProductSelling.Id,
GasmanId = tempProductSelling.GasmanId,
SellingDateTime = tempProductSelling.SellingDateTime,
ProdutcSellings = produtcSellings
};
}
}

View File

@ -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;

View File

@ -103,7 +103,7 @@ namespace GasStation
{
try
{
_container.Resolve<FormSellDistRep>().ShowDialog();
_container.Resolve<FormSupplyDistRep>().ShowDialog();
}
catch (Exception ex)
{

View File

@ -41,7 +41,7 @@ namespace GasStation.Forms
}
if (comboBoxProduct.SelectedIndex < 0)
{
throw new Exception("Не выбран корм");
throw new Exception("Не выбран товар");
}
if (dateTimePickerEnd.Value <= dateTimePickerBegin.Value)
{

View File

@ -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();
}

View File

@ -1,6 +1,6 @@
namespace GasStation.Reports
{
partial class FormSellDistRep
partial class FormSupplyDistRep
{
/// <summary>
/// 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();
}

View File

@ -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));

View File

@ -5,13 +5,13 @@ namespace GasStation.Reports;
internal class ChartReport
{
private readonly ISellingRepository _sellingRepository;
private readonly ISupplyRepository _supplyRepository;
private readonly ILogger<ChartReport> _logger;
public ChartReport(ISellingRepository sellingRepository, ILogger<ChartReport> logger)
public ChartReport(ISupplyRepository supplyRepository, ILogger<ChartReport> 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();
}
}

View File

@ -79,7 +79,7 @@ internal class DocReport
private List<string[]> GetProducts()
{
return [
["Тип корма", "Название"],
["Стоимость товара", "Тип товара"],
.. _productRepository
.ReadProduct()
.Select(x => new string[] { x.ProductCost.ToString(), x.ProductType.ToString() }),

View File

@ -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;

View File

@ -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

View File

@ -5,20 +5,15 @@ namespace GasStation.Reports;
internal class TableReport
{
private readonly ISupplyRepository _supplyRepository;
private readonly ISellingRepository _sellingRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = ["Сотрудник", "Дата", "Количество пришло", "Количество ушло"];
public TableReport(ISupplyRepository supplyRepository, ISellingRepository sellingRepository,
internal static readonly string[] item = ["Сотрудник", "Дата", "Продажа"];
public TableReport(ISellingRepository sellingRepository,
ILogger<TableReport> 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<string[]> 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);
return new List<string[]>() { 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();
}
}

View File

@ -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<int>(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<TempProductSelling>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(selling));
return selling.GroupBy(x => x.Id, y => y, (key, value) =>