22
33from flask import Blueprint , jsonify , request , render_template , current_app
44from flask_login import login_required
5- import sqlite3 , re
5+ import sqlite3
66from datetime import datetime ,timedelta , date
77from flask import request , jsonify
88import os , logging
@@ -281,7 +281,7 @@ def split_transaction():
281281
282282 # Fetch the original transaction details using TransactionKey
283283 cursor .execute (
284- "SELECT AccountID, AccountName, TransactionID, TransactionAmount, SubCategory, TransactionPosted, TransactionDescription, TransactionPayee FROM F_Transaction WHERE TransactionKey = ?" ,
284+ "SELECT AccountID, AccountName, TransactionID, COALESCE(TransactionAmountNew, TransactionAmount) AS TransactionAmount , SubCategory, TransactionPosted, TransactionDescription, TransactionPayee FROM F_Transaction WHERE TransactionKey = ?" ,
285285 (transaction_key ,)
286286 )
287287 original_transaction = cursor .fetchone ()
@@ -299,15 +299,38 @@ def split_transaction():
299299 if abs (split_amount ) >= abs (original_amount ) or abs (split_amount ) <= 0 :
300300 return jsonify ({"error" : "Invalid split amount!" }), 400
301301
302- # Check if the TransactionID ends with a _digit
303- match = re .match (r"(.+?)_(\d+)$" , original_transaction_id )
304- if match :
305- base_id = match .group (1 ) # Extract base part (before the last underscore)
306- max_suffix = int (match .group (2 )) # Get the current suffix number
307- new_transaction_id = f"{ base_id } _{ max_suffix + 1 } " # Increment suffix
302+ # Determine the base TransactionID (the part before the first underscore)
303+ if '_' in original_transaction_id :
304+ initial_id = original_transaction_id .split ('_' , 1 )[0 ]
308305 else :
309- base_id = original_transaction_id # No suffix, so it's the first split
310- new_transaction_id = f"{ base_id } _1" # Create the first split transaction
306+ initial_id = original_transaction_id
307+
308+ # Find the maximum existing suffix for this base transaction ID
309+ cursor .execute (
310+ "SELECT TransactionID FROM F_Transaction WHERE TransactionID LIKE ? || '_%'" ,
311+ (initial_id ,)
312+ )
313+ related_transactions = cursor .fetchall ()
314+
315+ max_suffix = 0
316+ for row in related_transactions :
317+ tx_id = row [0 ]
318+ try :
319+ # Extract the suffix after the first underscore
320+ suffix_str = tx_id .split ('_' , 1 )[1 ]
321+ # Check if the suffix is purely numeric
322+ if suffix_str .isdigit ():
323+ suffix = int (suffix_str )
324+ max_suffix = max (max_suffix , suffix )
325+ except (IndexError , ValueError ):
326+ # Handle cases where there's no suffix or it's not a simple number
327+ continue # Ignore this transaction ID as it doesn't fit the expected pattern
328+
329+ # Calculate the new suffix
330+ new_suffix = max_suffix + 1
331+
332+ # Construct the new transaction ID
333+ new_transaction_id = f"{ initial_id } _{ new_suffix } "
311334
312335 # Insert the new transaction for the split amount
313336 cursor .execute (
0 commit comments