diff --git a/VERSION b/VERSION index 845639e..9faa1b7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.4 +0.1.5 diff --git a/backend/app.py b/backend/app.py index b634fc9..3f57a3e 100644 --- a/backend/app.py +++ b/backend/app.py @@ -303,6 +303,108 @@ def new_service(): conn.close() return render_template("services/new.html", clients=clients) +@app.route("/services/edit/", methods=["GET", "POST"]) +def edit_service(service_id): + conn = get_db_connection() + cursor = conn.cursor(dictionary=True) + + if request.method == "POST": + client_id = request.form.get("client_id", "").strip() + service_name = request.form.get("service_name", "").strip() + service_type = request.form.get("service_type", "").strip() + billing_cycle = request.form.get("billing_cycle", "").strip() + currency_code = request.form.get("currency_code", "").strip() + recurring_amount = request.form.get("recurring_amount", "").strip() + status = request.form.get("status", "").strip() + start_date = request.form.get("start_date", "").strip() + description = request.form.get("description", "").strip() + + errors = [] + + if not client_id: + errors.append("Client is required.") + if not service_name: + errors.append("Service name is required.") + if not service_type: + errors.append("Service type is required.") + if not billing_cycle: + errors.append("Billing cycle is required.") + if not currency_code: + errors.append("Currency code is required.") + if not recurring_amount: + errors.append("Recurring amount is required.") + if not status: + errors.append("Status is required.") + + if not errors: + try: + recurring_amount_value = float(recurring_amount) + if recurring_amount_value < 0: + errors.append("Recurring amount cannot be negative.") + except ValueError: + errors.append("Recurring amount must be a valid number.") + + if errors: + cursor.execute(""" + SELECT s.*, c.company_name + FROM services s + LEFT JOIN clients c ON s.client_id = c.id + WHERE s.id = %s + """, (service_id,)) + service = cursor.fetchone() + + cursor.execute("SELECT id, client_code, company_name FROM clients ORDER BY company_name ASC") + clients = cursor.fetchall() + + conn.close() + return render_template("services/edit.html", service=service, clients=clients, errors=errors) + + update_cursor = conn.cursor() + update_cursor.execute(""" + UPDATE services + SET client_id = %s, + service_name = %s, + service_type = %s, + billing_cycle = %s, + status = %s, + currency_code = %s, + recurring_amount = %s, + start_date = %s, + description = %s + WHERE id = %s + """, ( + client_id, + service_name, + service_type, + billing_cycle, + status, + currency_code, + recurring_amount, + start_date or None, + description or None, + service_id + )) + conn.commit() + conn.close() + return redirect("/services") + + cursor.execute(""" + SELECT s.*, c.company_name + FROM services s + LEFT JOIN clients c ON s.client_id = c.id + WHERE s.id = %s + """, (service_id,)) + service = cursor.fetchone() + + cursor.execute("SELECT id, client_code, company_name FROM clients ORDER BY company_name ASC") + clients = cursor.fetchall() + conn.close() + + if not service: + return "Service not found", 404 + + return render_template("services/edit.html", service=service, clients=clients, errors=[]) + @app.route("/invoices") def invoices(): refresh_overdue_invoices() diff --git a/templates/services/edit.html b/templates/services/edit.html new file mode 100644 index 0000000..2f66fdc --- /dev/null +++ b/templates/services/edit.html @@ -0,0 +1,115 @@ + + + +Edit Service + + + +

Edit Service

+ +

Home

+

Back to Services

+ +{% if errors %} +
+ Please fix the following: + +
+{% endif %} + +
+ +

+Service Code
+ +

+ +

+Client *
+ +

+ +

+Service Name *
+ +

+ +

+Service Type *
+ +

+ +

+Billing Cycle *
+ +

+ +

+Currency Code *
+ +

+ +

+Recurring Amount *
+ +

+ +

+Status *
+ +

+ +

+Start Date
+ +

+ +

+Description
+ +

+ +

+ +

+ +
+ +{% include "footer.html" %} + + diff --git a/templates/services/list.html b/templates/services/list.html index fdb3f03..65351ee 100644 --- a/templates/services/list.html +++ b/templates/services/list.html @@ -22,6 +22,7 @@ Amount Status Start Date + Actions {% for s in services %} @@ -36,6 +37,7 @@ {{ s.recurring_amount|money(s.currency_code) }} {{ s.status }} {{ s.start_date }} + Edit {% endfor %}