Fix running balances not being invalidated when posting account changed

This commit is contained in:
RunasSudo 2025-01-28 00:29:12 +11:00
parent 93e0466dd1
commit 064f50e668
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 29 additions and 6 deletions

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
Copyright (C) 20222025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -108,13 +108,14 @@
import { ref } from 'vue';
import { DT_FORMAT, Transaction, db, deserialiseAmount } from '../db.ts';
import { DT_FORMAT, Posting, Transaction, db, deserialiseAmount } from '../db.ts';
import ComboBoxAccounts from './ComboBoxAccounts.vue';
interface EditingPosting {
id: number | null,
description: string | null,
account: string,
originalAccount: string | null,
sign: string, // Keep track of Dr/Cr status so this can be independently changed in the UI
amount_abs: string,
}
@ -135,6 +136,7 @@
id: null,
description: null,
account: '',
originalAccount: null,
sign: posting.sign, // Create the new posting with the same sign as the entry clicked on
amount_abs: ''
});
@ -158,9 +160,10 @@
id: posting.id,
description: posting.description,
account: posting.account,
originalAccount: posting.originalAccount,
quantity: posting.sign === 'dr' ? amount_abs.quantity : -amount_abs.quantity,
commodity: amount_abs.commodity
});
} as Posting);
}
// Validate transaction
@ -278,6 +281,23 @@
WHERE postings.id = p.id`,
[newTransaction.dt, posting.account]
);
// Must also invalidate running balance of original account, if the account has changed
const originalAccount = (posting as unknown as EditingPosting).originalAccount;
if (originalAccount && originalAccount !== posting.account) {
await dbTransaction.execute(
`UPDATE postings
SET running_balance = NULL
FROM (
SELECT postings.id
FROM transactions
JOIN postings ON transactions.id = postings.transaction_id
WHERE DATE(dt) >= DATE($1) AND account = $2
) p
WHERE postings.id = p.id`,
[newTransaction.dt, (posting as unknown as EditingPosting).originalAccount]
);
}
}
await dbTransaction.commit();

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
Copyright (C) 20222025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -61,8 +61,9 @@
// Format dt
rawTransaction.dt = dayjs(rawTransaction.dt).format('YYYY-MM-DD');
// Initialise sign and amount_abs
// Initialise originalAccount, sign and amount_abs
for (const posting of rawTransaction.postings) {
posting.originalAccount = posting.account;
posting.sign = posting.quantity >= 0 ? 'dr' : 'cr';
posting.amount_abs = serialiseAmount(Math.abs(posting.quantity), posting.commodity);
}

View File

@ -1,6 +1,6 @@
<!--
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
Copyright (C) 20222025 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
@ -42,6 +42,7 @@
id: null,
description: null,
account: '',
originalAccount: null,
sign: 'dr',
amount_abs: '',
},
@ -49,6 +50,7 @@
id: null,
description: null,
account: '',
originalAccount: null,
sign: 'cr',
amount_abs: '',
}