Skip to content

A local-first personal finance app

✨ Filter account with 'on budget' or 'off budget' (#3891)

lelemm2b908e9263d3
diff --git a/packages/desktop-client/src/components/filters/FilterExpression.tsx b/packages/desktop-client/src/components/filters/FilterExpression.tsx
index 1970466899..d9f4ada455 100644
--- a/packages/desktop-client/src/components/filters/FilterExpression.tsx
+++ b/packages/desktop-client/src/components/filters/FilterExpression.tsx
@@ -85,17 +85,19 @@ export function FilterExpression<T extends RuleConditionEntity>({
{mapField(field, options)}
</Text>{' '}
<Text>{friendlyOp(op, null)}</Text>{' '}
- <Value
- value={value}
- field={field}
- inline={true}
- valueIsRaw={
- op === 'contains' ||
- op === 'matches' ||
- op === 'doesNotContain' ||
- op === 'hasTags'
- }
- />
+ {!['onbudget', 'offbudget'].includes(op?.toLocaleLowerCase()) && (
+ <Value
+ value={value}
+ field={field}
+ inline={true}
+ valueIsRaw={
+ op === 'contains' ||
+ op === 'matches' ||
+ op === 'doesNotContain' ||
+ op === 'hasTags'
+ }
+ />
+ )}
</>
)}
</div>
diff --git a/packages/desktop-client/src/components/filters/FiltersMenu.jsx b/packages/desktop-client/src/components/filters/FiltersMenu.jsx
index c6ec6b583d..17ce8e7a45 100644
--- a/packages/desktop-client/src/components/filters/FiltersMenu.jsx
+++ b/packages/desktop-client/src/components/filters/FiltersMenu.jsx
@@ -117,8 +117,18 @@ function ConfigureField({
}}
/>
) : (
- titleFirst(mapField(field))
+ <View
+ style={{
+ flexDirection: 'row',
+ width: '100%',
+ alignItems: 'center',
+ padding: 0,
+ }}
+ >
+ <View style={{ flexGrow: 1 }}>{titleFirst(mapField(field))}</View>
+ </View>
)}
+
<View style={{ flex: 1 }} />
</Stack>
</View>
@@ -222,6 +232,7 @@ function ConfigureField({
}
value={value}
multi={op === 'oneOf' || op === 'notOneOf'}
+ op={op}
style={{ marginTop: 10 }}
onChange={v => {
dispatch({ type: 'set-value', value: v });
diff --git a/packages/desktop-client/src/components/filters/updateFilterReducer.ts b/packages/desktop-client/src/components/filters/updateFilterReducer.ts
index 9772197331..3b8d92b67b 100644
--- a/packages/desktop-client/src/components/filters/updateFilterReducer.ts
+++ b/packages/desktop-client/src/components/filters/updateFilterReducer.ts
@@ -19,7 +19,9 @@ export function updateFilterReducer(
action.op === 'is' ||
action.op === 'doesNotContain' ||
action.op === 'isNot' ||
- action.op === 'hasTags')
+ action.op === 'hasTags' ||
+ action.op === 'onBudget' ||
+ action.op === 'offBudget')
) {
// Clear out the value if switching between contains or
// is/oneof for the id or string type
diff --git a/packages/desktop-client/src/components/modals/EditRuleModal.jsx b/packages/desktop-client/src/components/modals/EditRuleModal.jsx
index c30d9ef94e..e9bb720813 100644
--- a/packages/desktop-client/src/components/modals/EditRuleModal.jsx
+++ b/packages/desktop-client/src/components/modals/EditRuleModal.jsx
@@ -262,6 +262,7 @@ function ConditionEditor({
field={field}
type={type}
value={value}
+ op={op}
multi={op === 'oneOf' || op === 'notOneOf'}
onChange={v => onChange('value', v)}
numberFormatType="currency"
@@ -461,6 +462,7 @@ function ActionEditor({ action, editorStyle, onChange, onDelete, onAdd }) {
<GenericInput
key={inputKey}
field={field}
+ op={op}
type="number"
numberFormatType={
options.method === 'fixed-percent' ? 'percentage' : 'currency'
diff --git a/packages/desktop-client/src/components/rules/ConditionExpression.tsx b/packages/desktop-client/src/components/rules/ConditionExpression.tsx
index 229868a338..cc2d092bcc 100644
--- a/packages/desktop-client/src/components/rules/ConditionExpression.tsx
+++ b/packages/desktop-client/src/components/rules/ConditionExpression.tsx
@@ -49,7 +49,11 @@ export function ConditionExpression({
{prefix && <Text>{prefix} </Text>}
<Text style={valueStyle}>{mapField(field, options)}</Text>{' '}
<Text>{friendlyOp(op)}</Text>{' '}
- <Value style={valueStyle} value={value} field={field} inline={inline} />
+ {!['onbudget', 'offbudget'].includes(
+ (op as string)?.toLocaleLowerCase(),
+ ) && (
+ <Value style={valueStyle} value={value} field={field} inline={inline} />
+ )}
</View>
);
}
diff --git a/packages/desktop-client/src/components/util/GenericInput.jsx b/packages/desktop-client/src/components/util/GenericInput.jsx
index ed59442c0a..f718cdcb41 100644
--- a/packages/desktop-client/src/components/util/GenericInput.jsx
+++ b/packages/desktop-client/src/components/util/GenericInput.jsx
@@ -32,6 +32,7 @@ export function GenericInput({
inputRef,
style,
onChange,
+ op = undefined,
}) {
const { grouped: categoryGroups } = useCategories();
const { data: savedReports } = useReports();
@@ -100,18 +101,26 @@ export function GenericInput({
break;
case 'account':
- content = (
- <AccountAutocomplete
- type={autocompleteType}
- value={value}
- openOnFocus={true}
- onSelect={onChange}
- inputProps={{
- inputRef,
- ...(showPlaceholder ? { placeholder: 'nothing' } : null),
- }}
- />
- );
+ switch (op) {
+ case 'onBudget':
+ case 'offBudget':
+ content = null;
+ break;
+ default:
+ content = (
+ <AccountAutocomplete
+ type={autocompleteType}
+ value={value}
+ openOnFocus={true}
+ onSelect={onChange}
+ inputProps={{
+ inputRef,
+ ...(showPlaceholder ? { placeholder: 'nothing' } : null),
+ }}
+ />
+ );
+ break;
+ }
break;
case 'category':
diff --git a/packages/loot-core/src/server/accounts/rules.ts b/packages/loot-core/src/server/accounts/rules.ts
index 20f9ea2d21..449590370f 100644
--- a/packages/loot-core/src/server/accounts/rules.ts
+++ b/packages/loot-core/src/server/accounts/rules.ts
@@ -201,6 +201,8 @@ const CONDITION_TYPES = {
'doesNotContain',
'notOneOf',
'and',
+ 'onBudget',
+ 'offBudget',
],
nullable: true,
parse(op, value, fieldName) {
@@ -518,6 +520,21 @@ export class Condition {
console.log('invalid regexp in matches condition', e);
return false;
}
+
+ case 'onBudget':
+ if (!object._account) {
+ return false;
+ }
+
+ return object._account.offbudget === 0;
+
+ case 'offBudget':
+ if (!object._account) {
+ return false;
+ }
+
+ return object._account.offbudget === 1;
+
default:
}
@@ -948,6 +965,8 @@ const OP_SCORES: Record<RuleConditionEntity['op'], number> = {
doesNotContain: 0,
matches: 0,
hasTags: 0,
+ onBudget: 0,
+ offBudget: 0,
};
function computeScore(rule: Rule): number {
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index aedf1e4f80..42ff79b628 100644
--- a/packages/loot-core/src/server/accounts/sync.ts
+++ b/packages/loot-core/src/server/accounts/sync.ts
@@ -523,6 +523,9 @@ export async function matchTransactions(
);
// The first pass runs the rules, and preps data for fuzzy matching
+ const accounts: AccountEntity[] = await db.getAccounts();
+ const accountsMap = new Map(accounts.map(account => [account.id, account]));
+
const transactionsStep1 = [];
for (const {
payee_name,
@@ -530,7 +533,7 @@ export async function matchTransactions(
subtransactions,
} of normalized) {
// Run the rules
- const trans = await runRules(originalTrans);
+ const trans = await runRules(originalTrans, accountsMap);
let match = null;
let fuzzyDataset = null;
@@ -673,9 +676,12 @@ export async function addTransactions(
{ rawPayeeName: true },
);
+ const accounts: AccountEntity[] = await db.getAccounts();
+ const accountsMap = new Map(accounts.map(account => [account.id, account]));
+
for (const { trans: originalTrans, subtransactions } of normalized) {
// Run the rules
- const trans = await runRules(originalTrans);
+ const trans = await runRules(originalTrans, accountsMap);
const finalTransaction = {
id: uuidv4(),
diff --git a/packages/loot-core/src/server/accounts/transaction-rules.ts b/packages/loot-core/src/server/accounts/transaction-rules.ts
index cfa534ef7c..8a8d52c72b 100644
--- a/packages/loot-core/src/server/accounts/transaction-rules.ts
+++ b/packages/loot-core/src/server/accounts/transaction-rules.ts
@@ -13,10 +13,11 @@ import {
type TransactionEntity,
type RuleActionEntity,
type RuleEntity,
+ AccountEntity,
} from '../../types/models';
import { schemaConfig } from '../aql';
import * as db from '../db';
-import { getPayee, getPayeeByName, insertPayee } from '../db';
+import { getPayee, getPayeeByName, insertPayee, getAccount } from '../db';
import { getMappings } from '../db/mappings';
import { RuleError } from '../errors';
import { requiredFields, toDateRepr } from '../models';
@@ -274,8 +275,20 @@ function onApplySync(oldValues, newValues) {
}
// Runner
-export async function runRules(trans) {
- let finalTrans = await prepareTransactionForRules({ ...trans });
+export async function runRules(
+ trans,
+ accounts: Map<string, AccountEntity> | null = null,
+) {
+ let accountsMap = null;
+ if (accounts === null) {
+ accountsMap = new Map(
+ (await db.getAccounts()).map(account => [account.id, account]),
+ );
+ } else {
+ accountsMap = accounts;
+ }
+
+ let finalTrans = await prepareTransactionForRules({ ...trans }, accountsMap);
const rules = rankRules(
fastSetMerge(
@@ -559,6 +572,12 @@ export function conditionsToAQL(conditions, { recurDateBounds = 100 } = {}) {
return {
$and: getValue(value).map(subExpr => mapConditionToActualQL(subExpr)),
};
+
+ case 'onBudget':
+ return { 'account.offbudget': false };
+ case 'offBudget':
+ return { 'account.offbudget': true };
+
default:
throw new Error('Unhandled operator: ' + op);
}
@@ -608,8 +627,14 @@ export async function applyActions(
return null;
}
+ const accounts: AccountEntity[] = await db.getAccounts();
const transactionsForRules = await Promise.all(
- transactions.map(prepareTransactionForRules),
+ transactions.map(transactions =>
+ prepareTransactionForRules(
+ transactions,
+ new Map(accounts.map(account => [account.id, account])),
+ ),
+ ),
);
const updated = transactionsForRules.flatMap(trans => {
@@ -840,10 +865,12 @@ export async function updateCategoryRules(transactions) {
export type TransactionForRules = TransactionEntity & {
payee_name?: string;
+ _account?: AccountEntity;
};
export async function prepareTransactionForRules(
trans: TransactionEntity,
+ accounts: Map<string, AccountEntity> | null = null,
): Promise<TransactionForRules> {
const r: TransactionForRules = { ...trans };
if (trans.payee) {
@@ -853,6 +880,14 @@ export async function prepareTransactionForRules(
}
}
+ if (trans.account) {
+ if (accounts !== null && accounts.has(trans.account)) {
+ r._account = accounts.get(trans.account);
+ } else {
+ r._account = await getAccount(trans.account);
+ }
+ }
+
return r;
}
diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts
index 0893c2aadc..dc324fca85 100644
--- a/packages/loot-core/src/server/db/index.ts
+++ b/packages/loot-core/src/server/db/index.ts
@@ -473,6 +473,10 @@ export async function getPayee(id) {
return first(`SELECT * FROM payees WHERE id = ?`, [id]);
}
+export async function getAccount(id) {
+ return first(`SELECT * FROM accounts WHERE id = ?`, [id]);
+}
+
export async function insertPayee(payee) {
payee = payeeModel.validate(payee);
let id;
diff --git a/packages/loot-core/src/shared/rules.ts b/packages/loot-core/src/shared/rules.ts
index cbfe591964..82c837fe17 100644
--- a/packages/loot-core/src/shared/rules.ts
+++ b/packages/loot-core/src/shared/rules.ts
@@ -21,6 +21,8 @@ const TYPE_INFO = {
'isNot',
'doesNotContain',
'notOneOf',
+ 'onBudget',
+ 'offBudget',
],
nullable: true,
},
@@ -65,12 +67,16 @@ const FIELD_INFO = {
type: 'string',
disallowedOps: new Set(['hasTags']),
},
- payee: { type: 'id' },
+ payee: { type: 'id', disallowedOps: new Set(['onBudget', 'offBudget']) },
payee_name: { type: 'string' },
date: { type: 'date' },
notes: { type: 'string' },
amount: { type: 'number' },
- category: { type: 'id', internalOps: new Set(['and']) },
+ category: {
+ type: 'id',
+ disallowedOps: new Set(['onBudget', 'offBudget']),
+ internalOps: new Set(['and']),
+ },
account: { type: 'id' },
cleared: { type: 'boolean' },
reconciled: { type: 'boolean' },
@@ -199,6 +205,10 @@ export function friendlyOp(op, type?) {
return t('and');
case 'or':
return 'or';
+ case 'onBudget':
+ return 'is on budget';
+ case 'offBudget':
+ return 'is off budget';
default:
return '';
}
diff --git a/packages/loot-core/src/types/models/rule.d.ts b/packages/loot-core/src/types/models/rule.d.ts
index 5cbbe7d369..8d85f2c663 100644
--- a/packages/loot-core/src/types/models/rule.d.ts
+++ b/packages/loot-core/src/types/models/rule.d.ts
@@ -27,7 +27,9 @@ export type RuleConditionOp =
| 'doesNotContain'
| 'hasTags'
| 'and'
- | 'matches';
+ | 'matches'
+ | 'onBudget'
+ | 'offBudget';
type FieldValueTypes = {
account: string;
@@ -76,6 +78,8 @@ export type RuleConditionEntity =
| 'contains'
| 'doesNotContain'
| 'matches'
+ | 'onBudget'
+ | 'offBudget'
>
| BaseConditionEntity<
'category',
diff --git a/upcoming-release-notes/3891.md b/upcoming-release-notes/3891.md
new file mode 100644
index 0000000000..034344b1d1
--- /dev/null
+++ b/upcoming-release-notes/3891.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [lelemm]
+---
+
+Filter accounts when on budget or off budget