added ability to link address/contacts if they already exist

This commit is contained in:
Casey 2026-02-14 08:47:53 -06:00
parent cf577f3ac7
commit 8ebd77540c
11 changed files with 450 additions and 224 deletions

View file

@ -9,6 +9,25 @@ from custom_ui.services import AddressService, ContactService, ClientService
# CLIENT MANAGEMENT API METHODS
# ===============================================================================
@frappe.whitelist()
def check_client_exists(client_name):
"""Check if a client exists as either a Customer or a Lead.
Additionally, return a list of potential matches based on the client name."""
print("DEBUG: check_client_exists called with client_name:", client_name)
try:
exact_customer_match = frappe.db.exists("Customer", client_name)
exact_lead_match = frappe.db.exists("Lead", {"custom_customer_name": client_name})
customer_matches = frappe.get_all("Customer", pluck="name", filters={"name": ["like", f"%{client_name}%"]})
lead_matches = frappe.get_all("Lead", pluck="custom_customer_name", filters={"custom_customer_name": ["like", f"%{client_name}%"]})
# remove duplicates from potential matches between customers and leads
return build_success_response({
"exact_match": exact_customer_match or exact_lead_match,
"potential_matches": list(set(customer_matches + lead_matches))
})
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def get_client_status_counts(weekly=False, week_start_date=None, week_end_date=None):
"""Get counts of clients by status categories with optional weekly filtering."""
@ -363,15 +382,15 @@ def upsert_client(data):
client_doc = check_and_get_client_doc(customer_name)
if client_doc:
return build_error_response(f"Client with name '{customer_name}' already exists.", 400)
for address in addresses:
if address_exists(
address.get("address_line1"),
address.get("address_line2"),
address.get("city"),
address.get("state"),
address.get("pincode")
):
return build_error_response("This address already exists. Please use a different address or search for the address to find the associated client.", 400)
# for address in addresses:
# if address_exists(
# address.get("address_line1"),
# address.get("address_line2"),
# address.get("city"),
# address.get("state"),
# address.get("pincode")
# ):
# 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
@ -444,25 +463,36 @@ def upsert_client(data):
# Handle address creation
address_docs = []
for address in addresses:
is_billing = True if address.get("is_billing_address") else False
is_service = True if address.get("is_service_address") else False
print("#####DEBUG: Creating address with data:", address)
address_doc = AddressService.create_address({
"address_title": AddressService.build_address_title(customer_name, address),
address_exists = frappe.db.exists("Address", {
"address_line1": address.get("address_line1"),
"address_line2": address.get("address_line2"),
"address_type": "Billing" if is_billing else "Service",
"custom_billing_address": is_billing,
"is_service_address": is_service,
"is_primary_address": is_billing,
"city": address.get("city"),
"state": address.get("state"),
"country": "United States",
"pincode": address.get("pincode"),
"customer_type": "Lead",
"customer_name": client_doc.name,
"companies": [{ "company": data.get("company_name") }]
"pincode": address.get("pincode")
})
address_doc = None
if address_exists:
address_doc = frappe.get_doc("Address", address_exists)
else:
print("#####DEBUG: Creating address with data:", address)
address_doc = AddressService.create_address({
"address_title": AddressService.build_address_title(customer_name, address),
"address_line1": address.get("address_line1"),
"address_line2": address.get("address_line2"),
"address_type": "Billing" if is_billing else "Service",
"custom_billing_address": is_billing,
"is_service_address": is_service,
"is_primary_address": is_billing,
"city": address.get("city"),
"state": address.get("state"),
"country": "United States",
"pincode": address.get("pincode"),
"customer_type": "Lead",
"customer_name": client_doc.name,
"companies": [{ "company": data.get("company_name") }]
})
AddressService.link_address_to_customer(address_doc, "Lead", client_doc.name)
address_doc.reload()
if is_billing: