This commit is contained in:
2026-05-04 07:29:54 +02:00
parent fdf3c86bd0
commit 392d8097e3
4 changed files with 102 additions and 23 deletions

View File

@ -0,0 +1,57 @@
using StockHistory.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StockHistory;
public class HandleCsvNotes : IHandleCsvNotes
{
private List<string> lines = new();
private string[] Header_table = new string[0];
public List<TransactionNote> GetTransactionNotes(string filePath)
{
lines = File.ReadAllLines(filePath).ToList();
Header_table = lines[0].Split(';');
var transactionNotes = new List<TransactionNote>();
foreach (var line in lines.Skip(1))
{
var values = line.Split(';');
var test = values[NamePos(Header_table, "Typ av transaktion")];
if (test == "Köp" || test == "Sälj")
{
var transactionNote = new TransactionNote
{
TransactionDate = DateTime.Parse(values[NamePos(Header_table, "Datum")]),
TransactionType = values[NamePos(Header_table, "Typ av transaktion")],
StockCode = values[NamePos(Header_table, @"Värdepapper/beskrivning")],
StockPrice = decimal.Parse((values[NamePos(Header_table, "Kurs")] == "") ? "0" : values[NamePos(Header_table, "Kurs")]),
StockQuantity = int.Parse(values[NamePos(Header_table, "Antal")]),
Commission = decimal.Parse((values[NamePos(Header_table, "Courtage")] == "") ? "0" : values[NamePos(Header_table, "Courtage")]),
TotalAmount = decimal.Parse((values[NamePos(Header_table, "Belopp")] == "") ? "0" : values[NamePos(Header_table, "Belopp")]) - decimal.Parse((values[NamePos(Header_table, "Courtage")] == "") ? "0" : values[NamePos(Header_table, "Courtage")]),
AmountToPay = decimal.Parse((values[NamePos(Header_table, "Belopp")] == "") ? "0" : values[NamePos(Header_table, "Belopp")])
};
transactionNotes.Add(transactionNote);
}
}
return transactionNotes;
}
private int NamePos(string[] h_table, string value)
{
var pos = Array.IndexOf(h_table, value);
if (pos != -1)
{
return pos;
}
else
{
Console.WriteLine($"Warning: Unable to find '{value}'");
return 0;
}
}
}

View File

@ -0,0 +1,9 @@
using StockHistory.Models;
namespace StockHistory
{
public interface IHandleCsvNotes
{
List<TransactionNote> GetTransactionNotes(string filePath);
}
}

View File

@ -41,6 +41,7 @@ namespace StockHistory
services.AddTransient<IPdfOpener, PdfOpener>();
services.AddTransient<IPdfFormatter, PdfFormatter>();
services.AddTransient<ITransactionNotesServices, TransactionNotesServices>();
services.AddTransient<IHandleCsvNotes, HandleCsvNotes>();
});
}

View File

@ -19,19 +19,22 @@ public partial class frmStockHistoryInit : Form
private readonly IPdfOpener _pdfOpener;
private readonly IPdfFormatter _pdfFormatter;
private readonly ITransactionNotesServices _transactionNotes;
private readonly IHandleCsvNotes _handleCsvNotes;
private List<PdfSida> _pdfSidor = new();
private List<PdfSida> localPdfSida = new();
public frmStockHistoryInit(
IPdfOpener pdfOpener,
IPdfFormatter pdfFormatter,
ITransactionNotesServices transactionNotes
ITransactionNotesServices transactionNotes,
IHandleCsvNotes handleCsvNotes
)
{
InitializeComponent();
_pdfOpener = pdfOpener;
_pdfFormatter = pdfFormatter;
_transactionNotes = transactionNotes;
_handleCsvNotes = handleCsvNotes;
btnAnalysera.Enabled = false;
Shown += async (s, e) => await LoadBusinessAsync();
@ -76,7 +79,7 @@ public partial class frmStockHistoryInit : Form
if (lblFileName.Text.ToLower().EndsWith("csv"))
{
var Result = string.Empty; //DocoticPdfReading.ExtractText(lblFileName.Text);
SelectAvanzaPdfData(Result);
SelectAvanzaPdfData(lblFileName.Text);
}
else
{
@ -87,32 +90,41 @@ public partial class frmStockHistoryInit : Form
lblFileName.Text = string.Empty;
}
private void SelectAvanzaPdfData(string PdfResult)
private void SelectAvanzaPdfData(string infile)
{
txtFound.Text = string.Empty;
List<string> result = PdfResult.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
//List<string> selected = new();
foreach (var line in result)
{
if (!string.IsNullOrWhiteSpace(line) && line.StartsWith("Affärsdag")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Handelstidpunkt")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Värdepapper")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Kurs")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Genomsnittlig")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Antal")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Köpeskilling")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Courtage")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Belopp")
|| !string.IsNullOrWhiteSpace(line) && line.StartsWith("Vi bekräftar")
)
{
Selected.Add(line);
txtFound.Text += line + Environment.NewLine;
List<TransactionNote> result = _handleCsvNotes.GetTransactionNotes(infile);
foreach (TransactionNote note in result) {
// Selected.Add(note);
txtFound.Text += note.ToString() + Environment.NewLine;
txtFound.Refresh();
}
}
//List<string> selected = new();
//foreach (var line in result)
//{
// if (!string.IsNullOrWhiteSpace(line) && line.StartsWith("Affärsdag")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Handelstidpunkt")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Värdepapper")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Kurs")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Genomsnittlig")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Antal")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Köpeskilling")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Courtage")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Belopp")
// || !string.IsNullOrWhiteSpace(line) && line.StartsWith("Vi bekräftar")
// )
// {
// Selected.Add(line);
// txtFound.Text += line + Environment.NewLine;
// txtFound.Refresh();
// }
//}
}
private void SelectPdfData(string PdfResult)