Skip to content

Commit

Permalink
improve summary report monthly average calculation (#4302)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-fidd authored Mar 4, 2025
1 parent d815a22 commit c8d14ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export function summarySpreadsheet(
);

endDay = d.parse(
monthUtils.lastDayOfMonth(end),
monthUtils.getMonth(end) ===
monthUtils.getMonth(monthUtils.currentDay())
? monthUtils.currentDay()
: monthUtils.lastDayOfMonth(end),
'yyyy-MM-dd',
new Date(),
);
Expand Down Expand Up @@ -196,13 +199,18 @@ function calculatePerMonth(
amount: monthlyData[d.format(m, 'yyyy-MM')] || 0,
}));

const lastMonth = months.at(-1)!;
const dayOfMonth = lastMonth.getDate();
const daysInMonth = monthUtils.getDay(monthUtils.lastDayOfMonth(lastMonth));
const numMonths = months.length - 1 + dayOfMonth / daysInMonth;

const totalAmount = monthsSum.reduce((sum, month) => sum + month.amount, 0);
const averageAmountPerMonth = totalAmount / months.length;
const averageAmountPerMonth = totalAmount / numMonths;

return {
total: averageAmountPerMonth / 100,
dividend: totalAmount / 100,
divisor: months.length,
divisor: numMonths,
};
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4302.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [matt-fidd]
---

Improve summary report monthly average calculation

0 comments on commit c8d14ca

Please sign in to comment.