big update
This commit is contained in:
parent
6853950cc5
commit
992672b51b
11 changed files with 647 additions and 64 deletions
|
|
@ -258,6 +258,9 @@ def upsert_client(data):
|
|||
contacts = data.get("contacts", [])
|
||||
|
||||
# Check for existing address
|
||||
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)
|
||||
if address_exists(
|
||||
data.get("address_line1"),
|
||||
data.get("address_line2"),
|
||||
|
|
@ -268,23 +271,22 @@ 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
|
||||
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("#####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
|
||||
|
|
@ -295,7 +297,9 @@ def upsert_client(data):
|
|||
"city": data.get("city"),
|
||||
"state": data.get("state"),
|
||||
"country": "United States",
|
||||
"pincode": data.get("pincode")
|
||||
"pincode": data.get("pincode"),
|
||||
"customer_type": "Lead",
|
||||
"customer_name": client_doc.name
|
||||
})
|
||||
|
||||
#Handle contact creation
|
||||
|
|
@ -330,24 +334,24 @@ def upsert_client(data):
|
|||
})
|
||||
contact_docs.append(contact_doc)
|
||||
|
||||
##### Create links
|
||||
# Customer -> Address
|
||||
if client_doc.doctype == "Customer":
|
||||
print("#####DEBUG: Linking address to customer.")
|
||||
client_doc.append("custom_select_address", {
|
||||
"address_name": address_doc.name,
|
||||
})
|
||||
# ##### Create links
|
||||
# # Customer -> Address
|
||||
# if client_doc.doctype == "Customer":
|
||||
# print("#####DEBUG: Linking address to customer.")
|
||||
# client_doc.append("custom_select_address", {
|
||||
# "address_name": address_doc.name,
|
||||
# })
|
||||
|
||||
# Customer -> Contact
|
||||
print("#####DEBUG: Linking contacts to customer.")
|
||||
for contact_doc in contact_docs:
|
||||
client_doc.append("custom_add_contacts", {
|
||||
"contact": contact_doc.name,
|
||||
"email": contact_doc.custom_email,
|
||||
"phone": contact_doc.phone,
|
||||
"role": contact_doc.role
|
||||
})
|
||||
client_doc.save(ignore_permissions=True)
|
||||
# # Customer -> Contact
|
||||
# print("#####DEBUG: Linking contacts to customer.")
|
||||
# for contact_doc in contact_docs:
|
||||
# client_doc.append("custom_add_contacts", {
|
||||
# "contact": contact_doc.name,
|
||||
# "email": contact_doc.custom_email,
|
||||
# "phone": contact_doc.phone,
|
||||
# "role": contact_doc.role
|
||||
# })
|
||||
# client_doc.save(ignore_permissions=True)
|
||||
|
||||
# Address -> Customer/Lead
|
||||
create_address_links(address_doc, client_doc, contact_docs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue