Skip to content

Commit 00dd48e

Browse files
authored
bugfix: updating split transactions (#29)
1 parent 5b28896 commit 00dd48e

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

main.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -240,34 +240,45 @@ def updateTransaction(newTxn: dict, oldTxnBody: dict) -> None:
240240
"""
241241
old_id = oldTxnBody["id"]
242242
oldTxnBody = oldTxnBody["attributes"]
243-
244-
for k, val in newTxn.items():
245-
if (old := oldTxnBody["transactions"][0][k]) != val:
246-
# Firefly has a lot of 0 after decimal
247-
if k == "amount" and float(old) == float(val):
248-
continue
249-
# Firefly stores time with timezone
250-
# See https://github.com/firefly-iii/firefly-iii/issues/6810
251-
if k == "date" or k == "payment_date":
252-
if getDate(old) == getDate(val):
243+
oldTxns = oldTxnBody["transactions"]
244+
newTxns: list[dict] = [newTxn] if isinstance(newTxn, dict) else newTxn
245+
246+
if len(newTxns) > 1:
247+
# order lists so that the first element is the one that is not the balance account transaction
248+
cover_new = [txn for txn in newTxns if txn['description'].startswith('Cover for:')]
249+
cover_old = [txn for txn in oldTxns if txn['description'].startswith('Cover for:')]
250+
newTxns = [txn for txn in newTxns if not txn['description'].startswith('Cover for:')] + cover_new
251+
oldTxns = [txn for txn in oldTxns if not txn['description'].startswith('Cover for:')] + cover_old
252+
253+
for old, new in zip(oldTxns, newTxns):
254+
for k, new_val in new.items():
255+
if (old_val := old[k]) != new_val:
256+
# Firefly has a lot of 0 after decimal
257+
if k == "amount" and float(old_val) == float(new_val):
253258
continue
254-
break
255-
else:
256-
print(f"No update needed for {newTxn['description']}")
257-
return
259+
# Firefly stores time with timezone
260+
# See https://github.com/firefly-iii/firefly-iii/issues/6810
261+
if k == "date" or k == "payment_date":
262+
if getDate(old_val) == getDate(new_val):
263+
continue
264+
break
265+
else:
266+
print(f"No update needed for {new['description']}")
267+
return
258268

259-
oldTxnBody["transactions"][0].update(newTxn)
269+
old.update(new)
260270

261-
# https://github.com/firefly-iii/firefly-iii/issues/6828
262-
del oldTxnBody["transactions"][0]["foreign_currency_id"]
271+
# https://github.com/firefly-iii/firefly-iii/issues/6828
272+
del old["foreign_currency_id"]
263273

274+
oldTxnBody["transactions"] = oldTxns
275+
descriptions = ','.join([txn['description'] for txn in oldTxns])
264276
try:
265277
callApi(f"transactions/{old_id}", method="PUT", body=oldTxnBody).json()
266278
except Exception as e:
267-
print(
268-
f"Transaction {newTxn['description']} errored, body: {oldTxnBody}, e: {e}")
279+
print(f"Transactions {descriptions} errored, body: {oldTxnBody}, e: {e}")
269280
raise
270-
print(f"Updated Transaction: {newTxn['description']}")
281+
print(f"Updated Transactions: {descriptions}")
271282

272283

273284
def addTransaction(newTxn: Union[dict, list[dict]], group_title=None) -> None:

0 commit comments

Comments
 (0)