big update

This commit is contained in:
Casey 2026-01-15 08:36:08 -06:00
parent d53ebf9ecd
commit 5c7e93fcc7
26 changed files with 1890 additions and 423 deletions

View file

@ -1,31 +1,15 @@
import frappe
import json
from custom_ui.db_utils import build_error_response, build_success_response
from custom_ui.services import ClientService, AddressService
@frappe.whitelist()
def get_address_by_full_address(full_address):
"""Get address by full_address, including associated contacts."""
print(f"DEBUG: get_address_by_full_address called with full_address: {full_address}")
try:
address = frappe.get_doc("Address", {"full_address": full_address}).as_dict()
customer_exists = frappe.db.exists("Customer", address.get("custom_customer_to_bill"))
doctype = "Customer" if customer_exists else "Lead"
name = ""
if doctype == "Customer":
name = address.get("custom_customer_to_bill")
else:
## filter through links for one with doctype Lead
lead_links = address.get("links", [])
print(f"DEBUG: lead_links: {lead_links}")
lead_name = [link.link_name for link in lead_links if link.link_doctype == "Lead"]
name = lead_name[0] if lead_name else ""
address["customer"] = frappe.get_doc(doctype, name).as_dict()
contacts = []
for contact_link in address.custom_linked_contacts:
contact_doc = frappe.get_doc("Contact", contact_link.contact)
contacts.append(contact_doc.as_dict())
address["contacts"] = contacts
return build_success_response(address)
address = AddressService.get_address_by_full_address(full_address)
return build_success_response(AddressService.build_full_dict(address))
except Exception as e:
return build_error_response(str(e), 500)
@ -33,23 +17,23 @@ def get_address_by_full_address(full_address):
def get_address(address_name):
"""Get a specific address by name."""
try:
address = frappe.get_doc("Address", address_name)
address = AddressService.get_or_throw(address_name)
return build_success_response(address.as_dict())
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def get_contacts_for_address(address_name):
"""Get contacts linked to a specific address."""
try:
address = frappe.get_doc("Address", address_name)
contacts = []
for contact_link in address.custom_linked_contacts:
contact = frappe.get_doc("Contact", contact_link.contact)
contacts.append(contact.as_dict())
return build_success_response(contacts)
except Exception as e:
return build_error_response(str(e), 500)
# @frappe.whitelist() #### DEPRECATED FUNCTION
# def get_contacts_for_address(address_name):
# """Get contacts linked to a specific address."""
# try:
# address = AddressService.get_or_throw(address_name)
# contacts = []
# for contact_link in address.custom_linked_contacts:
# contact = frappe.get_doc("Contact", contact_link.contact)
# contacts.append(contact.as_dict())
# return build_success_response(contacts)
# except Exception as e:
# return build_error_response(str(e), 500)
@frappe.whitelist()
def get_addresses(fields=["*"], filters={}):
@ -74,16 +58,6 @@ def get_addresses(fields=["*"], filters={}):
except Exception as e:
frappe.log_error(message=str(e), title="Get Addresses Failed")
return build_error_response(str(e), 500)
def create_address(address_data):
"""Create a new address."""
address = frappe.get_doc({
"doctype": "Address",
**address_data
})
address.insert(ignore_permissions=True)
return address
def update_address(address_data):
"""Update an existing address."""
@ -106,19 +80,10 @@ def address_exists(address_line1, address_line2, city, state, pincode):
}
return frappe.db.exists("Address", filters) is not None
def check_and_get_address_by_name(address_name):
"""Check if an address exists by name and return the address document if found."""
if frappe.db.exists("Address", address_name):
return frappe.get_doc("Address", address_name)
raise ValueError(f"Address with name {address_name} does not exist.")
def address_exists_by_name(address_name):
"""Check if an address with the given name exists."""
return frappe.db.exists("Address", address_name) is not None
def calculate_address_title(customer_name, address_data):
return f"{customer_name} - {address_data.get('address_line1', '')}, {address_data.get('city', '')} - {address_data.get('type', '')}"
def create_address_links(address_doc, client_doc, contact_docs):
print("#####DEBUG: Linking customer to address.")
print("#####DEBUG: Client Doc:", client_doc.as_dict(), "Address Doc:", address_doc.as_dict(), "Contact Docs:", [c.as_dict() for c in contact_docs])