Ejemplo n.º 1
0
        public ActionResult SellStockOptions([Bind(Include = "StockAccountID,StockMarketID,TradeID,Quantity,SaleDate")] SellStockTradeOptions SSTO)
        {
            // Get the stock to sell
            StockMarket StockToSell = db.StockMarket.Find(SSTO.StockMarketID);

            // Get the trade
            Trade CustomerTrade = db.Trades.Find(SSTO.TradeID);

            // Create a new sellstock object and send to the view
            SellStocksTrade SST = new SellStocksTrade
            {
                StockMarketID   = SSTO.StockMarketID,
                StockAccountID  = SSTO.StockAccountID,
                StockName       = StockToSell.CompanyName,
                QuantitySold    = SSTO.Quantity,
                Fee             = StockToSell.Fee,
                Profit          = ((StockToSell.StockPrice * SSTO.Quantity) - (CustomerTrade.PricePerShare * SSTO.Quantity)),
                SharesRemaining = (CustomerTrade.Quantity - SSTO.Quantity),
                TradeID         = SSTO.TradeID,
                SaleDate        = SSTO.SaleDate
            };

            return(View("SellStocks", SST));
        }
Ejemplo n.º 2
0
        public ActionResult SellStocks(SellStocksTrade Sale)
        {
            // Get the Customer
            // Query the Database for the logged in user
            var CustomerQuery = from c in db.Users
                                where c.UserName == User.Identity.Name
                                select c;
            // Get the Customer
            AppUser customer = CustomerQuery.FirstOrDefault();

            //Return frozen view if no go
            if (customer.ActiveStatus == false)
            {
                return(View("Frozen"));
            }

            // Get the original trade
            Trade OriginalTrade = db.Trades.Find(Sale.TradeID);

            // Get the Stock that is being sold
            StockMarket StockSale = db.StockMarket.Find(Sale.StockMarketID);

            // Get the Stock Account
            StockAccount CustomerStockAccount = db.StockAccount.Find(Sale.StockAccountID);

            // create a new transaction list for the trade
            List <BankingTransaction> TradeTrans = new List <BankingTransaction>();

            // Check the dates
            if (Sale.SaleDate < OriginalTrade.TransactionDate)
            {
                ViewBag.Error = "Cannot sell before purchase";
                return(View("SaleError"));
            }

            if (Sale.QuantitySold == 0)
            {
                ViewBag.Error = "Cannot sell with zero quantity";
                return(View("SaleError"));
            }

            // String for the description
            String Description = ($"Sale of {StockSale.CompanyName}, for {Sale.QuantitySold} shares, with initial price of {OriginalTrade.PricePerShare}, current price of {StockSale.StockPrice}, and a gain/loss of {Sale.Profit}");

            // Sale Amount
            Decimal SaleAmount = (Sale.QuantitySold * StockSale.StockPrice);

            // Create a new fee transaction and add it to the list
            BankingTransaction FeeTrans = new BankingTransaction
            {
                Amount = StockSale.Fee,
                BankingTransactionType = BankingTranactionType.Fee,
                Description            = ("Fee for sale of " + StockSale.CompanyName),
                StockAccount           = CustomerStockAccount,
                TransactionDate        = Sale.SaleDate,
                TransactionDispute     = DisputeStatus.NotDisputed
            };

            // Add the transaction to the list
            TradeTrans.Add(FeeTrans);

            // Make the trade happen
            Trade SaleTrade = new Trade()
            {
                TradeType           = TradeType.Sell,
                Amount              = (Sale.QuantitySold * StockSale.StockPrice),
                PricePerShare       = StockSale.StockPrice,
                Ticker              = StockSale.Ticker,
                Quantity            = Sale.QuantitySold,
                TransactionDate     = Sale.SaleDate,
                Description         = Description,
                StockMarket         = StockSale,
                StockAccount        = CustomerStockAccount,
                BankingTransactions = TradeTrans,
                TransactionDispute  = DisputeStatus.NotDisputed
            };

            // Create a new transaction for the actual sale
            BankingTransaction SaleTrans = new BankingTransaction
            {
                Amount = SaleAmount,
                BankingTransactionType = BankingTranactionType.Deposit,
                Description            = Description,
                StockAccount           = CustomerStockAccount,
                Trade              = SaleTrade,
                TransactionDate    = Sale.SaleDate,
                TransactionDispute = DisputeStatus.NotDisputed
            };

            // Add the transactions and the trades the the db
            db.BankingTransaction.Add(FeeTrans);
            db.SaveChanges();

            db.Trades.Add(SaleTrade);
            db.SaveChanges();

            db.BankingTransaction.Add(SaleTrans);
            db.SaveChanges();

            // Update the stock account

            // Take out the fee
            CustomerStockAccount.CashBalance -= Sale.Fee;

            // Add the fee to the account
            CustomerStockAccount.TradingFee += Sale.Fee;

            // Add/Subtract the profit
            CustomerStockAccount.CashBalance += Sale.Profit;

            // Update the Database
            db.Entry(CustomerStockAccount).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();


            // Remove the shares from the account

            // Check to see if there is any stock left
            // If there is no stock left then we need to remove the original buy trade
            if (OriginalTrade.Quantity - Sale.QuantitySold == 0)
            {
                // Clear the associated foriegn keys
                OriginalTrade.BankingTransactions.Clear();

                // Remove the original trade
                db.Trades.Remove(OriginalTrade);
            }

            // If the original trade quantity is not zero
            else
            {
                // update the quantity
                OriginalTrade.Quantity -= Sale.QuantitySold;

                // update the database
                db.Entry(OriginalTrade).State = System.Data.Entity.EntityState.Modified;
            }

            // Save the changes
            db.SaveChanges();

            // Check to see if the account is balanced
            BalancedPortfolio.CheckBalanced(db, customer);

            // Return users to the stock account details page
            return(RedirectToAction("Details", "StockAccounts"));
        }