146 lines
4.9 KiB
C#
146 lines
4.9 KiB
C#
using DataDomain;
|
|
using Helpers;
|
|
using StockDAL.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
|
|
namespace StockInfoCore
|
|
{
|
|
public partial class frmSelling : Form
|
|
{
|
|
private readonly IStockRepository _stockRepository;
|
|
private List<StockMember> remainingStocks = new();
|
|
public StockMember stkMemSelected { get; set; } = null;
|
|
|
|
public frmSelling(IStockRepository stockRepository)
|
|
{
|
|
InitializeComponent();
|
|
_stockRepository = stockRepository;
|
|
}
|
|
|
|
private void frmSelling_Load(object sender, EventArgs e)
|
|
{
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
ReloadRemainingStocks();
|
|
Cursor.Current = DefaultCursor;
|
|
|
|
}
|
|
|
|
private void ReloadRemainingStocks()
|
|
{
|
|
remainingStocks = _stockRepository.GetAllRemainingStocks().ToList();
|
|
foreach (var stock in remainingStocks)
|
|
{
|
|
var lvRow = lvSellCandidates.Items.Add(stock.StockId);
|
|
lvRow.Tag = stock.Id;
|
|
lvRow.SubItems.Add(stock.BuyDate.ToShortDateString());
|
|
lvRow.SubItems.Add(stock.BuyValue.ToString());
|
|
lvRow.SubItems.Add(stock.ActAmount.ToString());
|
|
lvRow.SubItems.Add(stock.ActValue.ToString());
|
|
lvRow.SubItems.Add((stock.ActValue * stock.ActAmount).ToString());
|
|
}
|
|
}
|
|
|
|
private void SelectStock(object tag)
|
|
{
|
|
//Debug.WriteLine($"selected item {(int)tag}");
|
|
foreach (var remStk in remainingStocks)
|
|
{
|
|
if (remStk.Id == (int)tag)
|
|
{
|
|
stkMemSelected = remStk;
|
|
break;
|
|
}
|
|
}
|
|
|
|
EditStockForSelling(stkMemSelected);
|
|
}
|
|
|
|
private void EditStockForSelling(StockMember stkMemSelected)
|
|
{
|
|
txtId.Text = stkMemSelected.Id.ToString();
|
|
txtStockId.Text = stkMemSelected.StockId;
|
|
txtBuyDate.Text = stkMemSelected.BuyDate.ToShortDateString();
|
|
txtBuyNumber.Text = 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("N2");
|
|
txtSoldAmount.Text = stkMemSelected.ActAmount.ToString();
|
|
}
|
|
|
|
|
|
private void lvSellCandidates_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
var selRows = lvSellCandidates.SelectedItems;
|
|
SelectStock(selRows[0].Tag);
|
|
}
|
|
|
|
private void txtSoldPrice_TextChanged(object sender, EventArgs e)
|
|
{
|
|
CalcNumberAndValue();
|
|
}
|
|
|
|
private void txtSoldAmount_TextChanged(object sender, EventArgs e)
|
|
{
|
|
CalcNumberAndValue();
|
|
}
|
|
|
|
private void CalcNumberAndValue()
|
|
{
|
|
if (txtSoldPrice.Text.IsNumeric() && txtSoldAmount.Text.IsNumeric())
|
|
{
|
|
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");
|
|
}
|
|
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
}
|
|
}
|