206 lines
7.5 KiB
C#
206 lines
7.5 KiB
C#
using DataDomain;
|
|
using StockDal.Interface;
|
|
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 frmRegisterStock : Form
|
|
{
|
|
Color hdr = Color.Red;
|
|
private readonly IStockRepository _stockRepository;
|
|
private readonly IStockMarketRepository _stockMarketRepository;
|
|
private readonly IStockScrapePage _scrapePage;
|
|
|
|
public List<string> StockNames { get; set; }
|
|
public Dictionary<string, DiTraderStockRow> Stocks { get; set; }
|
|
public List<StockMember> RegisteredStocks { get; set; } = new List<StockMember>();
|
|
|
|
public StockGroupModel StockGroupTmp { get; set; }
|
|
|
|
public frmRegisterStock(IStockRepository stockRepository,
|
|
IStockMarketRepository stockMarketRepository,
|
|
IStockScrapePage scrapePage)
|
|
{
|
|
InitializeComponent();
|
|
_stockRepository = stockRepository;
|
|
_stockMarketRepository = stockMarketRepository;
|
|
_scrapePage = scrapePage;
|
|
}
|
|
|
|
private void LoadStockCombo()
|
|
{
|
|
|
|
cmbStockChoser.Items.Clear();
|
|
|
|
foreach (var key in StockNames)
|
|
{
|
|
cmbStockChoser.Items.Add(key);
|
|
}
|
|
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void frmRegisterStock_Shown(object sender, EventArgs e)
|
|
{
|
|
StockNames = _stockRepository.GetStockNames();
|
|
LoadStockCombo();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Val av enstaka Aktie för sökning och registrering
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void cmbStockChoser_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (Stocks.ContainsKey(cmbStockChoser.SelectedItem.ToString()))
|
|
{
|
|
var stockChosen = Stocks[cmbStockChoser.SelectedItem.ToString()];
|
|
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)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
else if (StockNames.Contains(cmbStockChoser.SelectedItem.ToString()))
|
|
{
|
|
var stockChosen = _stockMarketRepository.LoadStockMarketStockData(cmbStockChoser.SelectedItem.ToString());
|
|
txtStockExtId.Text = stockChosen.StockName;
|
|
txtActValue.Text = stockChosen.LatestPrice.ToString();
|
|
txtActDate.Text = (DateTime.Today + stockChosen.TimeOfDay).ToString();
|
|
}
|
|
else
|
|
{
|
|
var stockChosen = _scrapePage.SearchStockAndCollect(cmbStockChoser.Text.Substring(0, 4));
|
|
if (stockChosen.Count > 0)
|
|
{
|
|
txtStockExtId.Text = stockChosen[0].StockName;
|
|
txtActValue.Text = stockChosen[0].LatestPrice.ToString();
|
|
txtActDate.Text = (DateTime.Today + stockChosen[0].TimeOfDay).ToString();
|
|
StockGroupTmp.StockGroup = "ZNotFound";
|
|
StockGroupTmp.StockName = stockChosen[0].StockName;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void btnSaveStock_Click(object sender, EventArgs e)
|
|
{
|
|
AddValidateData();
|
|
RefreshListViewFromRegList();
|
|
}
|
|
|
|
private void RefreshListViewFromRegList()
|
|
{
|
|
lwRegBuffer.Items.Clear();
|
|
foreach (var currStock in RegisteredStocks)
|
|
{
|
|
AddItemToListView(currStock);
|
|
}
|
|
lwRegBuffer.Refresh();
|
|
}
|
|
|
|
private void AddItemToListView(StockMember currStock)
|
|
{
|
|
var lv = lwRegBuffer.Items.Add(currStock.StockId);
|
|
lv.SubItems.Add(currStock.BuyValue.ToString());
|
|
lv.SubItems.Add(currStock.PostAmount.ToString());
|
|
lv.SubItems.Add(currStock.Comment);
|
|
//lv.BackColor = Color.Aquamarine;
|
|
|
|
}
|
|
|
|
private void AddValidateData()
|
|
{
|
|
var currentStock = new StockMember();
|
|
currentStock.StockId = cmbStockChoser.SelectedItem.ToString();
|
|
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 = null; //DateTime.MaxValue;
|
|
currentStock.SoldValue = decimal.Parse("0");
|
|
currentStock.Comment = txtComment.Text;
|
|
RegisteredStocks.Add(currentStock);
|
|
if (StockGroupTmp != null)
|
|
{
|
|
_stockRepository.SaveStockGroup(StockGroupTmp);
|
|
StockGroupTmp = null;
|
|
}
|
|
initiateRegWin();
|
|
}
|
|
|
|
private void initiateRegWin()
|
|
{
|
|
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)
|
|
{
|
|
if (RegisteredStocks.Count > 0)
|
|
{
|
|
_stockRepository.InsertMany(RegisteredStocks);
|
|
lwRegBuffer.Clear();
|
|
RegisteredStocks.Clear();
|
|
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|