PIbd-24 Antonova A.A. LabWork_4 #4
@ -12,9 +12,11 @@ public class ProductMovement
|
||||
{
|
||||
public int ID { get; private set; }
|
||||
|
||||
[DisplayName("Товар")]
|
||||
public int ProductID { get; private set; }
|
||||
|
||||
[DisplayName("Товар")]
|
||||
public string ProductName { get; private set; }
|
||||
|
||||
[DisplayName("Тип движения товара")]
|
||||
public Movement MovementType { get; private set; }
|
||||
|
||||
|
@ -24,7 +24,7 @@ public class ChartReport
|
||||
{
|
||||
new PdfBuilder(filePath)
|
||||
.AddHeader("Сумма покупки каждого клиента")
|
||||
.AddPieChart("Клиенты", GetData(dateTime))
|
||||
.AddPieChart($"Клиенты, сделавшие покупку на {dateTime: dd MMMM yyyy}", GetData(dateTime))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
@ -38,7 +38,7 @@ public class ChartReport
|
||||
private List<(string Caption, double Value)> GetData(DateTime dateTime)
|
||||
{
|
||||
return _invoiceRepository
|
||||
.ReadInvoices()
|
||||
.ReadInvoices(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
|
||||
.Where(x => x.DateInvoice.Date == dateTime.Date)
|
||||
.GroupBy(x => x.ClientID, (key, group) => new { ID = key, Count = group.Sum(y => y.SellingPrice)})
|
||||
.Select(x => (x.ID.ToString(), (double)x.Count))
|
||||
|
@ -31,7 +31,7 @@ public class TableReport
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по движению товара", 0, 4)
|
||||
.AddParagraph("За период", 0)
|
||||
.AddParagraph($"За период c {startDate:dd.MM.yyyy} по { endDate: dd.MM.yyyy}", 0)
|
||||
.AddTable([10, 10, 15, 15], GetData(productID, startDate, endDate))
|
||||
.Build();
|
||||
return true;
|
||||
@ -46,21 +46,20 @@ public class TableReport
|
||||
private List<string[]> GetData(int productID, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var data = _invoiceRepository
|
||||
.ReadInvoices()
|
||||
.Where(x => x.DateInvoice >= startDate && x.DateInvoice <= endDate && x.Products.Any(y => y.ProductID == productID))
|
||||
.Select(x => new {x.Products.First(y => y.ProductID == productID).ProductID, Date = x.DateInvoice, CountIn = (int?)null, CountOut =(int?) x.Products.FirstOrDefault(y => y.ProductID == productID)?.Count })
|
||||
.ReadInvoices(startDate, endDate)
|
||||
.Select(x => new {x.Products.FirstOrDefault(y => y.ProductID == productID).ProductName, Date = x.DateInvoice, CountIn = (int?)null, CountOut =(int?) x.Products.FirstOrDefault(y => y.ProductID == productID)?.Count })
|
||||
.Union(
|
||||
_productMovementRepository
|
||||
.ReadProductMovements()
|
||||
.ReadProductMovements(startDate, endDate, productID)
|
||||
.Where(x => x.Date >= startDate && x.Date <= endDate && x.ProductID == productID && x.MovementType == Movement.Reseipt)
|
||||
.Select(x => new {x.ProductID, x.Date, CountIn = (int?)x.Count, CountOut = (int?)null }))
|
||||
.Select(x => new {x.ProductName, x.Date, CountIn = (int?)x.Count, CountOut = (int?)null }))
|
||||
.OrderBy(x => x.Date);
|
||||
return
|
||||
new List<string[]>() { item }
|
||||
.Union(
|
||||
data
|
||||
.Select(x => new string[] {x.ProductID.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty}))
|
||||
.Union([["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString()]])
|
||||
.Select(x => new string[] {x.ProductName, x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ?? string.Empty, x.CountOut?.ToString() ?? string.Empty}))
|
||||
.Union([["Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString("N0")]])
|
||||
.ToList();
|
||||
}
|
||||
|
||||
|
@ -76,16 +76,31 @@ public class InvoiceRepository : IInvoiceRepository
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Invoice> ReadInvoices(DateTime? dateForm = null, DateTime? dateTo = null, int? clientId = null)
|
||||
public IEnumerable<Invoice> ReadInvoices(DateTime? dateForm = null, DateTime? dateTo = null, int? clientID = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateForm.HasValue)
|
||||
{
|
||||
builder.AddCondition("inv.DateInvoice >= @dateForm");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("inv.DateInvoice <= @dateTo");
|
||||
}
|
||||
if (clientID.HasValue)
|
||||
{
|
||||
builder.AddCondition("inv.ClientID = @clientID");
|
||||
}
|
||||
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT inv.*, cl.Name as ClientName, ipr.ProductID, ipr.Count
|
||||
var querySelect = @$"SELECT inv.*, cl.Name as ClientName, ipr.ProductID, ipr.Count
|
||||
FROM Invoices inv
|
||||
INNER JOIN InvoiceProducts ipr ON ipr.InvoiceID = inv.ID
|
||||
INNER JOIN Clients cl ON inv.ClientID = cl.ID";
|
||||
INNER JOIN Clients cl ON inv.ClientID = cl.ID
|
||||
{builder.Build()}";
|
||||
var productDict = new Dictionary<int, List<InvoiceProduct>>();
|
||||
var invoices = connection.Query<Invoice, InvoiceProduct, Invoice>(querySelect,
|
||||
(invoice, invoiceProducts) =>
|
||||
@ -97,7 +112,7 @@ public class InvoiceRepository : IInvoiceRepository
|
||||
}
|
||||
pr.Add(invoiceProducts);
|
||||
return invoice;
|
||||
}, splitOn: "ProductID", param: new {dateForm, dateTo});
|
||||
}, splitOn: "ProductID", param: new {dateForm, dateTo, clientID});
|
||||
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices));
|
||||
|
||||
|
@ -42,14 +42,30 @@ public class ProductMovementRepository : IProductMovementRepository
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ProductMovement> ReadProductMovements(DateTime? dateForm = null, DateTime? dateTo = null, int? productId = null)
|
||||
public IEnumerable<ProductMovement> ReadProductMovements(DateTime? dateForm = null, DateTime? dateTo = null, int? productID = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateForm.HasValue)
|
||||
{
|
||||
builder.AddCondition("pm.Date >= @dateForm");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("pm.Date <= @dateTo");
|
||||
}
|
||||
if (productID.HasValue)
|
||||
{
|
||||
builder.AddCondition("pm.ProductID = @productID");
|
||||
}
|
||||
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM ProductMovements";
|
||||
var productMovements = connection.Query<ProductMovement>(querySelect);
|
||||
var querySelect = @$"SELECT pm.* FROM ProductMovements pm
|
||||
{builder.Build()}";
|
||||
|
||||
var productMovements = connection.Query<ProductMovement>(querySelect, new {dateForm, dateTo, productID});
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(productMovements));
|
||||
return productMovements;
|
||||
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectCompanyFurniture.Repositories.Implementations;
|
||||
|
||||
public class QueryBuilder
|
||||
{
|
||||
private readonly StringBuilder _builder;
|
||||
public QueryBuilder()
|
||||
{
|
||||
_builder = new();
|
||||
}
|
||||
public QueryBuilder AddCondition(string condition)
|
||||
{
|
||||
if (_builder.Length > 0)
|
||||
{
|
||||
_builder.Append(" AND ");
|
||||
}
|
||||
_builder.Append(condition);
|
||||
return this;
|
||||
}
|
||||
public string Build()
|
||||
{
|
||||
if (_builder.Length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return $"WHERE {_builder}";
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user