fix
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user