Skip to content

A local-first personal finance app

extend fix splits tool to report splits with mismatched amounts (#3970)

Matt Fiddaman93262e7fb499
diff --git a/packages/desktop-client/src/components/settings/FixSplits.tsx b/packages/desktop-client/src/components/settings/FixSplits.tsx
index 2897ee431a..fa87cecfee 100644
--- a/packages/desktop-client/src/components/settings/FixSplits.tsx
+++ b/packages/desktop-client/src/components/settings/FixSplits.tsx
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
-import { useTranslation } from 'react-i18next';
+import { useTranslation, Trans } from 'react-i18next';
import { send } from 'loot-core/src/platform/client/fetch';
import { type Handlers } from 'loot-core/src/types/handlers';
@@ -14,53 +14,85 @@ import { Setting } from './UI';
type Results = Awaited<ReturnType<Handlers['tools/fix-split-transactions']>>;
-function renderResults(results: Results) {
- const { numBlankPayees, numCleared, numDeleted } = results;
- let result = '';
+function useRenderResults() {
+ const { t } = useTranslation();
- if (numBlankPayees === 0 && numCleared === 0 && numDeleted === 0) {
- result = 'No split transactions found needing repair.';
- } else {
- if (numBlankPayees > 0) {
- result += `Fixed ${numBlankPayees} splits with a blank payee.`;
- }
- if (numCleared > 0) {
- if (result !== '') {
- result += '\n';
+ function renderResults(results: Results) {
+ const { numBlankPayees, numCleared, numDeleted, mismatchedSplits } =
+ results;
+ const result: string[] = [];
+
+ if (
+ numBlankPayees === 0 &&
+ numCleared === 0 &&
+ numDeleted === 0 &&
+ mismatchedSplits.length === 0
+ ) {
+ result.push(t('No split transactions found needing repair.'));
+ } else {
+ if (numBlankPayees > 0) {
+ result.push(
+ t('Fixed {{count}} splits with a blank payee.', {
+ count: numBlankPayees,
+ }),
+ );
}
- result += `Fixed ${numCleared} splits with the wrong cleared flag.`;
- }
- if (numDeleted > 0) {
- if (result !== '') {
- result += '\n';
+ if (numCleared > 0) {
+ result.push(
+ t('Fixed {{count}} splits with the wrong cleared flag.', {
+ count: numCleared,
+ }),
+ );
+ }
+ if (numDeleted > 0) {
+ result.push(
+ t('Fixed {{count}} splits that weren’t properly deleted.', {
+ count: numDeleted,
+ }),
+ );
+ }
+ if (mismatchedSplits.length > 0) {
+ const mismatchedSplitInfo = mismatchedSplits
+ .map(t => `- ${t.date}`)
+ .join('\n');
+
+ result.push(
+ t(
+ 'Found {{count}} split transactions with mismatched amounts on the below dates. Please review them manually:',
+ { count: mismatchedSplits.length },
+ ) + `\n${mismatchedSplitInfo}`,
+ );
}
- result += `Fixed ${numDeleted} splits that weren’t properly deleted.`;
}
+
+ return (
+ <Paragraph
+ style={{
+ color:
+ mismatchedSplits.length === 0
+ ? theme.noticeTextLight
+ : theme.errorText,
+ whiteSpace: 'pre-wrap',
+ }}
+ >
+ {result.join('\n')}
+ </Paragraph>
+ );
}
- return (
- <Paragraph
- style={{
- color: theme.noticeTextLight,
- marginBottom: 0,
- marginLeft: '1em',
- textAlign: 'right',
- whiteSpace: 'pre-wrap',
- }}
- >
- {result}
- </Paragraph>
- );
+ return { renderResults };
}
export function FixSplits() {
- const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [results, setResults] = useState<Results | null>(null);
+ const { renderResults } = useRenderResults();
+
async function onFix() {
setLoading(true);
const res = await send('tools/fix-split-transactions');
+
setResults(res);
setLoading(false);
}
@@ -70,38 +102,46 @@ export function FixSplits() {
primaryAction={
<View
style={{
- flexDirection: 'row',
- justifyContent: 'space-between',
- maxWidth: 500,
- width: '100%',
- alignItems: 'center',
+ flexDirection: 'column',
+ alignItems: 'flex-start',
+ gap: '1em',
}}
>
<ButtonWithLoading isLoading={loading} onPress={onFix}>
- {t('Repair split transactions')}
+ <Trans>Repair split transactions</Trans>
</ButtonWithLoading>
{results && renderResults(results)}
</View>
}
>
- <Text>
- <strong>{t('Repair split transactions')}</strong>
- {t(
- ' if you are experiencing bugs relating to split transactions and the “Reset budget cache” button above does not help, this tool may fix them. Some examples of bugs include seeing blank payees on splits or incorrect account balances. This tool does two things:',
- )}
- </Text>
- <ul style={{ margin: 0, paddingLeft: '1.5em' }}>
- <li style={{ marginBottom: '0.5em' }}>
- {t(
- 'Ensures that deleted split transactions are fully deleted. In previous versions of the app, certain split transactions may appear deleted but not all of them are actually deleted. This causes the transactions list to look correct, but certain balances may be incorrect when filtering.',
- )}
- </li>
- <li>
- {t(
- 'Sync the payee and cleared flag of a split transaction to the main or “parent” transaction, if appropriate. The payee will only be set if it currently doesn’t have one.',
- )}
- </li>
- </ul>
+ <Trans>
+ <Text>
+ <strong>Repair split transactions</strong> if you are experiencing
+ bugs relating to split transactions and the “Reset budget cache”
+ button above does not help, this tool may fix them. Some examples of
+ bugs include seeing blank payees on splits or incorrect account
+ balances. This tool does three things:
+ </Text>
+ <ul style={{ margin: 0, paddingLeft: '1.5em' }}>
+ <li style={{ marginBottom: '0.5em' }}>
+ Ensures that deleted split transactions are fully deleted. In
+ previous versions of the app, certain split transactions may appear
+ deleted but not all of them are actually deleted. This causes the
+ transactions list to look correct, but certain balances may be
+ incorrect when filtering.
+ </li>
+ <li>
+ Sync the payee and cleared flag of a split transaction to the main
+ or “parent” transaction, if appropriate. The payee will only be set
+ if it currently doesn’t have one.
+ </li>
+ <li>
+ Checks that the sum of all child transactions adds up to the total
+ amount. If not, these will be flagged below to allow you to easily
+ locate and fix the amounts.
+ </li>
+ </ul>
+ </Trans>
</Setting>
);
}
diff --git a/packages/loot-core/src/server/tools/app.ts b/packages/loot-core/src/server/tools/app.ts
index 31b48447a8..95cb254f9f 100644
--- a/packages/loot-core/src/server/tools/app.ts
+++ b/packages/loot-core/src/server/tools/app.ts
@@ -1,6 +1,8 @@
// @ts-strict-ignore
+import { q } from '../../shared/query';
import { batchUpdateTransactions } from '../accounts/transactions';
import { createApp } from '../app';
+import { runQuery } from '../aql';
import * as db from '../db';
import { runMutator } from '../mutators';
@@ -54,9 +56,27 @@ app.method('tools/fix-split-transactions', async () => {
await batchUpdateTransactions({ updated });
});
+ const splitTransactions = (
+ await runQuery(
+ q('transactions')
+ .options({ splits: 'grouped' })
+ .filter({
+ is_parent: true,
+ })
+ .select('*'),
+ )
+ ).data;
+
+ const mismatchedSplits = splitTransactions.filter(t => {
+ const subValue = t.subtransactions.reduce((acc, st) => acc + st.amount, 0);
+
+ return subValue !== t.amount;
+ });
+
return {
numBlankPayees: blankPayeeRows.length,
numCleared: clearedRows.length,
numDeleted: deletedRows.length,
+ mismatchedSplits,
};
});
diff --git a/packages/loot-core/src/server/tools/types/handlers.d.ts b/packages/loot-core/src/server/tools/types/handlers.d.ts
index 841db08c08..8a31465a36 100644
--- a/packages/loot-core/src/server/tools/types/handlers.d.ts
+++ b/packages/loot-core/src/server/tools/types/handlers.d.ts
@@ -1,7 +1,10 @@
+import { TransactionEntity } from './models';
+
export interface ToolsHandlers {
'tools/fix-split-transactions': () => Promise<{
numBlankPayees: number;
numCleared: number;
numDeleted: number;
+ mismatchedSplits: TransactionEntity[];
}>;
}
diff --git a/upcoming-release-notes/3970.md b/upcoming-release-notes/3970.md
new file mode 100644
index 0000000000..ec541471f6
--- /dev/null
+++ b/upcoming-release-notes/3970.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [matt-fidd]
+---
+
+Extend fix splits tool to report splits with mismatched amounts