fix client creation

This commit is contained in:
Casey 2025-12-13 10:14:08 -06:00
parent 0c1bb52f1b
commit d4545d753a
2 changed files with 39 additions and 11 deletions

View File

@ -4,9 +4,21 @@ from custom_ui.db_utils import build_error_response, build_success_response
@frappe.whitelist()
def get_address_by_full_address(full_address):
"""Get address by full_address, including associated contacts."""
print(f"DEBUG: get_address_by_full_address called with full_address: {full_address}")
try:
address = frappe.get_doc("Address", {"full_address": full_address}).as_dict()
address["customer"] = frappe.get_doc("Customer", address.get("custom_customer_to_bill")).as_dict()
customer_exists = frappe.db.exists("Customer", address.get("custom_customer_to_bill"))
doctype = "Customer" if customer_exists else "Lead"
name = ""
if doctype == "Customer":
name = address.get("custom_customer_to_bill")
else:
## filter through links for one with doctype Lead
lead_links = address.get("links", [])
print(f"DEBUG: lead_links: {lead_links}")
lead_name = [link.link_name for link in lead_links if link.link_doctype == "Lead"]
name = lead_name[0] if lead_name else ""
address["customer"] = frappe.get_doc(doctype, name).as_dict()
contacts = []
for contact_link in address.custom_linked_contacts:
contact_doc = frappe.get_doc("Contact", contact_link.contact)

View File

@ -91,17 +91,30 @@ def get_client_status_counts(weekly=False, week_start_date=None, week_end_date=N
@frappe.whitelist()
def get_client(client_name):
"""Get detailed information for a specific client including address, customer, and projects."""
print("DEBUG: get_client called with client_name:", client_name)
try:
clientData = {"addresses": [], "contacts": [], "jobs": [], "sales_invoices": [], "payment_entries": [], "sales_orders": [], "tasks": []}
customer = frappe.get_doc("Customer", client_name)
client_exists = frappe.db.exists("Customer", client_name)
if client_exists:
customer = frappe.get_doc("Customer", client_name)
else:
print("DEBUG: Client not found as Customer. Checking Lead.")
lead_name = frappe.db.get_all("Lead", pluck="name", filters={"lead_name": client_name})[0]
customer = frappe.get_doc("Lead", lead_name)
clientData = {**clientData, **customer.as_dict()}
if customer.doctype == "Customer":
for contact_link in customer.custom_add_contacts:
contact_doc = frappe.get_doc("Contact", contact_link.contact)
clientData["contacts"].append(contact_doc.as_dict())
else:
contact_names = frappe.db.get_all("Contact", pluck="name", filters={"links": ["like", f'%{{"link_doctype": "Lead", "link_name": client_name}}%']})
for contact_name in contact_names:
contact_doc = frappe.get_doc("Contact", contact_name)
clientData["contacts"].append(contact_doc.as_dict())
for contact_link in customer.custom_add_contacts:
contact_doc = frappe.get_doc("Contact", contact_link.contact)
clientData["contacts"].append(contact_doc.as_dict())
for address_link in customer.custom_select_address:
address_doc = frappe.get_doc("Address", address_link.address_name)
if customer.doctype == "Customer":
for address_link in customer.custom_select_address:
address_doc = frappe.get_doc("Address", address_link.address_name)
# # addressData = {"jobs": [], "contacts": []}
# addressData = {**addressData, **address_doc.as_dict()}
# addressData["estimates"] = frappe.db.get_all("Quotation", fields=["*"], filters={"custom_installation_address": address_doc.address_title})
@ -125,7 +138,12 @@ def get_client(client_name):
# jobData["sales_orders"] = frappe.db.get_all("Sales Order", fields=["*"], filters={"project": job.name})
# jobData["tasks"] = frappe.db.get_all("Task", fields=["*"], filters={"project": job.name})
# addressData["jobs"].append(jobData)
clientData["addresses"].append(address_doc.as_dict())
clientData["addresses"].append(address_doc.as_dict())
else:
address_names = frappe.db.get_all("Address", pluck="name", filters={"links": ["like", f'%{{"link_doctype": "Lead", "link_name": client_name}}%']})
for address_name in address_names:
address_doc = frappe.get_doc("Address", address_name)
clientData["addresses"].append(address_doc.as_dict())
return build_success_response(clientData)
except frappe.ValidationError as ve:
return build_error_response(str(ve), 400)
@ -254,7 +272,6 @@ def upsert_client(data):
print("Existing address check:", existing_address)
if existing_address:
return build_error_response("Address already exists for this customer.", 400)
address_doc = frappe.get_doc({
"doctype": "Address",
"address_title": data.get("address_title"),
@ -263,7 +280,6 @@ def upsert_client(data):
"city": data.get("city"),
"state": data.get("state"),
"country": "United States",
# "custom_customer_to_bill": new_client_doc.name,
"pincode": data.get("pincode")
}).insert(ignore_permissions=True)
print("Address:", address_doc.as_dict())