updates for company effects

This commit is contained in:
Casey 2026-01-20 00:58:03 -06:00
parent 98ec082394
commit 7710a7c8fe
22 changed files with 941 additions and 186 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, map_lead_client, build_address_title
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, build_address_title, normalize_name
from erpnext.crm.doctype.lead.lead import make_customer
from custom_ui.api.db.addresses import address_exists
from custom_ui.api.db.contacts import check_and_get_contact, create_contact, create_contact_links
@ -167,6 +167,81 @@ def get_client_v2(client_name):
return build_error_response(str(ve), 400)
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def get_clients_table_data_v2(filters={}, sortings=[], page=1, page_size=10):
"""Get paginated client table data with filtering and sorting support."""
try:
filters = json.loads(filters) if isinstance(filters, str) else filters
sortings = json.loads(sortings) if isinstance(sortings, str) else sortings
page = int(page)
page_size = int(page_size)
print("DEBUG: Raw client table query received:", {
"filters": filters,
"sortings": sortings,
"page": page,
"page_size": page_size
})
where_clauses = []
values = []
if filters.get("company"):
where_clauses.append("c.company = %s")
values.append(filters["company"]["value"])
if filters.get("address"):
where_clauses.append("a.full_address LIKE %s")
values.append(f"%{filters['address']['value']}%")
if filters.get("customer_name"):
where_clauses.append("a.customer_name LIKE %s")
values.append(f"%{filters['customer_name']['value']}%")
where_sql = ""
if where_clauses:
where_sql = "WHERE " + " AND ".join(where_clauses)
offset = (page - 1) * page_size
address_names = frappe.db.sql(f"""
SELECT DISTINCT a.name
FROM `tabAddress` a
LEFT JOIN `tabAddress Company Link` c ON c.parent = a.name
{where_sql}
ORDER BY a.modified DESC
LIMIT %s OFFSET %s
""", values + [page_size, offset], as_dict=True)
print("DEBUG: Address names retrieved:", address_names)
count = frappe.db.sql(f"""
SELECT COUNT(DISTINCT a.name) as count
FROM `tabAddress` a
LEFT JOIN `tabAddress Company Link` c ON c.parent = a.name
{where_sql}
""", values, as_dict=True)[0]["count"]
tableRows = []
for address_name in address_names:
address = AddressService.get_or_throw(address_name["name"])
tableRow = {}
tableRow["id"] = address.name
tableRow["address"] = address.full_address
tableRow["client_type"] = address.customer_type
tableRow["customer_name"] = normalize_name(address.customer_name, "-#-")
tableRow["companies"] = ", ".join([link.company for link in address.get("companies", [])])
tableRows.append(tableRow)
table_data = build_datatable_dict(data=tableRows, count=count, page=page, page_size=page_size)
return build_success_response(table_data)
except frappe.ValidationError as ve:
return build_error_response(str(ve), 400)
except Exception as e:
print("ERROR in get_clients_table_data_v2:", str(e))
return build_error_response(str(e), 500)
@frappe.whitelist()