Skip to content

Commit

Permalink
Add support for taxes in the generic parser (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
VibeNL authored Jan 11, 2024
1 parent 6bcdae2 commit ece4fdd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,36 @@ public async Task ConvertActivitiesForAccount_SingleFee_Converted()
)
});
}

[Fact]
public async Task ConvertActivitiesForAccount_SingleDividend_Converted()
{
// Arrange
var parser = new GenericParser(api.Object);
var fixture = new Fixture();

var asset = fixture.Build<Model.SymbolProfile>().With(x => x.Currency, DefaultCurrency.USD).Create();
var account = fixture.Build<Account>().With(x => x.Balance, Balance.Empty(DefaultCurrency.USD)).Create();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByIdentifier("US2546871060", It.IsAny<Currency>(), It.IsAny<AssetClass[]>(), It.IsAny<AssetSubClass[]>(), true, false)).ReturnsAsync(asset);

// Act
account = await parser.ConvertActivitiesForAccount(account.Name, new[] { "./FileImporter/TestFiles/Generic/CashTransactions/single_dividend.csv" });

// Assert
account.Balance.Current(DummyPriceConverter.Instance).Should().BeEquivalentTo(new Money(DefaultCurrency.USD, 0.067669M, new DateTime(2023, 08, 8, 0, 0, 0, DateTimeKind.Utc)));
account.Activities.Should().BeEquivalentTo(new[] { new Activity(
ActivityType.Dividend,
asset,
new DateTime(2023,08,8, 0,0,0, DateTimeKind.Utc),
0.3247M,
new Money(DefaultCurrency.EUR, 0.2084046812442254388666461349M, new DateTime(2023,08,8, 0,0,0, DateTimeKind.Utc)),
new[] { new Money(DefaultCurrency.EUR, 0, new DateTime(2023,08,8, 0,0,0, DateTimeKind.Utc)) },
"Transaction Reference: [Dividend_US2546871060_2023-08-08_0.3247_EUR_0] (Details: asset US2546871060)",
"Dividend_US2546871060_2023-08-08_0.3247_EUR_0"
)
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ActivityType,Symbol,Date,Currency,Quantity,UnitPrice,Fee,Tax,Description,Id
Dividend,US2546871060,2023-08-8,EUR,0.3247,0.27,0,0.02,,
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
<None Update="FileImporter\TestFiles\DeGiro\PT\SellOrders\single_sell_euro.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FileImporter\TestFiles\Generic\CashTransactions\single_dividend.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FileImporter\TestFiles\Generic\CashTransactions\single_fee.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
11 changes: 10 additions & 1 deletion GhostfolioSidekick/FileImporter/Generic/GenericParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ public GenericParser(IGhostfolioAPI api) : base(api)
record.Id = $"{record.ActivityType}_{record.Symbol}_{record.Date.ToInvariantDateOnlyString()}_{record.Quantity.ToString(CultureInfo.InvariantCulture)}_{record.Currency}_{record.Fee?.ToString(CultureInfo.InvariantCulture)}";
}

var unitPrice = new Model.Money(CurrencyHelper.ParseCurrency(record.Currency), record.UnitPrice, record.Date);

if (record.Tax != null)
{
var totalWithTaxesSubtracted = (unitPrice.Amount * record.Quantity) - record.Tax.Value;
var newUnitPrice = totalWithTaxesSubtracted / record.Quantity;
unitPrice = new Model.Money(unitPrice.Currency, newUnitPrice, unitPrice.TimeOfRecord);
}

var order = new Model.Activity(
record.ActivityType,
asset,
record.Date,
record.Quantity,
new Model.Money(CurrencyHelper.ParseCurrency(record.Currency), record.UnitPrice, record.Date),
unitPrice,
new[] { new Model.Money(CurrencyHelper.ParseCurrency(record.Currency), record.Fee ?? 0, record.Date) },
TransactionReferenceUtilities.GetComment(record.Id, record.Symbol),
record.Id
Expand Down
3 changes: 3 additions & 0 deletions GhostfolioSidekick/FileImporter/Generic/GenericRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class GenericRecord

public decimal? Fee { get; set; }

[CsvHelper.Configuration.Attributes.Optional]
public decimal? Tax { get; set; }

[CsvHelper.Configuration.Attributes.Optional]
public string? Id { get; set; }
}
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ Beside the supported exchanges and brokers there is also a generic format. This
| Currency | The currency of the unitprice and fee |
| Quantity | The amount of units |
| UnitPrice | The paid price per unit |
| Fee | The total fee paid for the transaction |
| Fee | The total fee paid for the transaction. Is optional |
| Tax | The total tax on the transaction, is used to adjust the unitprice. Is optional |
| Description | A description, not used in ghostfolio itself. Is optional |
| Id | The transaction id. Is optional |

##### Example

Expand Down

0 comments on commit ece4fdd

Please sign in to comment.