public static StockSummaryListViewModel GetStockSummaryByUser(string username) { Entities entities = new Entities(); StockSummaryListViewModel result = new StockSummaryListViewModel(); DateTime current = DateTime.Now; var stocks = entities.Assets.Include("StockTransactions").Include("Liabilities").Where(x => x.Username.Equals(username) && x.AssetType == (int)Constants.Constants.ASSET_TYPE.STOCK && !x.DisabledDate.HasValue); foreach (var stock in stocks) { StockSummaryViewModel stockViewModel = new StockSummaryViewModel(); stockViewModel.Name = stock.AssetName; stockViewModel.Note = stock.Note; foreach (var transactions in stock.StockTransactions.Where(x => !x.DisabledDate.HasValue)) { StockTransactionViewModel transactionViewModel = StockQueries.CreateViewModel(transactions); stockViewModel.NumberOfStock += (int)transactionViewModel.NumberOfStock.Value; stockViewModel.SpotRice += transactionViewModel.SpotRice.Value; stockViewModel.StockValue += transactionViewModel.StockValue.Value; stockViewModel.ExpectedDividend += transactionViewModel.ExpectedDividend.Value; var liabilites = transactionViewModel.Liabilities.Liabilities.Where(x => x.StartDate <= current && x.EndDate >= current); stockViewModel.LiabilityValue = liabilites.Sum(x => x.Value.Value); stockViewModel.MonthlyInterestPayment = liabilites.Sum(x => x.MonthlyInterestPayment); stockViewModel.OriginalInterestPayment = liabilites.Sum(x => x.OriginalInterestPayment); stockViewModel.MonthlyPayment = liabilites.Sum(x => x.TotalMonthlyPayment); stockViewModel.AnnualPayment = stockViewModel.MonthlyPayment * 12; stockViewModel.RemainedValue = liabilites.Sum(x => x.RemainedValue); } stockViewModel.InterestRate = stockViewModel.LiabilityValue > 0 ? stockViewModel.OriginalInterestPayment / stockViewModel.LiabilityValue * 12 : 0; result.StockSummaries.Add(stockViewModel); } result.TotalLiabilityValue = result.StockSummaries.Sum(x => x.LiabilityValue); result.TotalNumberOfStock = result.StockSummaries.Sum(x => x.NumberOfStock); result.TotalStockValue = result.StockSummaries.Sum(x => x.StockValue); result.TotalMonthlyInterestPayment = result.StockSummaries.Sum(x => x.MonthlyInterestPayment); result.TotalInterestRate = result.TotalLiabilityValue > 0 ? result.StockSummaries.Sum(x => x.OriginalInterestPayment) / result.TotalLiabilityValue * 12 : 0; result.TotalMonthlyPayment = result.StockSummaries.Sum(x => x.MonthlyPayment); result.TotalAnnualPayment = result.StockSummaries.Sum(x => x.AnnualPayment); result.TotalRemainedValue = result.StockSummaries.Sum(x => x.RemainedValue); return(result); }
public static StockListViewModel GetStockByUser(string username) { Entities entities = new Entities(); StockListViewModel result = new StockListViewModel(); DateTime current = DateTime.Now; var stocks = entities.Assets.Include("StockTransactions").Include("Liabilities").Where(x => x.Username.Equals(username) && x.AssetType == (int)Constants.Constants.ASSET_TYPE.STOCK && !x.DisabledDate.HasValue); foreach (var stock in stocks) { StockViewModel stockViewModel = new StockViewModel(); stockViewModel.Id = stock.Id; stockViewModel.Name = stock.AssetName; stockViewModel.Note = stock.Note; foreach (var transactions in stock.StockTransactions.Where(x => !x.DisabledDate.HasValue)) { StockTransactionViewModel transactionViewModel = StockQueries.CreateViewModel(transactions); stockViewModel.Transactions.Transactions.Add(transactionViewModel); } var liabilities = stockViewModel.Transactions.Transactions.Select(x => x.Liabilities.Liabilities.Where(y => y.StartDate <= current && y.EndDate >= current)); stockViewModel.TotalLiabilityValue = liabilities.Sum(x => x.Sum(y => y.Value.HasValue ? y.Value.Value : 0)); stockViewModel.TotalOriginalPayment = liabilities.Sum(x => x.Sum(y => y.MonthlyOriginalPayment)); stockViewModel.TotalInterestPayment = liabilities.Sum(x => x.Sum(y => y.MonthlyInterestPayment)); stockViewModel.TotalMonthlyPayment = liabilities.Sum(x => x.Sum(y => y.TotalMonthlyPayment)); stockViewModel.TotalPayment = liabilities.Sum(x => x.Sum(y => y.TotalPayment)); stockViewModel.TotalRemainedValue = liabilities.Sum(x => x.Sum(y => y.RemainedValue)); stockViewModel.TotalOriginalInterestPayment = liabilities.Sum(x => x.Sum(y => y.OriginalInterestPayment)); stockViewModel.TotalInterestRate = stockViewModel.TotalLiabilityValue > 0 ? stockViewModel.TotalOriginalInterestPayment / stockViewModel.TotalLiabilityValue * 12 : 0; stockViewModel.RowSpan = stockViewModel.Transactions.Transactions.Any() ? stockViewModel.Transactions.Transactions.Count() + stockViewModel.Transactions.Transactions.Select(x => x.Liabilities.Liabilities).Count() + 4 : 4; if (stockViewModel.Transactions.Transactions.Any()) { stockViewModel.RowSpan = 4; bool flag = false; foreach (var transaction in stockViewModel.Transactions.Transactions) { if (transaction.Liabilities.Liabilities.Count() > 0) { if (flag == false) { flag = true; } stockViewModel.RowSpan += transaction.Liabilities.Liabilities.Count(); } } if (flag == true) { stockViewModel.RowSpan += 1; } } else { stockViewModel.RowSpan = 4; } result.Stocks.Add(stockViewModel); } result.TotalValue = result.Stocks.Select(x => x.Transactions.Transactions).Sum(x => x.Sum(y => y.StockValue.Value)); result.IsInitialized = UserQueries.IsCompleteInitialized(username); return(result); }