big update

This commit is contained in:
Casey 2026-01-13 08:11:58 -06:00
parent 6853950cc5
commit 992672b51b
11 changed files with 647 additions and 64 deletions

View file

@ -1,6 +1,7 @@
import frappe
import json
from custom_ui.db_utils import build_error_response, build_success_response, process_filters, process_sorting
from custom_ui.services import DbService
@frappe.whitelist()
def get_week_bid_meetings(week_start, week_end):
@ -66,10 +67,10 @@ def get_unscheduled_bid_meetings():
@frappe.whitelist()
def create_bid_meeting(address, notes=""):
def create_bid_meeting(address, notes="", company=None, contact=None):
"""Create a new On-Site Meeting with Unscheduled status."""
try:
print(f"DEBUG: Creating meeting with address='{address}', notes='{notes}'")
print(f"DEBUG: Creating meeting with address='{address}', notes='{notes}', company='{company}'")
# Validate address parameter
if not address or address == "None" or not address.strip():
@ -77,18 +78,24 @@ def create_bid_meeting(address, notes=""):
# Get the address document name from the full address string
address_name = frappe.db.get_value("Address", filters={"full_address": address}, fieldname="name")
print(f"DEBUG: Address lookup result: address_name='{address_name}'")
if not address_name:
return build_error_response(f"Address '{address}' not found in the system.", 404)
address_doc = DbService.get("Address", address_name)
# Create the meeting with Unscheduled status
meeting = frappe.get_doc({
"doctype": "On-Site Meeting",
"address": address_name,
"address": address_doc.name,
"notes": notes or "",
"status": "Unscheduled"
"status": "Unscheduled",
"company": company,
"contact": contact,
"party_type": address_doc.customer_type,
"party_name": address_doc.customer_name
})
meeting.flags.ignore_permissions = True
meeting.insert(ignore_permissions=True)
@ -113,7 +120,9 @@ def update_bid_meeting(name, data):
"end_time": None,
"notes": None,
"assigned_employee": None,
"completed_by": None
"completed_by": None,
"contact": None,
"status": None
}
try:
if isinstance(data, str):

View file

@ -258,6 +258,9 @@ def upsert_client(data):
contacts = data.get("contacts", [])
# Check for existing address
client_doc = check_and_get_client_doc(customer_name)
if client_doc:
return build_error_response(f"Client with name '{customer_name}' already exists.", 400)
if address_exists(
data.get("address_line1"),
data.get("address_line2"),
@ -268,23 +271,22 @@ def upsert_client(data):
return build_error_response("This address already exists. Please use a different address or search for the address to find the associated client.", 400)
# Handle customer creation/update
client_doc = check_and_get_client_doc(customer_name)
if not client_doc:
print("#####DEBUG: Creating new lead.")
customer_type = data.get("customer_type", "Individual")
primary_contact = find_primary_contact_or_throw(contacts)
lead_data = {
"first_name": primary_contact.get("first_name"),
"last_name": primary_contact.get("last_name"),
"email_id": primary_contact.get("email"),
"phone": primary_contact.get("phone_number"),
"company": data.get("company"),
"custom_customer_name": customer_name,
"customer_type": customer_type
}
if customer_type == "Company":
lead_data["company_name"] = data.get("customer_name")
client_doc = create_lead(lead_data)
print("#####DEBUG: Creating new lead.")
customer_type = data.get("customer_type", "Individual")
primary_contact = find_primary_contact_or_throw(contacts)
lead_data = {
"first_name": primary_contact.get("first_name"),
"last_name": primary_contact.get("last_name"),
"email_id": primary_contact.get("email"),
"phone": primary_contact.get("phone_number"),
"company": data.get("company"),
"custom_customer_name": customer_name,
"customer_type": customer_type
}
if customer_type == "Company":
lead_data["company_name"] = data.get("customer_name")
client_doc = create_lead(lead_data)
print(f"#####DEBUG: {client_doc.doctype}:", client_doc.as_dict())
# Handle address creation
@ -295,7 +297,9 @@ def upsert_client(data):
"city": data.get("city"),
"state": data.get("state"),
"country": "United States",
"pincode": data.get("pincode")
"pincode": data.get("pincode"),
"customer_type": "Lead",
"customer_name": client_doc.name
})
#Handle contact creation
@ -330,24 +334,24 @@ def upsert_client(data):
})
contact_docs.append(contact_doc)
##### Create links
# Customer -> Address
if client_doc.doctype == "Customer":
print("#####DEBUG: Linking address to customer.")
client_doc.append("custom_select_address", {
"address_name": address_doc.name,
})
# ##### Create links
# # Customer -> Address
# if client_doc.doctype == "Customer":
# print("#####DEBUG: Linking address to customer.")
# client_doc.append("custom_select_address", {
# "address_name": address_doc.name,
# })
# Customer -> Contact
print("#####DEBUG: Linking contacts to customer.")
for contact_doc in contact_docs:
client_doc.append("custom_add_contacts", {
"contact": contact_doc.name,
"email": contact_doc.custom_email,
"phone": contact_doc.phone,
"role": contact_doc.role
})
client_doc.save(ignore_permissions=True)
# # Customer -> Contact
# print("#####DEBUG: Linking contacts to customer.")
# for contact_doc in contact_docs:
# client_doc.append("custom_add_contacts", {
# "contact": contact_doc.name,
# "email": contact_doc.custom_email,
# "phone": contact_doc.phone,
# "role": contact_doc.role
# })
# client_doc.save(ignore_permissions=True)
# Address -> Customer/Lead
create_address_links(address_doc, client_doc, contact_docs)

