Ejemplo n.º 1
0
        public ActionResult PurchaseStocks(PurchaseStockTrade PurchcaseTrade)
        {
            try
            {
                // get the customer
                AppUser Customer = db.Users.Find(PurchcaseTrade.CustomerProfile.Id);

                if (Customer == null)
                {
                    return(RedirectToAction("Portal", "Home"));
                }

                if (PurchcaseTrade.TradeDate < DateTime.Today || PurchcaseTrade.TradeDate == null)
                {
                    ViewBag.Error = "Date cannot be before today";
                    return(View("PurchaseError"));
                }

                if (PurchcaseTrade.Quantity == 0)
                {
                    ViewBag.Error = "Quantity cannot be 0";
                    return(View("PurchaseError"));
                }
                // Get the stock
                StockMarket SelectedStock = db.StockMarket.Find(PurchcaseTrade.SelectedStock.StockMarketID);

                // Get the total amount
                Decimal decTotal = (PurchcaseTrade.Quantity * SelectedStock.StockPrice);

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

                // Create a new fee transaction and add it to the list
                BankingTransaction FeeTrans = new BankingTransaction
                {
                    Amount = SelectedStock.Fee,
                    BankingTransactionType = BankingTranactionType.Fee,
                    Description            = ("Fee for purchase of " + SelectedStock.CompanyName),
                    TransactionDate        = (PurchcaseTrade.TradeDate),
                    StockAccount           = Customer.StockAccount.FirstOrDefault(),
                    TransactionDispute     = DisputeStatus.NotDisputed
                };

                TradeTrans.Add(FeeTrans);

                // Make the trade happen
                Trade Trade = new Trade()
                {
                    Amount              = decTotal,
                    Quantity            = PurchcaseTrade.Quantity,
                    Ticker              = SelectedStock.Ticker,
                    TransactionDate     = PurchcaseTrade.TradeDate,
                    PricePerShare       = SelectedStock.StockPrice,
                    TradeType           = TradeType.Buy,
                    StockAccount        = Customer.StockAccount.FirstOrDefault(),
                    StockMarket         = SelectedStock,
                    BankingTransactions = TradeTrans,
                    DisputeMessage      = "None",
                    TransactionDispute  = DisputeStatus.NotDisputed,
                    Description         = "None",
                    CorrectedAmount     = 0
                };

                // Get the customer Savings account
                var SAQ = from sa in db.StockAccount
                          where sa.Customer.Id == Customer.Id
                          select sa;

                StockAccount CustomerStockAccount = SAQ.FirstOrDefault();

                // check the account nulls
                if (PurchcaseTrade.CheckingAccounts != null)
                {
                    // find the account
                    Checking CustomerChecking = db.CheckingAccount.Find(PurchcaseTrade.CheckingAccounts.CheckingID);

                    if (CustomerChecking == null)
                    {
                        return(RedirectToAction("Portal", "Home"));
                    }

                    // take the money from the account
                    if (CustomerChecking.Balance - decTotal >= 0)
                    {
                        // Check to see if the cash balance is enough for the fee
                        if (db.StockAccount.Find(Customer.StockAccount.FirstOrDefault().StockAccountID).CashBalance < SelectedStock.Fee)
                        {
                            ViewBag.Error = "Fee was greater than Stock Cash Balance";
                            return(View("PurchaseError"));
                        }

                        // create a list to hold the checking account
                        List <Checking> CheckingList = new List <Checking>();
                        CheckingList.Add(CustomerChecking);

                        //Create a new transaction
                        BankingTransaction CheckingTrans = new BankingTransaction
                        {
                            Amount = decTotal,
                            BankingTransactionType = BankingTranactionType.Withdrawl,
                            CheckingAccount        = CheckingList,
                            Description            = ("Stock Purchase - Stock Account " + Customer.StockAccount.FirstOrDefault().AccountNumber.ToString()),
                            TransactionDate        = PurchcaseTrade.TradeDate,
                            Trade = Trade,
                            TransactionDispute = DisputeStatus.NotDisputed
                        };

                        // add the stuff to the database
                        db.BankingTransaction.Add(FeeTrans);
                        db.SaveChanges();

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

                        // Take the money out
                        db.CheckingAccount.Find(CustomerChecking.CheckingID).Balance -= decTotal;

                        // take out the fee
                        CustomerStockAccount.CashBalance -= SelectedStock.Fee;

                        // take out the fee
                        CustomerStockAccount.TradingFee += SelectedStock.Fee;

                        db.Entry(CustomerStockAccount).State = System.Data.Entity.EntityState.Modified;
                        db.BankingTransaction.Add(CheckingTrans);
                        db.SaveChanges();
                    }

                    // HACK
                    else
                    {
                        ViewBag.Error = "Not Enough Money in Account to Purchase the Stocks";
                        return(View("PurchaseError"));
                    }


                    // Any further changes
                }
                else if (PurchcaseTrade.SavingsAccount != null)
                {
                    // Get the customer Savings account
                    Saving CustomerSavings = db.SavingsAccount.Find(PurchcaseTrade.SavingsAccount.SavingID);

                    // take the money from the account
                    if (CustomerSavings.Balance - decTotal >= 0)
                    {
                        // Check to see if the cash balance is enough for the fee
                        if (db.StockAccount.Find(Customer.StockAccount.FirstOrDefault().StockAccountID).CashBalance < SelectedStock.Fee)
                        {
                            ViewBag.Error = "Fee was greater than Stock Cash Balance";
                            return(View("PurchaseError"));
                        }

                        // create a list to hold the checking account
                        List <Saving> SavingsList = new List <Saving>();
                        SavingsList.Add(CustomerSavings);

                        //Create a new transaction
                        BankingTransaction SavingsTrans = new BankingTransaction
                        {
                            Amount = decTotal,
                            BankingTransactionType = BankingTranactionType.Withdrawl,
                            SavingsAccount         = SavingsList,
                            Description            = ("Stock Purchase - Stock Account " + Customer.StockAccount.FirstOrDefault().AccountNumber.ToString()),
                            TransactionDate        = PurchcaseTrade.TradeDate,
                            Trade = Trade,
                            TransactionDispute = DisputeStatus.NotDisputed
                        };

                        // add the stuff to the database
                        db.BankingTransaction.Add(FeeTrans);
                        db.SaveChanges();

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

                        // Take the money out
                        db.SavingsAccount.Find(CustomerSavings.SavingID).Balance -= decTotal;

                        // take out the fee
                        CustomerStockAccount.CashBalance -= SelectedStock.Fee;

                        // take out the fee
                        CustomerStockAccount.TradingFee += SelectedStock.Fee;

                        db.Entry(CustomerStockAccount).State = System.Data.Entity.EntityState.Modified;
                        db.BankingTransaction.Add(SavingsTrans);
                        db.SaveChanges();
                    }

                    // HACK
                    else
                    {
                        ViewBag.Error = "Not Enough Money in Account to Purchase the Stocks";
                        return(View("PurchaseError"));
                    }
                }
                else if (PurchcaseTrade.AccountStock != null)
                {
                    // take the money from the account
                    if (CustomerStockAccount.CashBalance - decTotal >= 0)
                    {
                        // Check to see if the cash balance is enough for the fee
                        if (db.StockAccount.Find(Customer.StockAccount.FirstOrDefault().StockAccountID).CashBalance < SelectedStock.Fee)
                        {
                            ViewBag.Error = "Fee was greater than Stock Cash Balance";
                            return(View("PurchaseError"));
                        }

                        // create a list to hold the checking account
                        List <StockAccount> StockAccountList = new List <StockAccount>();
                        StockAccountList.Add(CustomerStockAccount);

                        //Create a new transaction
                        BankingTransaction StocksTrans = new BankingTransaction()
                        {
                            Amount = decTotal,
                            BankingTransactionType = BankingTranactionType.Withdrawl,
                            StockAccount           = CustomerStockAccount,
                            Description            = ("Stock Purchase - Stock Account " + Customer.StockAccount.FirstOrDefault().AccountNumber.ToString()),
                            TransactionDate        = PurchcaseTrade.TradeDate,
                            Trade = Trade,
                            TransactionDispute = DisputeStatus.NotDisputed
                        };

                        // add the stuff to the database
                        db.BankingTransaction.Add(FeeTrans);
                        db.SaveChanges();

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

                        // Take the money out
                        CustomerStockAccount.CashBalance -= decTotal;

                        // take out the fee
                        CustomerStockAccount.CashBalance -= SelectedStock.Fee;

                        // take out the fee
                        CustomerStockAccount.TradingFee += SelectedStock.Fee;

                        db.Entry(CustomerStockAccount).State = System.Data.Entity.EntityState.Modified;
                        db.BankingTransaction.Add(StocksTrans);
                        db.SaveChanges();
                    }

                    // HACK
                    else
                    {
                        ViewBag.Error = "Not Enough Money in Account to Purchase the Stocks";
                        return(View("PurchaseError"));
                    }
                }

                else
                {
                    return(HttpNotFound());
                }

                // Add the stuff to the database
                // check to see if the porfolio is balanced
                BalancedPortfolio.CheckBalanced(db, Customer);

                return(View("PurchaseConfirmation"));
            }
            catch (Exception e)
            {
                ViewBag.Error = "An unknown error occured";
                return(View("PurchaseError"));
            }
        }
