update creation
This commit is contained in:
parent
97241f14ea
commit
a96832c21b
4 changed files with 119 additions and 71 deletions
|
|
@ -189,7 +189,7 @@ def get_clients_table_data(filters={}, sortings=[], page=1, page_size=10):
|
|||
customer_name = "N/A"
|
||||
elif not customer_name and customer_links:
|
||||
print("DEBUG: No customer to bill. Customer links found:", customer_links)
|
||||
customer_name = frappe.get_value("Lead", customer_links[0].link_name, "lead_name") if is_lead else customer_links[0].link_name
|
||||
customer_name = frappe.get_value("Lead", customer_links[0].link_name, "custom_customer_name") if is_lead else customer_links[0].link_name
|
||||
tableRow["id"] = address["name"]
|
||||
tableRow["customer_name"] = customer_name
|
||||
tableRow["address"] = (
|
||||
|
|
@ -213,10 +213,15 @@ def get_clients_table_data(filters={}, sortings=[], page=1, page_size=10):
|
|||
|
||||
@frappe.whitelist()
|
||||
def upsert_client(data):
|
||||
"""Create or update a client (customer and address)."""
|
||||
"""Create a client (customer and address)."""
|
||||
try:
|
||||
data = json.loads(data)
|
||||
print("#####DEBUG: Upsert client data received:", data)
|
||||
print("#####DEBUG: Create client data received:", data)
|
||||
|
||||
customer_name = data.get("customer_name")
|
||||
contacts = data.get("contacts", [])
|
||||
|
||||
# Check for existing address
|
||||
if address_exists(
|
||||
data.get("address_line1"),
|
||||
data.get("address_line2"),
|
||||
|
|
@ -227,37 +232,28 @@ def upsert_client(data):
|
|||
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
|
||||
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:
|
||||
print("#####DEBUG: No existing customer found. Checking for existing lead")
|
||||
customer = frappe.db.exists("Lead", {"lead_name": data.get("customer_name")})
|
||||
if not customer:
|
||||
print("#####DEBUG: No existing lead found. Creating new lead.")
|
||||
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)
|
||||
print("#####DEBUG: Primary contact found:", primary_contact)
|
||||
client_doc = create_lead({
|
||||
"lead_name": data.get("customer_name"),
|
||||
"first_name": primary_contact.get("first_name"),
|
||||
"last_name": primary_contact.get("last_name"),
|
||||
"email_id": primary_contact.get("email"),
|
||||
"phone": primary_contact.get("phone_number"),
|
||||
"customer_type": data.get("customer_type"),
|
||||
"company": data.get("company")
|
||||
})
|
||||
else:
|
||||
print("#####DEBUG: Existing lead found:", customer)
|
||||
client_doc = frappe.get_doc("Lead", customer)
|
||||
else:
|
||||
print("#####DEBUG: Existing customer found:", customer)
|
||||
client_doc = frappe.get_doc("Customer", customer)
|
||||
client_doc = check_and_get_client_doc(customer_name)
|
||||
if not client_doc:
|
||||
print("#####DEBUG: Creating new lead.")
|
||||
customer_type = data.get("customer_type", "Individual")
|
||||
primary_contact = find_primary_contact_or_throw(contacts)
|
||||
lead_data = {
|
||||
"first_name": primary_contact.get("first_name"),
|
||||
"last_name": primary_contact.get("last_name"),
|
||||
"email_id": primary_contact.get("email"),
|
||||
"phone": primary_contact.get("phone_number"),
|
||||
"company": data.get("company"),
|
||||
"custom_customer_name": customer_name,
|
||||
"customer_type": customer_type
|
||||
}
|
||||
if customer_type == "Company":
|
||||
lead_data["company_name"] = data.get("customer_name")
|
||||
client_doc = create_lead(lead_data)
|
||||
print(f"#####DEBUG: {client_doc.doctype}:", client_doc.as_dict())
|
||||
|
||||
# Handle address creation
|
||||
address_doc = create_address({
|
||||
"address_title": build_address_title(data.get("customer_name"), data),
|
||||
"address_title": build_address_title(customer_name, data),
|
||||
"address_line1": data.get("address_line1"),
|
||||
"address_line2": data.get("address_line2"),
|
||||
"city": data.get("city"),
|
||||
|
|
@ -268,7 +264,7 @@ def upsert_client(data):
|
|||
|
||||
#Handle contact creation
|
||||
contact_docs = []
|
||||
for contact_data in data.get("contacts", []):
|
||||
for contact_data in contacts:
|
||||
if isinstance(contact_data, str):
|
||||
contact_data = json.loads(contact_data)
|
||||
print("#####DEBUG: Processing contact data:", contact_data)
|
||||
|
|
@ -283,8 +279,6 @@ def upsert_client(data):
|
|||
contact_doc = create_contact({
|
||||
"first_name": contact_data.get("first_name"),
|
||||
"last_name": contact_data.get("last_name"),
|
||||
# "email_id": contact_data.get("email"),
|
||||
# "phone": contact_data.get("phone_number"),
|
||||
"role": contact_data.get("contact_role", "Other"),
|
||||
"custom_email": contact_data.get("email"),
|
||||
"is_primary_contact":1 if contact_data.get("is_primary", False) else 0,
|
||||
|
|
@ -362,7 +356,11 @@ def check_and_get_client_doc(client_name):
|
|||
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})
|
||||
lead_name = frappe.db.get_all("Lead", pluck="name", filters={"company_name": client_name})
|
||||
if not lead_name:
|
||||
lead_name = frappe.db.get_all("Lead", pluck="name", filters={"lead_name": client_name})
|
||||
if not lead_name:
|
||||
lead_name = frappe.db.get_all("Lead", pluck="name", filters={"custom_customer_name": client_name})
|
||||
if lead_name:
|
||||
print("DEBUG: Client found as Lead.")
|
||||
customer = frappe.get_doc("Lead", lead_name[0])
|
||||
|
|
@ -387,4 +385,11 @@ def get_customer_or_lead(client_name):
|
|||
return frappe.get_doc("Customer", client_name)
|
||||
else:
|
||||
lead_name = frappe.db.get_all("Lead", pluck="name", filters={"lead_name": client_name})[0]
|
||||
return frappe.get_doc("Lead", lead_name)
|
||||
return frappe.get_doc("Lead", lead_name)
|
||||
|
||||
def find_primary_contact_or_throw(contacts):
|
||||
for contact in contacts:
|
||||
if contact.get("is_primary"):
|
||||
print("#####DEBUG: Primary contact found:", contact)
|
||||
return contact
|
||||
raise ValueError("No primary contact found in contacts list.")
|
||||
Loading…
Add table
Add a link
Reference in a new issue