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(): return render_htmx_or_full('partials/index.html', 'index.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') # Extract invoice data from form new_invoice = { 'Rechnungsnummer': request.form['rechnungsnummer'], 'Email': request.form['email'], 'Anrede': request.form['anrede'], # Initialize 'Betrag' with a placeholder (to be updated) 'Betrag': 'N/A' } try: # Fetch the total gross amount for the new invoice using LexofficeAPI lexoffice_api = LexofficeAPI('your_access_token') # Replace with actual access token total_gross_amount = lexoffice_api.get_invoice_total_gross_amount(new_invoice['Rechnungsnummer']) # Update 'Betrag' in the new invoice data if total_gross_amount is not None: new_invoice['Betrag'] = total_gross_amount # Read existing data from CSV with open(csv_file_path, 'r', newline='', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile, delimiter=';') fieldnames = reader.fieldnames if 'Betrag' not in fieldnames: fieldnames.append('Betrag') existing_data = list(reader) # Add the new invoice at the beginning of the data existing_data.insert(0, new_invoice) # Write the updated data back to the CSV file with open(csv_file_path, 'w', newline='', encoding='utf-8') as csvfile: 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 updating CSV file: {e}") return "An error occurred while adding the invoice", 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)