DrCr/drcr/statements/views.py

90 lines
3.3 KiB
Python

# DrCr: Web-based double-entry bookkeeping framework
# Copyright (C) 2022 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import abort, redirect, render_template, request
from ..database import db_session
from ..webapp import app
from .models import StatementLine, StatementLinePosting, StatementLineTransaction
@app.route('/statement-lines')
def statement_lines():
return render_template('statement_lines.html', statement_lines=StatementLine.query.all())
@app.route('/statement-lines/charge', methods=['POST'])
def statement_line_charge():
statement_line = db_session.get(StatementLine, request.form['line-id'])
if not statement_line:
abort(404)
# Delete existing postings
if len(statement_line.postings) > 0:
for posting in statement_line.postings:
if len(posting.transaction.postings) > 2:
# Complex posting
raise Exception('Cannot automatically delete a StatementLineTransaction with >2 postings')
# Queue for deletion
db_session.delete(posting.transaction)
transaction = StatementLineTransaction(
dt=statement_line.dt,
description=statement_line.description,
postings=[
StatementLinePosting(statement_line=statement_line, account=statement_line.source_account, quantity=statement_line.quantity, commodity=statement_line.commodity),
StatementLinePosting(account=request.form['charge-account'], quantity=-statement_line.quantity, commodity=statement_line.commodity)
]
)
db_session.add(transaction)
db_session.commit()
return 'OK'
@app.route('/statement-lines/edit-transaction', methods=['GET', 'POST'])
def statement_line_edit_transaction():
statement_line = db_session.get(StatementLine, request.args['line-id'])
if not statement_line:
abort(404)
if request.method == 'GET':
# FIXME: Show existing transaction details
return render_template('statement_line_edit_transaction.html', statement_line=statement_line)
# Delete existing postings
if len(statement_line.postings) > 0:
for posting in statement_line.postings:
if len(posting.transaction.postings) > 2:
# Complex posting
raise Exception('Cannot automatically delete a StatementLineTransaction with >2 postings')
# Queue for deletion
db_session.delete(posting.transaction)
transaction = StatementLineTransaction(
dt=statement_line.dt,
description=request.form['description'],
postings=[
StatementLinePosting(statement_line=statement_line, account=statement_line.source_account, quantity=statement_line.quantity, commodity=statement_line.commodity),
StatementLinePosting(account=request.form['charge-account'], quantity=-statement_line.quantity, commodity=statement_line.commodity)
]
)
db_session.add(transaction)
db_session.commit()
return redirect('/statement-lines')