Ejemplo n.º 2
0
        // GET: /StockTrades/PurchaseStocks
        // Returns page to purchase stocks
        public ActionResult PurchaseStocks(int?StockID, int?Choice, int?AccountID)
        {
            // Check to see if any IDs are null
            if (StockID == null || Choice == null || AccountID == null)
            {
                return(HttpNotFound());
            }

            // Get the users account
            // 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 Selected Stock
            StockMarket SelectedStock = db.StockMarket.Find(StockID);

            // Create a new model instance
            PurchaseStockTrade StockTrade = new PurchaseStockTrade
            {
                CustomerProfile = customer,
                SelectedStock   = SelectedStock
            };

            switch (Choice)
            {
            case 1:
                // Find the Checking Account
                Checking SelectedChecking = db.CheckingAccount.Find(AccountID);

                // Add to model
                StockTrade.CheckingAccounts = SelectedChecking;

                break;

            case 2:
                // Find the savings account
                Saving SelectedSavings = db.SavingsAccount.Find(AccountID);

                StockTrade.SavingsAccount = SelectedSavings;

                break;

            case 3:
                // Find the portfolio
                StockAccount SelectedStockAccount = db.StockAccount.Find(AccountID);

                StockTrade.AccountStock = SelectedStockAccount;

                break;

            default:
                return(HttpNotFound());
            }

            return(View(StockTrade));
        }