public async Task GetPaperProfit()
        {
            var profit1 = await _portfolioService.GetPaperProfit(10, 1);

            var profit2 = await _portfolioService.GetPaperProfit(11, 1);

            var profit3 = await _portfolioService.GetPaperProfit(12, 2);

            var profit4 = await _portfolioService.GetPaperProfit(12, 1);

            Assert.IsTrue(profit1.IsSuccess, "Неуспешное выполнение операции");
            Assert.AreEqual(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, profit1.Result.Value, "Неверная прибыль");
            Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, 2000000),
                            profit1.Result.Percent, "Неверный процент");

            Assert.IsTrue(profit2.IsSuccess, "Неуспешное выполнение операции");
            Assert.AreEqual(26580, profit2.Result.Value, "Неверная прибыль");
            Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(26580, 1000000),
                            profit2.Result.Percent, "Неверный процент");

            Assert.IsTrue(profit3.IsSuccess, "Неуспешное выполнение операции");
            Assert.AreEqual(34720, profit3.Result.Value, "Неверная прибыль");
            Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(34720, 1500000), profit3.Result.Percent, "Неверный процент");

            Assert.IsFalse(profit4.IsSuccess, "Прибыль в чужом портфеле");
        }
示例#2
0
        public async Task <OperationResult <ValuePercent> > GetPortfolioPaymentProfit(int portfolioId, int userId)
        {
            var result = await GetPortfolioPayments(portfolioId, userId);

            if (!result.IsSuccess)
            {
                return(new OperationResult <ValuePercent>()
                {
                    IsSuccess = result.IsSuccess,
                    Message = result.Message,
                    Result = null
                });
            }

            var payments = result.Result;

            var profit       = payments.Aggregate(0, (sum, payment) => sum + payment.PaymentValue);
            var investingSum = _balanceService.GetInvestSum(portfolioId, userId);

            var percent = FinanceHelpers.DivWithOneDigitRound(profit, investingSum);

            return(new OperationResult <ValuePercent>()
            {
                IsSuccess = true,
                Message = "Дивидендная прибыль",
                Result = new ValuePercent()
                {
                    Value = profit,
                    Percent = percent
                }
            });
        }
        public async Task <OperationResult <ValuePercent> > AggregatePaperProfit(IEnumerable <int> portfolioIds, int userId)
        {
            var sumProfit = 0;
            var sumInvest = 0;

            var ids = portfolioIds.ToList();

            foreach (var portfolioId in ids)
            {
                var resultProfit = await _portfolioService.GetPaperProfit(portfolioId, userId);

                var resultInvestSum = _balanceService.GetInvestSum(portfolioId, userId);

                if (!resultProfit.IsSuccess)
                {
                    return(resultProfit);
                }

                sumProfit += resultProfit.Result.Value;
                sumInvest += resultInvestSum;
            }

            var percent = FinanceHelpers.DivWithOneDigitRound(sumProfit, sumInvest);

            return(new OperationResult <ValuePercent>()
            {
                IsSuccess = true,
                Message = $"Суммарная бумажная прибыль портфелей(я) c id={string.Join(", ", ids)}",
                Result = new ValuePercent()
                {
                    Value = sumProfit,
                    Percent = percent
                }
            });
        }
        public async Task AggregatePaperProfit()
        {
            var result1 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 10, 11 }, 1);

            var result2 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 10, 11, 12 }, 1);

            var result3 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 10 }, 1);

            var result4 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 12 }, 2);

            var result5 = await _aggregatePortfolioService.AggregatePaperProfit(new[] { 12 }, 1);

            Assert.IsTrue(result1.IsSuccess);
            Assert.AreEqual(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000 + 26580, result1.Result.Value);
            Assert.AreEqual(
                FinanceHelpers.DivWithOneDigitRound(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000 + 26580, 3000000),
                result1.Result.Percent);

            Assert.IsTrue(result3.IsSuccess);
            Assert.AreEqual(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, result3.Result.Value);
            Assert.AreEqual(
                FinanceHelpers.DivWithOneDigitRound(34720 * 2 + 26580 * 2 + 20000 + 6283 - 100000, 2000000),
                result3.Result.Percent);

            Assert.IsTrue(result4.IsSuccess);
            Assert.AreEqual(34720, result4.Result.Value);
            Assert.AreEqual(FinanceHelpers.DivWithOneDigitRound(34720, 1500000), result4.Result.Percent);

            Assert.IsFalse(result2.IsSuccess, "Считается портфель чужого пользователя");
            Assert.IsFalse(result5.IsSuccess, "Считается портфель чужого пользователя");
        }
示例#5
0
        public async Task <OperationResult <ValuePercent> > GetPaperProfit(int portfolioId, int userId)
        {
            var portfolio = await _financeData.EfContext.Portfolios.FindAsync(portfolioId);

            var validationResult = CommonValidate <ValuePercent>(portfolioId, userId, portfolio);

            if (validationResult != null)
            {
                return(validationResult);
            }

            var portfolioData = GetPortfolioData(portfolioId);

            var sumProfit = 0;

            foreach (var asset in portfolioData.Assets)
            {
                var profit = await asset.GetPaperProfit();

                sumProfit += profit;
            }

            var investingSum = _balanceService.GetInvestSum(portfolioId, userId);
            var percent      = FinanceHelpers.DivWithOneDigitRound(sumProfit, investingSum);

            return(new OperationResult <ValuePercent>()
            {
                IsSuccess = true,
                Message = $"Бумажная прибыль портфеля {portfolio.Name}",
                Result = new ValuePercent()
                {
                    Value = sumProfit,
                    Percent = percent
                }
            });
        }
        public async Task <double> GetPaperProfitPercent()
        {
            var profit = await GetPaperProfit();

            return(FinanceHelpers.DivWithOneDigitRound(profit, BoughtPrice));
        }