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,
@@ -15,8 +15,6 @@ public interface IAccountRepository
bool Remove(Account account);
List<Account> FindAccounts(Guid userId, string term);
decimal GetPeriodIncome(Guid userId, DateTime from, DateTime to);
decimal GetPeriodOutcome(Guid userId, DateTime from, DateTime to);
List<AccountSimple> GetRestAtDate(Guid userId, DateTime date);
List<MotionTotalSimple> GetIncomeByCategories(Guid userId, DateTime from, DateTime to);
List<MotionTotalSimple> GetIncomeByCategory(Guid userId, Guid categoryId, DateTime from, DateTime to);
@@ -7,4 +7,5 @@ public interface IItemGlobalRepository
ItemGlobal? Get(Guid id);
ItemGlobal? GetByName(string name);
bool Add(ItemGlobal itemGlobal);
List<ItemGlobal> GetAvailableToUser(Guid userId);
}
@@ -6,8 +6,7 @@ public interface IItemRepository
{
List<Item> GetAll(Guid userId);
List<Item> GetByCategory(Guid userId, Guid categoryId);
Item? GetByGlobal(Guid userId, Guid globalMotionId);
Item? GetByGlobal(Guid userId, Guid globalItemId);
bool Add(Item item);
bool Update(Item item);
List<Item> Find(Guid userId, string term, int limit);
@@ -1,5 +1,6 @@
namespace MyOffice.Data.Repositories.Item;
using Microsoft.EntityFrameworkCore;
using MyOffice.Data.Models.Items;
public class ItemGlobalRepository : AppRepository<ItemGlobal>, IItemGlobalRepository
@@ -18,4 +19,19 @@ public class ItemGlobalRepository : AppRepository<ItemGlobal>, IItemGlobalReposi
{
return AddBase(itemGlobal) > 0;
}
public List<ItemGlobal> GetAvailableToUser(Guid userId)
{
return _context.Motions
.Include(x => x.Item)
.ThenInclude(x => x.ItemGlobal)
.ThenInclude(x => x.Items)
.ThenInclude(x => x.Category)
.Where(x => x.Account.AccessRights!.Any(a => a.UserId == userId))
.Where(x => x.Item.ItemGlobal.Items.All(g => g.Category!.UserId != userId))
.Select(x => x.Item.ItemGlobal)
.GroupBy(x => x.Id)
.Select(x => x.First())
.ToList();
}
}
@@ -25,13 +25,13 @@ public class ItemRepository : AppRepository<Item>, IItemRepository
.ToList();
}
public Item? GetByGlobal(Guid userId, Guid globalMotionId)
public Item? GetByGlobal(Guid userId, Guid globalItemId)
{
return _context.Items
.Include(x => x.Motions)
.Include(x => x.Category)
.Include(x => x.ItemGlobal)
.FirstOrDefault(x => x.Category!.UserId == userId && x.ItemGlobalId == globalMotionId);
.FirstOrDefault(x => x.Category!.UserId == userId && x.ItemGlobalId == globalItemId);
}
public bool Update(Item item)