@@ -123,8 +123,9 @@ def add_subcategory_rule():
123123 try :
124124 # Get form data from the request
125125 default_subcategory = request .form ['subcategoryDropDown' ]
126+ account_name = request .form .get ('accountNameDropDown' , 'ALL' ) # Get AccountName, default to 'ALL'
126127 rule_category = 'Payee'
127- rule_pattern = 'Contains'
128+ rule_pattern = request . form . get ( 'rulePatternDropDown' , 'Contains' ) # Get Rule_Pattern, default to 'Contains'
128129 match_word = request .form ['matchword' ]
129130
130131 # Connect to the SQLite database
@@ -133,12 +134,12 @@ def add_subcategory_rule():
133134
134135 # SQL query to insert the new rule
135136 insert_query = """
136- INSERT INTO D_Category_Rule (Default_SubCategory, Rule_Category, Rule_Pattern, Match_Word)
137- VALUES (?, ?, ?, ?)
137+ INSERT INTO D_Category_Rule (Default_SubCategory, AccountName, Rule_Category, Rule_Pattern, Match_Word)
138+ VALUES (?, ?, ?, ?, ? )
138139 """
139140
140141 # Execute the query with the form data
141- cursor .execute (insert_query , (default_subcategory , rule_category , rule_pattern , match_word ))
142+ cursor .execute (insert_query , (default_subcategory , account_name , rule_category , rule_pattern , match_word ))
142143 conn .commit () # Commit the transaction
143144 conn .close ()
144145
@@ -250,13 +251,14 @@ def subcategory_rule_data():
250251 SELECT
251252 RuleKey,
252253 Default_SubCategory,
254+ AccountName,
253255 Rule_Category,
254256 Rule_Pattern,
255257 Match_Word
256258 FROM
257- D_Category_Rule
259+ D_Category_Rule
258260 ORDER BY
259- 2,3,4,5 ASC
261+ AccountName ASC, Default_SubCategory ASC, Rule_Category ASC, Rule_Pattern ASC, Match_Word ASC
260262 """
261263 cursor .execute (subcategory_rule_query )
262264 subcategory_rule_data = cursor .fetchall ()
@@ -267,9 +269,10 @@ def subcategory_rule_data():
267269 {
268270 "RuleKey" : row [0 ],
269271 "Default_SubCategory" : row [1 ],
270- "Rule_Category" : row [2 ],
271- "Rule_Pattern" : row [3 ],
272- "Match_Word" : row [4 ],
272+ "AccountName" : row [2 ],
273+ "Rule_Category" : row [3 ],
274+ "Rule_Pattern" : row [4 ],
275+ "Match_Word" : row [5 ],
273276 }
274277 for row in subcategory_rule_data
275278 ]
@@ -288,15 +291,17 @@ def update_rule():
288291 try :
289292 RuleKey = request .form ["RuleKey" ]
290293 Match_Word = request .form ["Match_Word" ]
294+ AccountName = request .form .get ("AccountName" , "ALL" )
295+ Rule_Pattern = request .form .get ("Rule_Pattern" , "Contains" )
291296 conn = sqlite3 .connect (current_app .config ['DATABASE_PATH' ])
292297 cursor = conn .cursor ()
293298
294299 update_rule_query = """
295300 UPDATE D_Category_Rule
296- SET Match_Word = ?
301+ SET Match_Word = ?, AccountName = ?, Rule_Pattern = ?
297302 WHERE RuleKey = ?
298303 """
299- update_rule_data = (Match_Word , RuleKey )
304+ update_rule_data = (Match_Word , AccountName , Rule_Pattern , RuleKey )
300305
301306 cursor .execute (update_rule_query , update_rule_data )
302307 conn .commit ()
@@ -333,6 +338,36 @@ def delete_rule(RuleKey):
333338 return jsonify ({"error" : str (e )}), 500
334339
335340
341+ @manage_categories_bp .route ('/getAccountNames' )
342+ @login_required
343+ def account_names_data ():
344+ try :
345+ conn = sqlite3 .connect (current_app .config ['DATABASE_PATH' ])
346+ cursor = conn .cursor ()
347+
348+ account_names_query = """
349+ SELECT DISTINCT
350+ COALESCE(DisplayAccountName, AccountName)
351+ FROM
352+ F_Balance
353+ ORDER BY
354+ 1 ASC
355+ """
356+ cursor .execute (account_names_query )
357+ account_names = cursor .fetchall ()
358+ conn .close ()
359+
360+ account_names_list = [{"AccountName" : row [0 ]} for row in account_names ]
361+ account_names_list .insert (0 , {"AccountName" : "ALL" }) # Add 'ALL' option
362+
363+ return jsonify (account_names_list )
364+ except Exception as e :
365+ logger .error (f"An error occurred: { str (e )} " , exc_info = True )
366+ return jsonify ({"error" : str (e )}), 500
367+
368+
369+
370+
336371@manage_categories_bp .route ('/getAccountTypes' )
337372@login_required
338373def account_type_data ():
@@ -356,7 +391,7 @@ def account_type_data():
356391 conn .close ()
357392
358393 # Format the fetched data for JSON response
359- account_types_list = [
394+ account_types_list = [
360395 {"AccountType" : row [0 ], "HideFromBudget" : row [1 ], "SortOrder" : row [2 ]} for row in account_types
361396 ] # Assuming 'AccountType', 'HideFromBudget', 'SortOrder' are column names in your table
362397
0 commit comments