54 lines
1.9 KiB
C#
54 lines
1.9 KiB
C#
using GasStation.Repositories;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace GasStation.Reports;
|
||
|
||
internal class TableReport
|
||
{
|
||
private readonly ISellingRepository _sellingRepository;
|
||
private readonly ILogger<TableReport> _logger;
|
||
|
||
internal static readonly string[] item = ["Сотрудник", "Дата", "Продажа"];
|
||
public TableReport(ISellingRepository sellingRepository,
|
||
ILogger<TableReport> logger)
|
||
{
|
||
_sellingRepository = sellingRepository ??
|
||
throw new ArgumentNullException(nameof(sellingRepository));
|
||
_logger = logger ??
|
||
throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
public bool CreateTable(string filePath, int productId, DateTime date)
|
||
{
|
||
try
|
||
{
|
||
new ExcelBuilder(filePath)
|
||
.AddHeader("Сводка по движению товара", 0, 3)
|
||
.AddParagraph("за период", 0)
|
||
.AddTable([10, 10, 15], GetData(productId, date))
|
||
.Build();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private List<string[]> GetData(int productId, DateTime date)
|
||
{
|
||
var data = _sellingRepository
|
||
.ReadSelling(date, productId)
|
||
.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("dd.MM.yyyy"), x.CountOut?.ToString("N0") ?? string.Empty}))
|
||
.Union(
|
||
[["Всего", "", data.Sum(x => x.CountOut ?? 0).ToString("N0")]]
|
||
).ToList();
|
||
}
|
||
}
|