74 lines
3.2 KiB
C#
74 lines
3.2 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using ProjectFamilyBudget.Repositories;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ProjectFamilyBudget.Reports;
|
||
|
||
public class TableReport
|
||
{
|
||
private readonly IPeopleIncome _peopleIncomeRepository;
|
||
private readonly IPeopleExpense _peopleExpenseRepository;
|
||
private readonly ILogger<TableReport> _logger;
|
||
internal static readonly string[] item = ["Человек", "Дата", "Заработано", "Потрачено"];
|
||
public TableReport(IPeopleIncome peopleIncomeRepository, IPeopleExpense peopleExpenseRepository, ILogger<TableReport> logger)
|
||
{
|
||
_peopleIncomeRepository = peopleIncomeRepository ?? throw new ArgumentNullException(nameof(peopleIncomeRepository));
|
||
_peopleExpenseRepository = peopleExpenseRepository ?? throw new ArgumentNullException(nameof(peopleExpenseRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
public bool CreateTable(string filePath, int peopleIncomeId, DateTime startDate, DateTime endDate)
|
||
{
|
||
try
|
||
{
|
||
new ExcelBuilder(filePath)
|
||
.AddHeader("Сводка по движению денег", 0, 3)
|
||
.AddParagraph("за период", 0)
|
||
.AddTable([10, 15, 15], GetData(sumId, startDate, endDate))
|
||
.Build();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
private List<string[]> GetData(int peopleIncomeId, DateTime startDate, DateTime endDate)
|
||
{
|
||
var data = _peopleIncomeRepository
|
||
.ReadPeopleIncome()
|
||
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.IncomePeopleIncomes.Any(y => y.PeopleIncomeId == peopleIncomeId))
|
||
.Select(x => new { x.Date, CountIn = x.ContractorFuelFuel.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity, CountOut = (int?)null })
|
||
.Union(
|
||
_peopleExpenseRepository
|
||
.ReadPeopleExpense()
|
||
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.ExpensePeopleExpenses.Any(y => y.PeopleExpenseId == fuelId))
|
||
.Select(x => new { Date = x.SaleDate, CountIn = (int?)null, CountOut = x.FuelFuelSale.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity })
|
||
)
|
||
.OrderBy(x => x.Date);
|
||
|
||
var groupedData = data
|
||
.GroupBy(x => x.Date)
|
||
.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<string[]>() { item }
|
||
.Union(groupedData
|
||
.Select(x => new string[] { x.Date.ToString("dd.MM.yyyy"), x.TotalIn.ToString()!, x.TotalOut.ToString()! }))
|
||
.Union(
|
||
new[] { new string[] { "Всего", groupedData.Sum(x => x.TotalIn).ToString()!, groupedData.Sum(x => x.TotalOut).ToString()! } }
|
||
)
|
||
.ToList();
|
||
}
|
||
}
|