This commit is contained in:
2023-08-10 21:57:55 +03:00
parent 25184e9edc
commit a09fc7ece3
19 changed files with 230 additions and 68 deletions
@@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Models.Accounts;
using MyOffice.Data.Models.Accounts.Domain;
using MyOffice.Data.Models.Currencies;
using MyOffice.Data.Repositories;
@@ -117,42 +118,40 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
.ToList();
}
public decimal GetPeriodIncome(Guid userId, DateTime from, DateTime to)
public List<AccountWithRate> GetAccountsWithRate(Guid userId, DateTime date)
{
return _context.Accounts
.Where(x => x.AccessRights!.Any(u => u.UserId == userId))
.Include(x => x.CurrencyGlobal!)
.ThenInclude(x => x.Currencies!)
.ThenInclude(x => x.CurrentRate!)
.Select(x => new {
Currency = x.CurrencyGlobal!.Currencies!.FirstOrDefault(x => x.UserId == userId),
Amount = x.Motions!
.Where(x => !x.Item!.Category!.IsInternal)
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Sum(m => m.AmountPlus)
})
.ToList()
.Select(x => x.Amount * (x.Currency?.CurrentRate?.Rate * x.Currency?.CurrentRate?.Quantity))
.Sum() ?? 0;
}
var accounts = _context.Accounts
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
.ToList();
public decimal GetPeriodOutcome(Guid userId, DateTime from, DateTime to)
{
return _context.Accounts
.Where(x => x.AccessRights!.Any(u => u.UserId == userId))
.Include(x => x.CurrencyGlobal!)
.ThenInclude(x => x.Currencies!)
.ThenInclude(x => x.CurrentRate!)
.Select(x => new {
Currency = x.CurrencyGlobal!.Currencies!.FirstOrDefault(x => x.UserId == userId),
Amount = x.Motions!
.Where(x => !x.Item!.Category!.IsInternal)
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Sum(m => m.AmountMinus)
var currencyIds = accounts
.Select(x => x.CurrencyGlobalId)
.ToList();
var rates = _context.CurrencyRates
.Include(x => x.Currency)
.Where(x => x.Currency!.UserId == userId)
.Where(x => x.DateTime <= date)
.Where(x => currencyIds.Contains(x.Currency!.CurrencyGlobalId))
.GroupBy(x => x.Currency!.CurrencyGlobalId)
.Select(x => x.OrderByDescending(r => r.DateTime).First())
.ToList();
return accounts
.Select(account =>
{
var rate = rates.FirstOrDefault(rate => rate.Currency!.CurrencyGlobalId == account.CurrencyGlobalId);
var result = new AccountWithRate
{
Account = account,
Currency = rate?.Currency,
Rate = rate,
};
return result;
})
.ToList()
.Select(x => x.Amount * (x.Currency?.CurrentRate?.Rate * x.Currency?.CurrentRate?.Quantity))
.Sum() ?? 0;
.ToList();
}
public List<AccountSimple> GetRestAtDate(Guid userId, DateTime date)
@@ -198,25 +197,31 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
public List<MotionTotalSimple> GetIncomeByCategories(Guid userId, DateTime from, DateTime to)
{
var accounts = GetAccountsWithRate(userId, to);
var accountIds = accounts.Select(x => x.Account.Id);
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => x.Item.Category!.UserId == userId)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.GroupBy(x => new
{
Id = x.Item.CategoryId,
Name = x.Item.Category!.Name,
IsUserCategory = x.Item.Category!.UserId == userId,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.ToList()
.Select(x => new MotionTotalSimple
{
Id = x.Key.Id,
Id = x.Key.IsUserCategory ? x.Key.Id : userId,
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountPlus),
}).ToList();
})
.ToList();
}
public List<MotionTotalSimple> GetIncomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to)
@@ -247,21 +252,26 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
public List<MotionTotalSimple> GetOutcomeByCategories(Guid userId, DateTime from, DateTime to)
{
var accounts = GetAccountsWithRate(userId, to);
var accountIds = accounts.Select(x => x.Account.Id);
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => x.Item.Category!.UserId == userId)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.GroupBy(x => new
{
Id = x.Item.CategoryId,
Name = x.Item.Category!.Name,
IsUserCategory = x.Item.Category!.UserId == userId,
CurrencyId = x.Account.CurrencyGlobalId,
CurrencyName = x.Account.CurrencyGlobal!.Name,
})
.Select(x => new MotionTotalSimple
{
Id = x.Key.Id,
Name = x.Key.Name,
Id = x.Key.IsUserCategory ? x.Key.Id : userId,
Name = x.Key.IsUserCategory ? x.Key.Name : "UnCategorized",
//Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyName = x.Key.CurrencyName,
Amount = x.Sum(a => a.AmountMinus),
@@ -271,11 +281,14 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
public List<MotionTotalSimple> GetOutcomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to)
{
var accounts = GetAccountsWithRate(userId, to);
var accountIds = accounts.Select(x => x.Account.Id);
return _context.Motions
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Where(x => x.Item.CategoryId == categoryId)
.Where(x => accountIds.Contains(x.AccountId))
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Item.Category!.UserId == userId)
.Where(x => x.Item.CategoryId == categoryId)
.GroupBy(x => new
{
Id = x.Item.ItemGlobalId,