From 2841ada2cfbeae9f14b0631c14e0962514750e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 4 Mar 2021 10:38:47 +0100 Subject: [PATCH] Handles even selling --- StockDAL/StockRepository.cs | 17 ++++++++- StockDal.Interface/IStockRepository.cs | 1 + StockInfo/frmSelling.Designer.cs | 30 ++++++++++++++-- StockInfo/frmSelling.cs | 49 ++++++++++++++++++++++++-- 4 files changed, 90 insertions(+), 7 deletions(-) diff --git a/StockDAL/StockRepository.cs b/StockDAL/StockRepository.cs index 279dbd9..d03cc93 100644 --- a/StockDAL/StockRepository.cs +++ b/StockDAL/StockRepository.cs @@ -33,6 +33,21 @@ namespace StockDAL context.SaveChanges(); } + public void UpdateActualForSell(int id, int sellAmount, decimal sellPrice, DateTime sellDate) + { + using var context = new StockContext(); + var entity = (from stk in context.Stocks + where stk.Id == id + select stk).FirstOrDefault(); + var rest = entity.ActAmount - sellAmount; + entity.ActAmount = rest < 0 ? 0 : rest; + entity.SoldDate = DateTime.Today; + entity.SoldValue = sellAmount * sellPrice; + + context.SaveChanges(); + } + + public IEnumerable GetAllStocks() { using var context = new StockContext(); @@ -44,7 +59,7 @@ namespace StockDAL { using var context = new StockContext(); var output = (from stk in context.Stocks - where stk.SoldDate == null || stk.ActValue > 0 + where stk.SoldDate == null || stk.ActAmount > 0 select stk).ToList(); return output; } diff --git a/StockDal.Interface/IStockRepository.cs b/StockDal.Interface/IStockRepository.cs index e7be085..442715f 100644 --- a/StockDal.Interface/IStockRepository.cs +++ b/StockDal.Interface/IStockRepository.cs @@ -13,6 +13,7 @@ namespace StockDAL.Interface IEnumerable GetAllStocks(); void InsertMany(List stockMembers); void SaveStockMember(StockMember stockMember); + void UpdateActualForSell(int id, int sellAmount, decimal sellPrice, DateTime sellDate); void UpdateActualPrice(int id, decimal price); } } diff --git a/StockInfo/frmSelling.Designer.cs b/StockInfo/frmSelling.Designer.cs index 333d05d..5481b35 100644 --- a/StockInfo/frmSelling.Designer.cs +++ b/StockInfo/frmSelling.Designer.cs @@ -50,6 +50,8 @@ namespace StockInfo this.txtId = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.gbSell = new System.Windows.Forms.GroupBox(); + this.txtGainLoose = new System.Windows.Forms.TextBox(); + this.label12 = new System.Windows.Forms.Label(); this.btnConfirm = new System.Windows.Forms.Button(); this.txtRemainingNo = new System.Windows.Forms.TextBox(); this.label11 = new System.Windows.Forms.Label(); @@ -137,7 +139,7 @@ namespace StockInfo this.gbInfo.Controls.Add(this.label1); this.gbInfo.Location = new System.Drawing.Point(13, 170); this.gbInfo.Name = "gbInfo"; - this.gbInfo.Size = new System.Drawing.Size(276, 216); + this.gbInfo.Size = new System.Drawing.Size(276, 250); this.gbInfo.TabIndex = 1; this.gbInfo.TabStop = false; this.gbInfo.Text = "Actual Stock"; @@ -246,6 +248,8 @@ namespace StockInfo // // gbSell // + this.gbSell.Controls.Add(this.txtGainLoose); + this.gbSell.Controls.Add(this.label12); this.gbSell.Controls.Add(this.btnConfirm); this.gbSell.Controls.Add(this.txtRemainingNo); this.gbSell.Controls.Add(this.label11); @@ -259,19 +263,37 @@ namespace StockInfo this.gbSell.Controls.Add(this.label7); this.gbSell.Location = new System.Drawing.Point(296, 170); this.gbSell.Name = "gbSell"; - this.gbSell.Size = new System.Drawing.Size(236, 216); + this.gbSell.Size = new System.Drawing.Size(236, 250); this.gbSell.TabIndex = 2; this.gbSell.TabStop = false; this.gbSell.Text = "Sell decision"; // + // txtGainLoose + // + this.txtGainLoose.Location = new System.Drawing.Point(121, 165); + this.txtGainLoose.Name = "txtGainLoose"; + this.txtGainLoose.ReadOnly = true; + this.txtGainLoose.Size = new System.Drawing.Size(73, 23); + this.txtGainLoose.TabIndex = 14; + // + // label12 + // + this.label12.AutoSize = true; + this.label12.Location = new System.Drawing.Point(12, 168); + this.label12.Name = "label12"; + this.label12.Size = new System.Drawing.Size(73, 15); + this.label12.TabIndex = 13; + this.label12.Text = "Gain / Loose"; + // // btnConfirm // - this.btnConfirm.Location = new System.Drawing.Point(121, 165); + this.btnConfirm.Location = new System.Drawing.Point(121, 221); this.btnConfirm.Name = "btnConfirm"; this.btnConfirm.Size = new System.Drawing.Size(75, 23); this.btnConfirm.TabIndex = 12; this.btnConfirm.Text = "Confirm"; this.btnConfirm.UseVisualStyleBackColor = true; + this.btnConfirm.Click += new System.EventHandler(this.btnConfirm_Click); // // txtRemainingNo // @@ -411,5 +433,7 @@ namespace StockInfo private System.Windows.Forms.Label label8; private System.Windows.Forms.TextBox txtSoldDate; private System.Windows.Forms.Label label7; + private System.Windows.Forms.TextBox txtGainLoose; + private System.Windows.Forms.Label label12; } } \ No newline at end of file diff --git a/StockInfo/frmSelling.cs b/StockInfo/frmSelling.cs index c7bdc59..cc8af3c 100644 --- a/StockInfo/frmSelling.cs +++ b/StockInfo/frmSelling.cs @@ -3,6 +3,7 @@ using Helpers; using StockDAL.Interface; using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Windows.Forms; @@ -21,6 +22,11 @@ namespace StockInfo } private void frmSelling_Load(object sender, EventArgs e) + { + ReloadRemainingStocks(); + } + + private void ReloadRemainingStocks() { remainingStocks = _stockRepository.GetAllRemainingStocks().ToList(); foreach (var stock in remainingStocks) @@ -56,11 +62,11 @@ namespace StockInfo txtStockId.Text = stkMemSelected.StockId; txtBuyDate.Text = stkMemSelected.BuyDate.ToShortDateString(); txtBuyNumber.Text = stkMemSelected.PostAmount.ToString(); - txtBuyPrice.Text = stkMemSelected.BuyValue.ToString(); - txtBuyValue.Text = (stkMemSelected.BuyValue * stkMemSelected.PostAmount).ToString(); + txtBuyPrice.Text = stkMemSelected.BuyValue.ToString("N2"); + txtBuyValue.Text = (stkMemSelected.BuyValue * stkMemSelected.PostAmount).ToString("N2"); txtSoldDate.Text = DateTime.Today.ToShortDateString(); - txtSoldPrice.Text = stkMemSelected.ActValue.ToString(); + txtSoldPrice.Text = stkMemSelected.ActValue.ToString("N2"); txtSoldAmount.Text = stkMemSelected.ActAmount.ToString(); } @@ -87,7 +93,44 @@ namespace StockInfo { txtSellValue.Text = (decimal.Parse(txtSoldPrice.Text) * long.Parse(txtSoldAmount.Text)).ToString(); txtRemainingNo.Text = stkMemSelected.ActAmount > 0 ? (stkMemSelected.PostAmount - long.Parse(txtSoldAmount.Text)).ToString() : 0.ToString(); + var gainLoose = (decimal.Parse(txtSellValue.Text) - decimal.Parse(txtBuyPrice.Text)*long.Parse(txtSoldAmount.Text)); + txtGainLoose.Text = gainLoose.ToString(); + if (gainLoose > 0) + { + txtGainLoose.BackColor = Color.LightGreen; + } + else if (gainLoose == 0) + { + txtGainLoose.BackColor = default; + } + else + { + txtGainLoose.BackColor = Color.LightPink; + } } } + + private void btnConfirm_Click(object sender, EventArgs e) + { + SellCurrentStock(); + } + + private void SellCurrentStock() + { + if (txtSoldAmount.Text.IsNumeric() && txtSoldPrice.Text.IsNumeric()) + { + _stockRepository.UpdateActualForSell(stkMemSelected.Id, int.Parse(txtSoldAmount.Text), decimal.Parse(txtSoldPrice.Text), DateTime.Parse(txtSoldDate.Text)); + var answer = MessageBox.Show($"Ok att sälja {stkMemSelected.StockId} ?","Selling" , MessageBoxButtons.OKCancel); + if (DialogResult.OK == answer) + { + ReloadRemainingStocks(); + } + } + else + { + MessageBox.Show("Something wrong with amounts"); + } + + } } }