From 1a3326d784de4853cca319ff84b7ff64a9b992d0 Mon Sep 17 00:00:00 2001 From: Julian Dominguez-Schatz Date: Sat, 18 Jan 2025 16:46:41 -0500 Subject: [PATCH 1/3] Fix rounding of split rules --- .../src/server/accounts/rules.test.ts | 31 +++++++++++++++++++ .../loot-core/src/server/accounts/rules.ts | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/loot-core/src/server/accounts/rules.test.ts b/packages/loot-core/src/server/accounts/rules.test.ts index 996cadc6b2a..9fd9076574c 100644 --- a/packages/loot-core/src/server/accounts/rules.test.ts +++ b/packages/loot-core/src/server/accounts/rules.test.ts @@ -677,6 +677,37 @@ describe('Rule', () => { ); }); + test('remainder rounds correctly', () => { + 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 }], + }, + ); + }); + 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 b140819fffb..e842fc08b5a 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); } From 0cb9fb82a19881dac2ec8a2c147389fb1634c6e8 Mon Sep 17 00:00:00 2001 From: Julian Dominguez-Schatz Date: Sat, 18 Jan 2025 16:50:48 -0500 Subject: [PATCH 2/3] Add release notes --- upcoming-release-notes/4190.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 upcoming-release-notes/4190.md diff --git a/upcoming-release-notes/4190.md b/upcoming-release-notes/4190.md new file mode 100644 index 00000000000..3576a01219a --- /dev/null +++ b/upcoming-release-notes/4190.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [jfdoming] +--- + +Fix rounding of split rules From 14bef9baf9ef78a548198ab069f77fc9afb12a4c Mon Sep 17 00:00:00 2001 From: Julian Dominguez-Schatz Date: Sat, 18 Jan 2025 17:07:08 -0500 Subject: [PATCH 3/3] PR feedback --- packages/loot-core/src/server/accounts/rules.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/loot-core/src/server/accounts/rules.test.ts b/packages/loot-core/src/server/accounts/rules.test.ts index 9fd9076574c..fa39f6424e8 100644 --- a/packages/loot-core/src/server/accounts/rules.test.ts +++ b/packages/loot-core/src/server/accounts/rules.test.ts @@ -677,7 +677,7 @@ describe('Rule', () => { ); }); - test('remainder rounds correctly', () => { + test('remainder rounds correctly and only if necessary', () => { const rule = new Rule({ conditionsOp: 'and', conditions: [{ op: 'is', field: 'imported_payee', value: 'James' }], @@ -706,6 +706,12 @@ describe('Rule', () => { 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', () => {