fix filtering and sorting

This commit is contained in:
Casey 2025-11-12 07:01:26 -06:00
parent ba507f8e6a
commit 3b86ed0ee7
7 changed files with 334 additions and 201 deletions

View file

@ -1,5 +1,6 @@
import frappe, json, re
from datetime import datetime, date
from custom_ui.db_utils import process_filters, process_sorting
@frappe.whitelist()
def get_client_status_counts(weekly=False, week_start_date=None, week_end_date=None):
@ -89,6 +90,7 @@ def get_client(client_name):
])
projects = [frappe.get_doc("Project", proj["name"]) for proj in project_names]
projects_data = []
customer = frappe.get_doc("Customer", customer_name)
# get all associated data as needed
return {
@ -99,110 +101,67 @@ def get_client(client_name):
@frappe.whitelist()
def get_clients_table_data(options):
options = json.loads(options)
print("DEBUG: Raw options received:", options)
defaultOptions = {
"filters": {},
"sorting": {},
"page": 1,
"page_size": 10,
"for_table": False
}
options = {**defaultOptions, **options}
print("DEBUG: Final options:", options)
# Map frontend field names to backend field names
def map_field_name(frontend_field):
field_mapping = {
"customerName": "customer_name",
"addressTitle": "address_title",
"addressName": "address_title", # Legacy support
"appointmentScheduledStatus": "address_title", # These are computed fields, sort by address_title
"estimateSentStatus": "address_title",
"paymentReceivedStatus": "address_title",
"jobStatus": "address_title"
}
return field_mapping.get(frontend_field, frontend_field)
# Process filters from PrimeVue format to Frappe format
processed_filters = {}
if options["filters"]:
for field_name, filter_obj in options["filters"].items():
if isinstance(filter_obj, dict) and "value" in filter_obj:
if filter_obj["value"] is not None and filter_obj["value"] != "":
# Map frontend field names to backend field names
if field_name == "customer_name":
mapped_field_name = "custom_customer_to_bill"
else:
mapped_field_name = field_name
# Handle different match modes
match_mode = filter_obj.get("matchMode", "contains")
if isinstance(match_mode, str):
match_mode = match_mode.lower()
if match_mode in ("contains", "contains"):
processed_filters[mapped_field_name] = ["like", f"%{filter_obj['value']}%"]
elif match_mode in ("startswith", "startsWith"):
processed_filters[mapped_field_name] = ["like", f"{filter_obj['value']}%"]
elif match_mode in ("endswith", "endsWith"):
processed_filters[mapped_field_name] = ["like", f"%{filter_obj['value']}"]
elif match_mode in ("equals", "equals"):
processed_filters[mapped_field_name] = filter_obj["value"]
else:
# Default to contains
processed_filters[mapped_field_name] = ["like", f"%{filter_obj['value']}%"]
# Process sorting
order_by = None
if options.get("sorting") and options["sorting"]:
sorting_str = options["sorting"]
if sorting_str and sorting_str.strip():
# Parse "field_name asc/desc" format
parts = sorting_str.strip().split()
if len(parts) >= 2:
sort_field = parts[0]
sort_direction = parts[1].lower()
# Map frontend field to backend field
backend_sort_field = map_field_name(sort_field)
order_by = f"{backend_sort_field} {sort_direction}"
else:
order_by = "modified desc"
def get_clients_table_data(filters = {}, sorting = [], page=1, page_size=10):
print("DEBUG: Raw client table options received:", {
"filters": filters,
"sorting": sorting,
"page": page,
"page_size": page_size
})
page = int(page)
page_size = int(page_size)
processed_filters = process_filters(filters)
print("DEBUG: Processed filters:", processed_filters)
print("DEBUG: Order by:", order_by)
processed_sortings = process_sorting(sorting)
print("DEBUG: Order by:", processed_sortings)
count = frappe.db.count("Address", filters=processed_filters)
print("DEBUG: Total addresses count:", count)
addresses = frappe.db.get_all(
print("DEBUG: Filters (repr):", repr(processed_filters), "Type:", type(processed_filters))
print("DEBUG: Order by (repr):", repr(processed_sortings), "Type:", type(processed_sortings))
address_names = frappe.db.get_all(
"Address",
fields=["name", "custom_customer_to_bill", "address_title", "custom_onsite_meeting_scheduled", "custom_estimate_sent_status", "custom_job_status", "custom_payment_received_status"],
filters=processed_filters,
limit=options["page_size"],
start=(options["page"] - 1) * options["page_size"],
order_by=order_by
fields=["name"],
filters=processed_filters if isinstance(processed_filters, dict) else None,
or_filters=processed_filters if isinstance(processed_filters, list) else None,
limit=page_size,
start=(page - 1) * page_size,
order_by=processed_sortings
)
print("DEBUG: Retrieved address names:", address_names)
addresses = [frappe.get_doc("Address", addr["name"]).as_dict() for addr in address_names]
rows = []
for address in addresses:
print("DEBUG: Processing address:", address)
links = address.links
print("DEBUG: Address links:", links)
customer_links = [link for link in links if link.link_doctype == "Customer"] if links else None
print("DEBUG: Extracted customer links:", customer_links)
customer_name = customer_links[0].link_name if customer_links else address["custom_customer_to_bill"]
tableRow = {}
tableRow["id"] = address["name"]
tableRow["customer_name"] = address["custom_customer_to_bill"]
tableRow["address_title"] = address["address_title"]
tableRow["appointment_scheduled_status"] = address["custom_onsite_meeting_scheduled"]
tableRow["estimate_sent_status"] = address["custom_estimate_sent_status"]
tableRow["job_status"] = address["custom_job_status"]
tableRow["payment_received_status"] = address["custom_payment_received_status"]
tableRow["customer_name"] = customer_name
tableRow["address"] = (
f"{address['address_line1']}"
f"{' ' + address['address_line2'] if address['address_line2'] else ''} "
f"{address['city']}, {address['state']} {address['pincode']}"
)
tableRow["appointment_scheduled_status"] = address.custom_onsite_meeting_scheduled
tableRow["estimate_sent_status"] = address.custom_estimate_sent_status
tableRow["job_status"] = address.custom_job_status
tableRow["payment_received_status"] = address.custom_payment_received_status
rows.append(tableRow)
return {
"pagination": {
"total": count,
"page": options["page"],
"page_size": options["page_size"],
"total_pages": (count + options["page_size"] - 1) // options["page_size"]
"page": page,
"page_size": page_size,
"total_pages": (count + page_size - 1) // page_size
},
"data": rows
}
@ -289,17 +248,6 @@ def get_jobs(options):
jobs = []
tableRows = []
# Map frontend field names to backend field names for Project doctype
def map_job_field_name(frontend_field):
field_mapping = {
"name": "name",
"customInstallationAddress": "custom_installation_address",
"customer": "customer",
"status": "status",
"percentComplete": "percent_complete"
}
return field_mapping.get(frontend_field, frontend_field)
# Process filters from PrimeVue format to Frappe format
processed_filters = {}
if options["filters"]:

View file

@ -1,27 +1,74 @@
def calculate_appointment_scheduled_status(on_site_meeting):
if not on_site_meeting:
return "Not Started"
return "Completed"
import json
def calculate_estimate_sent_status(quotation):
if not quotation:
return "Not Started"
if quotation["custom_sent"] == 1:
return "Completed"
return "In Progress"
def map_field_name(frontend_field):
field_mapping = {
"customer_name": "custom_customer_to_bill",
"address": "address_line1",
"appointment_scheduled_status": "custom_onsite_meeting_scheduled",
"estimate_sent_status": "custom_estimate_sent_status",
"payment_received_status": "custom_payment_received_status",
"job_status": "custom_job_status",
"installation_address": "custom_installation_address",
}
return field_mapping.get(frontend_field, frontend_field)
def calculate_payment_recieved_status(sales_invoice, payment_entries):
if not sales_invoice:
return "Not Started"
payment_sum = sum(entry["paid_amount"] for entry in payment_entries) if payment_entries else 0
if (sales_invoice and sales_invoice["status"] == "Paid") or sales_invoice["grand_total"] <= payment_sum:
return "Completed"
return "In Progress"
def process_filters(filters):
processed_filters = {}
if filters:
filters = json.loads(filters) if isinstance(filters, str) else filters
for field_name, filter_obj in filters.items():
if isinstance(filter_obj, dict) and "value" in filter_obj:
if filter_obj["value"] is not None and filter_obj["value"] != "":
# Map frontend field names to backend field names
address_fields = ["address_line1", "address_line2", "city", "state", "pincode"] if field_name == "address" else []
mapped_field_name = map_field_name(field_name)
def calculate_job_status(project, tasks=[]):
if not project or not tasks:
return "Not Started"
if any(task["status"] != "Completed" for task in tasks):
return "In Progress"
return "Completed"
# Handle different match modes
match_mode = filter_obj.get("match_mode", "contains")
if isinstance(match_mode, str):
match_mode = match_mode.lower()
# Special handling for address to search accross multiple fields
if address_fields:
address_filters = []
for addr_field in address_fields:
if match_mode in ("contains", "contains"):
address_filters.append([addr_field, "like", f"%{filter_obj['value']}%"])
elif match_mode in ("startswith", "starts_with"):
address_filters.append([addr_field, "like", f"{filter_obj['value']}%"])
elif match_mode in ("endswith", "ends_with"):
address_filters.append([addr_field, "like", f"%{filter_obj['value']}"])
elif match_mode in ("equals", "equals"):
address_filters.append([addr_field, "=", filter_obj["value"]])
else:
address_filters.append([addr_field, "like", f"%{filter_obj['value']}%"])
processed_filters = address_filters
continue # Skip the rest of the loop for address field
if match_mode in ("contains", "contains"):
processed_filters[mapped_field_name] = ["like", f"%{filter_obj['value']}%"]
elif match_mode in ("startswith", "starts_with"):
processed_filters[mapped_field_name] = ["like", f"{filter_obj['value']}%"]
elif match_mode in ("endswith", "ends_with"):
processed_filters[mapped_field_name] = ["like", f"%{filter_obj['value']}"]
elif match_mode in ("equals", "equals"):
processed_filters[mapped_field_name] = filter_obj["value"]
else:
# Default to contains
processed_filters[mapped_field_name] = ["like", f"%{filter_obj['value']}%"]
return processed_filters
def process_sorting(sortings):
sortings = json.loads(sortings) if isinstance(sortings, str) else sortings
order_by = ""
if sortings and len(sortings) > 0:
for sorting in sortings:
mapped_field = map_field_name(sorting[0].strip())
sort_direction = sorting[1].strip().lower()
order_by += f"{mapped_field} {sort_direction}, "
order_by = order_by.rstrip(", ")
else:
order_by = "modified desc"
return order_by