//Display Shares Owned and Stock's Value private void DisplayCurrentValue() { List <ValuationModel> Valuations = new List <ValuationModel>(); Valuations = GlobalConfig.Connection.Valuation_Stock(Stock.StockId); ValuationModel currentValue = Valuations.LastOrDefault(); //Display Shares Owned and Stock's Value lbl_SharesOwned.Text = currentValue.Shares.ToString("n"); lbl_CurrentValue.Text = currentValue.Value.ToString("c"); }
// Method to Update Current Valuation private ValuationModel CalculateCurrentValue(decimal p, decimal s = 0M, decimal f = 0M) { List <ValuationModel> values = GlobalConfig.Connection.Valuation_Stock(Stock.StockId); ValuationModel lastValue = values.LastOrDefault(); lastValue.Date = dtp_TransDate.Value.Date; lastValue.Shares += s; lastValue.Price = p; lastValue.Cost += (s * p) + f; return(lastValue); }
public void Stock_Valuation_Update(ValuationModel model) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { var p = new DynamicParameters(); p.Add("@ValuationID", model.ValuationID); p.Add("@Shares", model.Shares); p.Add("@Price", model.Price); p.Add("@Cost", model.Cost); connection.Execute("dbo.sp_Valuation_Update", p, commandType: CommandType.StoredProcedure); } }
// Method to Add Valution to Database public void Valuation_AddNew(ValuationModel model) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { var p = new DynamicParameters(); p.Add("@StockId", model.StockId); p.Add("@Date", model.Date); p.Add("@Shares", model.Shares); p.Add("@Price", model.Price); p.Add("@Cost", model.Cost); p.Add("@ValuationID", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.sp_Valuations_Insert", p, commandType: CommandType.StoredProcedure); model.ValuationID = p.Get <int>("@ValuationID"); } }
// Update Database of Stock Valuations public void UpDateValuations(ValuationModel model) { // Check to see if Stock has Valuation for This Date List <ValuationModel> lvalues = GlobalConfig.Connection.Valuation_Stock(model.StockId); foreach (ValuationModel val in lvalues) { if (model.Date == val.Date) // Update this Valuation { GlobalConfig.Connection.Stock_Valuation_Update(model); } else if (val == lvalues.LastOrDefault()) // Add new Valuation { GlobalConfig.Connection.Valuation_AddNew(model); } } // Check Other Stocks Owned for Valuation on This Date List <StockModel> Stocks = GlobalConfig.Connection.Stocks_LoadAll(); // Loop Through Each Stock Owned foreach (StockModel st in Stocks) { // Skip the Stock that was passed in if (st.StockId == model.StockId) { continue; } //Load All Valuations for this stock List <ValuationModel> stValues = GlobalConfig.Connection.Valuation_Stock(st.StockId); ValuationModel preValue = stValues.FirstOrDefault(); // Assign PreValue to first value foreach (ValuationModel vModel in stValues) { // If vModel date is less than model Date // Assign to vModel to Previous Value // And proceed to next record if (vModel.Date < model.Date && vModel != stValues.LastOrDefault()) { preValue = vModel; } // If vModel Date equal Model Date Stock has valuation for this date // Go To Next STock to check else if (vModel.Date == model.Date) { break; } else // vModel Date is Greater Than model Date { // Check For Model is Less than previous Date // If true, Stock was purchased after Model's Date if (model.Date < preValue.Date) { break; } else // Model Date is between { // Keep all of Previous Record Data // except Date preValue.Date = model.Date; // Add new Valuation Record GlobalConfig.Connection.Valuation_AddNew(preValue); break; } } } } }
// Save Button Click Event Handler private void Btn_Save_Click(object sender, EventArgs e) { //Create Instances of Transaction and Valuation TransactionModel transaction = new TransactionModel(); ValuationModel valuation = new ValuationModel(); decimal shares = 0m; decimal price = Decimal.Parse(tx_TransPrice.Text); broker = (BrokerageModel)cb_Broker.SelectedItem; switch (tType) { case TransactionType.Buy: { shares = GetShares(); //Load Information and Valuation into Correct variables transaction = CreateTransaction(price, shares, broker.BrokerId); valuation = CalculateCurrentValue(price, shares, broker.CommissionRate); //Save Transaction and Valuation to Database GlobalConfig.Connection.Transaction_AddNew(transaction); // Upate Valuations Processor.UpDateValuations(valuation); break; } case TransactionType.Sale: { decimal sharesOwned = Decimal.Parse(lbl_SharesOwned.Text); shares = GetShares(); if (shares > sharesOwned) { //Load Information and Valuation into Correct variables transaction = CreateTransaction(price, shares, broker.BrokerId); valuation = CalculateCurrentValue(price, -shares); //Save Transaction and Valuation to Database GlobalConfig.Connection.Transaction_AddNew(transaction); // CUPdate valuations Processor.UpDateValuations(valuation); break; } else { MessageBox.Show("You Can Not Sale More Shares than you own."); DialogResult = DialogResult.None; break; } } case TransactionType.Update: { //Load Valuation into Correct variable valuation = CalculateCurrentValue(price); //Save Valuation to Database and Update other Stocks Processor.UpDateValuations(valuation); break; } case TransactionType.Split: { // Uses Date, Price, Old Shares, New Shares Decimal oldShares = 0m, newShares = 0m; decimal sharesowned = Decimal.Parse(lbl_SharesOwned.Text); foreach (Control ctl in pnl_Split.Controls) { if (ctl is TextBox) { if (ctl.Name.Contains("Old")) { oldShares = Decimal.Parse(ctl.Text); } else if (ctl.Name.Contains("New")) { newShares = Decimal.Parse(ctl.Text); } } } //Create Transaction transaction = CalcStockSplit(price, newShares, oldShares, sharesowned); transaction.BrokerId = 4; // This should be 0 for stock splits and dividends // Save Transaction GlobalConfig.Connection.Transaction_AddNew(transaction); break; } case TransactionType.Dividend: { broker = GlobalConfig.Connection.Broker_GetAll().FirstOrDefault(); // Uses only Date and Price (Price is Dividend per Share) transaction = CreateTransaction(price, 0, 0); transaction.BrokerId = 4; // Set to Admin Broker Id transaction.Fee = 0M; // Set to NO Broker Fee GlobalConfig.Connection.Transaction_AddNew(transaction); break; } } }