06/02 Update add new balance option
This commit is contained in:
+20
-2
@@ -2,8 +2,8 @@ from datetime import date
|
||||
from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify
|
||||
from flask_login import login_required
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, SelectField, TextAreaField, SubmitField
|
||||
from wtforms.validators import DataRequired, Length, Optional
|
||||
from wtforms import StringField, SelectField, TextAreaField, SubmitField, DecimalField
|
||||
from wtforms.validators import DataRequired, Length, Optional, NumberRange
|
||||
from sqlalchemy import func, extract
|
||||
from app.extensions import db
|
||||
from app.models.account import Account
|
||||
@@ -44,6 +44,8 @@ class AccountForm(FlaskForm):
|
||||
color = StringField('Color', default='#4F81C7')
|
||||
icon = StringField('Icon', default='bi-bank')
|
||||
notes = TextAreaField('Notes', validators=[Optional(), Length(max=500)])
|
||||
opening_balance = DecimalField('Opening Balance', places=2, default=0,
|
||||
validators=[Optional()])
|
||||
submit = SubmitField('Save')
|
||||
|
||||
|
||||
@@ -103,7 +105,23 @@ def new():
|
||||
balance=0,
|
||||
)
|
||||
db.session.add(account)
|
||||
db.session.flush() # get account.id before creating transaction
|
||||
|
||||
opening = float(form.opening_balance.data or 0)
|
||||
if opening != 0:
|
||||
from app.models.transaction import Transaction
|
||||
txn_type = 'income' if opening > 0 else 'expense'
|
||||
db.session.add(Transaction(
|
||||
account_id=account.id,
|
||||
transaction_type=txn_type,
|
||||
amount=abs(opening),
|
||||
description='Opening Balance',
|
||||
date=date.today(),
|
||||
notes='Initial account balance',
|
||||
))
|
||||
|
||||
db.session.commit()
|
||||
calc_balance(account.id)
|
||||
flash(f'Account "{account.name}" created.', 'success')
|
||||
return redirect(url_for('accounts.index'))
|
||||
return render_template('accounts/form.html', form=form, title='New Account',
|
||||
|
||||
Reference in New Issue
Block a user