Skip to content

Commit

Permalink
Added chonological sorting for transactions and transaction views (#21)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
akrupnov authored May 1, 2024
1 parent 267b4f9 commit 2af83d0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion StatementParser/StatementParserCLI/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class Output
{
private Dictionary<string, List<Transaction>> GroupTransactions(IList<Transaction> 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<Transaction> transactions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace TaxReporterCLI.Models.Views
{
public class DividendBrokerSummaryView
public class DividendBrokerSummaryView : IView
{
[Description("Exchanged to currency")]
public Currency ExchangedToCurrency { get; }
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace TaxReporterCLI.Models.Views
{
public class DividendCurrencySummaryView
public class DividendCurrencySummaryView : IView
{
[Description("Exchanged to currency")]
public Currency ExchangedToCurrency { get; }
Expand Down Expand Up @@ -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;
}
}
}
8 changes: 8 additions & 0 deletions StatementParser/TaxReporterCLI/Models/Views/IView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace TaxReporterCLI.Models.Views
{
public interface IView : IComparable<IView>
{
}
}
14 changes: 12 additions & 2 deletions StatementParser/TaxReporterCLI/Models/Views/TransactionView.cs
Original file line number Diff line number Diff line change
@@ -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; }

Expand All @@ -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);
}
}
}
23 changes: 12 additions & 11 deletions StatementParser/TaxReporterCLI/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,52 @@
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using StatementParser.Attributes;
using TaxReporterCLI.Models.Views;

namespace TaxReporterCLI
{
internal class Output
{
private Dictionary<string, List<object>> GroupTransactions(IList<object> transactions)
private Dictionary<string, List<IView>> GroupViews(IList<IView> 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<object> transactions)
public void PrintAsJson(IList<IView> views)
{
var groupedTransactions = GroupTransactions(transactions);
var groupedTransactions = GroupViews(views);

Console.WriteLine(JsonConvert.SerializeObject(groupedTransactions));
}

public void SaveAsExcelSheet(string filePath, IList<object> transactions)
public void SaveAsExcelSheet(string filePath, IList<IView> 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));
}

wb1.Write(file);
}

public void PrintAsPlainText(IList<object> transactions)
public void PrintAsPlainText(IList<IView> 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);
Console.WriteLine(String.Join("\r\n", group.Value));
}
}

private ISheet CreateSheet(XSSFWorkbook workbook, string sheetName, IList<object> objects)
private ISheet CreateSheet(XSSFWorkbook workbook, string sheetName, IList<IView> objects)
{
var sheet = workbook.CreateSheet(sheetName);

Expand Down
8 changes: 4 additions & 4 deletions StatementParser/TaxReporterCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ private static async Task RunAsync(Options option)

var summaryViews = CreateDividendSummaryViews(transactionViews);

var views = new List<object>(transactionViews);
var views = new List<IView>(transactionViews);
views.AddRange(summaryViews);

Print(option, views);
}

private static IList<object> CreateDividendSummaryViews(IList<TransactionView> transactionViews)
private static IList<IView> CreateDividendSummaryViews(IList<TransactionView> transactionViews)
{
var summaryViews = new List<object>();
var summaryViews = new List<IView>();

var usedBrokers = transactionViews.Select(i => i.Transaction.Broker).Distinct();
var usedCurrencies = transactionViews.Select(i => i.Transaction.Currency).Distinct();
Expand Down Expand Up @@ -119,7 +119,7 @@ private static IList<object> CreateDividendSummaryViews(IList<TransactionView> t
return summaryViews;
}

private static void Print(Options option, IList<object> views)
private static void Print(Options option, IList<IView> views)
{
var printer = new Output();
if (option.ShouldPrintAsJson)
Expand Down

0 comments on commit 2af83d0

Please sign in to comment.