2024-12-11 18:13:28 +04:00
|
|
|
|
using GasStation.Repositories;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace GasStation.Reports;
|
|
|
|
|
|
|
|
|
|
internal class TableReport
|
|
|
|
|
{
|
|
|
|
|
private readonly ISellingRepository _sellingRepository;
|
|
|
|
|
private readonly ILogger<TableReport> _logger;
|
|
|
|
|
|
2024-12-21 10:43:19 +04:00
|
|
|
|
internal static readonly string[] item = ["Сотрудник", "Дата", "Продажа"];
|
|
|
|
|
public TableReport(ISellingRepository sellingRepository,
|
2024-12-11 18:13:28 +04:00
|
|
|
|
ILogger<TableReport> logger)
|
|
|
|
|
{
|
|
|
|
|
_sellingRepository = sellingRepository ??
|
2024-12-21 10:43:19 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(sellingRepository));
|
2024-12-11 18:13:28 +04:00
|
|
|
|
_logger = logger ??
|
|
|
|
|
throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
}
|
2024-12-21 15:34:40 +04:00
|
|
|
|
public bool CreateTable(string filePath, int productId, DateTime date)
|
2024-12-11 18:13:28 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
new ExcelBuilder(filePath)
|
2024-12-21 10:43:19 +04:00
|
|
|
|
.AddHeader("Сводка по движению товара", 0, 3)
|
2024-12-11 18:13:28 +04:00
|
|
|
|
.AddParagraph("за период", 0)
|
2024-12-21 15:34:40 +04:00
|
|
|
|
.AddTable([10, 10, 15], GetData(productId, date))
|
2024-12-11 18:13:28 +04:00
|
|
|
|
.Build();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при формировании документа");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-21 10:43:19 +04:00
|
|
|
|
|
2024-12-21 15:34:40 +04:00
|
|
|
|
private List<string[]> GetData(int productId, DateTime date)
|
2024-12-11 18:13:28 +04:00
|
|
|
|
{
|
2024-12-21 10:43:19 +04:00
|
|
|
|
var data = _sellingRepository
|
2024-12-21 15:34:40 +04:00
|
|
|
|
.ReadSelling(date, productId)
|
2024-12-21 10:43:19 +04:00
|
|
|
|
.Select(x => new {x.GasmanId, Date = x.SellingDateTime,
|
2024-12-11 18:13:28 +04:00
|
|
|
|
CountOut = x.ProdutcSellings.FirstOrDefault(y => y.ProductID == productId)?.Count })
|
2024-12-21 10:43:19 +04:00
|
|
|
|
.OrderBy(x => x.Date);
|
2024-12-11 18:13:28 +04:00
|
|
|
|
|
|
|
|
|
return new List<string[]>() { item }.Union(data
|
|
|
|
|
.Select(x =>
|
2024-12-21 15:34:40 +04:00
|
|
|
|
new string[] { x.GasmanId.ToString(), x.Date.ToString("dd.MM.yyyy"), x.CountOut?.ToString("N0") ?? string.Empty}))
|
2024-12-11 18:13:28 +04:00
|
|
|
|
.Union(
|
2024-12-21 15:34:40 +04:00
|
|
|
|
[["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString("N0")]]
|
2024-12-11 18:13:28 +04:00
|
|
|
|
).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|