forked from vladimir-aubrecht/StatementParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for parsing of dividends from Revolut.
- Loading branch information
Vladimír Aubrecht
authored and
Vladimír Aubrecht
committed
Mar 6, 2020
1 parent
a8034f7
commit 437119b
Showing
6 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace StatementParser.Models | ||
{ | ||
public enum Broker { MorganStanley, Fidelity, FxChoice, Degiro, Lynx } | ||
public enum Broker { MorganStanley, Fidelity, FxChoice, Degiro, Lynx, Revolut } | ||
} |
22 changes: 22 additions & 0 deletions
22
StatementParser/StatementParser/Parsers/Brokers/Revolut/PdfModels/ActivityDividendModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using ASoft.TextDeserializer.Attributes; | ||
|
||
namespace StatementParser.Parsers.Brokers.Revolut.PdfModels | ||
{ | ||
[DeserializeByRegex("(?<TradeDate>[0-9]{2}/[0-9]{2}/[0-9]{4})(?<SettleDate>[0-9]{2}/[0-9]{2}/[0-9]{4})(?<Currency>[A-Z]{3})(?<ActivityType>DIVNRA|DIV)(?<Description>.+?)00.00(?<AmountRaw>\\(?\\d+\\.\\d{2}\\)?)")] | ||
internal class ActivityDividendModel | ||
{ | ||
public DateTime TradeDate { get; set; } | ||
|
||
public DateTime SettleDate { get; set; } | ||
|
||
public string Currency { get; set; } | ||
|
||
public string ActivityType { get; set; } | ||
|
||
public string Description { get; set; } | ||
|
||
public decimal Amount => decimal.Parse(AmountRaw.Replace('(', '-').TrimEnd(')')); | ||
public string AmountRaw { get; set; } | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
StatementParser/StatementParser/Parsers/Brokers/Revolut/PdfModels/StatementModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using ASoft.TextDeserializer.Attributes; | ||
|
||
namespace StatementParser.Parsers.Brokers.Revolut.PdfModels | ||
{ | ||
[DeserializeByRegex(" (?<AccountNumber>[^ ]+)Account")] | ||
internal class StatementModel | ||
{ | ||
public string AccountNumber { get; set; } | ||
|
||
[DeserializeCollectionByRegex( | ||
"([0-9]{2}/[0-9]{2}/[0-9]{4}[0-9]{2}/[0-9]{2}/[0-9]{4})", | ||
"HOLDINGSACTIVITYTrade DateSettle DateCurrencyActivity TypeSymbol / DescriptionQuantityPriceAmount(.+)Page \\d+ of \\d+")] | ||
public ActivityDividendModel[] ActivityDividend { get; set; } | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
StatementParser/StatementParser/Parsers/Brokers/Revolut/RevolutStatementParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using ASoft.TextDeserializer; | ||
using ASoft.TextDeserializer.Exceptions; | ||
using StatementParser.Models; | ||
using StatementParser.Parsers.Brokers.Revolut.PdfModels; | ||
|
||
namespace StatementParser.Parsers.Brokers.Revolut | ||
{ | ||
internal class RevolutStatementParser : ITransactionParser | ||
{ | ||
private bool CanParse(string statementFilePath) | ||
{ | ||
return File.Exists(statementFilePath) && Path.GetExtension(statementFilePath).ToLowerInvariant() == ".pdf"; | ||
} | ||
|
||
public IList<Transaction> Parse(string statementFilePath) | ||
{ | ||
if (!CanParse(statementFilePath)) | ||
{ | ||
return null; | ||
} | ||
|
||
var transactions = new List<Transaction>(); | ||
|
||
using (var textSource = new TextSource(statementFilePath)) | ||
{ | ||
try | ||
{ | ||
var parsedDocument = new TextDocumentParser<StatementModel>().Parse(textSource); | ||
|
||
foreach (var transaction in parsedDocument.ActivityDividend) | ||
{ | ||
if (transaction.ActivityType == "DIV") | ||
{ | ||
transactions.Add(CreateDividendTransaction(transaction, parsedDocument.ActivityDividend)); | ||
} | ||
} | ||
|
||
} | ||
catch (TextException) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
return transactions; | ||
} | ||
|
||
private DividendTransaction CreateDividendTransaction(ActivityDividendModel activityDividendRow, ActivityDividendModel[] activities) | ||
{ | ||
decimal tax = SearchForTax(activityDividendRow, activities); | ||
var currency = (Currency)Enum.Parse(typeof(Currency), activityDividendRow.Currency); | ||
return new DividendTransaction(Broker.Revolut, activityDividendRow.SettleDate, activityDividendRow.Description, activityDividendRow.Amount, tax, currency); | ||
} | ||
|
||
private decimal SearchForTax(ActivityDividendModel dividendTransactionRow, ActivityDividendModel[] activities) | ||
{ | ||
var transactionRow = activities.FirstOrDefault(i => | ||
i.ActivityType == "DIVNRA" && | ||
i.TradeDate == dividendTransactionRow.TradeDate && | ||
i.SettleDate == dividendTransactionRow.SettleDate && | ||
i.Description.Substring(0, i.Description.IndexOf(" - DIV")) == dividendTransactionRow.Description.Substring(0, dividendTransactionRow.Description.IndexOf(" - DIV")) | ||
); | ||
|
||
return transactionRow?.Amount ?? 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters