This commit is contained in:
Yisroel Baum 2024-12-25 21:20:27 +02:00
parent c7868c6b4e
commit da1210a862
15 changed files with 105 additions and 5 deletions

View file

@ -38,8 +38,8 @@ def line_items_by_month():
files=files, files=files,
all_months=all_months) all_months=all_months)
@app.route('/upload_file/<filename>') @app.route('/upload_bank_statement/<filename>')
def upload_file(filename:str): def upload_bank_statement(filename:str):
bank_statement_file_path = get_bank_statements_path() + filename bank_statement_file_path = get_bank_statements_path() + filename
xl = openpyxl.load_workbook(bank_statement_file_path, read_only=True) xl = openpyxl.load_workbook(bank_statement_file_path, read_only=True)
wb = xl.worksheets[0] wb = xl.worksheets[0]
@ -91,10 +91,64 @@ def upload_file(filename:str):
@app.route('/split_line/<line_item_id>') @app.route('/split_line/<line_item_id>')
def split_line(line_item_id): def split_line(line_item_id):
existing_child_lines = db.session.execute(select(LineItem).where(LineItem.parent_line_item_id == line_item_id)).scalars()
li = db.session.execute(select(LineItem).where(LineItem.id == line_item_id)).scalar() li = db.session.execute(select(LineItem).where(LineItem.id == line_item_id)).scalar()
return render_template('split_line.html', return render_template('split_line.html',
li=li) li=li,
existing_child_lines=existing_child_lines)
@app.route('/import_file_selector/<line_item_id>')
def import_file_selector(line_item_id):
li = db.session.execute(select(LineItem).where(LineItem.id == line_item_id)).scalar()
files = os.listdir('app\static\Credit_Card_Statements')
return render_template('import_file_selector.html',
li=li,
files=files)
@app.route('/upload_file_for_line_split/<file_name>/<line_item_id>')
def upload_file_for_line_split(file_name, line_item_id):
file_path = f'C:/Users/Lenovo/Desktop/BudgetingApp/app/static/Credit_Card_Statements/{file_name}'
parent_line_item = db.session.execute(select(LineItem).where(LineItem.id == line_item_id)).scalar()
xl = openpyxl.load_workbook(file_path, read_only=True)
wb = xl.worksheets[0]
vendors = db.session.execute(select(Vendor.name)).all()
vendors_added = [vendor[0] for vendor in vendors]
items_to_add = []
for line, row in enumerate(wb.rows):
row = [x.value for x in row]
if line == 0:
columns = {x: i for i,x in enumerate(row)}
continue
if not row[columns['Vendor']] in vendors_added:
vendors_added.append(row[columns['Vendor']])
vendor = Vendor(
name=row[columns['Vendor']]
)
db.session.add(vendor)
db.session.commit()
vendor_id = db.session.execute(select(Vendor.id).where(Vendor.name==row[columns['Vendor']])).scalar()
line_item = LineItem(
parent_line_item_id=line_item_id,
amount=row[columns['Amount']] * -1,
currency_type='shekel',
vendor_id=vendor_id,
date=parent_line_item.date,
confirmation_code=row[columns['Confirmation Code']],
note=None
)
items_to_add.append(line_item)
xl.close()
db.session.add_all(items_to_add)
db.session.commit()
os.remove(file_path)
return redirect(url_for('split_line', line_item_id=line_item_id))
################### ###################

View file

@ -0,0 +1,25 @@
{% extends 'base.html' %}
{% block title %}Split Line{% endblock title %}
{% block content %}
<main>
<div class="container mt-5">
<div class="card">
<div class="card-header bg-primary text-white">
Select file to import
</div>
<!-- Display Details Section -->
<div class="card-body">
{% for file in files %}
<p><a href="{{url_for('upload_file_for_line_split', file_name=file, line_item_id=li.id)}}">{{file}}</a></p>
{% endfor %}
</div>
<div class="card-body">
</div>
</div>
</div>
<!-- <script src="{{url_for('.static', filename='split_line.js')}}"></script> -->
</main>
{% endblock content %}

View file

@ -21,7 +21,7 @@
<tbody> <tbody>
{% for file in files %} {% for file in files %}
<tr> <tr>
<th scope="row"><a href="{{url_for('upload_file', filename=file)}}">{{file}}</a></th> <th scope="row"><a href="{{url_for('upload_bank_statement', filename=file)}}">{{file}}</a></th>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

View file

@ -27,6 +27,9 @@
<th scope="row">Date:</th> <th scope="row">Date:</th>
<td>{{ li.display_date() }}</td> <td>{{ li.display_date() }}</td>
</tr> </tr>
<tr>
<th><a class="btn btn-primary" href="{{url_for('import_file_selector', line_item_id=li.id)}}">Import File</a></th>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>
@ -37,7 +40,25 @@
<table class="table table-bordered"> <table class="table table-bordered">
<input type="hidden" name="parent-id" value="{{li.id}}"/> <input type="hidden" name="parent-id" value="{{li.id}}"/>
<input type="hidden" name="date" value="{{li.date}}"/> <input type="hidden" name="date" value="{{li.date}}"/>
<tbody id="table-body"></tbody> <tbody id="table-body">
{% if existing_child_lines %}
<tr>
<th>Amount</th>
<th>Vendor</th>
<th>Confirmation Code</th>
<th>Note</th>
</tr>
{% for line in existing_child_lines %}
<tr>
<td>{{line.amount}}</td>
<td>{{line.get_vendor()}}</td>
<td>{{line.confirmation_code}}</td>
<td>{{line.note}}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table> </table>
</div> </div>
<button type="button" class="btn btn-primary" id="add-line-button">Add Another Line?</button> <button type="button" class="btn btn-primary" id="add-line-button">Add Another Line?</button>

Binary file not shown.