diff --git a/DataDomain/LatestSoldStock.cs b/DataDomain/LatestSoldStock.cs new file mode 100644 index 0000000..ba11e2e --- /dev/null +++ b/DataDomain/LatestSoldStock.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataDomain +{ + public class LatestSoldStock + { + public decimal SoldStockPrice { get; set; } + public DateTime? LatestSoldDate { get; set; } + } +} diff --git a/StockDAL/StockRepository.cs b/StockDAL/StockRepository.cs index 6d1a292..d9f82e1 100644 --- a/StockDAL/StockRepository.cs +++ b/StockDAL/StockRepository.cs @@ -43,10 +43,27 @@ namespace StockDAL entity.ActAmount = rest < 0 ? 0 : rest; entity.SoldDate = DateTime.Today; entity.SoldValue = sellAmount * sellPrice; + entity.SoldStockPrice = sellPrice; context.SaveChanges(); } + public LatestSoldStock LatestSell(string StockName) + { + using var context = new StockContext(); + var result = (from stk in context.Stocks + where stk.StockId == StockName + && stk.SoldDate == (from stk2 in context.Stocks + where stk2.StockId == stk.StockId + select stk2.SoldDate).Max() + select new LatestSoldStock + { + SoldStockPrice = stk.SoldStockPrice, + LatestSoldDate = stk.SoldDate + }).FirstOrDefault(); + + return result; + } public IEnumerable GetAllStocks() { @@ -100,7 +117,8 @@ namespace StockDAL SoldValue = o.SoldValue, SoldDate = o.SoldDate, Comment = o.Comment, - PostAmount = o.PostAmount + PostAmount = o.PostAmount, + SoldStockPrice = o.SoldStockPrice }).ToList(); context.Stocks.AddRange(insertStocks); context.SaveChanges(); diff --git a/StockDal.Interface/IStockRepository.cs b/StockDal.Interface/IStockRepository.cs index cdb4a88..c5de78e 100644 --- a/StockDal.Interface/IStockRepository.cs +++ b/StockDal.Interface/IStockRepository.cs @@ -12,6 +12,7 @@ namespace StockDAL.Interface IEnumerable GetAllRemainingStocks(); IEnumerable GetAllStocks(); void InsertMany(List stockMembers); + LatestSoldStock LatestSell(string StockName); void RemoveAllStocks(); void RestoreStockMembers(List stockMembers); void SaveStockMember(StockMember stockMember); diff --git a/StockInfoCore/frmRegisterStock.Designer.cs b/StockInfoCore/frmRegisterStock.Designer.cs index 58d306e..0efece5 100644 --- a/StockInfoCore/frmRegisterStock.Designer.cs +++ b/StockInfoCore/frmRegisterStock.Designer.cs @@ -58,12 +58,18 @@ namespace StockInfoCore this.Number = new System.Windows.Forms.ColumnHeader(); this.Comment = new System.Windows.Forms.ColumnHeader(); this.btnSaveToDB = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.txtLatestSoldPrice = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.txtLatestSoldDate = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // cmbStockChoser // this.cmbStockChoser.FormattingEnabled = true; - this.cmbStockChoser.Location = new System.Drawing.Point(38, 39); + this.cmbStockChoser.Location = new System.Drawing.Point(38, 12); this.cmbStockChoser.Name = "cmbStockChoser"; this.cmbStockChoser.Size = new System.Drawing.Size(179, 23); this.cmbStockChoser.TabIndex = 0; @@ -82,7 +88,7 @@ namespace StockInfoCore // lblStockExtId // this.lblStockExtId.AutoSize = true; - this.lblStockExtId.Location = new System.Drawing.Point(38, 78); + this.lblStockExtId.Location = new System.Drawing.Point(38, 44); this.lblStockExtId.Name = "lblStockExtId"; this.lblStockExtId.Size = new System.Drawing.Size(62, 15); this.lblStockExtId.TabIndex = 2; @@ -90,7 +96,7 @@ namespace StockInfoCore // // txtStockExtId // - this.txtStockExtId.Location = new System.Drawing.Point(153, 75); + this.txtStockExtId.Location = new System.Drawing.Point(153, 41); this.txtStockExtId.Name = "txtStockExtId"; this.txtStockExtId.Size = new System.Drawing.Size(269, 23); this.txtStockExtId.TabIndex = 3; @@ -303,11 +309,59 @@ namespace StockInfoCore this.btnSaveToDB.UseVisualStyleBackColor = true; this.btnSaveToDB.Click += new System.EventHandler(this.btnSaveToDB_Click); // + // groupBox1 + // + this.groupBox1.Controls.Add(this.txtLatestSoldDate); + this.groupBox1.Controls.Add(this.label2); + this.groupBox1.Controls.Add(this.txtLatestSoldPrice); + this.groupBox1.Controls.Add(this.label1); + this.groupBox1.Location = new System.Drawing.Point(301, 81); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(255, 83); + this.groupBox1.TabIndex = 26; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Latest Sold"; + // + // txtLatestSoldPrice + // + this.txtLatestSoldPrice.Location = new System.Drawing.Point(127, 23); + this.txtLatestSoldPrice.Name = "txtLatestSoldPrice"; + this.txtLatestSoldPrice.ReadOnly = true; + this.txtLatestSoldPrice.Size = new System.Drawing.Size(111, 23); + this.txtLatestSoldPrice.TabIndex = 7; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 26); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(59, 15); + this.label1.TabIndex = 6; + this.label1.Text = "Sold price"; + // + // txtLatestSoldDate + // + this.txtLatestSoldDate.Location = new System.Drawing.Point(127, 52); + this.txtLatestSoldDate.Name = "txtLatestSoldDate"; + this.txtLatestSoldDate.ReadOnly = true; + this.txtLatestSoldDate.Size = new System.Drawing.Size(111, 23); + this.txtLatestSoldDate.TabIndex = 9; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 55); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(56, 15); + this.label2.TabIndex = 8; + this.label2.Text = "Sold date"; + // // frmRegisterStock // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(441, 602); + this.ClientSize = new System.Drawing.Size(570, 602); + this.Controls.Add(this.groupBox1); this.Controls.Add(this.btnSaveToDB); this.Controls.Add(this.lwRegBuffer); this.Controls.Add(this.btnSaveStock); @@ -337,6 +391,8 @@ namespace StockInfoCore this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "frmRegisterStock"; this.Shown += new System.EventHandler(this.frmRegisterStock_Shown); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -373,5 +429,10 @@ namespace StockInfoCore private System.Windows.Forms.ColumnHeader Number; private System.Windows.Forms.ColumnHeader Comment; private System.Windows.Forms.Button btnSaveToDB; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.TextBox txtLatestSoldDate; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox txtLatestSoldPrice; + private System.Windows.Forms.Label label1; } } \ No newline at end of file diff --git a/StockInfoCore/frmRegisterStock.cs b/StockInfoCore/frmRegisterStock.cs index b194c1e..8eb6b7f 100644 --- a/StockInfoCore/frmRegisterStock.cs +++ b/StockInfoCore/frmRegisterStock.cs @@ -1,4 +1,5 @@ using DataDomain; +using StockDAL.Interface; using System; using System.Collections.Generic; using System.ComponentModel; @@ -14,12 +15,15 @@ namespace StockInfoCore public partial class frmRegisterStock : Form { Color hdr = Color.Red; + private readonly IStockRepository _stockRepository; + public Dictionary Stocks { get; set; } public List RegisteredStocks { get; set; } = new List(); - public frmRegisterStock() + public frmRegisterStock(IStockRepository stockRepository) { InitializeComponent(); + _stockRepository = stockRepository; } private void LoadStockCombo() @@ -50,6 +54,12 @@ namespace StockInfoCore txtStockExtId.Text = stockChosen.StockName; txtActValue.Text = stockChosen.LatestPrice.ToString(); txtActDate.Text = (DateTime.Today + stockChosen.TimeOfDay).ToString(); + var stockSold = _stockRepository.LatestSell(stockChosen.StockName.Trim()); + if (stockSold != null) + { + txtLatestSoldDate.Text = stockSold.LatestSoldDate.Value.ToString(); + txtLatestSoldPrice.Text = stockSold.SoldStockPrice.ToString(); + } } private void btnSaveStock_Click(object sender, EventArgs e)