Skip to content

Commit

Permalink
YNAB refuses transactions older than 5 years
Browse files Browse the repository at this point in the history
  • Loading branch information
wesselt committed Feb 17, 2024
1 parent de63583 commit ba06874
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/bunq_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def set_callbacks(bunq_user_id, url_end, new_callback):
def get_user_id(user_name):
for u in bunq.get('v1/user'):
for k, v in u.items():
if (v["display_name"].casefold() == user_name.casefold() or
if k == "UserApiKey":
name = next(iter(v["requested_by_user"].values()))["display_name"]
else:
name = v["display_name"]

if (name.casefold() == user_name.casefold() or
str(v["id"]) == user_name):
return str(v["id"])
raise Exception("BUNQ user '{0}' not found".format(user_name))
Expand Down Expand Up @@ -192,4 +197,4 @@ def get_oauth_url(oauth_state, oauth_client_id, oauth_redirect_url):
# Encode the parameters
encoded_params = urlencode(params)
url = f"{base_url}?{encoded_params}"
return url
return url
13 changes: 9 additions & 4 deletions lib/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ def matching_pairs(bunq, ynab, conf):
return True


# YNAB does not accept transactions older than 5 years
def get_minimum_date():
min_dt = datetime.datetime.utcnow() - datetime.timedelta(days=5*365-1)
return min_dt.strftime("%Y-%m-%d")


def get_last_transaction_date(transactions):
for t in reversed(transactions):
# Ignore manually entered transactions (they're uncleared)
if (t["payee_name"] != "Starting Balance" and
t["cleared"] != "uncleared"):
return t["date"]
return "2000-01-01"

return get_minimum_date()

class Sync:

Expand Down Expand Up @@ -154,7 +159,7 @@ def synchronize_account(self, syncpair):
get_all = config.get("all")
get_start = config.get("start")
if get_all:
start_dt = "2000-01-01"
start_dt = get_minimum_date()
elif get_start:
start_dt = get_start
else:
Expand All @@ -166,7 +171,7 @@ def synchronize_account(self, syncpair):
syncpair["ynab_account_id"], start_dt)
log.info("Retrieved {} ynab transactions...".format(len(transactions)))

if not get_all and not get_start:
if not get_all and not get_start and transactions:
# Push start date back to latest uncleared YNAB entry
start_dt = get_last_transaction_date(transactions)
today_dt = datetime.datetime.utcnow().strftime("%Y-%m-%d")
Expand Down

0 comments on commit ba06874

Please sign in to comment.