hook jobs up with real data

This commit is contained in:
Casey 2025-11-07 08:03:36 -06:00
parent f4d04e90a9
commit 942e9beeab
3 changed files with 178 additions and 24 deletions

View file

@ -204,4 +204,113 @@ def upsert_client(data):
"customer": customer_doc,
"address": address_doc,
"success": True
}
}
@frappe.whitelist()
def get_jobs(options):
options = json.loads(options)
print("DEBUG: Raw job options received:", options)
defaultOptions = {
"fields": ["*"],
"filters": {},
"sorting": {},
"page": 1,
"page_size": 10,
"for_table": False
}
options = {**defaultOptions, **options}
print("DEBUG: Final job options:", 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"]:
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
backend_field = map_job_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[backend_field] = ["like", f"%{filter_obj['value']}%"]
elif match_mode in ("startswith", "startsWith"):
processed_filters[backend_field] = ["like", f"{filter_obj['value']}%"]
elif match_mode in ("endswith", "endsWith"):
processed_filters[backend_field] = ["like", f"%{filter_obj['value']}"]
elif match_mode in ("equals", "equals"):
processed_filters[backend_field] = filter_obj["value"]
else:
# Default to contains
processed_filters[backend_field] = ["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_job_field_name(sort_field)
order_by = f"{backend_sort_field} {sort_direction}"
print("DEBUG: Processed job filters:", processed_filters)
print("DEBUG: Job order by:", order_by)
count = frappe.db.count("Project", filters=processed_filters)
print("DEBUG: Total projects count:", count)
projects = frappe.db.get_all(
"Project",
fields=options["fields"],
filters=processed_filters,
limit=options["page_size"],
start=(options["page"] - 1) * options["page_size"],
order_by=order_by
)
for project in projects:
job = {}
tableRow = {}
tableRow["id"] = project["name"]
tableRow["name"] = project["name"]
tableRow["customInstallationAddress"] = project.get("custom_installation_address", "")
tableRow["customer"] = project.get("customer", "")
tableRow["status"] = project.get("status", "")
tableRow["percentComplete"] = project.get("percent_complete", 0)
tableRows.append(tableRow)
job["project"] = project
jobs.append(job)
return {
"pagination": {
"total": count,
"page": options["page"],
"page_size": options["page_size"],
"total_pages": (count + options["page_size"] - 1) // options["page_size"]
},
"data": tableRows if options["for_table"] else jobs
}