167 lines
4.9 KiB
C#
167 lines
4.9 KiB
C#
namespace MyOffice.Data.Repositories.Account;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Models.Accounts;
|
|
using MyOffice.Data.Models.Currencies;
|
|
using MyOffice.Data.Repositories;
|
|
|
|
public class AccountRepository : AppRepository<Account>, IAccountRepository
|
|
{
|
|
public List<Account> GetAll(Guid userId)
|
|
{
|
|
return _context.Accounts!
|
|
.Include(x => x.CurrencyGlobal)
|
|
.Include(x => x.Categories)!
|
|
.ThenInclude(x => x.Category)
|
|
.Include(x => x.AccessRights)!
|
|
.ThenInclude(x => x.User)
|
|
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
|
|
.ToList();
|
|
}
|
|
|
|
public List<Account> GetByCategory(Guid userId, Guid categoryId)
|
|
{
|
|
return _context.Accounts!
|
|
.Include(x => x.CurrencyGlobal)
|
|
.Include(x => x.Categories)!
|
|
.ThenInclude(x => x.Category)
|
|
.Include(x => x.AccessRights)!
|
|
.ThenInclude(x => x.User)
|
|
.Where(x => x.Categories!.Any(c => c.CategoryId == categoryId) && x.AccessRights!.Any(a => a.UserId == userId))
|
|
.ToList();
|
|
}
|
|
|
|
public List<AccountDetailed> GetByCategoryDetailed(Guid userId, Guid categoryId)
|
|
{
|
|
return _context.Accounts!
|
|
.Include(x => x.CurrencyGlobal)
|
|
.Include(x => x.Categories)!
|
|
.ThenInclude(x => x.Category)
|
|
.Include(x => x.AccessRights)!
|
|
.ThenInclude(x => x.User)
|
|
.Where(x => x.Categories!.Any(c => c.CategoryId == categoryId) && x.AccessRights!.Any(a => a.UserId == userId))
|
|
.Select(x => new AccountDetailed
|
|
{
|
|
Account = x,
|
|
TotalPlus = x.Motions!.Sum(m => m.AmountPlus),
|
|
TotalMinus = x.Motions!.Sum(m => m.AmountMinus),
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
public AccountDetailed? GetByIdDetailed(Guid userId, Guid id)
|
|
{
|
|
return _context.Accounts!
|
|
.Include(x => x.CurrencyGlobal)
|
|
.Include(x => x.Categories)!
|
|
.ThenInclude(x => x.Category)
|
|
.Include(x => x.AccessRights)!
|
|
.ThenInclude(x => x.User)
|
|
.Where(x => x.Id == id && x.AccessRights!.Any(a => a.UserId == userId))
|
|
.Select(x => new AccountDetailed
|
|
{
|
|
Account = x,
|
|
TotalPlus = x.Motions!.Sum(m => m.AmountPlus),
|
|
TotalMinus = x.Motions!.Sum(m => m.AmountMinus),
|
|
})
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public bool Add(Account account)
|
|
{
|
|
return AddBase(account) > 0;
|
|
}
|
|
|
|
public Account? Get(Guid userId, Guid id)
|
|
{
|
|
return _context.Accounts!
|
|
.Include(x => x.Categories)!
|
|
.ThenInclude(x => x.Category)
|
|
.Include(x => x.AccessRights)!
|
|
.ThenInclude(x => x.User)
|
|
.FirstOrDefault(x => x.Id == id && x.AccessRights!.Any(r => r.UserId == userId));
|
|
}
|
|
|
|
public bool Update(Account account)
|
|
{
|
|
return UpdateBase(account) > 0;
|
|
}
|
|
|
|
public bool Remove(Account account)
|
|
{
|
|
return RemoveBase(account) > 0;
|
|
}
|
|
|
|
public List<Account> FindAccounts(Guid userId, string term)
|
|
{
|
|
return _context.Accounts
|
|
.Where(x => x.AccessRights!.Any(a => a.UserId == userId))
|
|
.Where(x => x.Name.Contains(term))
|
|
.ToList();
|
|
}
|
|
|
|
public decimal GetPeriodIncome(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.AmountPlus)
|
|
})
|
|
.ToList()
|
|
.Select(x => x.Amount * (x.Currency?.CurrentRate?.Rate * x.Currency?.CurrentRate?.Quantity))
|
|
.Sum() ?? 0;
|
|
}
|
|
|
|
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)
|
|
})
|
|
.ToList()
|
|
.Select(x => x.Amount * (x.Currency?.CurrentRate?.Rate * x.Currency?.CurrentRate?.Quantity))
|
|
.Sum() ?? 0;
|
|
}
|
|
|
|
public List<AccountSimple> GetRestAtDate(Guid userId, DateTime date)
|
|
{
|
|
return _context.Accounts
|
|
.Include(x => x.CurrencyGlobal!)
|
|
.ThenInclude(x => x.Currencies!)
|
|
.ThenInclude(x => x.CurrentRate!)
|
|
.Where(x => x.AccessRights!.Any(u => u.UserId == userId))
|
|
.Select(x => new {
|
|
Id = x.Id,
|
|
Name = x.Name,
|
|
Currency = x.CurrencyGlobal!.Currencies!.FirstOrDefault(x => x.UserId == userId),
|
|
Rest = x.Motions!.Sum(m => (m.AmountPlus - m.AmountMinus))
|
|
})
|
|
.ToList()
|
|
.Select(x => new AccountSimple
|
|
{
|
|
Id = x.Id,
|
|
Name = x.Name,
|
|
Balance = x.Rest,
|
|
CurrencyName = x.Currency?.Name,
|
|
CurrencyShortName = x.Currency?.ShortName,
|
|
CurrencyRate = x.Currency?.CurrentRate?.Rate,
|
|
CurrencyQuantity = x.Currency?.CurrentRate?.Quantity,
|
|
}).ToList();
|
|
}
|
|
} |