Handles even selling

This commit is contained in:
2021-03-04 10:38:47 +01:00
parent 3959943607
commit 2841ada2cf
4 changed files with 90 additions and 7 deletions

View File

@ -33,6 +33,21 @@ namespace StockDAL
context.SaveChanges(); 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<StockMember> GetAllStocks() public IEnumerable<StockMember> GetAllStocks()
{ {
using var context = new StockContext(); using var context = new StockContext();
@ -44,7 +59,7 @@ namespace StockDAL
{ {
using var context = new StockContext(); using var context = new StockContext();
var output = (from stk in context.Stocks var output = (from stk in context.Stocks
where stk.SoldDate == null || stk.ActValue > 0 where stk.SoldDate == null || stk.ActAmount > 0
select stk).ToList(); select stk).ToList();
return output; return output;
} }

View File

@ -13,6 +13,7 @@ namespace StockDAL.Interface
IEnumerable<StockMember> GetAllStocks(); IEnumerable<StockMember> GetAllStocks();
void InsertMany(List<StockMember> stockMembers); void InsertMany(List<StockMember> stockMembers);
void SaveStockMember(StockMember stockMember); void SaveStockMember(StockMember stockMember);
void UpdateActualForSell(int id, int sellAmount, decimal sellPrice, DateTime sellDate);
void UpdateActualPrice(int id, decimal price); void UpdateActualPrice(int id, decimal price);
} }
} }

View File

@ -50,6 +50,8 @@ namespace StockInfo
this.txtId = new System.Windows.Forms.TextBox(); this.txtId = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.gbSell = new System.Windows.Forms.GroupBox(); 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.btnConfirm = new System.Windows.Forms.Button();
this.txtRemainingNo = new System.Windows.Forms.TextBox(); this.txtRemainingNo = new System.Windows.Forms.TextBox();
this.label11 = new System.Windows.Forms.Label(); this.label11 = new System.Windows.Forms.Label();
@ -137,7 +139,7 @@ namespace StockInfo
this.gbInfo.Controls.Add(this.label1); this.gbInfo.Controls.Add(this.label1);
this.gbInfo.Location = new System.Drawing.Point(13, 170); this.gbInfo.Location = new System.Drawing.Point(13, 170);
this.gbInfo.Name = "gbInfo"; 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.TabIndex = 1;
this.gbInfo.TabStop = false; this.gbInfo.TabStop = false;
this.gbInfo.Text = "Actual Stock"; this.gbInfo.Text = "Actual Stock";
@ -246,6 +248,8 @@ namespace StockInfo
// //
// gbSell // gbSell
// //
this.gbSell.Controls.Add(this.txtGainLoose);
this.gbSell.Controls.Add(this.label12);
this.gbSell.Controls.Add(this.btnConfirm); this.gbSell.Controls.Add(this.btnConfirm);
this.gbSell.Controls.Add(this.txtRemainingNo); this.gbSell.Controls.Add(this.txtRemainingNo);
this.gbSell.Controls.Add(this.label11); this.gbSell.Controls.Add(this.label11);
@ -259,19 +263,37 @@ namespace StockInfo
this.gbSell.Controls.Add(this.label7); this.gbSell.Controls.Add(this.label7);
this.gbSell.Location = new System.Drawing.Point(296, 170); this.gbSell.Location = new System.Drawing.Point(296, 170);
this.gbSell.Name = "gbSell"; 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.TabIndex = 2;
this.gbSell.TabStop = false; this.gbSell.TabStop = false;
this.gbSell.Text = "Sell decision"; 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 // 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.Name = "btnConfirm";
this.btnConfirm.Size = new System.Drawing.Size(75, 23); this.btnConfirm.Size = new System.Drawing.Size(75, 23);
this.btnConfirm.TabIndex = 12; this.btnConfirm.TabIndex = 12;
this.btnConfirm.Text = "Confirm"; this.btnConfirm.Text = "Confirm";
this.btnConfirm.UseVisualStyleBackColor = true; this.btnConfirm.UseVisualStyleBackColor = true;
this.btnConfirm.Click += new System.EventHandler(this.btnConfirm_Click);
// //
// txtRemainingNo // txtRemainingNo
// //
@ -411,5 +433,7 @@ namespace StockInfo
private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox txtSoldDate; private System.Windows.Forms.TextBox txtSoldDate;
private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtGainLoose;
private System.Windows.Forms.Label label12;
} }
} }

View File

@ -3,6 +3,7 @@ using Helpers;
using StockDAL.Interface; using StockDAL.Interface;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
@ -21,6 +22,11 @@ namespace StockInfo
} }
private void frmSelling_Load(object sender, EventArgs e) private void frmSelling_Load(object sender, EventArgs e)
{
ReloadRemainingStocks();
}
private void ReloadRemainingStocks()
{ {
remainingStocks = _stockRepository.GetAllRemainingStocks().ToList(); remainingStocks = _stockRepository.GetAllRemainingStocks().ToList();
foreach (var stock in remainingStocks) foreach (var stock in remainingStocks)
@ -56,11 +62,11 @@ namespace StockInfo
txtStockId.Text = stkMemSelected.StockId; txtStockId.Text = stkMemSelected.StockId;
txtBuyDate.Text = stkMemSelected.BuyDate.ToShortDateString(); txtBuyDate.Text = stkMemSelected.BuyDate.ToShortDateString();
txtBuyNumber.Text = stkMemSelected.PostAmount.ToString(); txtBuyNumber.Text = stkMemSelected.PostAmount.ToString();
txtBuyPrice.Text = stkMemSelected.BuyValue.ToString(); txtBuyPrice.Text = stkMemSelected.BuyValue.ToString("N2");
txtBuyValue.Text = (stkMemSelected.BuyValue * stkMemSelected.PostAmount).ToString(); txtBuyValue.Text = (stkMemSelected.BuyValue * stkMemSelected.PostAmount).ToString("N2");
txtSoldDate.Text = DateTime.Today.ToShortDateString(); txtSoldDate.Text = DateTime.Today.ToShortDateString();
txtSoldPrice.Text = stkMemSelected.ActValue.ToString(); txtSoldPrice.Text = stkMemSelected.ActValue.ToString("N2");
txtSoldAmount.Text = stkMemSelected.ActAmount.ToString(); txtSoldAmount.Text = stkMemSelected.ActAmount.ToString();
} }
@ -87,7 +93,44 @@ namespace StockInfo
{ {
txtSellValue.Text = (decimal.Parse(txtSoldPrice.Text) * long.Parse(txtSoldAmount.Text)).ToString(); 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(); 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");
}
}
} }
} }