diff --git a/GasStation/GasStation/Reports/ChartReport.cs b/GasStation/GasStation/Reports/ChartReport.cs index a688fbd..0168463 100644 --- a/GasStation/GasStation/Reports/ChartReport.cs +++ b/GasStation/GasStation/Reports/ChartReport.cs @@ -27,7 +27,7 @@ internal class ChartReport { new PdfBuilder(filePath) .AddHeader("Поставки топлива") - .AddPieChart("Виды топлива", GetData(dateTime)) + .AddPieChart($"Поставленные виды топлива на {dateTime:dd.MM.yyyy}", GetData(dateTime)) .Build(); return true; } @@ -44,8 +44,7 @@ internal class ChartReport .ToDictionary(f => f.Id, f => f.Type); return _suppliersFuelRepository - .ReadSuppliersFuels() - .Where(x => x.Date.Date == dateTime.Date) + .ReadSuppliersFuels(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) .SelectMany(x => x.SuppliersFuelFuel) .GroupBy(x => x.FuelId) .Select(g => (Caption: fuelNames[g.Key].ToString(), Value: (double)g.Sum(x => x.Quantity))) diff --git a/GasStation/GasStation/Reports/TableReport.cs b/GasStation/GasStation/Reports/TableReport.cs index 79b710c..43dca2c 100644 --- a/GasStation/GasStation/Reports/TableReport.cs +++ b/GasStation/GasStation/Reports/TableReport.cs @@ -30,7 +30,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader("Сводка по движению топлива", 0, 3) - .AddParagraph("за период", 0) + .AddParagraph($"за период {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0) .AddTable([10, 15, 15], GetData(fuelId, startDate, endDate)) .Build(); return true; @@ -42,31 +42,35 @@ internal class TableReport } } - - private List GetData(int fuelId, DateTime startDate, DateTime endDate) { var suppliersData = _suppliersFuelRepository - .ReadSuppliersFuels() - .Where(x => x.Date >= startDate && x.Date <= endDate && x.SuppliersFuelFuel.Any(y => y.FuelId == fuelId)) - .Select(x => new { Date = x.Date, CountIn = x.SuppliersFuelFuel.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity, CountOut = (int?)null }); + .ReadSuppliersFuels(startDate, endDate, fuelId) + .Select(x => new { x.Date, CountIn = x.SuppliersFuelFuel.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity, CountOut = (int?)null }) + .Union( + _fuelSalesRepository + .ReadFuelSale(startDate, endDate, fuelId) + .Select(x => new { Date = x.SaleDate, CountIn = (int?)null, CountOut = x.FuelFuelSale.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity }) + ) + .OrderBy(x => x.Date); - var salesData = _fuelSalesRepository - .ReadFuelSale() - .Where(x => x.SaleDate >= startDate && x.SaleDate <= endDate && x.FuelFuelSale.Any(y => y.FuelId == fuelId)) - .Select(x => new { Date = x.SaleDate, CountIn = (int?)null, CountOut = x.FuelFuelSale.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity }); - - var combinedData = suppliersData.Union(salesData) + var groupedData = suppliersData .GroupBy(x => x.Date) - .Select(g => new { Date = g.Key, CountIn = g.Sum(x => x.CountIn ?? 0), CountOut = g.Sum(x => x.CountOut ?? 0) }) + .Select(g => new + { + Date = g.Key, + TotalIn = g.Sum(x => x.CountIn), + TotalOut = g.Sum(x => x.CountOut) + }) .OrderBy(x => x.Date); - return new List() { item } + return + new List() { item } + .Union(groupedData + .Select(x => new string[] { x.Date.ToString("dd.MM.yyyy"), x.TotalIn?.ToString("N0") ?? string.Empty, x.TotalOut?.ToString("N0") ?? string.Empty })) .Union( - combinedData - .Select(x => new string[] { x.Date.ToString(), x.CountIn.ToString(), x.CountOut.ToString() })) - .Union( - new List() { new string[] { "Всего", combinedData.Sum(x => x.CountIn).ToString(), combinedData.Sum(x => x.CountOut).ToString() } }) + new[] { new string[] { "Всего", groupedData.Sum(x => x.TotalIn)?.ToString("N0") ?? string.Empty, groupedData.Sum(x => x.TotalOut)?.ToString("N0") ?? string.Empty } } + ) .ToList(); } } \ No newline at end of file diff --git a/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs b/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs index 8559380..6afb3dc 100644 --- a/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs +++ b/GasStation/GasStation/Repositories/Implementations/FuelSalesRepository.cs @@ -97,8 +97,22 @@ public class FuelSalesRepository : IFuelSalesRepository _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("fs.SaleDate >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("fs.SaleDate <= @dateTo"); + } + if (fuelId.HasValue) + { + builder.AddCondition("ffs.FuelId = @fuelId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT + var querySelect = $@"SELECT fs.*, CONCAT(s.LastName, ' ', s.FirstName) as CashierName, ffs.FuelId, @@ -107,7 +121,8 @@ public class FuelSalesRepository : IFuelSalesRepository FROM FuelSale fs LEFT JOIN Cashier s on s.Id = fs.CashierId INNER JOIN FuelFuelSale ffs on ffs.FuelSaleId = fs.Id - LEFT JOIN Fuel f on f.id = ffs.FuelId"; + LEFT JOIN Fuel f on f.id = ffs.FuelId + {builder.Build()}"; var salesDict = new Dictionary>(); var fuelSales = connection.Query(querySelect, diff --git a/GasStation/GasStation/Repositories/Implementations/QueryBuilder.cs b/GasStation/GasStation/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..5f017f4 --- /dev/null +++ b/GasStation/GasStation/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,35 @@ +using System.Text; + +namespace GasStation.Repositories.Implementations; + +internal 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}"; + } +} \ No newline at end of file diff --git a/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs b/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs index dd63814..3665df8 100644 --- a/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs +++ b/GasStation/GasStation/Repositories/Implementations/SuppliersFuelRepository.cs @@ -94,8 +94,22 @@ public class SuppliersFuelRepository : ISuppliersFuelRepository _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateFrom.HasValue) + { + builder.AddCondition("cf.Date >= @dateFrom"); + } + if (dateTo.HasValue) + { + builder.AddCondition("cf.Date <= @dateTo"); + } + if (fuelId.HasValue) + { + builder.AddCondition("cff.FuelId = @fuelId"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT + var querySelect = $@"SELECT cf.*, c.Brand as SuppliersBrand, cff.FuelId, @@ -104,7 +118,8 @@ public class SuppliersFuelRepository : ISuppliersFuelRepository FROM SuppliersFuel cf LEFT JOIN Supplier c on c.Id = cf.SuppliersId INNER JOIN SuppliersFuelFuel cff on cff.SuppliersFuelId = cf.Id - LEFT JOIN Fuel f on f.Id = cff.FuelId"; + LEFT JOIN Fuel f on f.Id = cff.FuelId + {builder.Build()}"; var suppliersDict = new Dictionary>();