små justeringar i visningen av aktiehistorien

This commit is contained in:
2026-08-24 00:06:57 +02:00
parent 6d6b872e1c
commit e2847d456f
3 changed files with 99 additions and 83 deletions

View File

@ -61,84 +61,84 @@ public class StockEquityServices : IStockEquityServices
if (note.TransactionType == null && note.StockCode != null) { }
else
// Kontrollera om TransactionType är en köpnotering
// (ignorera null-värden)
if (note.TransactionType.ToLower() == "köp")
{
// Skapa en ny aktieinnehavspost baserat på köpnoteringen
// Kontrollera om TransactionType är en köpnotering
// (ignorera null-värden)
var equity = new StockEquity
if (note.TransactionType.ToLower() == "köp")
{
StockCode = note.StockCode,
Bought = note.TransactionDate,
BoughtPrice = note.StockPrice,
Quantity = note.StockQuantity,
Sold = DateTime.MinValue,
SoldPrice = 0m,
SoldQuant = 0
};
_dbContext.Stocks.Add(equity);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Köpnotering: {equity.StockCode} | {equity.Bought} | {equity.BoughtPrice} | {equity.Quantity} | {equity.BoughtPrice * equity.Quantity}");
}
// Skapa en ny aktieinnehavspost baserat på köpnoteringen
// Kontrollera om TransactionType är en säljnotering
else if (note.TransactionType.ToLower() == "sälj")
{
var equityList = await _dbContext.Stocks
.Where(s => s.StockCode == note.StockCode && s.SoldQuant == 0)
.OrderBy(s => s.Quantity).ToListAsync();
if (equityList != null)
{
var soldQuant = Math.Abs(note.StockQuantity);
foreach (var eq in equityList.OrderBy(e => e.Quantity))
var equity = new StockEquity
{
if (soldQuant == 0)
StockCode = note.StockCode,
Bought = note.TransactionDate,
BoughtPrice = note.StockPrice,
Quantity = note.StockQuantity,
Sold = DateTime.MinValue,
SoldPrice = 0m,
SoldQuant = 0
};
_dbContext.Stocks.Add(equity);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Köpnotering: {equity.StockCode} | {equity.Bought} | {equity.BoughtPrice} | {equity.Quantity} | {equity.BoughtPrice * equity.Quantity}");
}
// Kontrollera om TransactionType är en säljnotering
else if (note.TransactionType.ToLower() == "sälj")
{
var equityList = await _dbContext.Stocks
.Where(s => s.StockCode == note.StockCode && s.SoldQuant == 0)
.OrderBy(s => s.Quantity).ToListAsync();
if (equityList != null)
{
var soldQuant = Math.Abs(note.StockQuantity);
foreach (var eq in equityList.OrderBy(e => e.Quantity))
{
break;
}
else
{
if (eq.Quantity <= soldQuant)
if (soldQuant == 0)
{
eq.Sold = note.TransactionDate;
eq.SoldPrice = note.StockPrice;
eq.SoldQuant = eq.Quantity;
soldQuant -= eq.Quantity;
_dbContext.Stocks.Update(eq);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Säljnotering: {eq.StockCode} | {eq.Sold} | {eq.SoldPrice} | {eq.SoldQuant}");
break;
}
else
{
var splitEquity = new StockEquity
if (eq.Quantity <= soldQuant)
{
StockCode = eq.StockCode,
Bought = eq.Bought,
BoughtPrice = eq.BoughtPrice,
Quantity = soldQuant,
Sold = note.TransactionDate,
SoldPrice = note.StockPrice,
SoldQuant = soldQuant
};
_dbContext.Stocks.Add(splitEquity);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Säljnotering (split): {splitEquity.StockCode} | {splitEquity.Bought} | {splitEquity.BoughtPrice} | {splitEquity.SoldQuant} | {splitEquity.Sold} | {splitEquity.SoldPrice} | {splitEquity.SoldQuant}");
eq.Quantity -= soldQuant;
_dbContext.Stocks.Update(eq);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Säljnotering (split): {eq.StockCode} | {eq.Bought} | {eq.BoughtPrice} | {eq.SoldQuant} | {eq.Sold} | {eq.SoldPrice} | {eq.SoldQuant}");
soldQuant = 0;
eq.Sold = note.TransactionDate;
eq.SoldPrice = note.StockPrice;
eq.SoldQuant = eq.Quantity;
soldQuant -= eq.Quantity;
_dbContext.Stocks.Update(eq);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Säljnotering: {eq.StockCode} | {eq.Sold} | {eq.SoldPrice} | {eq.SoldQuant}");
}
else
{
var splitEquity = new StockEquity
{
StockCode = eq.StockCode,
Bought = eq.Bought,
BoughtPrice = eq.BoughtPrice,
Quantity = soldQuant,
Sold = note.TransactionDate,
SoldPrice = note.StockPrice,
SoldQuant = soldQuant
};
_dbContext.Stocks.Add(splitEquity);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Säljnotering (split): {splitEquity.StockCode} | {splitEquity.Bought} | {splitEquity.BoughtPrice} | {splitEquity.SoldQuant} | {splitEquity.Sold} | {splitEquity.SoldPrice} | {splitEquity.SoldQuant}");
eq.Quantity -= soldQuant;
_dbContext.Stocks.Update(eq);
await _dbContext.SaveChangesAsync();
Debug.WriteLine($"Säljnotering (split): {eq.StockCode} | {eq.Bought} | {eq.BoughtPrice} | {eq.SoldQuant} | {eq.Sold} | {eq.SoldPrice} | {eq.SoldQuant}");
soldQuant = 0;
}
}
}
}
}
}
}
}
}

