from flask import Flask, render_template, request, redirect from db import get_db_connection from utils import generate_client_code, generate_service_code app = Flask( __name__, template_folder="../templates", static_folder="../static", ) @app.route("/") def index(): return """
Version 0.0.7
""" @app.route("/dbtest") def dbtest(): try: conn = get_db_connection() cursor = conn.cursor() cursor.execute("SELECT NOW()") result = cursor.fetchone() conn.close() return f"{result[0]}
" except Exception as e: return f"{e}"
@app.route("/clients")
def clients():
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("SELECT * FROM clients ORDER BY id DESC")
clients = cursor.fetchall()
conn.close()
return render_template("clients/list.html", clients=clients)
@app.route("/services")
def services():
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT s.*, c.client_code, c.company_name
FROM services s
JOIN clients c ON s.client_id = c.id
ORDER BY s.id DESC
""")
services = cursor.fetchall()
conn.close()
return render_template("services/list.html", services=services)
@app.route("/invoices")
def invoices():
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT
i.*,
c.client_code,
c.company_name
FROM invoices i
JOIN clients c ON i.client_id = c.id
ORDER BY i.id DESC
""")
invoices = cursor.fetchall()
conn.close()
return render_template("invoices/list.html", invoices=invoices)
@app.route("/invoices/new", methods=["GET","POST"])
def new_invoice():
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
if request.method == "POST":
client_id = request.form["client_id"]
service_id = request.form["service_id"]
currency_code = request.form["currency_code"]
total_amount = request.form["total_amount"]
due_at = request.form["due_at"]
notes = request.form["notes"]
cursor.execute("SELECT MAX(id) as last_id FROM invoices")
result = cursor.fetchone()
number = (result["last_id"] or 0) + 1
invoice_number = f"INV-{number:04d}"
insert = conn.cursor()
insert.execute("""
INSERT INTO invoices
(
client_id,
service_id,
invoice_number,
currency_code,
total_amount,
subtotal_amount,
issued_at,
due_at,
status,
notes
)
VALUES (%s,%s,%s,%s,%s,%s,NOW(),%s,'pending',%s)
""",
(
client_id,
service_id,
invoice_number,
currency_code,
total_amount,
total_amount,
due_at,
notes
))
conn.commit()
conn.close()
return redirect("/invoices")
cursor.execute("""
SELECT id, client_code, company_name
FROM clients
ORDER BY company_name
""")
clients = cursor.fetchall()
cursor.execute("""
SELECT id, service_code, service_name
FROM services
ORDER BY service_name
""")
services = cursor.fetchall()
conn.close()
return render_template("invoices/new.html", clients=clients, services=services)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5050)