Exemplo n.º 1
0
        public ActionResult Buy(BuyModel model)
        {
            if (model.Quantity < 1)
            {
                TempData["BuyException"] = "Buying 0 stocks... Please try again";
                return(RedirectToAction("ConfirmBuy", new { symbol = model.Symbol }));
            }

            decimal transactionCost = (decimal)TransactionCost.BuyCost * (model.Quantity * model.PurchasePrice);

            string[]        temp       = model.Symbol.Split(',');
            SailiRepository repository = new SailiRepository();
            Buy             buy        = new Buy();

            buy.TradeDate         = DateTime.Now;
            buy.TickerSymbol      = temp[0];
            buy.Quantity          = model.Quantity;
            buy.PurchasePrice     = model.PurchasePrice;
            buy.TransactionAmount = model.Quantity * model.PurchasePrice + transactionCost;
            buy.TransactionCost   = transactionCost;
            buy = repository.Finalizebuy(buy, User.Identity.GetUserId());

            Owner owner = repository.GetOwner(User.Identity.GetUserId());

            TraderAccount trader = repository.GetTrader(owner.OwnerID);

            return(RedirectToAction("Details", "TraderAccount", new
            {
                TradingAccountID = trader.TradingAccountID
            }));
        }
Exemplo n.º 2
0
        public ActionResult Sell(Sell model)
        {
            try
            {
                Buy             buy        = DefaultConnection.Buys.Find(model.BuyID);
                Portfolio       portfolio  = DefaultConnection.Portfolios.Find(buy.PortfolioId);
                SailiRepository repository = new SailiRepository();
                Owner           owner      = new Owner();
                owner = repository.GetOwner(User.Identity.GetUserId());
                TraderAccount trader = new TraderAccount();
                trader = repository.GetTrader(owner.OwnerID);

                if (model.Quantity > buy.Quantity)
                {
                    TempData["SellException"] = "Selling more stocks than what is baught... Please try again";
                    return(RedirectToAction("ConfirmSell", new { tradingAccountID = portfolio.TradingAccountID, buyID = buy.BuyID }));
                }
                decimal transactionCost = (decimal)TransactionCost.SellCost * (model.Quantity * model.SoldPrice);
                model.PortfolioID       = portfolio.PortfolioID;
                model.TradeDate         = DateTime.Now;
                model.BuyID             = buy.BuyID;
                model.TransactionCost   = transactionCost;
                model.TransactionAmount = model.Quantity * model.SoldPrice - transactionCost;
                trader.Balance          = trader.Balance + model.TransactionAmount;
                buy.Quantity            = buy.Quantity - model.Quantity;
                TryUpdateModel(buy);
                DefaultConnection.Entry(buy).State = EntityState.Modified;
                DefaultConnection.SaveChanges();
                TryUpdateModel(trader);
                DefaultConnection.Entry(trader).State = EntityState.Modified;
                DefaultConnection.SaveChanges();
                TryUpdateModel(model);
                DefaultConnection.Sells.Add(model);
                DefaultConnection.SaveChanges();

                return(RedirectToAction("Details", "TraderAccount", new
                {
                    TradingAccountID = portfolio.TradingAccountID
                }));
            }
            catch
            {
                return(View(model));
            }
        }