From d0d43ff073851e70332e41e45995f37890a224ee Mon Sep 17 00:00:00 2001 From: Ronan-Yann Lorin Date: Sun, 21 Apr 2024 22:04:13 +0200 Subject: [PATCH] Dividends details bug fix --- .../Portfolio/Report/DividendsDetails.tsx | 2 +- src/models/bond_statement.model.ts | 5 +- src/routers/statements.router.ts | 5 +- src/routers/statements.types.ts | 2 +- src/routers/statements.utils.ts | 64 ++++++++++--------- src/routers/trades.router.ts | 4 +- 6 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/app/components/Portfolio/Report/DividendsDetails.tsx b/src/app/components/Portfolio/Report/DividendsDetails.tsx index 0d51a31..a66cc0c 100644 --- a/src/app/components/Portfolio/Report/DividendsDetails.tsx +++ b/src/app/components/Portfolio/Report/DividendsDetails.tsx @@ -42,7 +42,7 @@ const DividendsDetails = ({ theReports, ..._rest }: Props): React.ReactNode => { id: statement.id, date: new Date(statement.date), country: statement.country, - amount: statement.amount > 0 ? statement.amount * statement.fxRateToBase : 0, + amount: statement.amount * statement.fxRateToBase, tax: 0, description: statement.description, }; diff --git a/src/models/bond_statement.model.ts b/src/models/bond_statement.model.ts index 66e0924..8ffe800 100644 --- a/src/models/bond_statement.model.ts +++ b/src/models/bond_statement.model.ts @@ -21,8 +21,9 @@ export class BondStatement extends Model< @Column({ type: DataType.STRING(2), field: "country" }) declare country: string; - @Column({ type: DataType.FLOAT, field: "accruedInt" }) - declare accruedInterests: number; + // accruedInterests are part of PnL + // @Column({ type: DataType.FLOAT, field: "accruedInt" }) + // declare accruedInterests: number; /** proceeds */ @Column({ type: DataType.FLOAT(8, 2), defaultValue: 0 }) diff --git a/src/routers/statements.router.ts b/src/routers/statements.router.ts index b185266..9f21026 100644 --- a/src/routers/statements.router.ts +++ b/src/routers/statements.router.ts @@ -90,8 +90,9 @@ const makeSynthesys = (statements: Statement[]): Promise { - value[idx].interests += statement ? statement.accruedInterests * item.fxRateToBase : 0; - value[idx].total += statement ? statement.accruedInterests * item.fxRateToBase : 0; + // value[idx].interests += statement ? statement.accruedInterests * item.fxRateToBase : 0; + // value[idx].total += statement ? statement.accruedInterests * item.fxRateToBase : 0; + value[idx].total += statement ? statement.realizedPnL * item.fxRateToBase : 0; return value; }); case StatementTypes.TaxStatement: diff --git a/src/routers/statements.types.ts b/src/routers/statements.types.ts index 98eab95..565aad1 100644 --- a/src/routers/statements.types.ts +++ b/src/routers/statements.types.ts @@ -52,7 +52,7 @@ export type BondStatementEntry = BaseStatement & { statementType: "Bond"; country: string; underlying: StatementUnderlyingEntry | undefined; - accruedInterests: number; + // accruedInterests: number; quantity: number; pnl: number; fees: number; diff --git a/src/routers/statements.utils.ts b/src/routers/statements.utils.ts index a6286ca..f88fb55 100644 --- a/src/routers/statements.utils.ts +++ b/src/routers/statements.utils.ts @@ -134,7 +134,7 @@ export const statementModelToStatementEntry = (item: Statement): Promise => { } switch (statement.statementType) { case StatementTypes.DividendStatement: - dividendEntry = report.dividendsSummary.find((item) => item.country == statement.country); - if (!dividendEntry) { - dividendEntry = { country: statement.country, grossAmountInBase: 0, taxes: 0, netAmountInBase: 0 }; - report.dividendsSummary.push(dividendEntry); - } - dividendEntry.grossAmountInBase += statement.amount * statement.fxRateToBase; - dividendEntry.netAmountInBase += statement.amount * statement.fxRateToBase; - report.dividendsDetails.push(statement); - break; - case StatementTypes.TaxStatement: dividendEntry = report.dividendsSummary.find((item) => item.country == statement.country); if (!dividendEntry) { dividendEntry = { country: statement.country, grossAmountInBase: 0, taxes: 0, netAmountInBase: 0 }; report.dividendsSummary.push(dividendEntry); } - dividendEntry.taxes += statement.amount * statement.fxRateToBase; + if (statement.statementType == StatementTypes.DividendStatement) + dividendEntry.grossAmountInBase += statement.amount * statement.fxRateToBase; + else if (statement.statementType == StatementTypes.TaxStatement) + dividendEntry.taxes += statement.amount * statement.fxRateToBase; dividendEntry.netAmountInBase += statement.amount * statement.fxRateToBase; report.dividendsDetails.push(statement); break; + // case StatementTypes.TaxStatement: + // dividendEntry = report.dividendsSummary.find((item) => item.country == statement.country); + // if (!dividendEntry) { + // dividendEntry = { country: statement.country, grossAmountInBase: 0, taxes: 0, netAmountInBase: 0 }; + // report.dividendsSummary.push(dividendEntry); + // } + // dividendEntry.taxes += statement.amount * statement.fxRateToBase; + // dividendEntry.netAmountInBase += statement.amount * statement.fxRateToBase; + // report.dividendsDetails.push(statement); + // break; + case StatementTypes.InterestStatement: interestEntry = report.interestsSummary.find( (item) => item.country == (statement.country || portfolio.country), @@ -270,24 +274,24 @@ export const prepareReport = (portfolio: Portfolio): Promise => { case StatementTypes.BondStatement: // interest part - interestEntry = report.interestsSummary.find( - (item) => item.country == (statement.country || portfolio.country), - ); - if (!interestEntry) { - interestEntry = { - country: statement.country || portfolio.country, - grossCredit: 0, - netDebit: 0, - withHolding: 0, - netTotal: 0, - }; - report.interestsSummary.push(interestEntry); - } - if (statement.accruedInterests > 0) - interestEntry.grossCredit += statement.accruedInterests * statement.fxRateToBase; - else interestEntry.netDebit += statement.accruedInterests * statement.fxRateToBase; - interestEntry.netTotal += statement.accruedInterests * statement.fxRateToBase; - report.interestsDetails.push(statement); + // interestEntry = report.interestsSummary.find( + // (item) => item.country == (statement.country || portfolio.country), + // ); + // if (!interestEntry) { + // interestEntry = { + // country: statement.country || portfolio.country, + // grossCredit: 0, + // netDebit: 0, + // withHolding: 0, + // netTotal: 0, + // }; + // report.interestsSummary.push(interestEntry); + // } + // if (statement.accruedInterests > 0) + // interestEntry.grossCredit += statement.accruedInterests * statement.fxRateToBase; + // else interestEntry.netDebit += statement.accruedInterests * statement.fxRateToBase; + // interestEntry.netTotal += statement.accruedInterests * statement.fxRateToBase; + // report.interestsDetails.push(statement); // PnL part report.tradesSummary.bondPnLInBase += statement.pnl * statement.fxRateToBase; report.tradesSummary.totalPnL += statement.pnl * statement.fxRateToBase; diff --git a/src/routers/trades.router.ts b/src/routers/trades.router.ts index 52c45b5..592ace5 100644 --- a/src/routers/trades.router.ts +++ b/src/routers/trades.router.ts @@ -2,9 +2,9 @@ import { OptionType, SecType } from "@stoqey/ib"; import express from "express"; import { Op } from "sequelize"; import logger, { LogLevel } from "../logger"; -import { Contract, Currency, Portfolio, Position, Statement, StatementTypes, Trade } from "../models"; +import { Contract, Currency, Portfolio, Position, Statement, Trade } from "../models"; import { expirationToDate } from "../models/date_utils"; -import { ContractType, TradeStatus, TradeStrategy } from "../models/types"; +import { ContractType, StatementTypes, TradeStatus, TradeStrategy } from "../models/types"; import { preparePositions } from "./positions.router"; import { prepareStatements } from "./statements.utils"; import {