Exemple #1
0
        private List <DomainObjects.Portfolio.Portfolio> FillPortfoliosDistribution(List <DomainObjects.Portfolio.Portfolio> portfolio)
        {
            var distributions = DistributionBusiness.List(portfolio.Select(c => c.ProjectionId.Value));

            portfolio.ForEach(c => c.Projection.Distribution = distributions.Where(d => d.ProjectionId == c.ProjectionId.Value).ToList());
            return(portfolio);
        }
Exemple #2
0
        public Model.Portfolio Get(string email, int portfolioId)
        {
            var user      = UserBusiness.GetValidUser(email);
            var purchase  = Task.Factory.StartNew(() => BuyBusiness.Get(user.Id, portfolioId));
            var portfolio = Task.Factory.StartNew(() => Get(portfolioId));

            Task.WaitAll(portfolio, purchase);

            var owned     = user.Id == portfolio.Result.Advisor.UserId;
            var purchased = purchase.Result != null && BuyBusiness.IsValidPurchase(purchase.Result);

            if (portfolio.Result == null || (!owned && !purchased && (!portfolio.Result.Detail.Enabled || !portfolio.Result.Advisor.Detail.Enabled)))
            {
                throw new ArgumentException("Invalid portfolio.");
            }

            var portfolioQty = Task.Factory.StartNew(() => BuyBusiness.ListPortfoliosPurchases(new int[] { portfolioId }));
            var history      = Task.Factory.StartNew(() => PortfolioHistoryBusiness.ListHistory(portfolioId));

            Task <List <Distribution> > distribution   = null;
            Task <List <EscrowResult> > escrowResult   = null;
            Task <decimal?>             purchaseAmount = null;

            if (purchased && purchase.Result.LastTransaction.TransactionStatus == TransactionStatus.Success.Value)
            {
                distribution = Task.Factory.StartNew(() => DistributionBusiness.List(new int[] { portfolio.Result.ProjectionId.Value }));
                Task.WaitAll(history, portfolioQty, distribution);
            }
            else if (owned)
            {
                distribution   = Task.Factory.StartNew(() => DistributionBusiness.List(new int[] { portfolio.Result.ProjectionId.Value }));
                escrowResult   = Task.Factory.StartNew(() => EscrowResultBusiness.ListByPortfolio(portfolio.Result.Id));
                purchaseAmount = Task.Factory.StartNew(() => BuyBusiness.ListPortfolioPurchaseAmount(portfolio.Result.Id));
                Task.WaitAll(history, portfolioQty, distribution, escrowResult, purchaseAmount);
            }
            else
            {
                Task.WaitAll(history, portfolioQty);
            }

            portfolio.Result.PortfolioHistory = history.Result;
            var result = FillPortfolioModel(portfolio.Result, portfolio.Result.Advisor, user,
                                            purchase.Result != null ? new Buy[] { purchase.Result } : null, portfolioQty.Result);

            result.Purchased   = purchased;
            result.Histogram   = PortfolioHistoryBusiness.GetHistogram(history.Result);
            result.HistoryData = history.Result.Select(c => new Model.Portfolio.History()
            {
                Date  = c.Date,
                Value = c.RealValue
            }).ToList();
            result.AssetDistribution = distribution == null ? null : distribution.Result.Select(c => new Model.Portfolio.Distribution()
            {
                Id         = c.AssetId,
                Code       = c.Asset.Code,
                Name       = c.Asset.Name,
                Percentage = c.Percent,
                Type       = (int)c.Asset.Type
            }).OrderByDescending(c => c.Percentage).ToList();

            if (purchased)
            {
                GoalOption goalOption = null;
                if (purchase.Result.Goal != null)
                {
                    goalOption = GoalOptionsBusiness.Get(purchase.Result.Goal.GoalOptionId);
                }

                result.PurchaseData = new Model.Portfolio.Purchase()
                {
                    CreationDate      = purchase.Result.CreationDate,
                    ExpirationDate    = purchase.Result.ExpirationDate,
                    Price             = purchase.Result.PortfolioDetail.Price,
                    AucEscrow         = distribution != null ? purchase.Result.PortfolioDetail.Price : 0,
                    TransactionStatus = (int)purchase.Result.LastTransaction.TransactionStatus,
                    Risk = purchase.Result.Projection.Risk,
                    Goal = purchase.Result.Goal == null ? null : new Model.Portfolio.Goal()
                    {
                        Timeframe           = purchase.Result.Goal.Timeframe,
                        MonthlyContribution = purchase.Result.Goal.MonthlyContribution,
                        StartingAmount      = purchase.Result.Goal.StartingAmount,
                        TargetAmount        = purchase.Result.Goal.TargetAmount,
                        Risk = RiskType.Get(purchase.Result.Goal.Risk, goalOption.Risk).Value
                    }
                };
            }
            else if (owned)
            {
                result.OwnerData = new Model.Portfolio.Owner();
                if (purchaseAmount.Result.HasValue && purchaseAmount.Result.Value > 0)
                {
                    var validResults = escrowResult.Result.Where(c => c.LastTransaction.TransactionStatus == TransactionStatus.Success.Value);
                    result.OwnerData.AucReached = validResults.Sum(c => c.OwnerTokenResult);
                    result.OwnerData.AucLost    = validResults.Sum(c => c.BuyerTokenResult);
                    result.OwnerData.AucEscrow  = purchaseAmount.Result.Value - result.OwnerData.AucReached - result.OwnerData.AucLost;
                }
            }
            return(result);
        }