add date picker
This commit is contained in:
parent
09a514ae86
commit
82f9b1aac2
8 changed files with 1044 additions and 55 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import frappe, json
|
||||
import frappe, json, re
|
||||
from datetime import datetime, date
|
||||
from custom_ui.db_utils import calculate_appointment_scheduled_status, calculate_estimate_sent_status, calculate_payment_recieved_status, calculate_job_status
|
||||
|
||||
@frappe.whitelist()
|
||||
|
|
@ -316,5 +317,157 @@ def get_jobs(options):
|
|||
}
|
||||
|
||||
@frappe.whitelist()
|
||||
def upsert_estimate():
|
||||
pass
|
||||
def get_warranty_claims(options):
|
||||
options = json.loads(options)
|
||||
print("DEBUG: Raw warranty options received:", options)
|
||||
defaultOptions = {
|
||||
"fields": ["*"],
|
||||
"filters": {},
|
||||
"sorting": {},
|
||||
"page": 1,
|
||||
"page_size": 10,
|
||||
"for_table": False
|
||||
}
|
||||
options = {**defaultOptions, **options}
|
||||
print("DEBUG: Final warranty options:", options)
|
||||
|
||||
warranties = []
|
||||
tableRows = []
|
||||
|
||||
# Map frontend field names to backend field names for Warranty Claim doctype
|
||||
def map_warranty_field_name(frontend_field):
|
||||
field_mapping = {
|
||||
"warrantyId": "name",
|
||||
"customer": "customer_name",
|
||||
"serviceAddress": "service_address",
|
||||
"complaint": "complaint",
|
||||
"status": "status",
|
||||
"complaintDate": "complaint_date",
|
||||
"complaintRaisedBy": "complaint_raised_by",
|
||||
"fromCompany": "from_company",
|
||||
"territory": "territory",
|
||||
"resolutionDate": "resolution_date",
|
||||
"warrantyStatus": "warranty_amc_status"
|
||||
}
|
||||
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_warranty_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_warranty_field_name(sort_field)
|
||||
order_by = f"{backend_sort_field} {sort_direction}"
|
||||
|
||||
print("DEBUG: Processed warranty filters:", processed_filters)
|
||||
print("DEBUG: Warranty order by:", order_by)
|
||||
|
||||
count = frappe.db.count("Warranty Claim", filters=processed_filters)
|
||||
print("DEBUG: Total warranty claims count:", count)
|
||||
|
||||
warranty_claims = frappe.db.get_all(
|
||||
"Warranty Claim",
|
||||
fields=options["fields"],
|
||||
filters=processed_filters,
|
||||
limit=options["page_size"],
|
||||
start=(options["page"] - 1) * options["page_size"],
|
||||
order_by=order_by
|
||||
)
|
||||
|
||||
for warranty in warranty_claims:
|
||||
warranty_obj = {}
|
||||
tableRow = {}
|
||||
|
||||
tableRow["id"] = warranty["name"]
|
||||
tableRow["warrantyId"] = warranty["name"]
|
||||
tableRow["customer"] = warranty.get("customer_name", "")
|
||||
tableRow["serviceAddress"] = warranty.get("service_address", warranty.get("address_display", ""))
|
||||
|
||||
# Extract a brief description from the complaint HTML
|
||||
complaint_text = warranty.get("complaint", "")
|
||||
if complaint_text:
|
||||
# Simple HTML stripping for display - take first 100 chars
|
||||
clean_text = re.sub('<.*?>', '', complaint_text)
|
||||
clean_text = clean_text.strip()
|
||||
if len(clean_text) > 100:
|
||||
clean_text = clean_text[:100] + "..."
|
||||
tableRow["issueDescription"] = clean_text
|
||||
else:
|
||||
tableRow["issueDescription"] = ""
|
||||
|
||||
tableRow["status"] = warranty.get("status", "")
|
||||
tableRow["complaintDate"] = warranty.get("complaint_date", "")
|
||||
tableRow["complaintRaisedBy"] = warranty.get("complaint_raised_by", "")
|
||||
tableRow["fromCompany"] = warranty.get("from_company", "")
|
||||
tableRow["territory"] = warranty.get("territory", "")
|
||||
tableRow["resolutionDate"] = warranty.get("resolution_date", "")
|
||||
tableRow["warrantyStatus"] = warranty.get("warranty_amc_status", "")
|
||||
|
||||
# Add priority based on status and date (can be customized)
|
||||
if warranty.get("status") == "Open":
|
||||
# Calculate priority based on complaint date
|
||||
if warranty.get("complaint_date"):
|
||||
complaint_date = warranty.get("complaint_date")
|
||||
if isinstance(complaint_date, str):
|
||||
complaint_date = datetime.strptime(complaint_date, "%Y-%m-%d").date()
|
||||
elif isinstance(complaint_date, datetime):
|
||||
complaint_date = complaint_date.date()
|
||||
|
||||
days_old = (date.today() - complaint_date).days
|
||||
if days_old > 7:
|
||||
tableRow["priority"] = "High"
|
||||
elif days_old > 3:
|
||||
tableRow["priority"] = "Medium"
|
||||
else:
|
||||
tableRow["priority"] = "Low"
|
||||
else:
|
||||
tableRow["priority"] = "Medium"
|
||||
else:
|
||||
tableRow["priority"] = "Low"
|
||||
|
||||
tableRows.append(tableRow)
|
||||
|
||||
warranty_obj["warranty_claim"] = warranty
|
||||
warranties.append(warranty_obj)
|
||||
|
||||
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 warranties
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue