From 0d7976b140aa51eaba7c6ca223ec6d586a94e84b Mon Sep 17 00:00:00 2001 From: Casey Date: Fri, 12 Dec 2025 16:09:03 -0600 Subject: [PATCH] lead/customer backend --- custom_ui/api/db/clients.py | 94 ++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/custom_ui/api/db/clients.py b/custom_ui/api/db/clients.py index b3ffc67..1f88aee 100644 --- a/custom_ui/api/db/clients.py +++ b/custom_ui/api/db/clients.py @@ -210,17 +210,30 @@ def upsert_client(data): print("#####DEBUG: Checking for existing customer with name:", data.get("customer_name")) customer = frappe.db.exists("Customer", {"customer_name": data.get("customer_name")}) if not customer: - customer_doc = frappe.get_doc({ - "doctype": "Customer", + new_lead_data = {"doctype": "Lead"} + is_individual = data.get("customer_type") == "Individual" + primary_contact = next((c for c in data.get("contacts", []) if c.get("is_primary")), None) + if not primary_contact: + return build_error_response("Primary contact information is required to create a new customer.", 400) + if is_individual: + # Grab the contact that has is_primary true + new_lead_data["first_name"] = primary_contact.get("first_name") + new_lead_data["last_name"] = primary_contact.get("last_name") + else: + new_lead_data["company_name"] = data.get("customer_name") + new_lead_data["email_id"] = primary_contact.get("email") + new_lead_data["phone"] = primary_contact.get("phone_number") + new_client_doc = frappe.get_doc({ + "doctype": "Lead", "customer_name": data.get("customer_name"), "customer_type": data.get("customer_type") }).insert(ignore_permissions=True) else: - customer_doc = frappe.get_doc("Customer", data.get("customer_name")) - print("Customer:", customer_doc.as_dict()) + new_client_doc = frappe.get_doc("Customer", data.get("customer_name")) + print(f"#####DEBUG: {new_client_doc.doctype}:", new_client_doc.as_dict()) # Handle address creation - print("#####DEBUG: Checking for existing address for customer:", data.get("customer_name")) + print("#####DEBUG: Checking for existing address for customer/lead:", data.get("customer_name")) existing_address = frappe.db.exists( "Address", { @@ -230,7 +243,7 @@ def upsert_client(data): }) print("Existing address check:", existing_address) if existing_address: - frappe.throw(f"Address already exists for customer {data.get('customer_name')}.", frappe.ValidationError) + return build_error_response("Address already exists for this customer.", 400) address_doc = frappe.get_doc({ "doctype": "Address", @@ -240,8 +253,8 @@ def upsert_client(data): "city": data.get("city"), "state": data.get("state"), "country": "United States", - "pincode": data.get("pincode"), - "custom_customer_to_bill": customer_doc.name + # "custom_customer_to_bill": new_client_doc.name, + "pincode": data.get("pincode") }).insert(ignore_permissions=True) print("Address:", address_doc.as_dict()) @@ -281,34 +294,37 @@ def upsert_client(data): ##### Create links # Customer -> Address - print("#####DEBUG: Creating links between customer, address, and contacts.") - customer_doc.append("custom_select_address", { - "address_name": address_doc.name, - "address_line_1": address_doc.address_line1, - "city": address_doc.city, - "state": address_doc.state, - "pincode": address_doc.pincode - }) - - # Customer -> Contact - print("#####DEBUG: Linking contacts to customer.") - for contact_doc in contact_docs: - print("Linking contact:", contact_doc.as_dict()) - print("with role:", contact_doc.role) - print("customer to append to:", customer_doc.as_dict()) - customer_doc.append("custom_add_contacts", { - "contact": contact_doc.name, - "email": contact_doc.custom_email, - "phone": contact_doc.phone, - "role": contact_doc.role + if new_client_doc.doctype == "Customer": + print("#####DEBUG: Creating links between customer, address, and contacts.") + new_client_doc.append("custom_select_address", { + "address_name": address_doc.name, + "address_line_1": address_doc.address_line1, + "city": address_doc.city, + "state": address_doc.state, + "pincode": address_doc.pincode }) - # Address -> Customer + # Customer -> Contact + print("#####DEBUG: Linking contacts to customer.") + for contact_doc in contact_docs: + print("Linking contact:", contact_doc.as_dict()) + print("with role:", contact_doc.role) + print("customer to append to:", new_client_doc.as_dict()) + new_client_doc.append("custom_add_contacts", { + "contact": contact_doc.name, + "email": contact_doc.custom_email, + "phone": contact_doc.phone, + "role": contact_doc.role + }) + new_client_doc.save(ignore_permissions=True) + + # Address -> Customer/Lead print("#####DEBUG: Linking address to customer.") address_doc.append("links", { - "link_doctype": "Customer", - "link_name": customer_doc.name + "link_doctype": new_client_doc.doctype, + "link_name": new_client_doc.name }) + # Address -> Contact print("#####DEBUG: Linking address to contacts.") @@ -320,27 +336,27 @@ def upsert_client(data): "phone": contact_doc.phone, "role": contact_doc.role }) + + address_doc.save(ignore_permissions=True) - # Contact -> Customer & Address + # Contact -> Customer/Lead & Address print("#####DEBUG: Linking contacts to customer.") for contact_doc in contact_docs: contact_doc.address = address_doc.name contact_doc.append("links", { - "link_doctype": "Customer", - "link_name": customer_doc.name + "link_doctype": new_client_doc.doctype, + "link_name": new_client_doc.name }) contact_doc.append("links", { "link_doctype": "Address", "link_name": address_doc.name }) - contact_doc.custom_customer = customer_doc.name + contact_doc.custom_customer = new_client_doc.name contact_doc.save(ignore_permissions=True) - - address_doc.save(ignore_permissions=True) - customer_doc.save(ignore_permissions=True) + frappe.local.message_log = [] return build_success_response({ - "customer": customer_doc.as_dict(), + "customer": new_client_doc.as_dict(), "address": address_doc.as_dict(), "contacts": [contact_doc.as_dict() for contact_doc in contact_docs] })