From 705985a8df6abd7dd1c51729490c4062949a1737 Mon Sep 17 00:00:00 2001 From: Julian Dominguez-Schatz Date: Sat, 18 Jan 2025 20:02:02 -0500 Subject: [PATCH] Fix rounding of split rules (#4190) * Fix rounding of split rules * Add release notes * PR feedback --- .../src/server/accounts/rules.test.ts | 37 +++++++++++++++++++ .../loot-core/src/server/accounts/rules.ts | 2 +- upcoming-release-notes/4190.md | 6 +++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 upcoming-release-notes/4190.md diff --git a/packages/loot-core/src/server/accounts/rules.test.ts b/packages/loot-core/src/server/accounts/rules.test.ts index 996cadc6b2..fa39f6424e 100644 --- a/packages/loot-core/src/server/accounts/rules.test.ts +++ b/packages/loot-core/src/server/accounts/rules.test.ts @@ -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 }), diff --git a/packages/loot-core/src/server/accounts/rules.ts b/packages/loot-core/src/server/accounts/rules.ts index b140819fff..e842fc08b5 100644 --- a/packages/loot-core/src/server/accounts/rules.ts +++ b/packages/loot-core/src/server/accounts/rules.ts @@ -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); } diff --git a/upcoming-release-notes/4190.md b/upcoming-release-notes/4190.md new file mode 100644 index 0000000000..3576a01219 --- /dev/null +++ b/upcoming-release-notes/4190.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [jfdoming] +--- + +Fix rounding of split rules