вторая часть
This commit is contained in:
parent
6cb8b9cf25
commit
afeabbee26
@ -25,7 +25,7 @@ public class ChartReport
|
|||||||
{
|
{
|
||||||
new PdfBuilder(filePath)
|
new PdfBuilder(filePath)
|
||||||
.AddHeader("Траты людей")
|
.AddHeader("Траты людей")
|
||||||
.AddPieChart("Виды трат", GetData(expenseId, dateTime))
|
.AddPieChart($"Траты за {dateTime:dd MMMM yyyy}", GetData(expenseId, dateTime))
|
||||||
.Build();
|
.Build();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -38,14 +38,13 @@ public class ChartReport
|
|||||||
private List<(string Caption, double Value)> GetData(int expenseId, DateTime dateTime)
|
private List<(string Caption, double Value)> GetData(int expenseId, DateTime dateTime)
|
||||||
{
|
{
|
||||||
return _peopleExpense
|
return _peopleExpense
|
||||||
.ReadPeopleExpense()
|
.ReadPeopleExpense(dateTime.Date, dateTime.Date.AddDays(1), expenseId)
|
||||||
.Where(x => x.DataReciept.Date == dateTime.Date)
|
.GroupBy(x => x.PeopleName, (key, group) => new
|
||||||
.GroupBy(x => x.PeopleId, (key, group) => new
|
|
||||||
{
|
{
|
||||||
Id = key,
|
PeopleName = key,
|
||||||
Count = group.Sum(x => x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum ?? 0)
|
Count = group.Sum(x => x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum ?? 0)
|
||||||
})
|
})
|
||||||
.Select(x => (x.Id.ToString(), (double)x.Count))
|
.Select(x => (x.PeopleName, (double)x.Count))
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public class TableReport
|
|||||||
{
|
{
|
||||||
new ExcelBuilder(filePath)
|
new ExcelBuilder(filePath)
|
||||||
.AddHeader("Сводка по движению денег", 0, 4)
|
.AddHeader("Сводка по движению денег", 0, 4)
|
||||||
.AddParagraph("за период", 0)
|
.AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}",0)
|
||||||
.AddTable([15, 10, 15, 15], GetData(incomeId, expenseId, startDate, endDate))
|
.AddTable([15, 10, 15, 15], GetData(incomeId, expenseId, startDate, endDate))
|
||||||
.Build();
|
.Build();
|
||||||
return true;
|
return true;
|
||||||
@ -40,21 +40,22 @@ public class TableReport
|
|||||||
private List<string[]> GetData(int incomeId,int expenseId, DateTime startDate, DateTime endDate)
|
private List<string[]> GetData(int incomeId,int expenseId, DateTime startDate, DateTime endDate)
|
||||||
{
|
{
|
||||||
var data = _peopleIncomeRepository
|
var data = _peopleIncomeRepository
|
||||||
.ReadPeopleIncome()
|
.ReadPeopleIncome(dateForm: startDate, dateTo: endDate, incomeId, expenseId)
|
||||||
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.IncomePeopleIncomes.Any(y => y.IncomeId == incomeId))
|
.Select(x => new { x.PeopleName, Date = x.DataReciept,
|
||||||
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.IncomeId == incomeId)?.Sum, CountOut = (int?)null })
|
CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.IncomeId == incomeId)?.Sum, CountOut = (int?)null })
|
||||||
.Union(
|
.Union(
|
||||||
_peopleExpenseRepository
|
_peopleExpenseRepository
|
||||||
.ReadPeopleExpense()
|
.ReadPeopleExpense(dateForm: startDate, dateTo: endDate, incomeId, expenseId)
|
||||||
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.ExpensePeopleExpenses.Any(y => y.ExpenseId == expenseId))
|
.Select(x => new { x.PeopleName, Date = x.DataReciept, CountIn = (int?)null,
|
||||||
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = (int?)null, CountOut = x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum }))
|
CountOut = x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum }))
|
||||||
.OrderBy(x => x.Date);
|
.OrderBy(x => x.Date);
|
||||||
return
|
return
|
||||||
new List<string[]>() { item }
|
new List<string[]>() { item }
|
||||||
.Union(data.Select(x => new string[] {x.PeopleId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty,x.CountOut?.ToString() ?? string.Empty }))
|
|
||||||
.Union(
|
.Union(
|
||||||
[[ "Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString() ]]
|
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();
|
.ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,14 +90,58 @@ public class PeopleExpenseRepository : IPeopleExpense
|
|||||||
_logger.LogInformation("Получение всех объектов");
|
_logger.LogInformation("Получение всех объектов");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var builder = new QueryBuilder();
|
||||||
|
if (dateForm.HasValue)
|
||||||
|
{
|
||||||
|
builder.AddCondition("pe.DataReciept >= @dateForm");
|
||||||
|
}
|
||||||
|
if (dateTo.HasValue)
|
||||||
|
{
|
||||||
|
builder.AddCondition("pe.DataReciept <= @dateTo");
|
||||||
|
}
|
||||||
|
if (peopleId.HasValue)
|
||||||
|
{
|
||||||
|
builder.AddCondition("pe.PeopleId = @peopleId");
|
||||||
|
}
|
||||||
|
if (expenseId.HasValue)
|
||||||
|
{
|
||||||
|
builder.AddCondition("epe.ExpenseId = @expenseId");
|
||||||
|
}
|
||||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
var querySelect = @"SELECT pe.*, epe.ExpenseId, epe.Sum FROM PeopleExpense pe
|
var querySelect = $@"SELECT
|
||||||
INNER JOIN ExpensePeopleExpense epe on epe.PeopleExpenseId = pe.Id";
|
pe.*,
|
||||||
var peopleExpenses = connection.Query<TempExpensePeopleExpense>(querySelect);
|
CONCAT(p.Name, ' ', p.LastName) as PeopleName,
|
||||||
|
epe.ExpenseId,
|
||||||
|
epe.Sum,
|
||||||
|
e.Name as ExpenseName
|
||||||
|
FROM PeopleExpense pe
|
||||||
|
LEFT JOIN People p on p.Id = pe.PeopleId
|
||||||
|
INNER JOIN ExpensePeopleExpense epe on epe.PeopleExpenseId = pe.Id
|
||||||
|
LEFT JOIN Expense e on e.Id = epe.ExpenseId
|
||||||
|
{builder.Build()}";
|
||||||
|
var expenseDict = new Dictionary<int, List<ExpensePeopleExpense>>();
|
||||||
|
|
||||||
|
var peopleExpenses = connection.Query<PeopleExpense, ExpensePeopleExpense, PeopleExpense>(querySelect,
|
||||||
|
(expense, peopleExpenses) =>
|
||||||
|
{
|
||||||
|
if (!expenseDict.TryGetValue(expense.Id, out var epe))
|
||||||
|
{
|
||||||
|
epe = [];
|
||||||
|
expenseDict.Add(expense.Id, epe);
|
||||||
|
}
|
||||||
|
|
||||||
|
epe.Add(peopleExpenses);
|
||||||
|
return expense;
|
||||||
|
}, splitOn: "ExpenseId", param: new { dateForm, dateTo, peopleId, expenseId });
|
||||||
_logger.LogDebug("Полученные объекты: {json}",
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
JsonConvert.SerializeObject(peopleExpenses));
|
JsonConvert.SerializeObject(peopleExpenses));
|
||||||
return peopleExpenses.GroupBy(x => x.Id, y => y, (key, value) => PeopleExpense.CreateOperation(value.First(),
|
|
||||||
value.Select(z => ExpensePeopleExpense.CreateElement(0, z.ExpenseId, z.Sum)))).ToList();
|
return expenseDict.Select(x =>
|
||||||
|
{
|
||||||
|
var pe = peopleExpenses.First(y => y.Id == x.Key);
|
||||||
|
pe.SetExpensePeopleExpenses(x.Value);
|
||||||
|
return pe;
|
||||||
|
}).ToArray();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -94,52 +94,56 @@ public class PeopleIncomeRepository : IPeopleIncome
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var builder = new QueryBuilder();
|
var builder = new QueryBuilder();
|
||||||
if (dateFrom.HasValue)
|
if (dateForm.HasValue)
|
||||||
{
|
{
|
||||||
builder.AddCondition("cf.Date >= @dateFrom");
|
builder.AddCondition("pi.DataReciept >= @dateForm");
|
||||||
}
|
}
|
||||||
if (dateTo.HasValue)
|
if (dateTo.HasValue)
|
||||||
{
|
{
|
||||||
builder.AddCondition("cf.Date <= @dateTo");
|
builder.AddCondition("pi.DataReciept <= @dateTo");
|
||||||
}
|
}
|
||||||
if (fuelId.HasValue)
|
if (peopleId.HasValue)
|
||||||
{
|
{
|
||||||
builder.AddCondition("cff.FuelId = @fuelId");
|
builder.AddCondition("pi.PeopleId = @peopleId");
|
||||||
|
}
|
||||||
|
if (incomeId.HasValue)
|
||||||
|
{
|
||||||
|
builder.AddCondition("ipi.IncomeId = @incomeId");
|
||||||
}
|
}
|
||||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
var querySelect = $@"SELECT
|
var querySelect = $@"SELECT
|
||||||
cf.*,
|
pi.*,
|
||||||
c.Name as ContractorName,
|
CONCAT(p.Name, ' ', p.LastName) as PeopleName,
|
||||||
cff.FuelId,
|
ipi.IncomeId,
|
||||||
cff.Quantity,
|
ipi.Sum,
|
||||||
f.Type as FuelName
|
i.Name as IncomeName
|
||||||
FROM ContractorFuel cf
|
FROM PeopleIncome pi
|
||||||
LEFT JOIN Contractor c on c.Id = cf.ContractorId
|
LEFT JOIN People p on p.Id = pi.PeopleId
|
||||||
INNER JOIN ContractorFuelFuel cff on cff.ContractorFuelId = cf.Id
|
INNER JOIN IncomePeopleIncome ipi on ipi.PeopleIncomeId = pi.Id
|
||||||
LEFT JOIN Fuel f on f.Id = cff.FuelId
|
LEFT JOIN Income i on i.Id = ipi.IncomeId
|
||||||
{builder.Build()}";
|
{builder.Build()}";
|
||||||
var contractorsDict = new Dictionary<int, List<ContractorFuelFuel>>();
|
var incomeDict = new Dictionary<int, List<IncomePeopleIncome>>();
|
||||||
|
|
||||||
var contractorFuels = connection.Query<ContractorFuel, ContractorFuelFuel, ContractorFuel>(querySelect,
|
var peopleIncomes = connection.Query<PeopleIncome, IncomePeopleIncome, PeopleIncome>(querySelect,
|
||||||
(contractor, contractorFuel) =>
|
(income, peopleIncomes) =>
|
||||||
{
|
{
|
||||||
if (!contractorsDict.TryGetValue(contractor.Id, out var ccf))
|
if (!incomeDict.TryGetValue(income.Id, out var ipi))
|
||||||
{
|
{
|
||||||
ccf = [];
|
ipi = [];
|
||||||
contractorsDict.Add(contractor.Id, ccf);
|
incomeDict.Add(income.Id, ipi);
|
||||||
}
|
}
|
||||||
|
|
||||||
ccf.Add(contractorFuel);
|
ipi.Add(peopleIncomes);
|
||||||
return contractor;
|
return income;
|
||||||
}, splitOn: "FuelId", param: new { dateFrom, dateTo, fuelId });
|
}, splitOn: "IncomeId", param: new { dateForm, dateTo, peopleId, incomeId });
|
||||||
_logger.LogDebug("Полученные объекты: {json}",
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
JsonConvert.SerializeObject(contractorFuels));
|
JsonConvert.SerializeObject(peopleIncomes));
|
||||||
|
|
||||||
return contractorsDict.Select(x =>
|
return incomeDict.Select(x =>
|
||||||
{
|
{
|
||||||
var cf = contractorFuels.First(y => y.Id == x.Key);
|
var pi = peopleIncomes.First(y => y.Id == x.Key);
|
||||||
cf.SetContractorFuelFuel(x.Value);
|
pi.SetIncomePeopleIncomes(x.Value);
|
||||||
return cf;
|
return pi;
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectFamilyBudget.Repositories.Implementations;
|
||||||
|
|
||||||
|
public class QueryBuilder
|
||||||
|
{
|
||||||
|
private readonly StringBuilder _builder;
|
||||||
|
public QueryBuilder()
|
||||||
|
{
|
||||||
|
_builder = new();
|
||||||
|
}
|
||||||
|
public QueryBuilder AddCondition(string condition)
|
||||||
|
{
|
||||||
|
if (_builder.Length > 0)
|
||||||
|
{
|
||||||
|
_builder.Append(" AND ");
|
||||||
|
}
|
||||||
|
_builder.Append(condition);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public string Build()
|
||||||
|
{
|
||||||
|
if (_builder.Length == 0)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
return $"WHERE {_builder}";
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user