diff --git a/custom_ui/api/db.py b/custom_ui/api/db.py index 675d1b8..659f4c3 100644 --- a/custom_ui/api/db.py +++ b/custom_ui/api/db.py @@ -1,4 +1,5 @@ import frappe, json +from custom_ui.db_utils import calculate_appointment_scheduled_status, calculate_estimate_sent_status, calculate_payment_recieved_status, calculate_job_scheduled_status @frappe.whitelist() def get_clients(options): @@ -8,11 +9,13 @@ def get_clients(options): "filters": {}, "sorting": {}, "page": 1, - "page_size": 10 + "page_size": 10, + "for_table": False } options = {**defaultOptions, **options} clients = [] + tableRows = [] count = frappe.db.count("Address", filters=options["filters"]) print("DEBUG: Total addresses count:", count) @@ -28,29 +31,58 @@ def get_clients(options): for address in addresses: client = {} + tableRow = {} print("DEBUG: Processing address:", address) + on_site_meetings = frappe.db.get_all( + "On-Site Meeting", + fields=["*"], + filters={"address": address["address_title"]} + ) + + sales_invvoices = frappe.db.get_all( + "Sales Invoice", + fields=["*"], + filters={"custom_installation_address": address["address_title"]} + ) + quotations = frappe.db.get_all( "Quotation", fields=["*"], - filters={"custom_installation_address": address["name"]} + filters={"custom_installation_address": address["address_title"]} ) - jobs = frappe.db.get_all("Project", - fields=["*"], - filters={"custom_installation_address": address["name"], - "project_template": "SNW Install"}) + jobs = frappe.db.get_all( + "Project", + fields=["*"], + filters={ + "custom_installation_address": address["address_title"], + "project_template": "SNW Install" + } + ) + + tableRow["id"] = address["name"] + tableRow["address_name"] = address.get("address_title", "") + tableRow["appointment_scheduled_status"] = calculate_appointment_scheduled_status(on_site_meetings[0]) if on_site_meetings else "Not Started" + tableRow["estimate_sent_status"] = calculate_estimate_sent_status(quotations[0]) if quotations else "Not Started" + tableRow["payment_received_status"] = calculate_payment_recieved_status(sales_invvoices[0]) if sales_invvoices else "Not Started" + tableRow["job_scheduled_status"] = calculate_job_scheduled_status(jobs[0]) if jobs else "Not Started" + tableRows.append(tableRow) + client["address"] = address - client["on_site_meetings"] = [] + client["on_site_meetings"] = on_site_meetings client["jobs"] = jobs client["quotations"] = quotations clients.append(client) return { - "count": count, - "page": options["page"], - "page_size": options["page_size"], - "clients": clients + "pagination": { + "total": count, + "page": options["page"], + "page_size": options["page_size"], + "total_pages": (count + options["page_size"] - 1) // options["page_size"] + }, + "data": tableRows if options["for_table"] else clients } @frappe.whitelist() diff --git a/custom_ui/db_utils.py b/custom_ui/db_utils.py new file mode 100644 index 0000000..677848e --- /dev/null +++ b/custom_ui/db_utils.py @@ -0,0 +1,28 @@ + +def calculate_appointment_scheduled_status(on_site_meeting): + if not on_site_meeting: + return "Not Started" + # if on_site_meeting["end_time"] < today: + # return "In Progress" + return "Completed" + +def calculate_estimate_sent_status(quotation): + if not quotation: + return "Not Started" + if quotation["custom_sent"] == 1: + return "Completed" + return "In Progress" + +def calculate_payment_recieved_status(sales_invoice): + if not sales_invoice: + return "Not Started" + if sales_invoice and sales_invoice["status"] == "Paid": + return "Completed" + return "In Progress" + +def calculate_job_scheduled_status(project): + if not project: + return "Not Started" + if not project["start_time"]: + return "In Progress" + return "Completed" \ No newline at end of file diff --git a/frontend/src/api.js b/frontend/src/api.js index a10fee6..5e5b221 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -246,23 +246,30 @@ class Api { * Get paginated client data with filtering and sorting * @param {Object} paginationParams - Pagination parameters from store * @param {Object} filters - Filter parameters from store - * @returns {Promise<{data: Array, totalRecords: number}>} + * @returns {Promise<{data: Array, pagination: Object}>} */ static async getPaginatedClientDetails(paginationParams = {}, filters = {}) { const { page = 0, pageSize = 10, sortField = null, sortOrder = null } = paginationParams; const options = { - page: page + 1, - pageSize, + page: page + 1, // Backend expects 1-based pages + page_size: pageSize, filters, - sortField, - sortOrder, + sorting: sortField ? `${sortField} ${sortOrder === -1 ? "desc" : "asc"}` : null, + for_table: true, }; - const result = await this.getClientDetails(options); + const result = await this.request("custom_ui.api.db.get_clients", { options }); + // Transform the response to match what the frontend expects return { - ...result, + data: result.data, + pagination: { + total: result.pagination.total, + page: result.pagination.page, + pageSize: result.pagination.page_size, + totalPages: result.pagination.total_pages, + }, }; } diff --git a/frontend/src/components/common/DataTable.vue b/frontend/src/components/common/DataTable.vue index e0ad7d5..3453be2 100644 --- a/frontend/src/components/common/DataTable.vue +++ b/frontend/src/components/common/DataTable.vue @@ -1,6 +1,10 @@