updates for company effects
This commit is contained in:
parent
98ec082394
commit
7710a7c8fe
22 changed files with 941 additions and 186 deletions
|
|
@ -4,7 +4,7 @@ from custom_ui.db_utils import build_error_response, build_success_response, pro
|
|||
from custom_ui.services import DbService, ClientService, AddressService, ContactService
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_week_bid_meetings(week_start, week_end):
|
||||
def get_week_bid_meetings(week_start, week_end, company):
|
||||
"""Get On-Site Meetings scheduled within a specific week."""
|
||||
try:
|
||||
meetings = frappe.db.get_all(
|
||||
|
|
@ -12,7 +12,8 @@ def get_week_bid_meetings(week_start, week_end):
|
|||
fields=["*"],
|
||||
filters=[
|
||||
["start_time", ">=", week_start],
|
||||
["start_time", "<=", week_end]
|
||||
["start_time", "<=", week_end],
|
||||
["company", "=", company]
|
||||
],
|
||||
order_by="start_time asc"
|
||||
)
|
||||
|
|
@ -27,7 +28,7 @@ def get_week_bid_meetings(week_start, week_end):
|
|||
return build_error_response(str(e), 500)
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_bid_meetings(fields=["*"], filters={}):
|
||||
def get_bid_meetings(fields=["*"], filters={}, company=None):
|
||||
"""Get paginated On-Site Meetings with filtering and sorting support."""
|
||||
try:
|
||||
print("DEBUG: Raw bid meeting options received:", filters)
|
||||
|
|
@ -53,13 +54,13 @@ def get_bid_meetings(fields=["*"], filters={}):
|
|||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_unscheduled_bid_meetings():
|
||||
def get_unscheduled_bid_meetings(company):
|
||||
"""Get On-Site Meetings that are unscheduled."""
|
||||
try:
|
||||
meetings = frappe.db.get_all(
|
||||
"On-Site Meeting",
|
||||
fields=["*"],
|
||||
filters={"status": "Unscheduled"},
|
||||
filters={"status": "Unscheduled", "company": company},
|
||||
order_by="creation desc"
|
||||
)
|
||||
for meeting in meetings:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -50,6 +50,8 @@ def get_job(job_id=""):
|
|||
project = project.as_dict()
|
||||
project["job_address"] = address_doc
|
||||
project["client"] = ClientService.get_client_or_throw(project.customer)
|
||||
task_names = frappe.get_all("Task", filters={"project": job_id})
|
||||
project["tasks"] = [frappe.get_doc("Task", task_name).as_dict() for task_name in task_names]
|
||||
return build_success_response(project)
|
||||
except Exception as e:
|
||||
return build_error_response(str(e), 500)
|
||||
|
|
@ -167,55 +169,32 @@ def upsert_job(data):
|
|||
return {"status": "error", "message": str(e)}
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_install_projects(start_date=None, end_date=None):
|
||||
def get_projects_for_calendar(date, company=None, project_templates=[]):
|
||||
"""Get install projects for the calendar."""
|
||||
# Parse project_templates if it's a JSON string
|
||||
if isinstance(project_templates, str):
|
||||
project_templates = json.loads(project_templates)
|
||||
|
||||
# put some emojis in the print to make it stand out
|
||||
print("📅📅📅", date, "company:", company, "project_templates:", project_templates, "type:", type(project_templates))
|
||||
try:
|
||||
filters = {"project_template": "SNW Install"}
|
||||
filters = {"company": company} if company else {}
|
||||
if project_templates and len(project_templates) > 0:
|
||||
filters["project_template"] = ["in", project_templates]
|
||||
unscheduled_filters = filters.copy()
|
||||
unscheduled_filters["is_scheduled"] = 0
|
||||
filters["expected_start_date"] = date
|
||||
# If date range provided, we could filter, but for now let's fetch all open/active ones
|
||||
# or maybe filter by status not Closed/Completed if we want active ones.
|
||||
# The user said "unscheduled" are those with status "Open" (and no date).
|
||||
# extend filters into unscheduled_filters
|
||||
|
||||
projects = frappe.get_all("Project", fields=["*"], filters=filters)
|
||||
|
||||
calendar_events = []
|
||||
for project in projects:
|
||||
# Determine status
|
||||
status = "unscheduled"
|
||||
if project.get("expected_start_date"):
|
||||
status = "scheduled"
|
||||
|
||||
# Map to calendar event format
|
||||
event = {
|
||||
"id": project.name,
|
||||
"serviceType": project.project_name, # Using project name as service type/title
|
||||
"customer": project.customer,
|
||||
"status": status,
|
||||
"scheduledDate": project.expected_start_date,
|
||||
"scheduledTime": "08:00", # Default time if not specified? Project doesn't seem to have time.
|
||||
"duration": 480, # Default 8 hours?
|
||||
"foreman": project.get("custom_install_crew"),
|
||||
"crew": [], # Need to map crew
|
||||
"estimatedCost": project.estimated_costing,
|
||||
"priority": project.priority.lower() if project.priority else "medium",
|
||||
"notes": project.notes,
|
||||
"address": project.custom_installation_address
|
||||
}
|
||||
|
||||
calendar_events.append(event)
|
||||
|
||||
return {"status": "success", "data": calendar_events}
|
||||
project_names = frappe.get_all("Project", pluck="name", filters=filters)
|
||||
print("DEBUG: Found scheduled project names:", project_names)
|
||||
unscheduled_project_names = frappe.get_all("Project", pluck="name", filters=unscheduled_filters)
|
||||
print("DEBUG: Found unscheduled project names:", unscheduled_project_names)
|
||||
projects = [frappe.get_doc("Project", name).as_dict() for name in project_names]
|
||||
unscheduled_projects = [frappe.get_doc("Project", name).as_dict() for name in unscheduled_project_names]
|
||||
return build_success_response({ "projects": projects, "unscheduled_projects": unscheduled_projects })
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_project_templates_for_company(company_name):
|
||||
"""Get project templates for a specific company."""
|
||||
try:
|
||||
templates = frappe.get_all(
|
||||
"Project Template",
|
||||
fields=["*"],
|
||||
filters={"company": company_name}
|
||||
)
|
||||
return build_success_response(templates)
|
||||
except Exception as e:
|
||||
return build_error_response(str(e), 500),
|
||||
return build_error_response(str(e), 500)
|
||||
Loading…
Add table
Add a link
Reference in a new issue