get create client working

This commit is contained in:
Casey 2025-11-25 06:22:44 -06:00
parent 4a3576168a
commit cb33d0c3b3
11 changed files with 1179 additions and 336 deletions

View file

@ -96,6 +96,8 @@ def get_client(client_name):
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})
contacts = frappe.db.get_all("Contact", fields=["*"], filters={"custom_customer": client_name})
clientData["contacts"] = contacts
for address in addresses if addresses else []:
addressData = {"jobs": []}
addressData = {**addressData, **address}
@ -263,7 +265,7 @@ def upsert_client(data):
"customer_type": data.get("customer_type")
}).insert(ignore_permissions=True)
else:
customer_doc = frappe.get_doc("Customer", customer)
customer_doc = frappe.get_doc("Customer", data.get("customer_name"))
print("Customer:", customer_doc.as_dict())
@ -281,6 +283,7 @@ def upsert_client(data):
# Create address
address_doc = frappe.get_doc({
"doctype": "Address",
"address_title": data.get("address_title"),
"address_line1": data.get("address_line1"),
"address_line2": data.get("address_line2"),
"city": data.get("city"),
@ -297,12 +300,43 @@ def upsert_client(data):
}
address_doc.append("links", link)
address_doc.save(ignore_permissions=True)
contact_exists = frappe.db.exists("Contact", {"email_id": data.get("contact_email")})
if not contact_exists:
contact_doc = frappe.get_doc({
"doctype": "Contact",
"first_name": data.get("first_name"),
"last_name": data.get("last_name"),
"email_id": data.get("email"),
"phone": data.get("phone_number"),
"custom_customer": customer_doc.name,
"links": [{
"link_doctype": "Customer",
"link_name": customer_doc.name
}]
}).insert(ignore_permissions=True)
print("Created new contact:", contact_doc.as_dict())
else:
contact_doc = frappe.get_doc("Contact", {"email_id": data.get("contact_email")})
print("Contact already exists:", contact_doc.as_dict())
return build_success_response({
"customer": customer_doc.as_dict(),
"address": address_doc.as_dict()
"address": address_doc.as_dict(),
"contact": contact_doc.as_dict()
})
except frappe.ValidationError as ve:
return build_error_response(str(ve), 400)
except Exception as e:
return build_error_response(str(e), 500)
return build_error_response(str(e), 500)
@frappe.whitelist()
def get_client_names(search_term):
"""Search for client names matching the search term."""
try:
search_pattern = f"%{search_term}%"
client_names = frappe.db.get_all(
"Customer",
pluck="name")
return build_success_response(client_names)
except Exception as e:
return build_error_response(str(e), 500)

View file

@ -1,6 +1,7 @@
import frappe
import requests
from urllib.parse import urlparse
from custom_ui.db_utils import build_success_response, build_error_response
import logging
allowed_hosts = ["api.zippopotam.us", "nominatim.openstreetmap.org"] # Update this list with trusted domains as needed
@ -24,9 +25,9 @@ def request(url, method="GET", data=None, headers=None):
)
resp.raise_for_status()
try:
return resp.json()
return build_success_response(resp.json())
except ValueError:
return {"text": resp.text}
return build_success_response({"text": resp.text})
except requests.exceptions.RequestException as e:
frappe.log_error(message=str(e), title="Proxy Request Failed")
frappe.throw("Failed to fetch data from external API.")