public BuyAndHoldTransactionDto MapToDto(BuyAndHoldTransactionViewModel viewModel)
        {
            //decimal value;
            //double shares;
            //int xref;
            //DateTime validFrom;
            //DateTime validTo;
            //DateTime transactionDate;
            //DateTime sellDate;
            //int sellId;
            //int securityId;
            //int originalSecurityId;
            //int mergerId;
            //int spinOffId;
            //int splitId;

            var dto = new BuyAndHoldTransactionDto
            {
                Id = viewModel.Id,
                OriginalTransactionId = viewModel.OriginalTransactionId.ToNullableInt(),
                BuyAndHoldRawId = viewModel.BuyAndHoldRawId,
                TransactionDate = viewModel.TransactionDate.ToNullableDateTime(),
                SecurityId = viewModel.SecurityId.ToNullableInt(),
                //OriginalSecurityId = viewModel.OriginalSecurityId.ToNullableInt(),
                //TickerSymbol = viewModel.TickerSymbol,
               //OriginalTickerSymbol = viewModel.OriginalTickerSymbol,
                Period = viewModel.Period,
                //TransactionType = viewModel.TransactionType,
                TransactionTypeId = viewModel.TransactionTypeId,
                TransactionApplied = viewModel.TransactionApplied.ToNullableBool(),
                DividendAmount = viewModel.DividendAmount.ToNullableDecimal(),
                AmountInvested = viewModel.AmountInvested.ToNullableDecimal(),
                NumberShares = viewModel.NumberShares.ToNullableDouble(),
                BuyPrice = viewModel.BuyPrice.ToNullableDecimal(),
                Split = viewModel.Split,
                ValidFrom = viewModel.ValidFrom.ToNullableDateTime(),
                ValidTo = viewModel.ValidTo.ToNullableDateTime(),
                SellDate = viewModel.SellDate.ToNullableDateTime(),
                SellId = viewModel.SellId.ToNullableInt(),
                Notes = viewModel.Notes,
                Url1 = viewModel.Url1,
                Url2 = viewModel.Url2,
                Url3 = viewModel.Url3,
            MergerId = viewModel.MergerId.ToNullableInt(),
            SpinOffId = viewModel.SpinOffId.ToNullableInt(),
            SplitId = viewModel.Split.ToNullableInt(),
            }
            ;

            return dto;
        }
        public BuyAndHoldTransactionsViewModel BuildTransactionObjects(List<BuyAndHoldTransactionDto> transactions, NavDto nav, SecurityDto security, bool seperateReinvest)
        {
            var currentShareValue = double.Parse(nav.Nav.ToString());// GetLatestTickersNav("TWCUX");
            var splits = security.Splits;

            var viewModel = new BuyAndHoldTransactionsViewModel
            {
                Nav = currentShareValue.ToString("C"),
                SecurityTicker = security.TickerSymbol,
                SecurityName = security.CompanyName,
                Transactions = new List<BuyAndHoldTransactionViewModel>()
            };
            double totalInvested = 0;
            double totalReinvested = 0;
            double totalShares = 0;
            double currentValue = 0;
            double totalValue = 0;
            //double totalTotalValue = 0;
            double gain = 0;
            double totalGain = 0;

            decimal TOTAL = 0;
            //foreach (var transaction in transactions.Where(p => !p.ValidTo.HasValue || p.ValidTo.Value > DateTime.Now))
            foreach (var transaction in transactions)
            {
                // var transactionDate = transaction.TransactionDate.Value;
                var splitFactor = CalculateSplitFactor(splits, transaction.TransactionDate.Value);
                var currentShares = CalculateCurrentShares(transaction, splitFactor);

                var buyAndHoldTransaction = new BuyAndHoldTransactionViewModel
                {
                    Id = transaction.Id,
                    TransactionDate = transaction.TransactionDate.Value.ToShortDateString(),
                    SplitRatio = splitFactor != 1 ? Math.Round(splitFactor, 4).ToString() : string.Empty,
                    // TransactionType = transaction.TransactionType,
                    TransactionTypeId = transaction.TransactionTypeId,
                    NumberShares = CalculateShares(transaction, splitFactor, currentShares),
                    ValidTo = transaction.ValidTo.ToShortDateString(),
                    ValidFrom = transaction.ValidFrom.ToShortDateString()
                };

                if (transaction.OriginalTransaction != null)
                {
                    buyAndHoldTransaction.OriginalTransaction = BuildTransactionObject(transaction.OriginalTransaction, nav, splits, seperateReinvest);
                }

                if (IsMergerTransaction(transaction.TransactionTypeId)
                    && transaction.TransactionApplied.ToBool())
                {

                }
                else {

                    buyAndHoldTransaction.BuyPrice = CalculateBuyPrice(transaction, splitFactor);

                    var amountInvested = transaction.AmountInvested.HasValue ? double.Parse(transaction.AmountInvested.Value.ToString()) : 0;

                    if (transaction.TransactionType == "Buy" || !seperateReinvest)
                    {
                        totalInvested += amountInvested;
                    }
                    else
                    {
                        totalReinvested += amountInvested;
                    }
                    // var _shares = shares.HasValue ? decimal.Parse(shares.Value.ToString()) : 0;
                    // if (_shares > 0)
                    // {
                    //     TOTAL += _shares;
                    // }

                    if (transaction.TransactionTypeId != 15) //Split --            if (transaction.TransactionTypeId != 15) //Split                    {
                    {
                        totalShares += currentShares;
                        currentValue = currentShares * currentShareValue;
                        totalValue += currentValue;
                        gain = currentValue - amountInvested;
                        totalGain += gain;
                        //profit = currentValue - amountInvested;
                    }

                    //var sellTransaction = transaction.BuyAndHoldSellTransaction;
                    //var originalTransaction = transaction.BuyAndHoldOriginalTransaction;

                    //if (originalTransaction != null)
                    //{
                    //    var originalTransactionAmountInvested = originalTransaction.AmountInvested.HasValue ? originalTransaction.AmountInvested.Value : 0;

                    //    buyAndHoldTransaction.OriginalTransaction = new BuyAndHoldTransactionViewModel();
                    //    buyAndHoldTransaction.OriginalTransaction.Type = originalTransaction.Type + " [" + originalTransaction.OriginalSymbol.ToUpper() + "]";
                    //    buyAndHoldTransaction.OriginalTransaction.Id = originalTransaction.id;
                    //    buyAndHoldTransaction.OriginalTransaction.AmountInvested = originalTransactionAmountInvested.ToString("C");
                    //}

                    //if (sellTransaction != null)
                    //{
                    //    buyAndHoldTransaction.SellDate = sellTransaction.TransactionDate.Value.ToShortDateString();
                    //    buyAndHoldTransaction.SellPrice = sellTransaction.BuyPrice.HasValue ? sellTransaction.BuyPrice.Value.ToString("C") : "";
                    //    buyAndHoldTransaction.Proceeds = sellTransaction.AmountInvested.HasValue ? sellTransaction.AmountInvested.Value.ToString("C") : "";
                    //    buyAndHoldTransaction.Profit = ((sellTransaction.AmountInvested.HasValue ? sellTransaction.AmountInvested.Value : 0) - amountInvested).ToString("C");
                    //}

                    if (transaction.TransactionType == "Buy" || !seperateReinvest)
                    {
                        buyAndHoldTransaction.AmountInvested = amountInvested.ToString("C");
                    }
                    else if (amountInvested != 0)
                    {
                        buyAndHoldTransaction.AmountReinvested = amountInvested.ToString("C");
                    }
                    buyAndHoldTransaction.DividendAmount = transaction.DividendAmount.HasValue ? transaction.DividendAmount.Value.ToString("C") : "";

                    if (transaction.NumberShares.HasValue && transaction.TransactionType != "Split")
                    {
                        buyAndHoldTransaction.CurrentValue = currentValue.ToString("C");
                    }
                    if (transaction.TransactionType != "Split")
                    {
                        buyAndHoldTransaction.IsGainNegative = gain < 0;
                        buyAndHoldTransaction.Gain = gain.ToString("C");
                    }

                    buyAndHoldTransaction.TransactionType = transaction.TransactionType; //.ToString();

                    if ((transaction.TransactionType != null && transaction.TransactionType == "Split") || transaction.TransactionTypeId == 15)
                    {
                        buyAndHoldTransaction.TransactionType += " - " + transaction.Split;
                    }

                    buyAndHoldTransaction.TotalShares = double.Parse(totalShares.ToString("F3"));
                    buyAndHoldTransaction.TotalValue = totalValue.ToString("C"); //TOTAL.ToString("C");

                    //if (transaction.AmericanCenturySell != null)
                    //{
                    //    var sellPrice = transaction.AmericanCenturySell.SellPrice;
                    //    var proceeds = shares * sellPrice;

                    //    AmericanCenturyTransaction.SellDate = transaction.AmericanCenturySell.SellDate.Date.ToShortDateString();
                    //    AmericanCenturyTransaction.SellPrice = transaction.AmericanCenturySell.SellPrice.ToString("C");
                    //    AmericanCenturyTransaction.Proceeds = proceeds.ToString("C");
                    //    AmericanCenturyTransaction.Profit = (proceeds - amountInvested).ToString("C");
                    //    AmericanCenturyTransaction.CurrentValue = null;
                    //    AmericanCenturyTransaction.Gain = null;
                    //}
                }
                viewModel.Transactions.Add(buyAndHoldTransaction);
            }
            viewModel.TotalInvested = totalInvested.ToString("C");
            viewModel.TotalReinvested = totalReinvested.ToString("C");
            viewModel.TotalShares = totalShares.ToString("F3");
            viewModel.TotalValue = totalValue.ToString("C");
            viewModel.TotalGain = totalGain.ToString("C");
            return viewModel;
        }
        private BuyAndHoldTransactionViewModel BuildTransactionObject(BuyAndHoldTransactionDto transactionDto, NavDto nav, List<BuyAndHoldSplitDto> splits, bool seperateReinvest)
        {
            var currentShareValue = double.Parse(nav.Nav.ToString());// GetLatestTickersNav("TWCUX");

            // var transactionDate = transaction.TransactionDate.Value;
            var splitFactor = CalculateSplitFactor(splits, transactionDto.TransactionDate.Value);
            var currentShares = CalculateCurrentShares(transactionDto, splitFactor);

            var transaction = new BuyAndHoldTransactionViewModel
            {
                Id = transactionDto.Id,
                TransactionDate = transactionDto.TransactionDate.Value.ToShortDateString(),
                SplitRatio = splitFactor != 1 ? Math.Round(splitFactor, 4).ToString() : string.Empty,
               // TransactionType = transactionDto.TransactionType,
                TransactionTypeId = transactionDto.TransactionTypeId,
                NumberShares = CalculateShares(transactionDto, splitFactor, currentShares),
            };

            if (transactionDto.OriginalTransaction != null)
            {
                transaction.OriginalTransaction = BuildTransactionObject(transactionDto.OriginalTransaction, nav, splits, seperateReinvest);
            }
            //var shares = transaction.NumberShares;

            if (IsMergerTransaction(transactionDto.TransactionTypeId) && transactionDto.TransactionApplied.ToBool())
            {

            }
            else {

                transaction.BuyPrice = CalculateBuyPrice(transactionDto, splitFactor);

                var amountInvested = transactionDto.AmountInvested.HasValue ? double.Parse(transactionDto.AmountInvested.Value.ToString()) : 0;

                if (transactionDto.TransactionType == "Buy" || !seperateReinvest)
                {
                    transaction.AmountInvested = amountInvested.ToString("C");
                }
                else if (amountInvested != 0)
                {
                    transaction.AmountReinvested = amountInvested.ToString("C");
                }
                transaction.DividendAmount = transactionDto.DividendAmount.HasValue ? transactionDto.DividendAmount.Value.ToString("C") : "";

                transaction.TransactionType = transactionDto.TransactionType.ToString();

                if (transactionDto.TransactionType != null && transactionDto.TransactionType == "Split")
                {
                    transaction.TransactionType += " - " + transactionDto.Split;
                }
            }
            return transaction;
        }
        public BuyAndHoldTransactionViewModel MapToViewModel(BuyAndHoldTransactionDto dto)
        {
            var ViewModel = new BuyAndHoldTransactionViewModel
            {
                Id = dto.Id,
                BuyAndHoldRawId = dto.BuyAndHoldRawId,
                OriginalTransactionId = dto.OriginalTransactionId.ToString(),
                TransactionDate = dto.TransactionDate.ToShortDateString(),
                SecurityId = dto.SecurityId.ToString(),
                //TickerSymbol = dto.TickerSymbol,
                //OriginalTickerSymbol = dto.OriginalTickerSymbol,
                //OriginalSecurityId = dto.OriginalSecurityId.ToString(),
                Period = dto.Period,
                //TransactionType = dto.TransactionType,
                TransactionTypeId = dto.TransactionTypeId,
                TransactionApplied = dto.TransactionApplied.ToString(),
                DividendAmount = dto.DividendAmount.ToString(),
                AmountInvested = dto.AmountInvested.ToString(),
                NumberShares = dto.NumberShares.ToString(),
                BuyPrice = dto.BuyPrice.ToString(),
                Split = dto.Split,
                ValidFrom = dto.ValidFrom.ToShortDateString(),
                ValidTo = dto.ValidTo.ToShortDateString(),
                SellDate = dto.SellDate.ToShortDateString(),
                SellId = dto.SellId.ToString(),
                Notes = dto.Notes,
                Url1 = dto.Url1,
                Url2 = dto.Url2,
                Url3 = dto.Url3,
                MergerId = dto.MergerId.ToString(),
                SpinOffId = dto.SpinOffId.ToString(),
                SplitId = dto.SplitId.ToString(),
            };

            return ViewModel;
        }
Example #5
0
        public ActionResult EditTransaction(BuyAndHoldTransactionViewModel model)
        {
            var dto = _buyAndHoldTransactionDtoMap.MapToDto(model);

            dto = _buyAndHoldService.UpdateBuyAndHoldTransaction(dto);

            var viewModel = BuildBuyAndHoldTransactionViewModel(dto);
            return View(viewModel);
        }