From c8d14cae0977a0614dc6412e6903dd757195de2f Mon Sep 17 00:00:00 2001 From: Matt Fiddaman Date: Tue, 4 Mar 2025 11:33:29 +0000 Subject: [PATCH] improve summary report monthly average calculation (#4302) --- .../reports/spreadsheets/summary-spreadsheet.ts | 14 +++++++++++--- upcoming-release-notes/4302.md | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 upcoming-release-notes/4302.md diff --git a/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts index 1846b66b6a8..68fa418c09d 100644 --- a/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts +++ b/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts @@ -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(), ); @@ -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, }; } diff --git a/upcoming-release-notes/4302.md b/upcoming-release-notes/4302.md new file mode 100644 index 00000000000..5ef11bf8e6d --- /dev/null +++ b/upcoming-release-notes/4302.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [matt-fidd] +--- + +Improve summary report monthly average calculation