From 770d86258fdc2697f9f16c055d70e29151360128 Mon Sep 17 00:00:00 2001
From: Julian Dominguez-Schatz <julian.dominguezschatz@gmail.com>
Date: Wed, 10 Apr 2024 23:11:54 -0400
Subject: [PATCH] Support splits in create-rule option on account page (#2569)

* Support splits in create-rule option on account page

* Add release notes
---
 .../src/components/accounts/Account.jsx       | 50 +++++++++++++++----
 upcoming-release-notes/2569.md                |  6 +++
 2 files changed, 47 insertions(+), 9 deletions(-)
 create mode 100644 upcoming-release-notes/2569.md

diff --git a/packages/desktop-client/src/components/accounts/Account.jsx b/packages/desktop-client/src/components/accounts/Account.jsx
index ac611444a..028295f3b 100644
--- a/packages/desktop-client/src/components/accounts/Account.jsx
+++ b/packages/desktop-client/src/components/accounts/Account.jsx
@@ -1056,18 +1056,24 @@ class AccountInternal extends PureComponent {
         .select('*')
         .options({ splits: 'grouped' }),
     );
+
     const transactions = ungroupTransactions(data);
-    const payeeCondition = transactions[0].imported_payee
+    const ruleTransaction = transactions[0];
+    const childTransactions = transactions.filter(
+      t => t.parent_id === ruleTransaction.id,
+    );
+
+    const payeeCondition = ruleTransaction.imported_payee
       ? {
           field: 'imported_payee',
           op: 'is',
-          value: transactions[0].imported_payee,
+          value: ruleTransaction.imported_payee,
           type: 'string',
         }
       : {
           field: 'payee',
           op: 'is',
-          value: transactions[0].payee,
+          value: ruleTransaction.payee,
           type: 'id',
         };
 
@@ -1076,12 +1082,38 @@ class AccountInternal extends PureComponent {
       conditionsOp: 'and',
       conditions: [payeeCondition],
       actions: [
-        {
-          op: 'set',
-          field: 'category',
-          value: transactions[0].category,
-          type: 'id',
-        },
+        ...(childTransactions.length === 0
+          ? [
+              {
+                op: 'set',
+                field: 'category',
+                value: ruleTransaction.category,
+                type: 'id',
+                options: {
+                  splitIndex: 0,
+                },
+              },
+            ]
+          : []),
+        ...childTransactions.flatMap((sub, index) => [
+          {
+            op: 'set-split-amount',
+            value: sub.amount,
+            options: {
+              splitIndex: index + 1,
+              method: 'fixed-amount',
+            },
+          },
+          {
+            op: 'set',
+            field: 'category',
+            value: sub.category,
+            type: 'id',
+            options: {
+              splitIndex: index + 1,
+            },
+          },
+        ]),
       ],
     };
 
diff --git a/upcoming-release-notes/2569.md b/upcoming-release-notes/2569.md
new file mode 100644
index 000000000..5d6e23010
--- /dev/null
+++ b/upcoming-release-notes/2569.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [jfdoming]
+---
+
+Support creating rules from split transactions on the accounts page
-- 
GitLab