112 lines
3.6 KiB
Python
112 lines
3.6 KiB
Python
from flask import render_template, redirect, url_for, current_app, render_template_string, request
|
|
import os
|
|
import csv
|
|
from app import app, htmx
|
|
import logging
|
|
|
|
from app.lexoffice import LexofficeAPI
|
|
|
|
|
|
def read_invoices_from_csv(csv_file_path):
|
|
try:
|
|
with open(csv_file_path, mode='r', encoding='utf-8') as file:
|
|
return list(csv.DictReader(file, delimiter=';'))
|
|
except FileNotFoundError:
|
|
logging.error(f"CSV file not found at {csv_file_path}")
|
|
return []
|
|
except csv.Error as e:
|
|
logging.error(f"Error reading CSV file: {e}")
|
|
return []
|
|
|
|
|
|
@app.route('/load-invoices')
|
|
def load_invoices():
|
|
csv_file_path = os.path.join(current_app.root_path, 'data', 'invoices.csv')
|
|
invoices = read_invoices_from_csv(csv_file_path)
|
|
if not invoices:
|
|
return "No invoice data available", 500
|
|
return render_template('partials/invoice_table.html', invoices=invoices)
|
|
|
|
|
|
def render_htmx_or_full(template_partial, template_full):
|
|
try:
|
|
if htmx:
|
|
return render_template(template_partial)
|
|
return render_template(template_full)
|
|
except Exception as e:
|
|
current_app.logger.error(f"Error rendering template: {e}")
|
|
return "An error occurred", 500
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
dashboard_content = render_template('partials/dashboard.html')
|
|
return render_template('index.html', dashboard_content=dashboard_content)
|
|
|
|
|
|
@app.route('/dashboard')
|
|
def dashboard():
|
|
return render_template('partials/dashboard.html')
|
|
|
|
|
|
@app.route('/buchhaltung')
|
|
def buchhaltung():
|
|
return render_htmx_or_full('partials/buchhaltung.html', 'index.html')
|
|
|
|
|
|
@app.route('/add-invoice', methods=['POST'])
|
|
def add_invoice():
|
|
csv_file_path = os.path.join(current_app.root_path, 'data', 'invoices.csv')
|
|
new_invoice = {
|
|
'Rechnungsnummer': request.form['rechnungsnummer'],
|
|
'Email': request.form['email'],
|
|
'Anrede': request.form['anrede'],
|
|
'Betrag': 'N/A'
|
|
}
|
|
|
|
try:
|
|
lexoffice_api = LexofficeAPI()
|
|
total_gross_amount = lexoffice_api.get_invoice_total_gross_amount(new_invoice['Rechnungsnummer'])
|
|
new_invoice['Betrag'] = total_gross_amount if total_gross_amount is not None else 'N/A'
|
|
|
|
with open(csv_file_path, 'r+', newline='', encoding='utf-8') as csvfile:
|
|
reader = csv.DictReader(csvfile, delimiter=';')
|
|
existing_data = list(reader)
|
|
fieldnames = reader.fieldnames
|
|
if 'Betrag' not in fieldnames:
|
|
fieldnames.append('Betrag')
|
|
existing_data.insert(0, new_invoice)
|
|
csvfile.seek(0)
|
|
csvfile.truncate()
|
|
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=';')
|
|
writer.writeheader()
|
|
writer.writerows(existing_data)
|
|
|
|
return redirect(url_for('buchhaltung'))
|
|
|
|
except Exception as e:
|
|
logging.error(f"Error in add_invoice: {e}")
|
|
return "An error occurred", 500
|
|
|
|
|
|
# Other imports and routes...
|
|
|
|
@app.route('/zahlungserinnerungen')
|
|
def zahlungserinnerungen():
|
|
return render_htmx_or_full('partials/zahlungserinnerungen.html', 'index.html')
|
|
|
|
|
|
@app.route('/flottenmanagement')
|
|
def flottenmanagement():
|
|
return render_htmx_or_full('partials/flottenmanagement.html', 'index.html')
|
|
|
|
|
|
@app.route('/debug/templates')
|
|
def debug_templates():
|
|
template_dir = os.path.join(current_app.root_path, 'templates')
|
|
all_files = []
|
|
for path, subdirs, files in os.walk(template_dir):
|
|
for name in files:
|
|
all_files.append(os.path.join(path, name))
|
|
return '\n'.join(all_files)
|