Skip to content

Commit

Permalink
Small cleanup for combining fees
Browse files Browse the repository at this point in the history
  • Loading branch information
dickwolff committed Jan 20, 2025
1 parent 33fc9ad commit e6e1093
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/converters/xtbConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("xtbConverter", () => {
// Assert
expect(actualExport).toBeTruthy();
expect(actualExport.activities.length).toBeGreaterThan(0);
expect(actualExport.activities.length).toBe(28);
expect(actualExport.activities.length).toBe(29);

done();
}, () => { done.fail("Should not have an error!"); });
Expand Down
19 changes: 6 additions & 13 deletions src/converters/xtbConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ export class XtbConverter extends AbstractConverter {
else if (type.indexOf("stocks/etf sale") > -1 || type.indexOf("ações/etf vende") > -1) {
return "sell";
}
else if (type.indexOf("sec fee") > -1 || type.indexOf("swap") > -1 || type.indexOf("commission") > -1) {

else if (type.indexOf("sec fee") > -1 || type.indexOf("swap") > -1 || type.indexOf("commission") > -1 || type.indexOf("free funds interests tax") > -1) {
return "fee";
}
else if (type.indexOf("free funds interests tax") > -1) {
return "tax";
}
else if (type.indexOf("free funds interests") > -1) {
return "interest";
}
Expand Down Expand Up @@ -130,14 +126,11 @@ export class XtbConverter extends AbstractConverter {
// Interest does not have a security, so add those immediately.
if (record.type.toLocaleLowerCase() === "interest") {

// Interest records can have a tax record attached. Look it up (if present).
const taxRecord = this.lookupTaxRecord(record, records, idx);

// Add interest record to export.
result.activities.push({
accountId: process.env.GHOSTFOLIO_ACCOUNT_ID,
comment: record.comment,
fee: taxRecord ? Math.abs(taxRecord.amount) : 0,
fee: 0,
quantity: 1,
type: GhostfolioOrderType[record.type],
unitPrice: Math.abs(record.amount),
Expand Down Expand Up @@ -203,7 +196,7 @@ export class XtbConverter extends AbstractConverter {
// Dividend usually goes with a dividend tax record, so look it up.
if (record.type.toLocaleLowerCase() === "dividend") {

const taxRecord = this.lookupTaxRecord(record, records, idx);
const taxRecord = this.lookupDividendTaxRecord(record.id, records, idx);

// If there was a dividend tax record found, check if it matches the dividend record.
if (taxRecord && taxRecord.symbol === record.symbol && taxRecord.time === record.time) {
Expand Down Expand Up @@ -263,14 +256,14 @@ export class XtbConverter extends AbstractConverter {
return ignoredRecordTypes.some(t => record.type.toLocaleLowerCase().indexOf(t) > -1)
}

private lookupTaxRecord(currentRecord: XtbRecord, records: XtbRecord[], idx: number): XtbRecord | undefined {
private lookupDividendTaxRecord(currentRecordId: number, records: XtbRecord[], idx: number): XtbRecord | undefined {

let taxRecord;

// Look ahead at the next record (if there are any records left).
if (idx > 0 && records.length - 1 > idx + 1) {
const nextRecord = records[idx + 1];
if (nextRecord.type.toLocaleLowerCase().indexOf("tax") > -1 && (currentRecord.id + 1 === nextRecord.id) || currentRecord.comment === nextRecord.comment.replace("Tax ", "")) {
if (nextRecord.type.toLocaleLowerCase().indexOf("tax") > -1 && currentRecordId + 1 === nextRecord.id) {
taxRecord = nextRecord;
}
}
Expand All @@ -279,7 +272,7 @@ export class XtbConverter extends AbstractConverter {
if (!taxRecord) {

const previousRecord = records[idx - 1];
if (previousRecord.type.toLocaleLowerCase().indexOf("tax") > -1 && (currentRecord.id + 1 === previousRecord.id) || currentRecord.comment === previousRecord.comment.replace("Tax ", "")) {
if (previousRecord.type.toLocaleLowerCase().indexOf("tax") > -1 && currentRecordId + 1 === previousRecord.id) {
taxRecord = previousRecord;
}
}
Expand Down

0 comments on commit e6e1093

Please sign in to comment.