View File

@ -35,17 +35,17 @@
lwStocks = new ListView();
chStock = new ColumnHeader();
chBought = new ColumnHeader();
chPrize = new ColumnHeader();
chNumber = new ColumnHeader();
chPrize = new ColumnHeader();
chValue = new ColumnHeader();
lblTotalValue = new Label();
lwStockHistory = new ListView();
histDatum = new ColumnHeader();
histAktie = new ColumnHeader();
histKöptSålt = new ColumnHeader();
histAntal = new ColumnHeader();
histPris = new ColumnHeader();
histTotBelopp = new ColumnHeader();
columnHeader1 = new ColumnHeader();
histType = new ColumnHeader();
btnStockHistory = new Button();
SuspendLayout();
//
@ -97,7 +97,7 @@
//
// lwStocks
//
lwStocks.Columns.AddRange(new ColumnHeader[] { chStock, chBought, chPrize, chNumber, chValue });
lwStocks.Columns.AddRange(new ColumnHeader[] { chStock, chBought, chNumber, chPrize, chValue });
lwStocks.FullRowSelect = true;
lwStocks.Location = new Point(111, 103);
lwStocks.Name = "lwStocks";
@ -115,21 +115,25 @@
// chBought
//
chBought.Text = "Köpt";
chBought.Width = 100;
//
// chPrize
//
chPrize.Text = "Köpkurs";
chPrize.Width = 100;
chBought.Width = 120;
//
// chNumber
//
chNumber.DisplayIndex = 3;
chNumber.Text = "Antal";
chNumber.Width = 100;
chNumber.TextAlign = HorizontalAlignment.Right;
//
// chPrize
//
chPrize.DisplayIndex = 2;
chPrize.Text = "Köpkurs";
chPrize.TextAlign = HorizontalAlignment.Right;
chPrize.Width = 100;
//
// chValue
//
chValue.Text = "Total";
chValue.TextAlign = HorizontalAlignment.Right;
chValue.Width = 90;
//
// lblTotalValue
@ -144,7 +148,7 @@
//
// lwStockHistory
//
lwStockHistory.Columns.AddRange(new ColumnHeader[] { histDatum, histKöptSålt, histAntal, histPris, histTotBelopp, columnHeader1 });
lwStockHistory.Columns.AddRange(new ColumnHeader[] { histAktie, histKöptSålt, histAntal, histPris, histTotBelopp, histType });
lwStockHistory.Location = new Point(113, 311);
lwStockHistory.Name = "lwStockHistory";
lwStockHistory.Size = new Size(676, 153);
@ -152,25 +156,36 @@
lwStockHistory.UseCompatibleStateImageBehavior = false;
lwStockHistory.View = View.Details;
//
// histDatum
// histAktie
//
histDatum.Text = "Datum";
histAktie.Text = "Aktie";
histAktie.Width = 100;
//
// histKöptSålt
//
histKöptSålt.Text = "Köpt/Sålt";
histKöptSålt.Width = 120;
//
// histAntal
//
histAntal.Text = "Antal";
histAntal.TextAlign = HorizontalAlignment.Right;
//
// histPris
//
histPris.Text = "Betalt/Erhållit";
histPris.TextAlign = HorizontalAlignment.Right;
histPris.Width = 100;
//
// histTotBelopp
//
histTotBelopp.Text = "Summa";
histTotBelopp.TextAlign = HorizontalAlignment.Right;
histTotBelopp.Width = 100;
//
// histType
//
histType.Text = "K/S/B";
//
// btnStockHistory
//
@ -220,11 +235,11 @@
private ColumnHeader chValue;
private ListView lwStockHistory;
private Button btnStockHistory;
private ColumnHeader histDatum;
private ColumnHeader histAktie;
private ColumnHeader histKöptSålt;
private ColumnHeader histAntal;
private ColumnHeader histPris;
private ColumnHeader histTotBelopp;
private ColumnHeader columnHeader1;
private ColumnHeader histType;
}
}

