Skip to content

Commit

Permalink
Merge pull request #13 from Sha-yol/foreign-currency
Browse files Browse the repository at this point in the history
Foreign currency
  • Loading branch information
adyanth authored Oct 9, 2024
2 parents 2b723d5 + c5d7f6f commit 783a2df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ FIREFLY_DEFAULT_TRXFER_ACCOUNT=Charles Schwab
FIREFLY_DEFAULT_CATEGORY=Groceries
FIREFLY_DRY_RUN=true
SPLITWISE_DAYS=1
FOREIGN_CURRENCY_TOFIX_TAG=fixme/foreign-currency
38 changes: 37 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from splitwise import Splitwise, Expense, User, Comment
from splitwise.user import ExpenseUser
from typing import Generator, TypedDict
from functools import wraps

import os
import requests
Expand All @@ -28,6 +29,7 @@ def load_config() -> Config:
"FIREFLY_DEFAULT_TRXFR_ACCOUNT": os.getenv("FIREFLY_DEFAULT_TRXFR_ACCOUNT", "Chase Checking"),
"FIREFLY_DRY_RUN": bool(os.getenv("FIREFLY_DRY_RUN", True)),
"SPLITWISE_DAYS": int(os.getenv("SPLITWISE_DAYS", 1)),
"FOREIGN_CURRENCY_TOFIX_TAG": os.getenv("FOREIGN_CURRENCY_TOFIX_TAG")
}

time_now = datetime.now().astimezone()
Expand Down Expand Up @@ -348,18 +350,52 @@ def getExpenseTransactionBody(exp: Expense, myshare: ExpenseUser, data: list[str
"category_name": category,
"type": "withdrawal",
"amount": myshare.getOwedShare(),
"currency_code": exp.getCurrencyCode(),
"date": getDate(exp.getCreatedAt()).isoformat(),
"payment_date": getDate(exp.getDate()).isoformat(),
"description": description,
"reconciled": False,
"notes": notes,
"external_url": getSWUrlForExpense(exp),
}
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"]]
print(
f"Processing {category} {formatExpense(exp, myshare)} from {source} to {dest}")
return newTxn

def getAccounts(account_type: str="asset") -> list:
return callApi("accounts/", method="GET", params={"type": account_type}).json()['data']

def cache_account_currency(function):
account_name_currency = dict(
map(
lambda x: (x["attributes"]["name"], x["attributes"]["currency_code"]),
getAccounts("asset"),
)
)

@wraps(function)
def cached(account_name: str) -> str:
try:
return account_name_currency[account_name]
except KeyError:
raise ValueError(f"Account {account_name} not found in asset accounts.")

return cached

@cache_account_currency
def getAccountCurrencyCode(account_name: str) -> str:
"""Get the currency of an account on Firefly.
:param account: The account name
:return: The currency code
:raises: ValueError if the account is not found
"""
raise Exception("Will not be called")


if __name__ == "__main__":
"""
Expand Down

0 comments on commit 783a2df

Please sign in to comment.