Fix rounding of split rules (#4190)

* Fix rounding of split rules

* Add release notes

* PR feedback
This commit is contained in:
Julian Dominguez-Schatz
2025-01-18 20:02:02 -05:00
committed by GitHub
parent eb31071043
commit 705985a8df
3 changed files with 44 additions and 1 deletions

View File

@@ -677,6 +677,43 @@ describe('Rule', () => {
);
});
test('remainder rounds correctly and only if necessary', () => {
const rule = new Rule({
conditionsOp: 'and',
conditions: [{ op: 'is', field: 'imported_payee', value: 'James' }],
actions: [
{
op: 'set-split-amount',
field: 'amount',
options: { splitIndex: 1, method: 'remainder' },
},
{
op: 'set-split-amount',
field: 'amount',
options: { splitIndex: 2, method: 'remainder' },
},
],
});
expect(
rule.exec({ imported_payee: 'James', amount: -2397 }),
).toMatchObject({
subtransactions: [{ amount: -1198 }, { amount: -1199 }],
});
expect(rule.exec({ imported_payee: 'James', amount: 123 })).toMatchObject(
{
subtransactions: [{ amount: 62 }, { amount: 61 }],
},
);
expect(rule.exec({ imported_payee: 'James', amount: 100 })).toMatchObject(
{
subtransactions: [{ amount: 50 }, { amount: 50 }],
},
);
});
test('generate errors when fixed amounts exceed the total', () => {
expect(
fixedAmountRule.exec({ imported_payee: 'James', amount: 100 }),

View File

@@ -753,7 +753,7 @@ function execSplitActions(actions: Action[], transaction) {
});
// The last remainder split will be adjusted for any leftovers from rounding.
newTransactions[lastNonFixedTransactionIndex].amount -=
newTransactions[lastNonFixedTransactionIndex].amount +=
getSplitRemainder(newTransactions);
}

View File

@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [jfdoming]
---
Fix rounding of split rules