update mapping of data return for a lead

This commit is contained in:
Casey 2025-12-16 10:26:58 -06:00
parent 948c0b07a4
commit e7a6bd8ed2
3 changed files with 49 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import frappe, json
from custom_ui.db_utils import build_error_response, process_query_conditions, build_datatable_dict, get_count_or_filters, build_success_response
from custom_ui.db_utils import build_error_response, process_query_conditions, build_datatable_dict, get_count_or_filters, build_success_response, map_lead_client
# ===============================================================================
# CLIENT MANAGEMENT API METHODS
@ -105,9 +105,18 @@ def get_client(client_name):
return build_error_response(f"Client '{client_name}' not found as Customer or Lead.", 404)
print("DEBUG: Retrieved customer/lead document:", customer.as_dict())
clientData = {**clientData, **customer.as_dict()}
if customer.doctype == "Lead":
clientData.update(map_lead_client(clientData))
links = []
if customer.doctype == "Customer":
links = customer.get("custom_add_contacts", []) + customer.get("custom_select_address", [])
links = (
[{"link_doctype": "Contact", "link_name": row.contact}
for row in customer.get("custom_add_contacts", [])]
+
[{"link_doctype": "Address", "link_name": row.address}
for row in customer.get("custom_select_address", [])]
)
else:
links = frappe.get_all(
"Dynamic Link",
@ -124,10 +133,11 @@ def get_client(client_name):
print("DEBUG: Retrieved links from lead:", links)
for link in links:
linked_doc = frappe.get_doc(link.link_doctype, link.link_name)
if link.link_doctype == "Contact":
print("DEBUG: Processing link:", link)
linked_doc = frappe.get_doc(link["link_doctype"], link["link_name"])
if link["link_doctype"] == "Contact":
clientData["contacts"].append(linked_doc.as_dict())
elif link.link_doctype == "Address":
elif link["link_doctype"] == "Address":
clientData["addresses"].append(linked_doc.as_dict())
# TODO: Continue getting other linked docs like jobs, invoices, etc.
return build_success_response(clientData)

View File

@ -158,4 +158,31 @@ def build_full_address(doc):
if first and second:
return f"{first}, {second}"
return first or second or ""
def map_lead_client(client_data):
mappings = {
"lead_name": "customer_name",
"customer_type": "customer_type",
"territory": "territory",
"company_name": "company"
}
for lead_field, client_field in mappings.items():
if lead_field in client_data:
print(f"DEBUG: Mapping field {lead_field} to {client_field} with value {client_data[lead_field]}")
client_data[client_field] = client_data[lead_field]
client_data["customer_group"] = "" # Leads don't have customer groups
return client_data
def map_lead_update(client_data):
mappings = {
"customer_name": "lead_name",
"customer_type": "customer_type",
"territory": "territory",
"company": "company_name"
}
for client_field, lead_field in mappings.items():
if client_field in client_data:
print(f"DEBUG: Mapping field {client_field} to {lead_field} with value {client_data[client_field]}")
client_data[lead_field] = client_data[client_field]
return client_data

View File

@ -10,11 +10,11 @@
/>
</template>
<div class="status-cards">
<!-- Form Mode (new=true or edit mode) -->
<template v-if="isNew || editMode">
<template v-if="isNew || editMode">
<ClientInformationForm
ref="clientInfoRef"
v-model:form-data="formData"
:form-data="formData"
@update:form-data="formData = $event"
:is-submitting="isSubmitting"
:is-edit-mode="editMode"
@new-client-toggle="handleNewClientToggle"
@ -23,7 +23,8 @@
<ContactInformationForm
ref="contactInfoRef"
v-model:form-data="formData"
:form-data="formData"
@update:form-data="formData = $event"
:is-submitting="isSubmitting"
:is-edit-mode="editMode"
:is-new-client-locked="isNewClientMode"
@ -32,7 +33,8 @@
/>
<AddressInformationForm
v-model:form-data="formData"
:form-data="formData"
@update:form-data="formData = $event"
:is-submitting="isSubmitting"
:is-edit-mode="editMode"
/>