Ejemplo n.º 1
0
        public void AwardInvestmentProfits()
        {
            var businessInvestments = _context.BusinessInvestments
                                      .Include(s => s.Business)
                                      .Include(s => s.Investment)
                                      .ThenInclude(s => s.BusinessInvestments)
                                      .Where(s => s.InvestmentType == InvestmentType.Investment)
                                      .AsEnumerable()
                                      .Where(s => { var elapsedTime = DateTime.UtcNow - s.Investment.InvestmentExpiration.Date.ToUniversalTime(); return(elapsedTime.TotalHours > 12); })
                                      .ToList()
                                      .GroupBy(s => s.InvestmentId);


            foreach (var item in businessInvestments)
            {
                var investorInvestment = item.First(s => s.InvestmentDirection == InvestmentDirection.Investor);
                var investeeInvestment = item.First(s => s.InvestmentDirection == InvestmentDirection.Investee);
                var investorsProfit    = InvestmentHelper.CalculateInvestmentProfit(investeeInvestment);

                // Investor
                investorInvestment.Business.Cash          += investorsProfit;
                investorInvestment.Business.CashPerSecond += investorInvestment.Investment.InvestmentAmount;
                investorInvestment.Business.ReceivedMessages.Add(new Message()
                {
                    DateReceived        = DateTime.UtcNow,
                    MessageBody         = $"You gained ${investorsProfit.ToKMB()} from your investments in {investeeInvestment.Business.Name}",
                    ReceivingBusinessId = investorInvestment.Business.Id,
                    ReadByBusiness      = false
                });

                // Investee
                investeeInvestment.Business.CashPerSecond -= investeeInvestment.Investment.InvestmentAmount;
                investeeInvestment.Business.ReceivedMessages.Add(new Message()
                {
                    DateReceived        = DateTime.UtcNow,
                    MessageBody         = $"After investments were removed, you lost ${investeeInvestment.Investment.InvestmentAmount.ToKMB()} CPS",
                    ReceivingBusinessId = investeeInvestment.Business.Id,
                    ReadByBusiness      = false
                });

                _context.Business.Update(investorInvestment.Business);
                _context.Business.Update(investeeInvestment.Business);

                _context.BusinessInvestments.Remove(investorInvestment);
                _context.Investments.Remove(investorInvestment.Investment);
                _context.BusinessInvestments.Remove(investeeInvestment);
                _context.Investments.Remove(investeeInvestment.Investment);
            }

            _context.SaveChanges();
        }
Ejemplo n.º 2
0
        public void AwardInvestmentProfits()
        {
            var investments = _context.Investments
                              .Include(s => s.BusinessToInvest)
                              .Include(s => s.InvestingBusiness)
                              .Where(s => s.InvestmentExpiration.Date <= DateTime.UtcNow)
                              .Where(s => s.InvestmentType == InvestmentType.Investment)
                              .ToList();

            foreach (var item in investments)
            {
                var investorsProfit = InvestmentHelper.CalculateInvestmentProfit(item);

                item.InvestingBusiness.Cash          += investorsProfit;
                item.InvestingBusiness.CashPerSecond += item.InvestmentAmount;
                item.BusinessToInvest.CashPerSecond  -= item.InvestmentAmount;

                _context.Business.Update(item.InvestingBusiness);
                _context.Business.Update(item.BusinessToInvest);
                _context.Investments.Remove(item);
            }

            _context.SaveChanges();
        }