View file

@ -111,14 +111,14 @@ def get_estimate_items():
@frappe.whitelist()
def get_estimate_from_address(full_address):
address_name = frappe.db.get_value("Address", {"full_address": full_address}, "name")
quotation_name = frappe.db.get_value("Quotation", {"custom_installation_address": address_name}, "name")
quotation_name = frappe.db.get_value("Quotation", {"custom_job_address": address_name}, "name")
quotation_doc = frappe.get_doc("Quotation", quotation_name)
return build_success_response(quotation_doc.as_dict())
# quotation = frappe.db.sql("""
# SELECT q.name, q.custom_installation_address
# SELECT q.name, q.custom_job_address
# FROM `tabQuotation` q
# JOIN `tabAddress` a
# ON q.custom_installation_address = a.name
# ON q.custom_job_address = a.name
# WHERE a.full_address =%s
# """, (full_address,), as_dict=True)
# if quotation:
@ -156,8 +156,8 @@ def send_estimate_email(estimate_name):
primary = next((e for e in party.email_ids if e.is_primary), None)
email = primary.email_id if primary else party.email_ids[0].email_id
if not email and quotation.custom_installation_address:
address = frappe.get_doc("Address", quotation.custom_installation_address)
if not email and quotation.custom_job_address:
address = frappe.get_doc("Address", quotation.custom_job_address)
email = getattr(address, 'email_id', None)
if not email:
return build_error_response("No email found for the customer or address.", 400)
@ -431,7 +431,8 @@ def upsert_estimate(data):
"customer_address": data.get("address_name"),
"contact_person": data.get("contact_name"),
"letter_head": data.get("company"),
"custom_project_template": data.get("project_template", None)
"custom_project_template": data.get("project_template", None),
"from_onsite_meeting": data.get("onsite_meeting", None)
})
for item in data.get("items", []):
item = json.loads(item) if isinstance(item, str) else item

View file

@ -25,11 +25,13 @@ def create_job_from_sales_order(sales_order_name):
project_template = frappe.get_doc("Project Template", "SNW Install")
new_job = frappe.get_doc({
"doctype": "Project",
"custom_installation_address": sales_order.custom_installation_address,
"project_name": sales_order.custom_installation_address,
"custom_address": sales_order.custom_job_address,
# "custom_installation_address": sales_order.custom_installation_address,
"project_name": sales_order.custom_job_address,
"project_template": project_template,
"custom_warranty_duration_days": 90,
"sales_order": sales_order
"sales_order": sales_order,
"custom_company": sales_order.company
})
new_job.insert()
return build_success_response(new_job.as_dict())

View file

@ -29,6 +29,7 @@ def get_job_task_table_data(filters={}, sortings={}, page=1, page_size=10):
tableRows = []
for task in tasks:
print("DEBUG: Processing task:", task)
tableRow = {}
tableRow["id"] = task["name"]
tableRow["subject"] = task["subject"]