This commit is contained in:
2023-07-21 23:01:56 +03:00
parent fa5a8a9001
commit d0cdaa85b6
18 changed files with 863 additions and 178 deletions
@@ -1,6 +1,7 @@
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;
@@ -102,42 +103,65 @@ public class AccountRepository : AppRepository<Account>, IAccountRepository
public decimal GetPeriodIncome(Guid userId, DateTime from, DateTime to)
{
return _context.Motions
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Account!.AccessRights!.Any(u => u.UserId == userId))
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Sum(x => x.AmountPlus);
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.Motions
.Where(x => !x.Item.Category!.IsInternal)
.Where(x => x.Account!.AccessRights!.Any(u => u.UserId == userId))
.Where(x => x.DateTime >= from && x.DateTime <= to)
.Sum(x => x.AmountMinus);
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.Motions
.Where(x => x.Account!.AccessRights!.Any(u => u.UserId == userId))
.Where(x => x.DateTime <= date)
.GroupBy(x => new
{
x.AccountId,
x.Account.Name,
CurrencyId = x.Account!.CurrencyGlobalId,
CurrencyName = x.Account!.CurrencyGlobal!.Name,
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.Key.AccountId,
Name = x.Key.Name,
CurrencyId = x.Key.CurrencyId,
CurrencyShortName = x.Key.CurrencyName,
Rest = x.Sum(g => (g.AmountPlus - g.AmountMinus))
})
.ToList();
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();
}
}
@@ -5,10 +5,13 @@ using Models.Currencies;
public class CurrencyRateRepository : AppRepository<CurrencyRate>, ICurrencyRateRepository
{
public List<CurrencyRate> GetLastRates(Guid currencyId, int count = 1)
public List<CurrencyRate> GetLastRates(Guid currencyId, DateTime? before = null, int count = 1)
{
before = before ?? DateTime.UtcNow;
return _context.CurrencyRates
.Where(x => x.CurrencyId == currencyId)
.Where(x => x.DateTime <= before)
.OrderByDescending(x => x.DateTime)
.ThenByDescending(x => x.Id)
.Take(count)
@@ -4,7 +4,7 @@ using Models.Currencies;
public interface ICurrencyRateRepository
{
List<CurrencyRate> GetLastRates(Guid currencyId, int count = 1);
List<CurrencyRate> GetLastRates(Guid currencyId, DateTime? before = null, int count = 1);
List<CurrencyRate> GetLastRates(List<string> currencyIds);
bool AddRate(CurrencyRate currencyRate);
List<CurrencyRate> GetAtDate(Guid currencyId, DateTime date);