From 2af83d0656736fc72c98733608b69a5dcf6cdec1 Mon Sep 17 00:00:00 2001 From: Alex Krupnov Date: Wed, 1 May 2024 21:22:03 +0200 Subject: [PATCH] Added chonological sorting for transactions and transaction views (#21) * Added chonological sorting for transactions and views * Checking if PDF has a title for Morgan Stanley parser * Revert "Checking if PDF has a title for Morgan Stanley parser" Added a separate pull request for it --- StatementParser/StatementParserCLI/Output.cs | 2 +- .../Models/Views/DividendBrokerSummaryView.cs | 9 ++++++-- .../Views/DividendCurrencySummaryView.cs | 9 ++++++-- .../TaxReporterCLI/Models/Views/IView.cs | 8 +++++++ .../Models/Views/TransactionView.cs | 14 +++++++++-- StatementParser/TaxReporterCLI/Output.cs | 23 ++++++++++--------- StatementParser/TaxReporterCLI/Program.cs | 8 +++---- 7 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 StatementParser/TaxReporterCLI/Models/Views/IView.cs diff --git a/StatementParser/StatementParserCLI/Output.cs b/StatementParser/StatementParserCLI/Output.cs index bdc2eb1..944a7bf 100644 --- a/StatementParser/StatementParserCLI/Output.cs +++ b/StatementParser/StatementParserCLI/Output.cs @@ -13,7 +13,7 @@ internal class Output { private Dictionary> GroupTransactions(IList transactions) { - return transactions.GroupBy(i => i.GetType()).ToDictionary(k => k.Key.Name, i => i.Select(a => a).ToList()); + return transactions.GroupBy(i => i.GetType()).ToDictionary(k => k.Key.Name, i => i.Select(a => a).OrderBy(t => t.Date).ToList()); } public void PrintAsJson(IList transactions) diff --git a/StatementParser/TaxReporterCLI/Models/Views/DividendBrokerSummaryView.cs b/StatementParser/TaxReporterCLI/Models/Views/DividendBrokerSummaryView.cs index 405d113..763423d 100644 --- a/StatementParser/TaxReporterCLI/Models/Views/DividendBrokerSummaryView.cs +++ b/StatementParser/TaxReporterCLI/Models/Views/DividendBrokerSummaryView.cs @@ -6,7 +6,7 @@ namespace TaxReporterCLI.Models.Views { - public class DividendBrokerSummaryView + public class DividendBrokerSummaryView : IView { [Description("Exchanged to currency")] public Currency ExchangedToCurrency { get; } @@ -74,5 +74,10 @@ public override string ToString() { return $"{nameof(ExchangedToCurrency)}: {ExchangedToCurrency} {nameof(Broker)}: {Broker} {nameof(Currency)}: {Currency} {nameof(TotalIncome)}: {TotalIncome} {nameof(TotalTax)}: {TotalTax} {nameof(ExchangedPerDayTotalIncome)}: {ExchangedPerDayTotalIncome} {nameof(ExchangedPerYearTotalIncome)}: {ExchangedPerYearTotalIncome} {nameof(ExchangedPerDayTotalTax)}: {ExchangedPerDayTotalTax} {nameof(ExchangedPerYearTotalTax)}: {ExchangedPerYearTotalTax}"; } - } + + public int CompareTo(IView other) + { + return 0; + } + } } \ No newline at end of file diff --git a/StatementParser/TaxReporterCLI/Models/Views/DividendCurrencySummaryView.cs b/StatementParser/TaxReporterCLI/Models/Views/DividendCurrencySummaryView.cs index 7c0bb5a..9737158 100644 --- a/StatementParser/TaxReporterCLI/Models/Views/DividendCurrencySummaryView.cs +++ b/StatementParser/TaxReporterCLI/Models/Views/DividendCurrencySummaryView.cs @@ -6,7 +6,7 @@ namespace TaxReporterCLI.Models.Views { - public class DividendCurrencySummaryView + public class DividendCurrencySummaryView : IView { [Description("Exchanged to currency")] public Currency ExchangedToCurrency { get; } @@ -71,5 +71,10 @@ public override string ToString() { return $"{nameof(ExchangedToCurrency)}: {ExchangedToCurrency} {nameof(Currency)}: {Currency} {nameof(TotalIncome)}: {TotalIncome} {nameof(TotalTax)}: {TotalTax} {nameof(ExchangedPerDayTotalIncome)}: {ExchangedPerDayTotalIncome} {nameof(ExchangedPerYearTotalIncome)}: {ExchangedPerYearTotalIncome} {nameof(ExchangedPerDayTotalTax)}: {ExchangedPerDayTotalTax} {nameof(ExchangedPerYearTotalTax)}: {ExchangedPerYearTotalTax}"; } - } + + public int CompareTo(IView other) + { + return 0; + } + } } \ No newline at end of file diff --git a/StatementParser/TaxReporterCLI/Models/Views/IView.cs b/StatementParser/TaxReporterCLI/Models/Views/IView.cs new file mode 100644 index 0000000..343affd --- /dev/null +++ b/StatementParser/TaxReporterCLI/Models/Views/IView.cs @@ -0,0 +1,8 @@ +using System; + +namespace TaxReporterCLI.Models.Views +{ + public interface IView : IComparable + { + } +} \ No newline at end of file diff --git a/StatementParser/TaxReporterCLI/Models/Views/TransactionView.cs b/StatementParser/TaxReporterCLI/Models/Views/TransactionView.cs index bd908e6..780ea60 100644 --- a/StatementParser/TaxReporterCLI/Models/Views/TransactionView.cs +++ b/StatementParser/TaxReporterCLI/Models/Views/TransactionView.cs @@ -1,9 +1,10 @@ +using System; using StatementParser.Attributes; using StatementParser.Models; namespace TaxReporterCLI.Models.Views { - public class TransactionView + public class TransactionView : IView { public Transaction Transaction { get; } @@ -28,5 +29,14 @@ public override string ToString() { return $"{Transaction} {nameof(ExchangedToCurrency)}:{ExchangedToCurrency} {nameof(ExchangeRatePerDay)}: {ExchangeRatePerDay} {nameof(ExchangeRatePerYear)}: {ExchangeRatePerYear}"; } - } + + public int CompareTo(IView other) + { + if(!(other is TransactionView)) + { + return 0; + } + return this.Transaction.Date.CompareTo((other as TransactionView).Transaction.Date); + } + } } \ No newline at end of file diff --git a/StatementParser/TaxReporterCLI/Output.cs b/StatementParser/TaxReporterCLI/Output.cs index 1bbac4a..ef33bd9 100644 --- a/StatementParser/TaxReporterCLI/Output.cs +++ b/StatementParser/TaxReporterCLI/Output.cs @@ -7,31 +7,32 @@ using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using StatementParser.Attributes; +using TaxReporterCLI.Models.Views; namespace TaxReporterCLI { internal class Output { - private Dictionary> GroupTransactions(IList transactions) + private Dictionary> GroupViews(IList views) { - return transactions.GroupBy(i => i.GetType()).ToDictionary(k => k.Key.Name, i => i.Select(a => a).ToList()); + return views.GroupBy(i => i.GetType()).ToDictionary(k => k.Key.Name, i => i.Select(a => a).OrderBy(x => x).ToList()); } - public void PrintAsJson(IList transactions) + public void PrintAsJson(IList views) { - var groupedTransactions = GroupTransactions(transactions); + var groupedTransactions = GroupViews(views); Console.WriteLine(JsonConvert.SerializeObject(groupedTransactions)); } - public void SaveAsExcelSheet(string filePath, IList transactions) + public void SaveAsExcelSheet(string filePath, IList views) { - var groupedTransactions = GroupTransactions(transactions); + var groupedViews = GroupViews(views); using FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite); var wb1 = new XSSFWorkbook(); - foreach (var group in groupedTransactions) + foreach (var group in groupedViews) { wb1.Add(CreateSheet(wb1, @group.Key, @group.Value)); } @@ -39,11 +40,11 @@ public void SaveAsExcelSheet(string filePath, IList transactions) wb1.Write(file); } - public void PrintAsPlainText(IList transactions) + public void PrintAsPlainText(IList views) { - var groupedTransactions = GroupTransactions(transactions); + var groupedViews = GroupViews(views); - foreach (var group in groupedTransactions) + foreach (var group in groupedViews) { Console.WriteLine(); Console.WriteLine(group.Key); @@ -51,7 +52,7 @@ public void PrintAsPlainText(IList transactions) } } - private ISheet CreateSheet(XSSFWorkbook workbook, string sheetName, IList objects) + private ISheet CreateSheet(XSSFWorkbook workbook, string sheetName, IList objects) { var sheet = workbook.CreateSheet(sheetName); diff --git a/StatementParser/TaxReporterCLI/Program.cs b/StatementParser/TaxReporterCLI/Program.cs index e800d9d..19656c9 100644 --- a/StatementParser/TaxReporterCLI/Program.cs +++ b/StatementParser/TaxReporterCLI/Program.cs @@ -83,15 +83,15 @@ private static async Task RunAsync(Options option) var summaryViews = CreateDividendSummaryViews(transactionViews); - var views = new List(transactionViews); + var views = new List(transactionViews); views.AddRange(summaryViews); Print(option, views); } - private static IList CreateDividendSummaryViews(IList transactionViews) + private static IList CreateDividendSummaryViews(IList transactionViews) { - var summaryViews = new List(); + var summaryViews = new List(); var usedBrokers = transactionViews.Select(i => i.Transaction.Broker).Distinct(); var usedCurrencies = transactionViews.Select(i => i.Transaction.Currency).Distinct(); @@ -119,7 +119,7 @@ private static IList CreateDividendSummaryViews(IList t return summaryViews; } - private static void Print(Options option, IList views) + private static void Print(Options option, IList views) { var printer = new Output(); if (option.ShouldPrintAsJson)