From 14e799a1d485d204ca45a9dc50e1e40c85924545 Mon Sep 17 00:00:00 2001 From: Swapnil9693 Date: Tue, 24 Mar 2026 15:51:55 +0530 Subject: [PATCH] Inoive add update delete messages shown properly --- model/Invoice.py | 2 +- static/js/invoice.js | 303 +++++++++++++--- templates/add_invoice.html | 674 ++++++++++++++++++++++-------------- templates/edit_invoice.html | 35 +- 4 files changed, 696 insertions(+), 318 deletions(-) diff --git a/model/Invoice.py b/model/Invoice.py index b002fb3..d82bd0d 100644 --- a/model/Invoice.py +++ b/model/Invoice.py @@ -66,7 +66,7 @@ def get_all_villages(): return fetch_all(cursor) return execute_db_operation(operation) - + # ------------------- Invoice Functions ------------------- def insert_invoice(data, village_id): def operation(cursor): diff --git a/static/js/invoice.js b/static/js/invoice.js index 9b3f2a0..d3e0778 100644 --- a/static/js/invoice.js +++ b/static/js/invoice.js @@ -1,62 +1,271 @@ -// Subcontractor autocomplete functionality - $(document).ready(function () { - $("#subcontractor").keyup(function () { +// $(document).ready(function () { +// // =============================== +// // FORM / TABLE TOGGLE +// // =============================== +// if ($('#addForm').length && $('#addTable').length) { +// $('#addForm').show(); +// $('#addTable').hide(); + +// $('#addButton').click(function () { +// $('#addForm').show(); +// $('#addTable').hide(); +// }); + +// $('#displayButton').click(function () { +// $('#addForm').hide(); +// $('#addTable').show(); +// }); +// } + +// // =============================== +// // Subcontractor autocomplete +// // =============================== +// $("#subcontractor").keyup(function () { +// let query = $(this).val(); +// if (query !== "") { +// $.ajax({ +// url: "/search_subcontractor", +// method: "POST", +// data: { query: query }, +// success: function (data) { +// $("#subcontractor_list").fadeIn().html(data); +// } +// }); +// } else { +// $("#subcontractor_list").fadeOut(); +// } +// }); + +// $(document).on("click", "li", function () { +// $("#subcontractor").val($(this).text()); +// $("#subcontractor_id").val($(this).attr("data-id")); +// $("#subcontractor_list").fadeOut(); +// }); + +// // Focus +// if (document.getElementById('subcontractor')) { +// document.getElementById('subcontractor').focus(); +// } + +// // =============================== +// // ADD INVOICE +// // =============================== +// if ($('#invoiceForm').length && window.location.href.includes('add_invoice')) { +// $("#invoiceForm").on("submit", function (e) { +// e.preventDefault(); +// let formData = $(this).serialize(); + +// $.ajax({ +// url: '/add_invoice', +// method: 'POST', +// data: formData, +// dataType: 'json', +// success: function (response) { +// if (response.status === "success") { +// alert(response.message || "Invoice added successfully!"); +// $('#invoiceForm')[0].reset(); // clear form +// $('#addForm').hide(); +// $('#addTable').show(); // switch to table +// location.reload(); // optional refresh +// } else { +// alert(response.message || "Error adding invoice."); +// } +// }, +// error: function (xhr) { +// alert(xhr.responseJSON?.message || "Submission failed. Please try again."); +// } +// }); +// }); +// } +// // Example AJAX update function +// function updateInvoice(invoiceId, formElement) { +// const formData = $(formElement).serialize(); + +// $.ajax({ +// url: '/update_invoice/' + invoiceId, +// method: 'POST', +// data: formData, +// dataType: 'json', +// success: function(response) { +// if(response.status === "success") { +// alert(response.message || "Invoice updated successfully!"); + +// // ✅ Hide Add Form, Show Table +// $('#addForm').hide(); +// $('#addTable').show(); + +// // Optional: reload table or refresh page +// location.reload(); +// } else { +// alert(response.message || "Update failed. Please try again."); +// } +// }, +// error: function(xhr) { +// alert(xhr.responseJSON?.message || "Error updating invoice."); +// } +// }); +// } + +// // =============================== +// // DELETE INVOICE +// // =============================== +// function deleteInvoice(invoiceId, element) { +// if (!confirm("Are you sure you want to delete this invoice?")) return; + +// $.ajax({ +// url: '/delete_invoice/' + invoiceId, +// method: 'GET', +// dataType: 'json', +// success: function (response) { +// alert(response.message || "Invoice deleted successfully!"); +// if (element) $(element).closest("tr").remove(); +// }, +// error: function (xhr) { +// alert(xhr.responseJSON?.message || "Error deleting invoice. Please try again."); +// } +// }); +// } + +$(document).ready(function () { + // =============================== + // FORM / TABLE TOGGLE + // =============================== + if ($('#addForm').length && $('#addTable').length) { + // Default: show form, hide table + $('#addForm').show(); + $('#addTable').hide(); + + // ✅ Check URL hash to show table instead + if (window.location.hash === "#addTable") { + $('#addForm').hide(); + $('#addTable').show(); + } + + $('#addButton').click(function () { + $('#addForm').show(); + $('#addTable').hide(); + }); + + $('#displayButton').click(function () { + $('#addForm').hide(); + $('#addTable').show(); + }); + } + + // =============================== + // Subcontractor autocomplete + // =============================== + $("#subcontractor").keyup(function () { let query = $(this).val(); if (query !== "") { - $.ajax({ - url: "/search_subcontractor", - method: "POST", - data: { query: query }, - success: function (data) { - $("#subcontractor_list").fadeIn().html(data); - } - }); + $.ajax({ + url: "/search_subcontractor", + method: "POST", + data: { query: query }, + success: function (data) { + $("#subcontractor_list").fadeIn().html(data); + } + }); } else { - $("#subcontractor_list").fadeOut(); + $("#subcontractor_list").fadeOut(); } - }); + }); - $(document).on("click", "li", function () { + $(document).on("click", "li", function () { $("#subcontractor").val($(this).text()); $("#subcontractor_id").val($(this).attr("data-id")); $("#subcontractor_list").fadeOut(); - }); }); - // Success Alert: show alert and reload after 3 seconds - function showSuccessAlert() { - const alertBox = document.getElementById("invoiceSuccessAlert"); - alertBox.classList.add("show"); - setTimeout(() => { - alertBox.classList.remove("show"); - // Reload page or redirect after alert hides - window.location.href = '/add_invoice'; - }, 3000); + // Focus + if (document.getElementById('subcontractor')) { + document.getElementById('subcontractor').focus(); } - // Submit form via AJAX - $("#invoiceForm").on("submit", function (e) { - e.preventDefault(); - let formData = $(this).serialize(); - $.ajax({ - url: '/add_invoice', - method: 'POST', - data: formData, - success: function (response) { - if(response.status === "success") { - showSuccessAlert(); - } else { - alert(response.message); - } - }, - error: function (xhr, status, error) { - alert("Submission failed: " + error); - } - }); - }); + // =============================== + // ADD INVOICE + // =============================== + if ($('#invoiceForm').length && window.location.href.includes('add_invoice')) { + $("#invoiceForm").on("submit", function (e) { + e.preventDefault(); + let formData = $(this).serialize(); + $.ajax({ + url: '/add_invoice', + method: 'POST', + data: formData, + dataType: 'json', + success: function (response) { + if (response.status === "success") { + alert(response.message || "Invoice added successfully!"); + $('#invoiceForm')[0].reset(); // clear form + $('#addForm').hide(); + $('#addTable').show(); // switch to table + location.reload(); // optional refresh + } else { + alert(response.message || "Error adding invoice."); + } + }, + error: function (xhr) { + let msg = xhr.responseJSON?.message || "Submission failed. Please try again."; + alert(msg); + } + }); + }); + } + // =============================== + // UPDATE INVOICE + // =============================== + function updateInvoice(invoiceId, formElement) { + const formData = $(formElement).serialize(); -window.onload = function () { - document.getElementById('subcontractor').focus(); - }; \ No newline at end of file + $.ajax({ + url: '/update_invoice/' + invoiceId, + method: 'POST', + data: formData, + dataType: 'json', + success: function(response) { + if(response.status === "success") { + alert(response.message || "Invoice updated successfully!"); + + // Redirect to Add Invoice page's table part + window.location.href = "/add_invoice#addTable"; + } else { + alert(response.message || "Update failed. Please try again."); + } + }, + error: function(xhr) { + let msg = xhr.responseJSON?.message || "Error updating invoice."; + alert(msg); + } + }); + } + window.updateInvoice = updateInvoice; // make globally accessible + + // =============================== + // DELETE INVOICE + // =============================== + function deleteInvoice(invoiceId, element) { + if (!confirm("Are you sure you want to delete this invoice?")) return; + + $.ajax({ + url: '/delete_invoice/' + invoiceId, + method: 'GET', + dataType: 'json', + success: function (response) { + if (response.status === "success") { + alert(response.message || "Invoice deleted successfully!"); + if (element) $(element).closest("tr").remove(); + } else { + alert(response.message || "Error deleting invoice."); + } + }, + error: function (xhr) { + let msg = xhr.responseJSON?.message || "Error deleting invoice. Please try again."; + alert(msg); + } + }); + } + window.deleteInvoice = deleteInvoice; // make globally accessible +}); \ No newline at end of file diff --git a/templates/add_invoice.html b/templates/add_invoice.html index 399fa07..e6372e9 100644 --- a/templates/add_invoice.html +++ b/templates/add_invoice.html @@ -1,176 +1,322 @@ -{% extends 'base.html' %} -{% block content %} +{% extends 'base.html' %} {% block content %} - - - Add Invoice - - - - + + + Add Invoice + + + + -{% if success == 'true' %} - {% endif %} -{% endwith %} -
+ + {% with messages = get_flashed_messages(with_categories=true) %} {% if + messages %} +
+ {% for category, message in messages %} +
{{ message }}
+ {% endfor %} +
+ {% endif %} {% endwith %} + +
-
+
-