diff --git a/app/routes/accounts.py b/app/routes/accounts.py index 570e02e..8c61447 100644 --- a/app/routes/accounts.py +++ b/app/routes/accounts.py @@ -137,3 +137,59 @@ def delete(id): db.session.commit() flash(f'Account "{account.name}" removed.', 'info') return redirect(url_for('accounts.index')) + + +@accounts_bp.route('//adjust', methods=['GET', 'POST']) +@login_required +def adjust_balance(id): + account = db.get_or_404(Account, id) + calc_balance(account.id) + db.session.refresh(account) + current_balance = float(account.balance) + + if request.method == 'POST': + from app.models.transaction import Transaction + from app.models.category import Category + from wtforms import ValidationError + + try: + new_balance = float(request.form.get('new_balance', '').replace(',', '')) + except (ValueError, AttributeError): + flash('Invalid amount entered.', 'danger') + return redirect(url_for('accounts.adjust_balance', id=id)) + + difference = round(new_balance - current_balance, 2) + + if difference == 0: + flash('Balance is already at that amount — no adjustment needed.', 'info') + return redirect(url_for('accounts.index', + tab='credit' if account.account_type == 'credit_card' else 'bank')) + + txn_type = 'income' if difference > 0 else 'expense' + amount = abs(difference) + notes = request.form.get('notes', '').strip() or 'Manual balance adjustment' + + txn = Transaction( + account_id=account.id, + transaction_type=txn_type, + amount=amount, + description='Balance Adjustment', + date=date.today(), + notes=notes, + ) + db.session.add(txn) + db.session.commit() + calc_balance(account.id) + + flash( + f'Balance adjusted by ' + f'{"+" if difference > 0 else ""}{difference:,.2f}. ' + f'New balance: {new_balance:,.2f}.', + 'success' + ) + return redirect(url_for('accounts.index', + tab='credit' if account.account_type == 'credit_card' else 'bank')) + + return render_template('accounts/adjust.html', + account=account, + current_balance=current_balance) diff --git a/app/templates/accounts/adjust.html b/app/templates/accounts/adjust.html new file mode 100644 index 0000000..d0f708c --- /dev/null +++ b/app/templates/accounts/adjust.html @@ -0,0 +1,107 @@ +{% extends "base.html" %} +{% block title %}Adjust Balance — {{ account.name }}{% endblock %} +{% block page_title %}Adjust Balance{% endblock %} + +{% block content %} +
+
+ +
+
+
+ +
+
+
{{ account.name }}
+
{{ account.account_type | replace('_',' ') | title }}
+
+
+
+
+
Current Balance
+
+ {{ current_balance | abs | currency }}{% if current_balance < 0 %} (owed){% endif %} +
+
+
+
+ +
+

+ Enter the correct balance. An adjustment transaction will be created for the difference to bring the account in line. +

+
+ + +
+ +
+ {{ current_user.currency_symbol }} + +
+
+ + + + +
+ + +
+ +
+ + Cancel +
+
+
+ +
+
+{% endblock %} + +{% block extra_js %} + +{% endblock %} diff --git a/app/templates/accounts/index.html b/app/templates/accounts/index.html index 2a8f6ba..d6ec190 100644 --- a/app/templates/accounts/index.html +++ b/app/templates/accounts/index.html @@ -43,6 +43,7 @@