From 3ceb2d92adfbfc09c378461a5e40c31d02fed88e Mon Sep 17 00:00:00 2001
From: shall0pass <20625555+shall0pass@users.noreply.github.com>
Date: Sat, 22 Apr 2023 10:44:07 -0500
Subject: [PATCH] [Feature] Initial concept for adding percentage based goals
 (#858)

This is an initial concept for adding percent based goal targets.

This version works by using a string in the form of:
#template 10% of Income <- Income is an income category name. Only
income category names will work here currently.
or
#template 10% of all income<- 'all income' is a keyword in this context
and will base the calculation on the total income for the month.

Some of the nicer touches like Jed's polite notification that the syntax
isn't correct is not implemented here yet.
---
 .../src/server/budget/goaltemplates.js        | 31 ++++++++++++-------
 upcoming-release-notes/858.md                 |  6 ++++
 2 files changed, 25 insertions(+), 12 deletions(-)
 create mode 100644 upcoming-release-notes/858.md

diff --git a/packages/loot-core/src/server/budget/goaltemplates.js b/packages/loot-core/src/server/budget/goaltemplates.js
index c09253588..285915fe3 100644
--- a/packages/loot-core/src/server/budget/goaltemplates.js
+++ b/packages/loot-core/src/server/budget/goaltemplates.js
@@ -337,19 +337,26 @@ async function applyCategoryTemplate(category, template_lines, month, force) {
         break;
       }
       case 'percentage': {
-        /*
-          let income_category = (await actual.getCategories()).filter(c => c.is_income == true && c.name == template.category);
-          let func = (getBudgetMonthTestFunc || getBudgetMonth);
-          let budget = await func(month);
-          for (var g = 0; g < budget.categoryGroups.length; g++) {
-            if (income_category.group_id == budget.categoryGroups[g].id) {
-              for (var c = 0; c < budget.categoryGroups[g].categories.length; c++)
-                if (income_category.id == budget.categoryGroups[g].categories[c].id) {
-                  let month_category = budget.categoryGroups[g].categories[c];
-                }
-            }
+        let percent = template.percent;
+        let monthlyIncome = 0;
+        if (template.category.toLowerCase() === 'all income') {
+          monthlyIncome = await getSheetValue(sheetName, `total-income`);
+        } else {
+          let income_category = (await db.getCategories()).find(
+            c =>
+              c.is_income &&
+              c.name.toLowerCase() === template.category.toLowerCase(),
+          );
+          if (!income_category) {
+            errors.push(`Could not find category “${template.category}”`);
+            return { errors };
           }
-          */
+          monthlyIncome = await getSheetValue(
+            sheetName,
+            `sum-amount-${income_category.id}`,
+          );
+        }
+        to_budget = Math.max(0, Math.round(monthlyIncome * (percent / 100)));
         break;
       }
       case 'error':
diff --git a/upcoming-release-notes/858.md b/upcoming-release-notes/858.md
new file mode 100644
index 000000000..38a4f140f
--- /dev/null
+++ b/upcoming-release-notes/858.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [shall0pass]
+---
+
+Goals: Added support for percentage driven targets
\ No newline at end of file
-- 
GitLab