2022-12-24 12:31:45 +11:00
|
|
|
# DrCr: Web-based double-entry bookkeeping framework
|
2024-04-03 21:08:52 +11:00
|
|
|
# Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
|
2022-12-24 12:31:45 +11:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
from flask import abort, redirect, render_template, request, url_for
|
2022-12-24 12:31:45 +11:00
|
|
|
|
2022-12-24 13:22:18 +11:00
|
|
|
from .. import AMOUNT_DPS
|
2023-01-02 16:59:12 +11:00
|
|
|
from ..database import db
|
2023-01-27 22:19:08 +11:00
|
|
|
from ..models import Amount, Posting, Transaction
|
2024-04-06 01:42:34 +11:00
|
|
|
from ..webapp import all_accounts, app
|
2023-01-02 18:57:25 +11:00
|
|
|
from .models import StatementLine, StatementLineReconciliation
|
2022-12-24 12:31:45 +11:00
|
|
|
|
2023-01-27 22:19:08 +11:00
|
|
|
from datetime import datetime
|
|
|
|
|
2022-12-24 12:31:45 +11:00
|
|
|
@app.route('/statement-lines')
|
|
|
|
def statement_lines():
|
2023-01-05 01:15:39 +11:00
|
|
|
# JOIN all associated postings (called in is_complex/charge_account)
|
|
|
|
statement_lines = db.select(StatementLine).options(
|
|
|
|
db.joinedload(StatementLine.reconciliation).joinedload(StatementLineReconciliation.posting).joinedload(Posting.transaction).joinedload(Transaction.postings)
|
2024-04-04 01:53:46 +11:00
|
|
|
).order_by(StatementLine.dt.desc(), StatementLine.id.desc())
|
2023-01-05 01:15:39 +11:00
|
|
|
|
2022-12-24 15:14:19 +11:00
|
|
|
if 'account' in request.args:
|
2023-01-05 01:15:39 +11:00
|
|
|
statement_lines = statement_lines.where(StatementLine.source_account == request.args['account'])
|
2022-12-24 15:14:19 +11:00
|
|
|
|
2023-05-28 12:08:31 +10:00
|
|
|
if request.args.get('unclassified', '0') == '1':
|
|
|
|
statement_lines = statement_lines.where(StatementLine.reconciliation == None)
|
|
|
|
|
2023-01-05 01:15:39 +11:00
|
|
|
page = db.paginate(statement_lines, per_page=int(request.args.get('per_page', 1000)))
|
2023-01-04 18:23:25 +11:00
|
|
|
|
2024-04-06 01:42:34 +11:00
|
|
|
return render_template('statements/statement_lines.html', page=page, all_accounts=all_accounts())
|
2022-12-24 12:31:45 +11:00
|
|
|
|
|
|
|
@app.route('/statement-lines/charge', methods=['POST'])
|
|
|
|
def statement_line_charge():
|
2023-01-02 16:59:12 +11:00
|
|
|
statement_line = db.session.get(StatementLine, request.form['line-id'])
|
2022-12-24 12:31:45 +11:00
|
|
|
if not statement_line:
|
|
|
|
abort(404)
|
|
|
|
|
2023-01-02 18:57:25 +11:00
|
|
|
if statement_line.reconciliation:
|
|
|
|
raise Exception('NYI')
|
2022-12-24 12:31:45 +11:00
|
|
|
|
2023-01-02 18:57:25 +11:00
|
|
|
# Create transaction
|
|
|
|
posting_source = Posting(account=statement_line.source_account, quantity=statement_line.quantity, commodity=statement_line.commodity)
|
|
|
|
posting_charge = Posting(account=request.form['charge-account'], quantity=-statement_line.quantity, commodity=statement_line.commodity)
|
|
|
|
transaction = Transaction(
|
2022-12-24 12:31:45 +11:00
|
|
|
dt=statement_line.dt,
|
|
|
|
description=statement_line.description,
|
2023-01-02 18:57:25 +11:00
|
|
|
postings=[posting_source, posting_charge]
|
2022-12-24 12:31:45 +11:00
|
|
|
)
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.add(transaction)
|
2023-01-02 18:57:25 +11:00
|
|
|
|
|
|
|
# Reconcile statement line
|
|
|
|
reconciliation = StatementLineReconciliation(
|
|
|
|
statement_line=statement_line,
|
|
|
|
posting=posting_source
|
|
|
|
)
|
|
|
|
db.session.add(reconciliation)
|
|
|
|
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.commit()
|
2022-12-24 12:31:45 +11:00
|
|
|
|
|
|
|
return 'OK'
|
|
|
|
|
2023-01-27 22:19:08 +11:00
|
|
|
@app.route('/statement-lines/charge-complex', methods=['GET', 'POST'])
|
|
|
|
def statement_line_charge_complex():
|
|
|
|
statement_line = db.session.get(StatementLine, request.args['line-id'])
|
|
|
|
if not statement_line:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if statement_line.reconciliation:
|
|
|
|
raise Exception('Cannot reconcile an already reconciled statement line')
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
# Create template transaction
|
|
|
|
posting_source = Posting(account=statement_line.source_account, quantity=statement_line.quantity, commodity=statement_line.commodity)
|
|
|
|
posting_charge = Posting(account='', quantity=-statement_line.quantity, commodity=statement_line.commodity)
|
|
|
|
transaction = Transaction(
|
|
|
|
dt=statement_line.dt,
|
|
|
|
description=statement_line.description,
|
|
|
|
postings=[posting_source, posting_charge]
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template('journal/journal_edit_transaction.html', transaction=transaction)
|
|
|
|
|
|
|
|
# New transaction
|
|
|
|
transaction = Transaction(
|
|
|
|
dt=datetime.strptime(request.form['dt'], '%Y-%m-%d'),
|
|
|
|
description=request.form['description'],
|
|
|
|
postings=[]
|
|
|
|
)
|
|
|
|
|
|
|
|
for account, sign, amount_str in zip(request.form.getlist('account'), request.form.getlist('sign'), request.form.getlist('amount')):
|
|
|
|
amount = Amount.parse(amount_str)
|
|
|
|
if sign == 'cr':
|
|
|
|
amount = -amount
|
|
|
|
|
|
|
|
posting = Posting(
|
|
|
|
account=account,
|
|
|
|
quantity=amount.quantity,
|
|
|
|
commodity=amount.commodity
|
|
|
|
)
|
|
|
|
transaction.postings.append(posting)
|
|
|
|
|
|
|
|
transaction.assert_valid()
|
|
|
|
db.session.add(transaction)
|
|
|
|
|
|
|
|
# Reconcile statement line
|
|
|
|
posting_source = next((p for p in transaction.postings if p.account == statement_line.source_account and p.quantity == statement_line.quantity and p.commodity == statement_line.commodity), None)
|
|
|
|
if not posting_source:
|
|
|
|
raise Exception('Transaction must have posting corresponding to statement line')
|
|
|
|
|
|
|
|
reconciliation = StatementLineReconciliation(
|
|
|
|
statement_line=statement_line,
|
|
|
|
posting=posting_source
|
|
|
|
)
|
|
|
|
db.session.add(reconciliation)
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return redirect(url_for('statement_lines'))
|
|
|
|
|
2022-12-24 16:16:47 +11:00
|
|
|
@app.route('/statement-lines/reconcile-transfer', methods=['POST'])
|
|
|
|
def statement_line_reconcile_transfer():
|
|
|
|
line_ids = request.form.getlist('sel-line-id')
|
|
|
|
if len(line_ids) != 2:
|
|
|
|
raise Exception('Must select exactly 2 statement lines')
|
|
|
|
|
2023-01-02 16:59:12 +11:00
|
|
|
line1 = db.session.get(StatementLine, line_ids[0])
|
|
|
|
line2 = db.session.get(StatementLine, line_ids[1])
|
2022-12-24 16:16:47 +11:00
|
|
|
|
|
|
|
# Check same amount
|
|
|
|
if line1.quantity != -line2.quantity or line1.commodity != line2.commodity:
|
|
|
|
raise Exception('Selected statement line debits/credits must equal')
|
|
|
|
|
2023-01-29 19:18:30 +11:00
|
|
|
if line1.reconciliation or line2.reconciliation:
|
|
|
|
raise Exception('NYI')
|
2022-12-24 16:16:47 +11:00
|
|
|
|
2023-01-29 19:18:30 +11:00
|
|
|
# Create transaction
|
|
|
|
posting1 = Posting(account=line1.source_account, quantity=line1.quantity, commodity=line1.commodity)
|
|
|
|
posting2 = Posting(account=line2.source_account, quantity=line2.quantity, commodity=line2.commodity)
|
|
|
|
transaction = Transaction(
|
2022-12-24 16:16:47 +11:00
|
|
|
dt=line1.dt,
|
|
|
|
description=line1.description,
|
2023-01-29 19:18:30 +11:00
|
|
|
postings=[posting1, posting2]
|
2022-12-24 16:16:47 +11:00
|
|
|
)
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.add(transaction)
|
2023-01-29 19:18:30 +11:00
|
|
|
|
|
|
|
# Reconcile statement lines
|
|
|
|
db.session.add(StatementLineReconciliation(
|
|
|
|
statement_line=line1,
|
|
|
|
posting=posting1
|
|
|
|
))
|
|
|
|
db.session.add(StatementLineReconciliation(
|
|
|
|
statement_line=line2,
|
|
|
|
posting=posting2
|
|
|
|
))
|
|
|
|
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.commit()
|
2022-12-24 16:16:47 +11:00
|
|
|
|
2023-05-28 12:12:04 +10:00
|
|
|
return redirect(request.referrer or url_for('statement_lines'))
|
2023-01-02 18:50:49 +11:00
|
|
|
|
|
|
|
@app.route('/statement-lines/import', methods=['GET', 'POST'])
|
|
|
|
def statement_lines_import():
|
|
|
|
if request.method == 'GET':
|
2024-04-06 01:42:34 +11:00
|
|
|
return render_template('statements/import.html', all_accounts=all_accounts())
|
2023-01-02 18:50:49 +11:00
|
|
|
|
|
|
|
# Import using importer
|
|
|
|
if request.form['format'] == 'ofx1':
|
|
|
|
from .importers.ofx1 import import_ofx1
|
|
|
|
statement_lines = import_ofx1(request.files['file'])
|
2023-01-04 18:37:05 +11:00
|
|
|
elif request.form['format'] == 'ofx2':
|
|
|
|
from .importers.ofx2 import import_ofx2
|
|
|
|
statement_lines = import_ofx2(request.files['file'])
|
2023-01-02 18:50:49 +11:00
|
|
|
else:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
# Fill in source_account
|
|
|
|
for statement_line in statement_lines:
|
|
|
|
statement_line.source_account = request.form['source-account']
|
|
|
|
|
2024-04-03 21:08:52 +11:00
|
|
|
# Ignore pending, etc.
|
|
|
|
# FIXME: This needs to be customisable
|
|
|
|
statement_lines = [l for l in statement_lines if 'PENDING' not in l.description]
|
|
|
|
|
2023-01-02 18:50:49 +11:00
|
|
|
if request.form['action'] == 'preview':
|
|
|
|
return render_template('statements/import_preview.html', statement_lines=statement_lines)
|
|
|
|
|
|
|
|
# Add to database
|
|
|
|
for statement_line in statement_lines:
|
|
|
|
db.session.add(statement_line)
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
return redirect(url_for('statement_lines'))
|