add query for client details

This commit is contained in:
Casey 2025-11-13 15:17:43 -06:00
parent 172927e069
commit 0b280cec8e
8 changed files with 114 additions and 69 deletions

View file

@ -92,50 +92,90 @@ def get_client_status_counts(weekly=False, week_start_date=None, week_end_date=N
def get_client(client_name):
"""Get detailed information for a specific client including address, customer, and projects."""
try:
address = frappe.get_doc("Address", client_name)
customer_name = address.custom_customer_to_bill if address.custom_customer_to_bill else [link.link_name for link in address.links if link.link_doctype == "Customer"][0] if address.links else None
if not customer_name:
raise Exception(f"No customer linked to address {client_name}. Suggested fix: Ensure the address is linked to a customer via the ERPnext UI.")
project_names = frappe.db.get_all("Project", fields=["name"], or_filters=[
["custom_installation_address", "=", address.address_title],
["custom_address", "=", address.address_title]
], limit_page_length=100)
# contacts = [] # currently not needed as the customer doctype comes with contacts
onsite_meetings = frappe.db.get_all(
"On-Site Meeting",
fields=["*"],
filters={"address": address.address_title}
)
quotations = frappe.db.get_all(
"Quotation",
fields=["*"],
filters={"custom_installation_address": address.address_title}
)
sales_orders = []
projects = [frappe.get_doc("Project", proj["name"]) for proj in project_names]
sales_invoices = []
payment_entries = frappe.db.get_all(
doctype="Payment Entry",
fields=["*"],
filters={"party": customer_name})
payment_orders = []
jobs = []
for project in projects:
job = []
jobs.append(job)
customer = frappe.get_doc("Customer", customer_name)
# get all associated data as needed
return build_success_response({
"address": address,
"customer": customer,
# "contacts": [], # currently not needed as the customer doctype comes with contacts
"jobs": jobs,
"sales_invoices": sales_invoices,
"payment_entries": payment_entries,
"sales_orders": sales_orders,
"quotations": quotations,
"onsite_meetings": onsite_meetings,
})
clientData = {"addresses": []}
customer = frappe.get_doc("Customer", client_name)
clientData = {**clientData, **customer.as_dict()}
addresses = frappe.db.get_all("Address", fields=["*"], filters={"custom_customer_to_bill": client_name})
for address in addresses if addresses else []:
addressData = {"jobs": []}
addressData = {**addressData, **address}
addressData["estimates"] = frappe.db.get_all("Quotation", fields=["*"], filters={"custom_installation_address": address.address_title})
addressData["onsite_meetings"] = frappe.db.get_all("On-Site Meeting", fields=["*"], filters={"address": address.address_title})
jobs = frappe.db.get_all("Project", fields=["*"], or_filters=[
["custom_installation_address", "=", address.address_title],
["custom_address", "=", address.address_title]
])
for job in jobs if jobs else []:
jobData = {}
jobData = {**jobData, **job}
jobData["sales_invoices"] = frappe.db.get_all("Sales Invoice", fields=["*"], filters={"project": job.name})
jobData["payment_entries"] = frappe.db.get_all(
"Payment Entry",
fields=["*"],
filters={"party_type": "Customer"},
or_filters=[
["party", "=", client_name],
["party_name", "=", 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(addressData)
return build_success_response(clientData)
except frappe.ValidationError as ve:
return build_error_response(str(ve), 400)
except Exception as e:
return build_error_response(str(e), 500)
# address = frappe.get_doc("Address", client_name)
# customer_name = address.custom_customer_to_bill if address.custom_customer_to_bill else [link.link_name for link in address.links if link.link_doctype == "Customer"][0] if address.links else None
# if not customer_name:
# raise Exception(f"No customer linked to address {client_name}. Suggested fix: Ensure the address is linked to a customer via the ERPnext UI.")
# project_names = frappe.db.get_all("Project", fields=["name"], or_filters=[
# ["custom_installation_address", "=", address.address_title],
# ["custom_address", "=", address.address_title]
# ], limit_page_length=100)
# # contacts = [] # currently not needed as the customer doctype comes with contacts
# onsite_meetings = frappe.db.get_all(
# "On-Site Meeting",
# fields=["*"],
# filters={"address": address.address_title}
# )
# quotations = frappe.db.get_all(
# "Quotation",
# fields=["*"],
# filters={"custom_installation_address": address.address_title}
# )
# sales_orders = []
# projects = [frappe.get_doc("Project", proj["name"]) for proj in project_names]
# sales_invoices = []
# payment_entries = frappe.db.get_all(
# "Payment Entry",
# fields=["*"],
# filters={"party_type": "Customer"},
# or_filters=[
# ["party", "=", customer_name],
# ["party_name", "=", customer_name]
# ])
# payment_orders = []
# jobs = []
# for project in projects:
# job = []
# jobs.append(job)
# customer = frappe.get_doc("Customer", customer_name)
# # get all associated data as needed
# return build_success_response({
# "address": address,
# "customer": customer,
# # "contacts": [], # currently not needed as the customer doctype comes with contacts
# "jobs": jobs,
# "sales_invoices": sales_invoices,
# "payment_entries": payment_entries,
# "sales_orders": sales_orders,
# "quotations": quotations,
# "onsite_meetings": onsite_meetings,
# })
except frappe.ValidationError as ve:
return build_error_response(str(ve), 400)
except Exception as e: