Skip to content

Commit

Permalink
support auto topup of pots
Browse files Browse the repository at this point in the history
  • Loading branch information
m4rkw committed May 26, 2024
1 parent 43e5d92 commit 6aef05a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.3 - 26/05/2024

- support auto topup of pots

## 0.1.2 - 25/05/2024

- show all accounts in summary
Expand Down
7 changes: 7 additions & 0 deletions docs/payments_config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ payments_to_pots:
amount: 50
topup: true # if this is set top up the pot to the specified value

# top up pot if it drops below the threshold
# if topup_amount is omited it will be topped up to the threshold amount
pot_auto_topup:
- name: Travel
threshold: 20
topup_amount: 10

# indicate when a refund is pending
# this amount will be deducted from the total due in the salary period
# immediately after its due_after date
Expand Down
46 changes: 46 additions & 0 deletions monzo_utils/lib/monzo_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ def main(self):
if self.check_pot_payments():
sync_required = True

if self.check_pot_auto_topup():
sync_required = True

if sync_required:
ms = MonzoSync()
ms.sync(3, self.account)
Expand Down Expand Up @@ -760,6 +763,49 @@ def check_pot_payments(self):
return sync_required


def check_pot_auto_topup(self):
m = None
sync_required = False

if 'pot_auto_topup' not in self.config or type(self.config['pot_auto_topup']) != list:
return sync_required

for payment in self.config['pot_auto_topup']:
pot = Pot.one("select * from pot where name = %s and deleted = %s", [payment['name'], 0])

if not pot:
continue

if pot.balance < payment['threshold']:
if 'topup_amount' not in payment:
amount_to_transfer = int(payment['threshold'] * 100) - int(pot.balance * 100)
else:
amount_to_transfer = int(payment['topup_amount'] * 100)

if not m:
m = MonzoAPI()
sys.stdout.write("\n")

sys.stdout.write("Topping up pot %s with £%.2f ... " % (pot.name, amount_to_transfer / 100))
sys.stdout.flush()

if m.deposit_to_pot(self.account.account_id, pot, amount_to_transfer / 100):
sys.stdout.write("OK\n")

pot.last_monthly_transfer_date = self.last_salary_date
pot.save()

sync_required = True

self.notify(pot.name, "Topped up by £%.2f\nNew balance: £%.2f" % (amount_to_transfer / 100, float(pot.balance) + (amount_to_transfer / 100)))
else:
sys.stdout.write("FAILED\n\n")

sys.stderr.write("ERROR: failed to transfer £%.2f to pot: %s\n" % (amount_to_transfer / 100, pot.name))

return sync_required


def get_transfer_amount(self, pot, payment):
amount = round(payment['amount'] * 100)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup

setup(name='monzo-utils',
version='0.1.2',
version='0.1.3',
description='Monzo Utils',
author='Mark Wadham',
url='https://github.com/m4rkw/monzo-utils',
Expand Down

0 comments on commit 6aef05a

Please sign in to comment.