diff --git a/CHANGELOG.md b/CHANGELOG.md index af4e88f..0e230e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/payments_config_example.yaml b/docs/payments_config_example.yaml index abc95eb..8a05d18 100644 --- a/docs/payments_config_example.yaml +++ b/docs/payments_config_example.yaml @@ -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 diff --git a/monzo_utils/lib/monzo_payments.py b/monzo_utils/lib/monzo_payments.py index 6499a82..e45a144 100755 --- a/monzo_utils/lib/monzo_payments.py +++ b/monzo_utils/lib/monzo_payments.py @@ -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) @@ -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) diff --git a/setup.py b/setup.py index 0f72286..0df7b03 100644 --- a/setup.py +++ b/setup.py @@ -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',