finish client table for now

This commit is contained in:
Casey 2025-11-07 07:50:19 -06:00
parent 60d3f35988
commit f4d04e90a9
6 changed files with 254 additions and 106 deletions

View file

@ -1,9 +1,10 @@
import frappe, json
from custom_ui.db_utils import calculate_appointment_scheduled_status, calculate_estimate_sent_status, calculate_payment_recieved_status, calculate_job_scheduled_status
from custom_ui.db_utils import calculate_appointment_scheduled_status, calculate_estimate_sent_status, calculate_payment_recieved_status, calculate_job_status
@frappe.whitelist()
def get_clients(options):
options = json.loads(options)
print("DEBUG: Raw options received:", options)
defaultOptions = {
"fields": ["*"],
"filters": {},
@ -13,26 +14,81 @@ def get_clients(options):
"for_table": False
}
options = {**defaultOptions, **options}
print("DEBUG: Final options:", options)
clients = []
tableRows = []
count = frappe.db.count("Address", filters=options["filters"])
# Map frontend field names to backend field names
def map_field_name(frontend_field):
field_mapping = {
"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
backend_field = map_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_field_name(sort_field)
order_by = f"{backend_sort_field} {sort_direction}"
print("DEBUG: Processed filters:", processed_filters)
print("DEBUG: Order by:", order_by)
count = frappe.db.count("Address", filters=processed_filters)
print("DEBUG: Total addresses count:", count)
addresses = frappe.db.get_all(
"Address",
fields=options["fields"],
filters=options["filters"],
filters=processed_filters,
limit=options["page_size"],
start=(options["page"] - 1) * options["page_size"],
order_by=(options["sorting"])
order_by=order_by
)
for address in addresses:
client = {}
tableRow = {}
print("DEBUG: Processing address:", address)
on_site_meetings = frappe.db.get_all(
"On-Site Meeting",
@ -40,14 +96,26 @@ def get_clients(options):
filters={"address": address["address_title"]}
)
quotations = frappe.db.get_all(
"Quotation",
fields=["*"],
filters={"custom_installation_address": address["address_title"]}
)
sales_orders = frappe.db.get_all(
"Sales Order",
fields=["*"],
filters={"custom_installation_address": address["address_title"]}
)
sales_invvoices = frappe.db.get_all(
"Sales Invoice",
fields=["*"],
filters={"custom_installation_address": address["address_title"]}
)
quotations = frappe.db.get_all(
"Quotation",
payment_entries = frappe.db.get_all(
"Payment Entry",
fields=["*"],
filters={"custom_installation_address": address["address_title"]}
)
@ -60,13 +128,19 @@ def get_clients(options):
"project_template": "SNW Install"
}
)
tasks = frappe.db.get_all(
"Task",
fields=["*"],
filters={"project": jobs[0]["name"]}
) if jobs else []
tableRow["id"] = address["name"]
tableRow["address_name"] = address.get("address_title", "")
tableRow["address_title"] = address["address_title"]
tableRow["appointment_scheduled_status"] = calculate_appointment_scheduled_status(on_site_meetings[0]) if on_site_meetings else "Not Started"
tableRow["estimate_sent_status"] = calculate_estimate_sent_status(quotations[0]) if quotations else "Not Started"
tableRow["payment_received_status"] = calculate_payment_recieved_status(sales_invvoices[0]) if sales_invvoices else "Not Started"
tableRow["job_scheduled_status"] = calculate_job_scheduled_status(jobs[0]) if jobs else "Not Started"
tableRow["payment_received_status"] = calculate_payment_recieved_status(sales_invvoices[0], payment_entries) if sales_invvoices and payment_entries else "Not Started"
tableRow["job_status"] = calculate_job_status(jobs[0], tasks) if jobs and tasks else "Not Started"
tableRows.append(tableRow)
client["address"] = address

View file

@ -2,8 +2,6 @@
def calculate_appointment_scheduled_status(on_site_meeting):
if not on_site_meeting:
return "Not Started"
# if on_site_meeting["end_time"] < today:
# return "In Progress"
return "Completed"
def calculate_estimate_sent_status(quotation):
@ -13,16 +11,17 @@ def calculate_estimate_sent_status(quotation):
return "Completed"
return "In Progress"
def calculate_payment_recieved_status(sales_invoice):
def calculate_payment_recieved_status(sales_invoice, payment_entries):
if not sales_invoice:
return "Not Started"
if sales_invoice and sales_invoice["status"] == "Paid":
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 calculate_job_scheduled_status(project):
if not project:
def calculate_job_status(project, tasks=[]):
if not project or not tasks:
return "Not Started"
if not project["start_time"]:
if any(task["status"] != "Completed" for task in tasks):
return "In Progress"
return "Completed"