diff --git a/custom_ui/api/db/clients.py b/custom_ui/api/db/clients.py index c1c6194..811fa2d 100644 --- a/custom_ui/api/db/clients.py +++ b/custom_ui/api/db/clients.py @@ -28,30 +28,30 @@ def get_client_status_counts(weekly=False, week_start_date=None, week_end_date=N onsite_meeting_scheduled_status_counts = { "label": "On-Site Meeting Scheduled", - "not_started": frappe.db.count("Address", filters=get_filters("custom_onsite_meeting_scheduled", "Not Started")), - "in_progress": frappe.db.count("Address", filters=get_filters("custom_onsite_meeting_scheduled", "In Progress")), - "completed": frappe.db.count("Address", filters=get_filters("custom_onsite_meeting_scheduled", "Completed")) + "not_started": frappe.db.count("Address", filters=get_filters("onsite_meeting_scheduled", "Not Started")), + "in_progress": frappe.db.count("Address", filters=get_filters("onsite_meeting_scheduled", "In Progress")), + "completed": frappe.db.count("Address", filters=get_filters("onsite_meeting_scheduled", "Completed")) } estimate_sent_status_counts = { "label": "Estimate Sent", - "not_started": frappe.db.count("Address", filters=get_filters("custom_estimate_sent_status", "Not Started")), - "in_progress": frappe.db.count("Address", filters=get_filters("custom_estimate_sent_status", "In Progress")), - "completed": frappe.db.count("Address", filters=get_filters("custom_estimate_sent_status", "Completed")) + "not_started": frappe.db.count("Address", filters=get_filters("estimate_sent_status", "Not Started")), + "in_progress": frappe.db.count("Address", filters=get_filters("estimate_sent_status", "In Progress")), + "completed": frappe.db.count("Address", filters=get_filters("estimate_sent_status", "Completed")) } job_status_counts = { "label": "Job Status", - "not_started": frappe.db.count("Address", filters=get_filters("custom_job_status", "Not Started")), - "in_progress": frappe.db.count("Address", filters=get_filters("custom_job_status", "In Progress")), - "completed": frappe.db.count("Address", filters=get_filters("custom_job_status", "Completed")) + "not_started": frappe.db.count("Address", filters=get_filters("job_status", "Not Started")), + "in_progress": frappe.db.count("Address", filters=get_filters("job_status", "In Progress")), + "completed": frappe.db.count("Address", filters=get_filters("job_status", "Completed")) } payment_received_status_counts = { "label": "Payment Received", - "not_started": frappe.db.count("Address", filters=get_filters("custom_payment_received_status", "Not Started")), - "in_progress": frappe.db.count("Address", filters=get_filters("custom_payment_received_status", "In Progress")), - "completed": frappe.db.count("Address", filters=get_filters("custom_payment_received_status", "Completed")) + "not_started": frappe.db.count("Address", filters=get_filters("payment_received_status", "Not Started")), + "in_progress": frappe.db.count("Address", filters=get_filters("payment_received_status", "In Progress")), + "completed": frappe.db.count("Address", filters=get_filters("payment_received_status", "Completed")) } status_dicts = [ @@ -135,7 +135,7 @@ def get_client(client_name): clientData["contacts"].append(linked_doc.as_dict()) elif link["link_doctype"] == "Address": clientData["addresses"].append(linked_doc.as_dict()) - + # TODO: Continue getting other linked docs like jobs, invoices, etc. print("DEBUG: Final client data prepared:", clientData) return build_success_response(clientData) @@ -143,7 +143,7 @@ def get_client(client_name): return build_error_response(str(ve), 400) except Exception as e: return build_error_response(str(e), 500) - + @frappe.whitelist() def get_client_v2(client_name): """Get detailed information for a specific client including address, customer, and projects.""" @@ -159,7 +159,7 @@ def get_client_v2(client_name): clientData["addresses"] = [AddressService.get_or_throw(link.address) for link in clientData["properties"]] if clientData["doctype"] == "Lead": clientData["customer_name"] = customer.custom_customer_name - + # TODO: Continue getting other linked docs like jobs, invoices, etc. print("DEBUG: Final client data prepared:", clientData) return build_success_response(clientData) @@ -167,9 +167,9 @@ def get_client_v2(client_name): return build_error_response(str(ve), 400) except Exception as e: return build_error_response(str(e), 500) - - + + @frappe.whitelist() def get_clients_table_data_v2(filters={}, sortings=[], page=1, page_size=10): """Get paginated client table data with filtering and sorting support.""" @@ -189,21 +189,21 @@ def get_clients_table_data_v2(filters={}, sortings=[], page=1, page_size=10): if filters.get("company"): where_clauses.append("c.company = %s") values.append(filters["company"]["value"]) - + if filters.get("address"): where_clauses.append("a.full_address LIKE %s") values.append(f"%{filters['address']['value']}%") - + if filters.get("customer_name"): where_clauses.append("a.customer_name LIKE %s") values.append(f"%{filters['customer_name']['value']}%") - + where_sql = "" if where_clauses: where_sql = "WHERE " + " AND ".join(where_clauses) - + offset = (page - 1) * page_size - + address_names = frappe.db.sql(f""" SELECT DISTINCT a.name FROM `tabAddress` a @@ -213,7 +213,7 @@ def get_clients_table_data_v2(filters={}, sortings=[], page=1, page_size=10): LIMIT %s OFFSET %s """, values + [page_size, offset], as_dict=True) print("DEBUG: Address names retrieved:", address_names) - + count = frappe.db.sql(f""" SELECT COUNT(DISTINCT a.name) as count FROM `tabAddress` a @@ -230,18 +230,18 @@ def get_clients_table_data_v2(filters={}, sortings=[], page=1, page_size=10): tableRow["customer_name"] = normalize_name(address.customer_name, "-#-") tableRow["companies"] = ", ".join([link.company for link in address.get("companies", [])]) tableRows.append(tableRow) - + table_data = build_datatable_dict(data=tableRows, count=count, page=page, page_size=page_size) - + return build_success_response(table_data) except frappe.ValidationError as ve: return build_error_response(str(ve), 400) except Exception as e: print("ERROR in get_clients_table_data_v2:", str(e)) return build_error_response(str(e), 500) - - - + + + @frappe.whitelist() @@ -354,11 +354,11 @@ def upsert_client(data): try: data = json.loads(data) print("#####DEBUG: Create client data received:", data) - + customer_name = data.get("customer_name") contacts = data.get("contacts", []) addresses = data.get("addresses", []) - + # Check for existing address client_doc = check_and_get_client_doc(customer_name) if client_doc: @@ -374,7 +374,7 @@ def upsert_client(data): return build_error_response("This address already exists. Please use a different address or search for the address to find the associated client.", 400) # Handle customer creation/update - + print("#####DEBUG: Creating new lead.") customer_type = data.get("customer_type", "Individual") primary_contact = find_primary_contact_or_throw(contacts) @@ -392,7 +392,7 @@ def upsert_client(data): lead_data["company_name"] = data.get("customer_name") client_doc = create_lead(lead_data) print(f"#####DEBUG: {client_doc.doctype}:", client_doc.as_dict()) - + #Handle contact creation contact_docs = [] for contact_data in contacts: @@ -427,7 +427,7 @@ def upsert_client(data): }) ContactService.link_contact_to_customer(contact_doc, "Lead", client_doc.name) contact_docs.append(contact_doc) - + # Link all contacts to client after creating them client_doc.reload() for idx, contact_data in enumerate(contacts): @@ -476,7 +476,7 @@ def upsert_client(data): primary_contact = contact_docs[address.get("primary_contact)", 0)] AddressService.set_primary_contact(address_doc.name, primary_contact.name) address_docs.append(address_doc) - + # Link all addresses to client after creating them client_doc.reload() for address_doc in address_docs: @@ -487,7 +487,7 @@ def upsert_client(data): client_dict = client_doc.as_dict() client_dict["contacts"] = [contact.as_dict() for contact in contact_docs] client_dict["addresses"] = [address.as_dict() for address in address_docs] - + frappe.local.message_log = [] return build_success_response(client_dict) except frappe.ValidationError as ve: @@ -535,8 +535,8 @@ def convert_lead_to_customer(lead_name): lead = frappe.get_doc("Lead", lead_name) customer = make_customer(lead) customer.insert(ignore_permissions=True) - - + + def create_lead(lead_data): lead = frappe.get_doc({ "doctype": "Lead", @@ -551,10 +551,10 @@ def get_customer_or_lead(client_name): else: lead_name = frappe.db.get_all("Lead", pluck="name", filters={"lead_name": client_name})[0] return frappe.get_doc("Lead", lead_name) - + def find_primary_contact_or_throw(contacts): for contact in contacts: if contact.get("is_primary"): print("#####DEBUG: Primary contact found:", contact) return contact - raise ValueError("No primary contact found in contacts list.") \ No newline at end of file + raise ValueError("No primary contact found in contacts list.")