From a5df62825e26b52d81ad22dc3a498db6ec2c43d9 Mon Sep 17 00:00:00 2001 From: "LAPTOP-8G8AGL8G\\yotam" Date: Tue, 22 Oct 2024 00:59:30 +0300 Subject: [PATCH 1/2] Define function to apply amount to transaction Take into account the source account currency when applying the amount. --- main.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/main.py b/main.py index 4b1dc15..aa9991b 100644 --- a/main.py +++ b/main.py @@ -366,6 +366,24 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str f"Processing {category} {formatExpense(exp, myshare)} from {source} to {dest}") return newTxn +def applyExpenseAmountToTransaction(transaction: dict, exp: Expense, myshare: ExpenseUser) -> dict: + """Apply the amount to the transaction based on the currency of the account. + + :param transaction: The transaction dictionary + :param exp: The Splitwise expense + :param myshare: The user's share in the expense + :return: The updated transaction dictionary + """ + amount = myshare.getOwedShare() + if getAccountCurrencyCode(transaction["source_name"]) == exp.getCurrencyCode(): + transaction["amount"] = amount + else: + transaction["foreign_currency_code"] = exp.getCurrencyCode() + transaction["foreign_amount"] = amount + transaction["amount"] = 0.1 + transaction["tags"].append(conf["FOREIGN_CURRENCY_TOFIX_TAG"]) + + def getAccounts(account_type: str="asset") -> list: """Get accounts from Firefly. From 9f706fad6a07338569c3669d2357825a07428a54 Mon Sep 17 00:00:00 2001 From: "LAPTOP-8G8AGL8G\\yotam" Date: Tue, 22 Oct 2024 00:59:47 +0300 Subject: [PATCH 2/2] Use new function --- main.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index aa9991b..76c7d0c 100644 --- a/main.py +++ b/main.py @@ -349,19 +349,15 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str "destination_name": dest, "category_name": category, "type": "withdrawal", - "amount": myshare.getOwedShare(), "date": getDate(exp.getCreatedAt()).isoformat(), "payment_date": getDate(exp.getDate()).isoformat(), "description": description, "reconciled": False, "notes": notes, "external_url": getSWUrlForExpense(exp), + "tags": [], } - if getAccountCurrencyCode(source) != exp.getCurrencyCode(): - newTxn["foreign_currency_code"] = exp.getCurrencyCode() - newTxn["foreign_amount"] = myshare.getOwedShare() - newTxn["amount"] = 0.1 - newTxn["tags"] = [conf["FOREIGN_CURRENCY_TOFIX_TAG"]] + newTxn = applyExpenseAmountToTransaction(newTxn, exp, myshare) print( f"Processing {category} {formatExpense(exp, myshare)} from {source} to {dest}") return newTxn