|
@ -235,8 +235,8 @@ export class PortfolioService { |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (groupBy === 'month') { |
|
|
if (groupBy) { |
|
|
dividends = this.getDividendsByMonth(dividends); |
|
|
dividends = this.getDividendsByGroup(dividends, groupBy); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const startDate = this.getStartDate( |
|
|
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) { |
|
|
if (aDividends.length === 0) { |
|
|
return []; |
|
|
return []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const dividends = []; |
|
|
const dividends = []; |
|
|
let currentDate: Date; |
|
|
let currentDate: Date; |
|
|
let investmentByMonth = new Big(0); |
|
|
let investmentByGroup = new Big(0); |
|
|
|
|
|
|
|
|
for (const [index, dividend] of aDividends.entries()) { |
|
|
for (const [index, dividend] of aDividends.entries()) { |
|
|
if ( |
|
|
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 { |
|
|
} else { |
|
|
// New month: Store previous month and reset
|
|
|
// New group: Store previous group and reset
|
|
|
|
|
|
|
|
|
if (currentDate) { |
|
|
if (currentDate) { |
|
|
dividends.push({ |
|
|
dividends.push({ |
|
|
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), |
|
|
date: format( |
|
|
investment: investmentByMonth |
|
|
set( |
|
|
|
|
|
currentDate, |
|
|
|
|
|
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 } |
|
|
|
|
|
), |
|
|
|
|
|
DATE_FORMAT |
|
|
|
|
|
), |
|
|
|
|
|
investment: investmentByGroup |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
currentDate = parseDate(dividend.date); |
|
|
currentDate = parseDate(dividend.date); |
|
|
investmentByMonth = new Big(dividend.investment); |
|
|
investmentByGroup = new Big(dividend.investment); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (index === aDividends.length - 1) { |
|
|
if (index === aDividends.length - 1) { |
|
|
// Store current month (latest order)
|
|
|
// Store current month (latest order)
|
|
|
dividends.push({ |
|
|
dividends.push({ |
|
|
date: format(set(currentDate, { date: 1 }), DATE_FORMAT), |
|
|
date: format( |
|
|
investment: investmentByMonth |
|
|
set( |
|
|
|
|
|
currentDate, |
|
|
|
|
|
groupBy === 'month' ? { date: 1 } : { date: 1, month: 1 } |
|
|
|
|
|
), |
|
|
|
|
|
DATE_FORMAT |
|
|
|
|
|
), |
|
|
|
|
|
investment: investmentByGroup |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|