From eee61da915cb642a5817e22bf2ca8f7e6fa34085 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 19 Dec 2021 12:40:57 +0100 Subject: [PATCH] Add dividend to portfolio summary --- .../src/app/portfolio/portfolio.controller.ts | 1 + .../src/app/portfolio/portfolio.service.ts | 63 +++++++++++++------ .../portfolio-summary.component.html | 14 +++++ .../interfaces/portfolio-summary.interface.ts | 1 + 4 files changed, 61 insertions(+), 18 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.controller.ts b/apps/api/src/app/portfolio/portfolio.controller.ts index a0e93ea0c..ce178ee1c 100644 --- a/apps/api/src/app/portfolio/portfolio.controller.ts +++ b/apps/api/src/app/portfolio/portfolio.controller.ts @@ -330,6 +330,7 @@ export class PortfolioController { 'currentGrossPerformance', 'currentNetPerformance', 'currentValue', + 'dividend', 'fees', 'netWorth', 'totalBuy', diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 4bcf9565c..c6bb6051e 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -729,22 +729,6 @@ export class PortfolioService { }; } - public getFees(orders: OrderWithAccount[], date = new Date(0)) { - return orders - .filter((order) => { - // Filter out all orders before given date - return isBefore(date, new Date(order.date)); - }) - .map((order) => { - return this.exchangeRateDataService.toCurrency( - order.fee, - order.currency, - this.request.user.Settings.currency - ); - }) - .reduce((previous, current) => previous + current, 0); - } - public async getReport(impersonationId: string): Promise { const currency = this.request.user.Settings.currency; const userId = await this.getUserId(impersonationId, this.request.user.id); @@ -825,7 +809,7 @@ export class PortfolioService { new FeeRatioInitialInvestment( this.exchangeRateDataService, currentPositions.totalInvestment.toNumber(), - this.getFees(orders) + this.getFees(orders).toNumber() ) ], { baseCurrency: currency } @@ -845,7 +829,8 @@ export class PortfolioService { currency ); const orders = await this.orderService.getOrders({ userId }); - const fees = this.getFees(orders); + const dividend = this.getDividend(orders).toNumber(); + const fees = this.getFees(orders).toNumber(); const firstOrderDate = orders[0]?.date; const totalBuy = this.getTotalByType(orders, currency, 'BUY'); @@ -859,6 +844,7 @@ export class PortfolioService { return { ...performanceInformation.performance, + dividend, fees, firstOrderDate, netWorth, @@ -939,6 +925,47 @@ export class PortfolioService { return cashPositions; } + private getDividend(orders: OrderWithAccount[], date = new Date(0)) { + return orders + .filter((order) => { + // Filter out all orders before given date and type dividend + return ( + isBefore(date, new Date(order.date)) && + order.type === TypeOfOrder.DIVIDEND + ); + }) + .map((order) => { + return this.exchangeRateDataService.toCurrency( + new Big(order.quantity).mul(order.unitPrice).toNumber(), + order.currency, + this.request.user.Settings.currency + ); + }) + .reduce( + (previous, current) => new Big(previous).plus(current), + new Big(0) + ); + } + + private getFees(orders: OrderWithAccount[], date = new Date(0)) { + return orders + .filter((order) => { + // Filter out all orders before given date + return isBefore(date, new Date(order.date)); + }) + .map((order) => { + return this.exchangeRateDataService.toCurrency( + order.fee, + order.currency, + this.request.user.Settings.currency + ); + }) + .reduce( + (previous, current) => new Big(previous).plus(current), + new Big(0) + ); + } + private getStartDate(aDateRange: DateRange, portfolioStart: Date) { switch (aDateRange) { case '1d': diff --git a/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html b/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html index 893718044..69fc2111c 100644 --- a/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html +++ b/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html @@ -48,6 +48,20 @@ > +
+
Dividend
+
+ + + +
+
Absolute Gross Performance
diff --git a/libs/common/src/lib/interfaces/portfolio-summary.interface.ts b/libs/common/src/lib/interfaces/portfolio-summary.interface.ts index 8a771c557..10025f694 100644 --- a/libs/common/src/lib/interfaces/portfolio-summary.interface.ts +++ b/libs/common/src/lib/interfaces/portfolio-summary.interface.ts @@ -3,6 +3,7 @@ import { PortfolioPerformance } from './portfolio-performance.interface'; export interface PortfolioSummary extends PortfolioPerformance { annualizedPerformancePercent: number; cash: number; + dividend: number; committedFunds: number; fees: number; firstOrderDate: Date;