62 lines
3.0 KiB
C#
Raw Permalink 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 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 incomeId, int expenseId, DateTime startDate, DateTime endDate)
{
try
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению денег", 0, 4)
.AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}",0)
.AddTable([15, 10, 15, 15], GetData(incomeId, expenseId, startDate, endDate))
.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetData(int incomeId,int expenseId, DateTime startDate, DateTime endDate)
{
var data = _peopleIncomeRepository
.ReadPeopleIncome(dateForm: startDate, dateTo: endDate, incomeId: incomeId)
.Select(x => new { x.PeopleName, Date = x.DataReciept,
CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.IncomeId == incomeId)?.Sum, CountOut = (int?)null })
.Union(
_peopleExpenseRepository
.ReadPeopleExpense(dateForm: startDate, dateTo: endDate, expenseId: expenseId)
.Select(x => new { x.PeopleName, Date = x.DataReciept, CountIn = (int?)null,
CountOut = x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum }))
.OrderBy(x => x.Date);
return
new List<string[]>() { item }
.Union(
data.Select(x => new string[] {x.PeopleName, x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ?? string.Empty,
x.CountOut?.ToString("N0") ?? string.Empty }))
.Union(
[[ "Всего", "", data.Sum(x => x.CountIn ?? 0).ToString("N0"), data.Sum(x => x.CountOut ?? 0).ToString("N0") ]])
.ToList();
}
}