Skip to content

Commit

Permalink
Fix rounding of split rules (#4190)
Browse files Browse the repository at this point in the history
* Fix rounding of split rules

* Add release notes

* PR feedback
  • Loading branch information
jfdoming authored Jan 19, 2025
1 parent eb31071 commit 705985a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
37 changes: 37 additions & 0 deletions packages/loot-core/src/server/accounts/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/accounts/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4190.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [jfdoming]
---

Fix rounding of split rules

0 comments on commit 705985a

Please sign in to comment.