Skip to content

Commit

Permalink
Add partnered accounts (make a manual account track growth/losses fro…
Browse files Browse the repository at this point in the history
…m another account)
  • Loading branch information
grablair committed May 6, 2024
1 parent 8418d04 commit c8402cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions mint_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def run_auto_processor(args):
# Process auto-splits
mm.handle_auto_splits(config["auto_splits"])

if "account_growth_partners" in config:
# Sync account growth between partner accounts
mm.sync_account_growth(config["account_growth_partners"])

logger.info("Splitwise-Budgeting auto-processing via Monarch Money complete!")
else:
if (args.remote is None) != (args.remote_url is None):
Expand Down
55 changes: 47 additions & 8 deletions monarch_money_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,30 @@ def __init__(self, creds, db, session_file):
))

# Set up "Automated Transactions" account, if not present
logger.info("Fetching 'Automated Transactions' dummy account...")
logger.info("Fetching accounts...")
result = asyncio.run(self.mm.get_accounts())
filtered_accounts = list(filter(lambda x: x['displayName'] == "Automated Transactions", result['accounts']))

if len(filtered_accounts) == 0:
self.account_map = {}
for account in result['accounts']:
if account['displayName'] in self.account_map:
if account['displayName'] is "Automated Transactions":
logger.error("More than one account exists with the name 'Automated Transactions'. Please rename extra accounts with that name.")
sys.exit(1)
else:
logger.error(f"Multiple accounts with name '{category['name']}' exist. This may result in unintended behavior.")

self.account_map[account['displayName']] = account['id']

logger.debug(f"Accounts fetched: {self.account_map}")

if "Automated Transactions" not in self.account_map:
# TODO: set up account automatically
logger.error("No 'Automated Transactions' dummy account exists. Please create one.")
sys.exit(1)
elif len(filtered_accounts) > 1:
logger.error("More than one account exists with the name 'Automated Transactions'. Please rename extra accounts with that name.")
sys.exit(1)
else:
self.automated_account_id = filtered_accounts[0]['id']
self.automated_account_id = self.account_map['Automated Transactions']

logger.debug(f"'Automated Transactions' account fetched: {filtered_accounts[0]}")
logger.debug(f"'Automated Transactions' account found: {filtered_accounts[0]}")

# Set up the category map
logger.info("Fetching categories and setting up category -> id map...")
Expand Down Expand Up @@ -284,3 +293,33 @@ def handle_txn_in_auto_split(txn):

for txn in txns:
handle_txn_in_auto_split(txn)

def update_partner_account_balances(self, partner_account_mapping):
today = date.today()
yesterday = today - timedelta(days=1)

for partner_account_mapping in partner_account_mapping:
child_account = partner_account_mapping['child_account']
parent_account = partner_account_mapping['parent_account']
parent_account_history = asyncio.run(self.mm.get_account_history(self.account_map[parent_account]))

yesterday_balance = next(snapshot['signedBalance'] for snapshot in parent_account_history if snapshot['date'] == yesterday.strftime("%Y-%m-%d"))
today_balance = next(snapshot['signedBalance'] for snapshot in parent_account_history if snapshot['date'] == today.strftime("%Y-%m-%d"))

difference = today_balance - yesterday_balance

if difference != 0:
child_account_history = asyncio.run(self.mm.get_account_history(self.account_map[child_account]))
child_balance_yesterday = next(snapshot['signedBalance'] for snapshot in child_account_history if snapshot['date'] == yesterday.strftime("%Y-%m-%d"))

new_child_account_balance = child_balance_yesterday * (difference / yesterday_balance)

child_account_txns_today = asyncio.run(self.mm.get_transactions(
account_ids=[str(self.account_map[child_account])],
start_date=today.strftime("%Y-%m-%d"),
end_date=today.strftime("%Y-%m-%d")
))['allTransactions']['results']

new_child_account_balance += sum([txn['amount'] for txn in child_account_txns_today])

asyncio.run(self.mm.update_account(child_account, account_balance=new_child_account_balance))
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ recurrent==0.4.1
selenium==4.10.0
splitwise==3.0.0
SQLAlchemy==2.0.15
monarchmoney==0.1.10
monarchmoney @ git+https://github.com/grablair/monarchmoney@fc61c82
requests==2.31.0
2 changes: 2 additions & 0 deletions splitwise_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ def import_payments_from_budgeting_app(self, rules):
txns = list(filter(lambda txn: all([search not in txn['merchant']['name'] and search not in str(txn['notes']) for search in ["LOANPAYMENT", "LOANINTERESTPAYMENT"]]),
self.budgeting_app.search_transactions(search=rule['search_string'], limit=5)))

logger.info(f"Processing following payments: {txns}")

for txn in txns:
expenses = list(filter(lambda e: not e.getDeletedAt(), self.splitwise.getExpenses(
friend_id = user_id,
Expand Down

0 comments on commit c8402cb

Please sign in to comment.