lead/customer backend
This commit is contained in:
parent
ae5cb23e6e
commit
0d7976b140
1 changed files with 55 additions and 39 deletions
|
|
@ -210,17 +210,30 @@ def upsert_client(data):
|
||||||
print("#####DEBUG: Checking for existing customer with name:", data.get("customer_name"))
|
print("#####DEBUG: Checking for existing customer with name:", data.get("customer_name"))
|
||||||
customer = frappe.db.exists("Customer", {"customer_name": data.get("customer_name")})
|
customer = frappe.db.exists("Customer", {"customer_name": data.get("customer_name")})
|
||||||
if not customer:
|
if not customer:
|
||||||
customer_doc = frappe.get_doc({
|
new_lead_data = {"doctype": "Lead"}
|
||||||
"doctype": "Customer",
|
is_individual = data.get("customer_type") == "Individual"
|
||||||
|
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)
|
||||||
|
if is_individual:
|
||||||
|
# Grab the contact that has is_primary true
|
||||||
|
new_lead_data["first_name"] = primary_contact.get("first_name")
|
||||||
|
new_lead_data["last_name"] = primary_contact.get("last_name")
|
||||||
|
else:
|
||||||
|
new_lead_data["company_name"] = data.get("customer_name")
|
||||||
|
new_lead_data["email_id"] = primary_contact.get("email")
|
||||||
|
new_lead_data["phone"] = primary_contact.get("phone_number")
|
||||||
|
new_client_doc = frappe.get_doc({
|
||||||
|
"doctype": "Lead",
|
||||||
"customer_name": data.get("customer_name"),
|
"customer_name": data.get("customer_name"),
|
||||||
"customer_type": data.get("customer_type")
|
"customer_type": data.get("customer_type")
|
||||||
}).insert(ignore_permissions=True)
|
}).insert(ignore_permissions=True)
|
||||||
else:
|
else:
|
||||||
customer_doc = frappe.get_doc("Customer", data.get("customer_name"))
|
new_client_doc = frappe.get_doc("Customer", data.get("customer_name"))
|
||||||
print("Customer:", customer_doc.as_dict())
|
print(f"#####DEBUG: {new_client_doc.doctype}:", new_client_doc.as_dict())
|
||||||
|
|
||||||
# Handle address creation
|
# Handle address creation
|
||||||
print("#####DEBUG: Checking for existing address for customer:", data.get("customer_name"))
|
print("#####DEBUG: Checking for existing address for customer/lead:", data.get("customer_name"))
|
||||||
existing_address = frappe.db.exists(
|
existing_address = frappe.db.exists(
|
||||||
"Address",
|
"Address",
|
||||||
{
|
{
|
||||||
|
|
@ -230,7 +243,7 @@ def upsert_client(data):
|
||||||
})
|
})
|
||||||
print("Existing address check:", existing_address)
|
print("Existing address check:", existing_address)
|
||||||
if existing_address:
|
if existing_address:
|
||||||
frappe.throw(f"Address already exists for customer {data.get('customer_name')}.", frappe.ValidationError)
|
return build_error_response("Address already exists for this customer.", 400)
|
||||||
|
|
||||||
address_doc = frappe.get_doc({
|
address_doc = frappe.get_doc({
|
||||||
"doctype": "Address",
|
"doctype": "Address",
|
||||||
|
|
@ -240,8 +253,8 @@ def upsert_client(data):
|
||||||
"city": data.get("city"),
|
"city": data.get("city"),
|
||||||
"state": data.get("state"),
|
"state": data.get("state"),
|
||||||
"country": "United States",
|
"country": "United States",
|
||||||
"pincode": data.get("pincode"),
|
# "custom_customer_to_bill": new_client_doc.name,
|
||||||
"custom_customer_to_bill": customer_doc.name
|
"pincode": data.get("pincode")
|
||||||
}).insert(ignore_permissions=True)
|
}).insert(ignore_permissions=True)
|
||||||
print("Address:", address_doc.as_dict())
|
print("Address:", address_doc.as_dict())
|
||||||
|
|
||||||
|
|
@ -281,34 +294,37 @@ def upsert_client(data):
|
||||||
|
|
||||||
##### Create links
|
##### Create links
|
||||||
# Customer -> Address
|
# Customer -> Address
|
||||||
print("#####DEBUG: Creating links between customer, address, and contacts.")
|
if new_client_doc.doctype == "Customer":
|
||||||
customer_doc.append("custom_select_address", {
|
print("#####DEBUG: Creating links between customer, address, and contacts.")
|
||||||
"address_name": address_doc.name,
|
new_client_doc.append("custom_select_address", {
|
||||||
"address_line_1": address_doc.address_line1,
|
"address_name": address_doc.name,
|
||||||
"city": address_doc.city,
|
"address_line_1": address_doc.address_line1,
|
||||||
"state": address_doc.state,
|
"city": address_doc.city,
|
||||||
"pincode": address_doc.pincode
|
"state": address_doc.state,
|
||||||
})
|
"pincode": address_doc.pincode
|
||||||
|
|
||||||
# Customer -> Contact
|
|
||||||
print("#####DEBUG: Linking contacts to customer.")
|
|
||||||
for contact_doc in contact_docs:
|
|
||||||
print("Linking contact:", contact_doc.as_dict())
|
|
||||||
print("with role:", contact_doc.role)
|
|
||||||
print("customer to append to:", customer_doc.as_dict())
|
|
||||||
customer_doc.append("custom_add_contacts", {
|
|
||||||
"contact": contact_doc.name,
|
|
||||||
"email": contact_doc.custom_email,
|
|
||||||
"phone": contact_doc.phone,
|
|
||||||
"role": contact_doc.role
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# Address -> Customer
|
# Customer -> Contact
|
||||||
|
print("#####DEBUG: Linking contacts to customer.")
|
||||||
|
for contact_doc in contact_docs:
|
||||||
|
print("Linking contact:", contact_doc.as_dict())
|
||||||
|
print("with role:", contact_doc.role)
|
||||||
|
print("customer to append to:", new_client_doc.as_dict())
|
||||||
|
new_client_doc.append("custom_add_contacts", {
|
||||||
|
"contact": contact_doc.name,
|
||||||
|
"email": contact_doc.custom_email,
|
||||||
|
"phone": contact_doc.phone,
|
||||||
|
"role": contact_doc.role
|
||||||
|
})
|
||||||
|
new_client_doc.save(ignore_permissions=True)
|
||||||
|
|
||||||
|
# Address -> Customer/Lead
|
||||||
print("#####DEBUG: Linking address to customer.")
|
print("#####DEBUG: Linking address to customer.")
|
||||||
address_doc.append("links", {
|
address_doc.append("links", {
|
||||||
"link_doctype": "Customer",
|
"link_doctype": new_client_doc.doctype,
|
||||||
"link_name": customer_doc.name
|
"link_name": new_client_doc.name
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
# Address -> Contact
|
# Address -> Contact
|
||||||
print("#####DEBUG: Linking address to contacts.")
|
print("#####DEBUG: Linking address to contacts.")
|
||||||
|
|
@ -320,27 +336,27 @@ def upsert_client(data):
|
||||||
"phone": contact_doc.phone,
|
"phone": contact_doc.phone,
|
||||||
"role": contact_doc.role
|
"role": contact_doc.role
|
||||||
})
|
})
|
||||||
|
|
||||||
|
address_doc.save(ignore_permissions=True)
|
||||||
|
|
||||||
# Contact -> Customer & Address
|
# Contact -> Customer/Lead & Address
|
||||||
print("#####DEBUG: Linking contacts to customer.")
|
print("#####DEBUG: Linking contacts to customer.")
|
||||||
for contact_doc in contact_docs:
|
for contact_doc in contact_docs:
|
||||||
contact_doc.address = address_doc.name
|
contact_doc.address = address_doc.name
|
||||||
contact_doc.append("links", {
|
contact_doc.append("links", {
|
||||||
"link_doctype": "Customer",
|
"link_doctype": new_client_doc.doctype,
|
||||||
"link_name": customer_doc.name
|
"link_name": new_client_doc.name
|
||||||
})
|
})
|
||||||
contact_doc.append("links", {
|
contact_doc.append("links", {
|
||||||
"link_doctype": "Address",
|
"link_doctype": "Address",
|
||||||
"link_name": address_doc.name
|
"link_name": address_doc.name
|
||||||
})
|
})
|
||||||
contact_doc.custom_customer = customer_doc.name
|
contact_doc.custom_customer = new_client_doc.name
|
||||||
contact_doc.save(ignore_permissions=True)
|
contact_doc.save(ignore_permissions=True)
|
||||||
|
|
||||||
address_doc.save(ignore_permissions=True)
|
|
||||||
customer_doc.save(ignore_permissions=True)
|
|
||||||
frappe.local.message_log = []
|
frappe.local.message_log = []
|
||||||
return build_success_response({
|
return build_success_response({
|
||||||
"customer": customer_doc.as_dict(),
|
"customer": new_client_doc.as_dict(),
|
||||||
"address": address_doc.as_dict(),
|
"address": address_doc.as_dict(),
|
||||||
"contacts": [contact_doc.as_dict() for contact_doc in contact_docs]
|
"contacts": [contact_doc.as_dict() for contact_doc in contact_docs]
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue