From e348c36d7e561eea1b552b3dcd1bdd5275befce5 Mon Sep 17 00:00:00 2001 From: "LAPTOP-8G8AGL8G\\yotam" Date: Sun, 2 Mar 2025 23:30:44 +0200 Subject: [PATCH] bugfix: updating split transactions --- main.py | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index 8949c1c..1fc1f63 100644 --- a/main.py +++ b/main.py @@ -240,34 +240,45 @@ def updateTransaction(newTxn: dict, oldTxnBody: dict) -> None: """ old_id = oldTxnBody["id"] oldTxnBody = oldTxnBody["attributes"] - - for k, val in newTxn.items(): - if (old := oldTxnBody["transactions"][0][k]) != val: - # Firefly has a lot of 0 after decimal - if k == "amount" and float(old) == float(val): - continue - # Firefly stores time with timezone - # See https://github.com/firefly-iii/firefly-iii/issues/6810 - if k == "date" or k == "payment_date": - if getDate(old) == getDate(val): + oldTxns = oldTxnBody["transactions"] + newTxns: list[dict] = [newTxn] if isinstance(newTxn, dict) else newTxn + + if len(newTxns) > 1: + # order lists so that the first element is the one that is not the balance account transaction + cover_new = [txn for txn in newTxns if txn['description'].startswith('Cover for:')] + cover_old = [txn for txn in oldTxns if txn['description'].startswith('Cover for:')] + newTxns = [txn for txn in newTxns if not txn['description'].startswith('Cover for:')] + cover_new + oldTxns = [txn for txn in oldTxns if not txn['description'].startswith('Cover for:')] + cover_old + + for old, new in zip(oldTxns, newTxns): + for k, new_val in new.items(): + if (old_val := old[k]) != new_val: + # Firefly has a lot of 0 after decimal + if k == "amount" and float(old_val) == float(new_val): continue - break - else: - print(f"No update needed for {newTxn['description']}") - return + # Firefly stores time with timezone + # See https://github.com/firefly-iii/firefly-iii/issues/6810 + if k == "date" or k == "payment_date": + if getDate(old_val) == getDate(new_val): + continue + break + else: + print(f"No update needed for {new['description']}") + return - oldTxnBody["transactions"][0].update(newTxn) + old.update(new) - # https://github.com/firefly-iii/firefly-iii/issues/6828 - del oldTxnBody["transactions"][0]["foreign_currency_id"] + # https://github.com/firefly-iii/firefly-iii/issues/6828 + del old["foreign_currency_id"] + oldTxnBody["transactions"] = oldTxns + descriptions = ','.join([txn['description'] for txn in oldTxns]) try: callApi(f"transactions/{old_id}", method="PUT", body=oldTxnBody).json() except Exception as e: - print( - f"Transaction {newTxn['description']} errored, body: {oldTxnBody}, e: {e}") + print(f"Transactions {descriptions} errored, body: {oldTxnBody}, e: {e}") raise - print(f"Updated Transaction: {newTxn['description']}") + print(f"Updated Transactions: {descriptions}") def addTransaction(newTxn: Union[dict, list[dict]], group_title=None) -> None: