示例#1
0
        public ActionResult Create([Bind(Include = "SalesId,Shares,date,NetProfit,SharesLeft,PurchaseId")] Sales sales)
        {
            AppUser        customer       = db.Users.Find(User.Identity.GetUserId());
            PurchasedStock purchasedstock = customer.StockPortfolio.purchasedstocks.Find(c => c.PurchasedStockId == sales.PurchaseId);

            sales.NetProfit  = Convert.ToDecimal(purchasedstock.InitialPrice * purchasedstock.Shares) + (sales.Shares * purchasedstock.stock.LastPrice);
            sales.SharesLeft = purchasedstock.Shares - sales.Shares;

            //sales.PurchaseId = customer.StockPortfolio.sale;



            if (sales.date < purchasedstock.Date)
            {
                return(View("Error"));
            }
            if (sales.SharesLeft < 0)
            {
                return(View("Error"));
            }
            if (ModelState.IsValid)
            {
                db.Sales.Add(sales);
                db.SaveChanges();
                return(RedirectToAction("Details", sales.SalesId));
            }

            return(View(sales));
        }
        public IActionResult ShowUserProfile()
        {
            var  users              = _dataContext.Users.Include(x => x.Wallet).ThenInclude(y => y.Deals).ThenInclude(z => z.Stock).ThenInclude(t => t.Company);
            User currentUser        = users.FirstOrDefault(p => p.UserName == User.Identity.Name);
            UserInfoViewModel model = new UserInfoViewModel
            {
                Id            = currentUser.Id,
                UserName      = currentUser.UserName,
                TotalMoneySum = currentUser.Wallet.TotalSum
            };
            List <PurchasedStock> purchasedStocks = new List <PurchasedStock>();

            foreach (var deal in currentUser.Wallet.Deals)
            {
                PurchasedStock purchasedStock = new PurchasedStock
                {
                    StockId              = deal.Stock.Id,
                    CompanyFullName      = deal.Stock.Company.FullName,
                    CompanyShortName     = deal.Stock.Company.ShortName,
                    PurchasedStockNumber = deal.StockNumber,
                    StockGrowth          = deal.Stock.CurrentPrice - deal.CurrentStockPrice,
                    StockGrowthInPercent = (deal.Stock.CurrentPrice - deal.CurrentStockPrice) / deal.Stock.CurrentPrice * 100
                };
                purchasedStocks.Add(purchasedStock);
            }
            foreach (var p in purchasedStocks)
            {
                PurchasedStock purchasedStock = new PurchasedStock
                {
                    StockId              = p.StockId,
                    CompanyFullName      = p.CompanyFullName,
                    CompanyShortName     = p.CompanyShortName,
                    StockGrowth          = p.StockGrowth,
                    StockGrowthInPercent = p.StockGrowthInPercent,
                    PurchasedStockNumber = 0
                };
                foreach (var t in purchasedStocks)
                {
                    if (p.CompanyShortName == t.CompanyShortName)
                    {
                        purchasedStock.PurchasedStockNumber += t.PurchasedStockNumber;
                    }
                }
                model.PurchasedStocks.Add(purchasedStock);
            }
            model.PurchasedStocks = model.PurchasedStocks.GroupBy(x => x.CompanyShortName).Select(x => x.First()).ToList();
            foreach (var deal in currentUser.Wallet.Deals)
            {
                DealDetails dealDetails = new DealDetails
                {
                    CompanyName       = deal.Stock.Company.FullName,
                    CompanyShortName  = deal.Stock.Company.ShortName,
                    IsBought          = deal.IsBought,
                    DealDate          = deal.DealDate,
                    StockNumberInDeal = deal.StockNumber
                };
                model.DealsDetails.Add(dealDetails);
            }
            return(View(model));
        }
示例#3
0
        public static double ComputeActualSellPrice(this PurchasedStock purchasedStock, double targetSellPrice)
        {
            var brokerCommission = purchasedStock.NumberOfShares * targetSellPrice * Properties.Settings.Default.BrokersChargePercentage;

            brokerCommission = brokerCommission > 20 ? brokerCommission : 20;

            return(brokerCommission
                   + purchasedStock.NumberOfShares * targetSellPrice * Properties.Settings.Default.SccpChargePercentage
                   + Properties.Settings.Default.VatPercentage * brokerCommission
                   + Properties.Settings.Default.SalesTaxPercentage * purchasedStock.NumberOfShares * targetSellPrice);
        }
示例#4
0
        // GET: PurchasedStocks/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PurchasedStock purchasedStock = db.PurchasedStocks.Find(id);

            if (purchasedStock == null)
            {
                return(HttpNotFound());
            }
            return(View(purchasedStock));
        }
