Skip to content

A local-first personal finance app

Fix rounding of split rules (#4190)

Julian Dominguez-Schatz705985a8df6a
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