A local-first personal finance app
add average per year calculation to the summary report (#5838)
Matt Fiddaman4cf5f9b18314
diff --git a/packages/desktop-client/src/components/reports/reports/Summary.tsx b/packages/desktop-client/src/components/reports/reports/Summary.tsxindex 0e7677cebb..40765215fc 100644--- a/packages/desktop-client/src/components/reports/reports/Summary.tsx+++ b/packages/desktop-client/src/components/reports/reports/Summary.tsx@@ -297,6 +297,8 @@ function SummaryInner({ widget }: SummaryInnerProps) {const getDivisorFormatted = (contentType: string, value: number) => {if (contentType === 'avgPerMonth') {return format(value, 'number');+ } else if (contentType === 'avgPerYear') {+ return format(value, 'number');} else if (contentType === 'avgPerTransact') {return format(value, 'number');}@@ -376,12 +378,18 @@ function SummaryInner({ widget }: SummaryInnerProps) {fields={[['sum', t('Sum')],['avgPerMonth', t('Average per month')],+ ['avgPerYear', t('Average per year')],['avgPerTransact', t('Average per transaction')],['percentage', t('Percentage')],]}value={content.type ?? 'sum'}onChange={(- newValue: 'sum' | 'avgPerMonth' | 'avgPerTransact' | 'percentage',+ newValue:+ | 'sum'+ | 'avgPerMonth'+ | 'avgPerYear'+ | 'avgPerTransact'+ | 'percentage',) =>setContent((prev: SummaryContent) =>@@ -508,7 +516,7 @@ function SummaryInner({ widget }: SummaryInnerProps) {}type OperatorProps = {- type: 'sum' | 'avgPerMonth' | 'avgPerTransact' | 'percentage';+ type: 'sum' | 'avgPerMonth' | 'avgPerYear' | 'avgPerTransact' | 'percentage';dividendFilterObject: FilterObject;divisorFilterObject: FilterObject;fromRange: string;@@ -566,7 +574,9 @@ function Operator({>{type === 'avgPerMonth'? t('number of months')- : t('number of transactions')}+ : type === 'avgPerYear'+ ? t('number of years')+ : t('number of transactions')}</Text></>)}diff --git a/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.tsindex aa9aa37585..550c9672a0 100644--- a/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts+++ b/packages/desktop-client/src/components/reports/spreadsheets/summary-spreadsheet.ts@@ -111,7 +111,10 @@ export function summarySpreadsheet(let query = makeRootQuery();- if (summaryContent.type === 'avgPerMonth') {+ if (+ summaryContent.type === 'avgPerMonth' ||+ summaryContent.type === 'avgPerYear'+ ) {query = query.groupBy(['date']);}@@ -156,6 +159,14 @@ export function summarySpreadsheet(break;}+ case 'avgPerYear': {+ setData({+ ...dateRanges,+ ...calculatePerYear(data.data, startDay, endDay),+ });+ break;+ }+case 'percentage':setData({...dateRanges,@@ -217,6 +228,32 @@ function calculatePerMonth(};}+function calculatePerYear(+ data: Array<{+ date: string;+ amount: number;+ count: number;+ }>,+ startDate: Date,+ endDate: Date,+) {+ if (!data.length) {+ return { total: 0, dividend: 0, divisor: 0 };+ }++ const totalAmount = data.reduce((sum, day) => sum + day.amount, 0);+ const totalDays = d.differenceInDays(endDate, startDate) + 1;+ const numYears = totalDays / 365.25;++ const averageAmountPerYear = totalAmount / numYears;++ return {+ total: averageAmountPerYear,+ dividend: totalAmount,+ divisor: numYears,+ };+}+async function calculatePercentage(data: Array<{amount: number;diff --git a/packages/loot-core/src/types/models/dashboard.ts b/packages/loot-core/src/types/models/dashboard.tsindex f9c564f0e2..511029b544 100644--- a/packages/loot-core/src/types/models/dashboard.ts+++ b/packages/loot-core/src/types/models/dashboard.ts@@ -110,7 +110,7 @@ export type SummaryWidget = AbstractWidget<>;export type BaseSummaryContent = {- type: 'sum' | 'avgPerMonth' | 'avgPerTransact';+ type: 'sum' | 'avgPerMonth' | 'avgPerYear' | 'avgPerTransact';fontSize?: number;};diff --git a/upcoming-release-notes/5838.md b/upcoming-release-notes/5838.mdnew file mode 100644index 0000000000..834750bb07--- /dev/null+++ b/upcoming-release-notes/5838.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [matt-fidd]+---++Add average per year calculation to the summary report