using DataDomain; using StockDAL.Interface; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace StockInfoCore { public partial class frmEditStock : Form { Color hdr = Color.Red; private readonly IStockRepository _stockRepository; public int CurrentStockMember { get; set; } //public Dictionary Stocks { get; set; } //public List RegisteredStocks { get; set; } = new List(); public frmEditStock(IStockRepository stockRepository) { InitializeComponent(); _stockRepository = stockRepository; } //private void LoadStockCombo() //{ // if (Stocks.Count() > 0) // { // foreach (var key in Stocks.Keys) // { // cmbStockChoser.Items.Add(key); // } // } //} private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void frmRegisterStock_Shown(object sender, EventArgs e) { LoadStockData(); //LoadStockCombo(); } private void LoadStockData() { var stockChosen = _stockRepository.GetStockMember(CurrentStockMember); txtStockName.Text = stockChosen.StockId; txtStockExtId.Text = stockChosen.StockExtId; txtActValue.Text = stockChosen.ActValue.ToString(); //txtActDate.Text = (DateTime.Today + stockChosen.ActDate.Value.TimeOfDay).ToString(); var stockSold = _stockRepository.LatestSell(stockChosen.StockId.Trim()); if (stockSold != null) { if (stockSold.LatestSoldDate != null) { txtLatestSoldDate.Text = stockSold.LatestSoldDate.Value.ToString(); txtLatestSoldPrice.Text = stockSold.SoldStockPrice.ToString(); } else { txtLatestSoldDate.Text = string.Empty; txtLatestSoldPrice.Text = string.Empty; } } else { txtLatestSoldDate.Text = string.Empty; txtLatestSoldPrice.Text = string.Empty; } txtBuyPrice.Text = stockChosen.BuyValue.ToString(); // = decimal.Parse(string.IsNullOrEmpty(txtBuyPrice.Text) ? "0" : txtBuyPrice.Text); txtBoughtAmount.Text = stockChosen.PostAmount.ToString(); // = long.Parse(string.IsNullOrEmpty(txtBoughtAmount.Text) ? "0" : txtBoughtAmount.Text); txtActDate.Text = stockChosen.ActDate.ToString(); // = DateTime.Parse(txtActDate.Text); txtBuyDate.Text = stockChosen.BuyDate.ToString(); // = string.IsNullOrWhiteSpace(txtBuyDate.Text) ? DateTime.Today : DateTime.Parse(txtBuyDate.Text); txtActValue.Text = stockChosen.ActValue.ToString(); // = decimal.Parse(string.IsNullOrEmpty(txtActValue.Text) ? "0" : txtActValue.Text); txtActAmount.Text = stockChosen.ActAmount.ToString(); // = long.Parse(string.IsNullOrEmpty(txtActAmount.Text) ? "0" : txtActAmount.Text); txtSoldDate.Text = stockChosen.SoldDate == DateTime.MinValue ? String.Empty : stockChosen.SoldDate.ToString(); // = null; //DateTime.MaxValue; txtSoldPrice.Text = stockChosen.SoldValue.ToString(); // = decimal.Parse("0"); txtComment.Text = stockChosen.Comment; } private void AddValidateData(bool New) { var currentStock = new StockMember(); currentStock.StockId = txtStockName.Text.Trim(); currentStock.StockExtId = txtStockExtId.Text; currentStock.BuyValue = decimal.Parse(string.IsNullOrEmpty(txtBuyPrice.Text) ? "0" : txtBuyPrice.Text); currentStock.PostAmount = long.Parse(string.IsNullOrEmpty(txtBoughtAmount.Text) ? "0" : txtBoughtAmount.Text); currentStock.ActDate = DateTime.Parse(txtActDate.Text); currentStock.BuyDate = string.IsNullOrWhiteSpace(txtBuyDate.Text) ? DateTime.Today : DateTime.Parse(txtBuyDate.Text); currentStock.ActValue = decimal.Parse(string.IsNullOrEmpty(txtActValue.Text) ? "0" : txtActValue.Text); currentStock.ActAmount = long.Parse(string.IsNullOrEmpty(txtActAmount.Text) ? "0" : txtActAmount.Text); currentStock.SoldDate = txtSoldDate.Text.Trim() == "" ? DateTime.MinValue : DateTime.Parse(txtSoldDate.Text); currentStock.SoldValue = decimal.Parse(txtSoldPrice.Text); currentStock.Comment = txtComment.Text.Length<1?String.Empty: txtComment.Text; //RegisteredStocks.Add(currentStock); if (!New) { currentStock.Id = CurrentStockMember; } try { _stockRepository.SaveStockMember(currentStock); initiateRegWin(); } catch (Exception ex) { MessageBox.Show($"Problems with update :{ex.Message}"); } } private void initiateRegWin() { txtStockName.Text = ""; txtStockExtId.Text = ""; txtBuyPrice.Text = ""; txtBuyDate.Text = ""; txtBoughtAmount.Text = ""; txtActValue.Text = ""; txtActDate.Text = ""; txtActAmount.Text = ""; txtSoldPrice.Text = ""; txtSoldDate.Text = ""; txtComment.Text = ""; } private void btnSaveToDB_Click(object sender, EventArgs e) { AddValidateData(false); } private void txtBoughtAmount_TextChanged(object sender, EventArgs e) { txtActAmount.Text = txtBoughtAmount.Text; } private void lwRegBuffer_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { using (Brush hBr = new SolidBrush(hdr)) { e.Graphics.FillRectangle(hBr, e.Bounds); e.DrawText(); } } private void btnSaveNew_Click(object sender, EventArgs e) { AddValidateData(true); } } }