View File

@ -58,15 +58,15 @@ public partial class frmStockHistoryAnalyse : Form
{
var item = new ListViewItem(equityTemp.StockCode);
item.SubItems.Add(equityTemp.Bought.ToString());
item.SubItems.Add(equityTemp.BoughtPrice.ToString());
item.SubItems.Add(equityTemp.Quantity.ToString());
item.SubItems.Add(equityTemp.BoughtPrice.ToString());
item.SubItems.Add((equityTemp.TotalValue).ToString("C"));
lwStocks.Items.Add(item);
}
equityTemp.StockCode = t.StockCode;
equityTemp.Bought = t.Bought;
equityTemp.BoughtPrice = t.BoughtPrice;
equityTemp.Quantity = t.Quantity;
equityTemp.BoughtPrice = t.BoughtPrice;
continue;
}
else
@ -81,8 +81,8 @@ public partial class frmStockHistoryAnalyse : Form
}
var l_item = new ListViewItem(equityTemp.StockCode);
l_item.SubItems.Add(equityTemp.Bought.ToString());
l_item.SubItems.Add(equityTemp.BoughtPrice.ToString());
l_item.SubItems.Add(equityTemp.Quantity.ToString());
l_item.SubItems.Add(equityTemp.BoughtPrice.ToString());
l_item.SubItems.Add((equityTemp.TotalValue).ToString("C"));
lwStocks.Items.Add(l_item);
}
@ -110,7 +110,7 @@ public partial class frmStockHistoryAnalyse : Form
LoadHistoryRecsAsync(item.Text).Wait();
}
}
private async Task LoadHistoryRecsAsync(string stockName)
private async Task LoadHistoryRecsAsync(string stockName) //, DateTime equityDate)
{
//MessageBox.Show("Du klickade på: " + item.Text);
var result = _equityServices.GetByCodeAsync(stockName);
@ -121,11 +121,12 @@ public partial class frmStockHistoryAnalyse : Form
{
var l_item = new ListViewItem(histRec.StockCode);
l_item.SubItems.Add(histRec.Bought.ToString());
l_item.SubItems.Add(histRec.BoughtPrice.ToString());
l_item.SubItems.Add(histRec.Quantity.ToString());
l_item.SubItems.Add(histRec.BoughtPrice.ToString());
Debug.WriteLine(histRec.ToString());
var totval = (histRec.SoldQuant > 0) ? histRec.SoldQuant * histRec.SoldPrice - histRec.BoughtPrice * histRec.Quantity : histRec.BoughtPrice * histRec.Quantity;
l_item.SubItems.Add((totval).ToString("C"));
l_item.SubItems.Add((histRec.SoldQuant > 0) ? "B" : "K");
lwStockHistory.Items.Add(l_item);
}
}