Implement OFX 1.x import

This commit is contained in:
RunasSudo 2023-01-02 18:50:49 +11:00
parent dbc2bca8be
commit 33cf00ecec
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
9 changed files with 182 additions and 3 deletions

View File

View File

View File

@ -0,0 +1,57 @@
# DrCr: Web-based double-entry bookkeeping framework
# Copyright (C) 2022–2023 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/>.
import lxml.etree as ET
from ..models import StatementLine
from datetime import datetime
from io import StringIO
def import_ofx1(file):
raw_ofx = file.read().decode('utf-8')
# Convert OFX header to XML and parse
raw_payload = raw_ofx[raw_ofx.index('<OFX>'):]
xml_input = StringIO(raw_payload.replace('&', '&amp;'))
try:
tree = ET.parse(xml_input, ET.HTMLParser())
except Exception as ex:
raise ex
root = tree.getroot()
# Read transactions
lines = [] # Do first pass to catch "extra description lines"
for transaction in root.find('.//banktranlist').findall('.//stmttrn'):
date = transaction.find('.//dtposted').text
date = date[0:4] + '-' + date[4:6] + '-' + date[6:8]
description = transaction.find('.//memo').text.strip()
amount = transaction.find('.//trnamt').text.strip()
lines.append([date, description, amount, []])
imported_statement_lines = []
# Import
for date, description, amount, notes in lines:
imported_statement_lines.append(StatementLine(
dt=datetime.strptime(date, '%Y-%m-%d'),
description=description,
quantity=round(float(amount)*100),
commodity='$'
))
return imported_statement_lines

View File

@ -82,3 +82,29 @@ def statement_line_reconcile_transfer():
db.session.commit() db.session.commit()
return redirect('/statement-lines') return redirect('/statement-lines')
@app.route('/statement-lines/import', methods=['GET', 'POST'])
def statement_lines_import():
if request.method == 'GET':
return render_template('statements/import.html')
# Import using importer
if request.form['format'] == 'ofx1':
from .importers.ofx1 import import_ofx1
statement_lines = import_ofx1(request.files['file'])
else:
abort(400)
# Fill in source_account
for statement_line in statement_lines:
statement_line.source_account = request.form['source-account']
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()
return redirect('/statement-lines')

View File

@ -49,6 +49,7 @@
</div> </div>
{% endblock %} {% endblock %}
{#<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>#}
{% block scripts %}{% endblock %} {% block scripts %}{% endblock %}
</body> </body>
</html> </html>

View File

@ -0,0 +1,43 @@
{# DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022–2023 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/>.
#}
{% extends 'base.html' %}
{% block title %}Import statement{% endblock %}
{% block content %}
<h1 class="h2 my-4">Import statement</h1>
<h2 class="h3">OFX 1.x</h2>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="format" value="ofx1">
<div class="d-flex">
<div class="flex-grow-1 me-2">
<input class="form-control" name="source-account" placeholder="Source account">
</div>
<div class="flex-grow-1">
<input class="form-control" type="file" name="file" accept=".ofx">
</div>
<div>
<button type="submit" name="action" value="preview" class="btn btn-secondary ms-2">Preview</button>
</div>
<div>
<button type="submit" name="action" value="import" class="btn btn-primary ms-2">Import</button>
</div>
</div>
</form>
{% endblock %}

View File

@ -0,0 +1,48 @@
{# DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022–2023 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/>.
#}
{% extends 'base.html' %}
{% block title %}Import statement preview{% endblock %}
{% block content %}
<h1 class="h2 my-4">Import statement preview</h1>
<h2 class="h3 mb-4">Imported statement lines</h2>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th class="text-end">Dr</th>
<th class="text-end">Cr</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>
{% for line in statement_lines %}
<tr>
<td>{{ line.dt.strftime('%Y-%m-%d') }}</td>
<td>{{ line.description }}</td>
<td class="text-end">{{ line.amount().format() if line.quantity >= 0 else '' }}</td>
<td class="text-end">{{ (line.amount()|abs).format() if line.quantity < 0 else '' }}</td>
<td class="text-end">{{ line.balance or '' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -22,9 +22,10 @@
<h1 class="h2 my-4">Statement lines</h1> <h1 class="h2 my-4">Statement lines</h1>
<form method="POST"> <form method="POST">
{#<div class="mb-2"> <div class="mb-2">
<button type="submit" class="btn btn-outline-secondary" formaction="/statement-lines/reconcile-transfer">Reconcile selected as transfer</button> {#<button type="submit" class="btn btn-outline-secondary" formaction="/statement-lines/reconcile-transfer">Reconcile selected as transfer</button>#}
</div>#} <a href="/statement-lines/import" class="btn btn-outline-secondary">Import statement</a>
</div>
<table class="table"> <table class="table">
<thead> <thead>

View File

@ -1,6 +1,9 @@
Flask==2.2.2 Flask==2.2.2
Flask-SQLAlchemy==3.0.2 Flask-SQLAlchemy==3.0.2
# For OFX 1.x import
lxml==4.9.2
# Dependencies # Dependencies
click==8.1.3 click==8.1.3
greenlet==2.0.1 greenlet==2.0.1