From 666e946833a0071517fc21de0a3dd4647609248d Mon Sep 17 00:00:00 2001 From: def Date: Sun, 8 Mar 2026 07:29:19 +0000 Subject: [PATCH] Add v0.0.5 automatic client_code generation --- VERSION | 2 +- backend/app.py | 41 +++++++++++++++++++++----------------- backend/utils.py | 14 +++++++++++++ templates/clients/new.html | 5 ----- 4 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 backend/utils.py diff --git a/VERSION b/VERSION index 81340c7..bbdeab6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.4 +0.0.5 diff --git a/backend/app.py b/backend/app.py index 1319bbf..ddb6f5e 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,5 +1,6 @@ from flask import Flask, render_template, request, redirect from db import get_db_connection +from utils import generate_client_code app = Flask( __name__, @@ -11,52 +12,55 @@ app = Flask( def index(): return """

OTB Billing

-

Version 0.0.4

+

Version 0.0.5

Clients

""" -@app.route("/dbtest") -def dbtest(): - try: - conn = get_db_connection() - cursor = conn.cursor() - cursor.execute("SELECT NOW()") - result = cursor.fetchone() - conn.close() - - return f"

Database OK

{result[0]}

" - except Exception as e: - return f"

Database FAILED

{e}
" - @app.route("/clients") def clients(): + conn = get_db_connection() cursor = conn.cursor(dictionary=True) - cursor.execute("SELECT * FROM clients ORDER BY created_at DESC") + + cursor.execute("SELECT * FROM clients ORDER BY id DESC") clients = cursor.fetchall() + conn.close() return render_template("clients/list.html", clients=clients) -@app.route("/clients/new", methods=["GET", "POST"]) + +@app.route("/clients/new", methods=["GET","POST"]) def new_client(): + if request.method == "POST": - client_code = request.form["client_code"] + company_name = request.form["company_name"] contact_name = request.form["contact_name"] email = request.form["email"] phone = request.form["phone"] conn = get_db_connection() + cursor = conn.cursor(dictionary=True) + + cursor.execute("SELECT MAX(id) as last_id FROM clients") + result = cursor.fetchone() + + last_number = result["last_id"] if result["last_id"] else 0 + + client_code = generate_client_code(company_name, last_number) + cursor = conn.cursor() + cursor.execute( """ INSERT INTO clients (client_code, company_name, contact_name, email, phone) - VALUES (%s, %s, %s, %s, %s) + VALUES (%s,%s,%s,%s,%s) """, (client_code, company_name, contact_name, email, phone) ) + conn.commit() conn.close() @@ -64,5 +68,6 @@ def new_client(): return render_template("clients/new.html") + if __name__ == "__main__": app.run(host="0.0.0.0", port=5050) diff --git a/backend/utils.py b/backend/utils.py new file mode 100644 index 0000000..e432bbc --- /dev/null +++ b/backend/utils.py @@ -0,0 +1,14 @@ +import re + +def generate_client_code(company_name, last_number): + # remove non letters + cleaned = re.sub(r'[^A-Za-z]', '', company_name.upper()) + + # first 5 characters + suffix = cleaned[:5] + + number = last_number + 1 + + code = f"{number:04d}-{suffix}" + + return code diff --git a/templates/clients/new.html b/templates/clients/new.html index bda022c..1cbea2e 100644 --- a/templates/clients/new.html +++ b/templates/clients/new.html @@ -10,11 +10,6 @@
-

-Client Code
- -

-

Company Name