public List <string> ValidateAccrueProfits(ApplicationUser user, InvestmentProgramAccrual accrual) { var periodErrors = ValidateClosePeriod(user, accrual.InvestmentProgramId); if (periodErrors.Any()) { return(periodErrors); } var result = new List <string>(); var investorIds = accrual.Accruals.Select(x => x.InvestorId).ToList(); foreach (var acc in accrual.Accruals) { var investorTokens = context.InvestorTokens .Include(p => p.ManagerToken) .ThenInclude(p => p.InvestmentProgram) .Where(p => p.InvestorAccountId == acc.InvestorId) .FirstOrDefault(p => p.ManagerToken.InvestmentProgram.Id == accrual.InvestmentProgramId); if (investorTokens == null) { result.Add($"Investor {acc.InvestorId} doesn't belong to investment program"); } } return(result); }
public IActionResult AccrueProfits([FromBody] InvestmentProgramAccrual accrual) { var errors = brokerValidator.ValidateAccrueProfits(CurrentUser, accrual); if (errors.Any()) { return(BadRequest(ErrorResult.GetResult(errors, ErrorCodes.ValidationError))); } var result = trustManagementService.AccrueProfits(accrual); if (!result.IsSuccess) { return(BadRequest(ErrorResult.GetResult(result))); } return(Ok()); }
public OperationResult AccrueProfits(InvestmentProgramAccrual accrual) { return(InvokeOperations.InvokeOperation(() => { var GVTUSDRate = rateService.GetRate(Currency.GVT, Currency.USD); if (!GVTUSDRate.IsSuccess) { throw new Exception("Error at GetRate: " + string.Join(", ", GVTUSDRate.Errors)); } var lastPeriod = context.Periods .Where(x => x.InvestmentProgramId == accrual.InvestmentProgramId) .OrderByDescending(x => x.Number) .First(); var investmentProgram = context.InvestmentPrograms .Include(x => x.ManagerAccount) .ThenInclude(x => x.BrokerTradeServer) .ThenInclude(x => x.Broker) .ThenInclude(x => x.User) .ThenInclude(x => x.Wallets) .First(x => x.Id == accrual.InvestmentProgramId); var totalProfit = accrual.Accruals.Sum(a => a.Amount); var wallet = investmentProgram.ManagerAccount.BrokerTradeServer.Broker.User.Wallets.First(x => x.Currency == Currency.GVT); var brokerAmount = totalProfit / GVTUSDRate.Data; wallet.Amount -= brokerAmount; var brokerTx = new WalletTransactions { Id = Guid.NewGuid(), Type = WalletTransactionsType.ProfitFromProgram, Amount = -brokerAmount, Date = DateTime.UtcNow, WalletId = wallet.Id, InvestmentProgramtId = accrual.InvestmentProgramId }; var brokerProfitDistribution = new ProfitDistributionTransactions { PeriodId = lastPeriod.Id, WalletTransactionId = brokerTx.Id }; context.Add(brokerTx); context.Add(brokerProfitDistribution); foreach (var acc in accrual.Accruals) { var investor = context.InvestorAccounts .Include(x => x.User) .ThenInclude(x => x.Wallets) .First(x => x.UserId == acc.InvestorId); var investorWallet = investor.User.Wallets.First(x => x.Currency == Currency.GVT); var investorAmount = acc.Amount / GVTUSDRate.Data; investorWallet.Amount += investorAmount; var investorTx = new WalletTransactions { Id = Guid.NewGuid(), Type = WalletTransactionsType.ProfitFromProgram, Amount = investorAmount, Date = DateTime.UtcNow, WalletId = investorWallet.Id, InvestmentProgramtId = accrual.InvestmentProgramId }; var investorProfitDistribution = new ProfitDistributionTransactions { PeriodId = lastPeriod.Id, WalletTransactionId = investorTx.Id }; context.Add(investorTx); context.Add(investorProfitDistribution); } //ToDo: update manager's next period balance context.SaveChanges(); })); }