Browse Source

Add option to group dividends yearly

pull/1568/head
yksolanki9 3 years ago
parent
commit
42bae5b719
  1. 44
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 2
      apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

44
apps/api/src/app/portfolio/portfolio.service.ts

@ -235,8 +235,8 @@ export class PortfolioService {
};
});
if (groupBy === 'month') {
dividends = this.getDividendsByMonth(dividends);
if (groupBy) {
dividends = this.getDividendsByGroup(dividends, groupBy);
}
const startDate = this.getStartDate(
@ -1269,42 +1269,58 @@ export class PortfolioService {
);
}
private getDividendsByMonth(aDividends: InvestmentItem[]): InvestmentItem[] {
private getDividendsByGroup(
aDividends: InvestmentItem[],
groupBy: GroupBy
): InvestmentItem[] {
if (aDividends.length === 0) {
return [];
}
const dividends = [];
let currentDate: Date;
let investmentByMonth = new Big(0);
let investmentByGroup = new Big(0);
for (const [index, dividend] of aDividends.entries()) {
if (
isSameMonth(parseDate(dividend.date), currentDate) &&
isSameYear(parseDate(dividend.date), currentDate)
isSameYear(parseDate(dividend.date), currentDate) &&
(groupBy === 'year' ||
isSameMonth(parseDate(dividend.date), currentDate))
) {
// Same month: Add up divididends
// Same group: Add up dividends
investmentByMonth = investmentByMonth.plus(dividend.investment);
investmentByGroup = investmentByGroup.plus(dividend.investment);
} else {
// New month: Store previous month and reset
// New group: Store previous group and reset
if (currentDate) {
dividends.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT),
investment: investmentByMonth
date: format(
set(
currentDate,
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 }
),
DATE_FORMAT
),
investment: investmentByGroup
});
}
currentDate = parseDate(dividend.date);
investmentByMonth = new Big(dividend.investment);
investmentByGroup = new Big(dividend.investment);
}
if (index === aDividends.length - 1) {
// Store current month (latest order)
dividends.push({
date: format(set(currentDate, { date: 1 }), DATE_FORMAT),
investment: investmentByMonth
date: format(
set(
currentDate,
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 }
),
DATE_FORMAT
),
investment: investmentByGroup
});
}
}

2
apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

@ -296,7 +296,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
this.dataService
.fetchDividends({
filters: this.activeFilters,
groupBy: 'month',
groupBy: this.mode,
range: this.user?.settings?.dateRange
})
.pipe(takeUntil(this.unsubscribeSubject))

Loading…
Cancel
Save