diff --git a/StockHistory/HandleCsvNotes.cs b/StockHistory/HandleCsvNotes.cs new file mode 100644 index 0000000..46183b3 --- /dev/null +++ b/StockHistory/HandleCsvNotes.cs @@ -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 lines = new(); + private string[] Header_table = new string[0]; + + public List GetTransactionNotes(string filePath) + { + lines = File.ReadAllLines(filePath).ToList(); + Header_table = lines[0].Split(';'); + var transactionNotes = new List(); + 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; + } + } +} \ No newline at end of file diff --git a/StockHistory/IHandleCsvNotes.cs b/StockHistory/IHandleCsvNotes.cs new file mode 100644 index 0000000..b6a21af --- /dev/null +++ b/StockHistory/IHandleCsvNotes.cs @@ -0,0 +1,9 @@ +using StockHistory.Models; + +namespace StockHistory +{ + public interface IHandleCsvNotes + { + List GetTransactionNotes(string filePath); + } +} \ No newline at end of file diff --git a/StockHistory/Program.cs b/StockHistory/Program.cs index 29c331b..bac714d 100644 --- a/StockHistory/Program.cs +++ b/StockHistory/Program.cs @@ -41,6 +41,7 @@ namespace StockHistory services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); }); } diff --git a/StockHistory/frmStockHistoryInit.cs b/StockHistory/frmStockHistoryInit.cs index 2ec5521..3d910a5 100644 --- a/StockHistory/frmStockHistoryInit.cs +++ b/StockHistory/frmStockHistoryInit.cs @@ -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 _pdfSidor = new(); private List 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 result = PdfResult.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList(); + + List result = _handleCsvNotes.GetTransactionNotes(infile); + + foreach (TransactionNote note in result) { + // Selected.Add(note); + txtFound.Text += note.ToString() + Environment.NewLine; + txtFound.Refresh(); + + + } //List 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(); + //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)