示例#5
0
        public ActionResult Details([Bind(Include = "SalesId,Shares,date,NetProfit,SharesLeft,PurchaseId")] Sales sales)
        {
            AppUser        customer       = db.Users.Find(User.Identity.GetUserId());
            PurchasedStock purchasedstock = customer.StockPortfolio.purchasedstocks.Find(c => c.PurchasedStockId == sales.PurchaseId);

            purchasedstock.Shares                = sales.SharesLeft;
            purchasedstock.TotalFees            += sales.Fees;
            customer.StockPortfolio.CashBalance += sales.NetProfit;

            if (sales.SharesLeft == 0)
            {
                db.PurchasedStocks.Remove(purchasedstock);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
示例#6
0
 public StockCrudEvent(CrudType crudType, Guid entityId, PurchasedStock purchasedStock) : base(crudType, entityId)
 {
     PurchasedStock = purchasedStock;
 }
示例#7
0
        public ActionResult Purchase([Bind(Include = "Id,Shares,Date")] PurchasedStock stock, Int32 StockID, Int32 BankAccountID)
        {
            //get customer
            AppUser customer = db.Users.Find(User.Identity.GetUserId());
            //get purchased stock
            Stock FoundStock = db.Stocks.Find(StockID);
            //get bank acount to get money from
            BankAccount Account = db.BankAccounts.Find(BankAccountID);

            stock.InitialPrice = Convert.ToDecimal(GetQuote.GetStock(FoundStock.Symbol, DateTime.Parse(Convert.ToString(stock.Date))).LastTradePrice);

            //cash Balance of 0
            if (FoundStock.Fees > customer.StockPortfolio.CashBalance)
            {
                return(View("Error"));
            }

            //check to see if customer selected a stock portfolio
            if (Account.Type == AccountTypes.Stock)
            {
                //if so, see if balance is adequate
                if ((Convert.ToDecimal(stock.Shares * Convert.ToDecimal(GetQuote.GetStock(FoundStock.Symbol, DateTime.Parse(Convert.ToString(stock.Date))).LastTradePrice))) > customer.StockPortfolio.CashBalance)
                {
                    return(View("Error"));
                }
                else
                {
                    customer.StockPortfolio.CashBalance = customer.StockPortfolio.CashBalance - (Convert.ToDecimal(stock.Shares * Convert.ToDecimal(GetQuote.GetStock(FoundStock.Symbol, DateTime.Parse(Convert.ToString(stock.Date))).LastTradePrice)));
                }
            }
            else
            {
                if ((stock.Shares * Convert.ToDecimal(GetQuote.GetStock(FoundStock.Symbol, DateTime.Parse(Convert.ToString(stock.Date))).LastTradePrice)) > Account.Balance)
                {
                    return(View("Error"));
                }
            }
            if (customer.StockPortfolio.purchasedstocks != null)
            {
                foreach (PurchasedStock item in customer.StockPortfolio.purchasedstocks)
                {
                    if (item.stock.StockID == StockID)
                    {
                        //add purchased shares to existing purchased share number
                        item.Shares = item.Shares + stock.Shares;
                        //add to total fees
                        customer.StockPortfolio.Fees = customer.StockPortfolio.Fees + FoundStock.Fees;
                        //if successful, redirect here, must put adequate spot

                        //create new transaction for Fees
                        Transaction TransactionFees1 = new Transaction();
                        TransactionFees1.Date        = stock.Date;
                        TransactionFees1.Type        = TransactionTypes.Fee;
                        TransactionFees1.Amount      = FoundStock.Fees;
                        TransactionFees1.Description = "Fee: " + FoundStock.Name;
                        TransactionFees1.FromAccount = Account;
                        customer.Transactions.Add(TransactionFees1);
                        db.Transactions.Add(TransactionFees1);
                        db.SaveChanges();

                        //create transaction for withdrawl
                        Transaction TransactionWithdrawl1 = new Transaction();
                        TransactionWithdrawl1.Date        = stock.Date;
                        TransactionWithdrawl1.Type        = TransactionTypes.Withdrawal;
                        TransactionWithdrawl1.Amount      = Convert.ToDecimal(stock.Shares * FoundStock.LastPrice);
                        TransactionWithdrawl1.Description = "Stock Purchase - Account " + Account.AccountNumber;
                        TransactionWithdrawl1.FromAccount = Account;
                        customer.Transactions.Add(TransactionWithdrawl1);
                        db.Transactions.Add(TransactionWithdrawl1);
                        db.SaveChanges();


                        return(RedirectToAction("Index", "Customers"));
                    }
                }
            }

            //stock is not present in the portfolio
            customer.StockPortfolio.Fees = customer.StockPortfolio.Fees + FoundStock.Fees;
            //assign stock to purchased stock
            stock.stock = FoundStock;
            //assign stockp
            stock.stockportfolio = customer.StockPortfolio;
            customer.StockPortfolio.purchasedstocks.Add(stock);

            //create new transaction for Fees
            Transaction TransactionFees = new Transaction();

            TransactionFees.Date        = stock.Date;
            TransactionFees.Type        = TransactionTypes.Fee;
            TransactionFees.Amount      = FoundStock.Fees;
            TransactionFees.Description = "Fee: " + FoundStock.Name;
            TransactionFees.FromAccount = Account;
            customer.Transactions.Add(TransactionFees);
            db.Transactions.Add(TransactionFees);
            db.SaveChanges();

            //create transaction for withdrawl
            Transaction TransactionWithdrawl = new Transaction();

            TransactionWithdrawl.Date        = stock.Date;
            TransactionWithdrawl.Type        = TransactionTypes.Withdrawal;
            TransactionWithdrawl.Amount      = Convert.ToDecimal(stock.Shares * FoundStock.LastPrice);
            TransactionWithdrawl.Description = "Stock Purchase - Account " + Account.AccountNumber;
            TransactionWithdrawl.FromAccount = Account;
            customer.Transactions.Add(TransactionWithdrawl);
            db.Transactions.Add(TransactionWithdrawl);
            db.SaveChanges();

            return(RedirectToAction("Index", "Customers"));
        }
示例#8
0
        public static bool WillBreakEven(this PurchasedStock purchasedStock, double targetSellPrice)
        {
            var actualSellPrice = purchasedStock.ComputeActualSellPrice(targetSellPrice);

            return(purchasedStock.ActualBuyPrice <= actualSellPrice);
        }