64 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectSellPC.Repos;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using System.Reflection.PortableExecutable;
namespace ProjectSellPC.DocBuilder
{
internal class ChartReport
{
private readonly IChequeRepository _chequeRepository; // Репозиторий для получения данных о чеках
private readonly IProductRepository _productRepository; // Репозиторий для получения данных о продуктах
private readonly ILogger<ChartReport> _logger;
public ChartReport(IChequeRepository checkRepository, IProductRepository productRepository, ILoggerFactory loggerFactory)
{
_chequeRepository = checkRepository ?? throw new ArgumentNullException(nameof(checkRepository));
_productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository));
_logger = loggerFactory.CreateLogger<ChartReport>();
}
public bool CreateChart(string filePath, DateTime dateTime)
{
try
{
new PdfBuilder(filePath)
.AddHeader("Отчет по продажам товаров")
.AddPieChart("Проданные товары", GetData(dateTime)) // Диаграмма с продуктами
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<(string Caption, double Value)> GetData(DateTime dateTime)
{
// Получаем все чеки, которые были оформлены в указанную дату
var checkData = _chequeRepository
.ReadAll()
.Where(x => x.PurchaseDate.Date == dateTime.Date) // Фильтрация по дате
.SelectMany(x => x.Products) // Получаем все продукты из всех чеков
.GroupBy(p => p.ProductID) // Группируем по ID продукта
.Select(g =>
{
// Получаем продукт по ProductID
var product = _productRepository.Read(g.Key);
return (Caption: product.Name, Value: (double)g.Sum(p => p.Count)); // Преобразуем Count в double
})
.ToList();
return checkData;
}
}
}