merge main

This commit is contained in:
Casey 2026-01-09 12:50:46 -06:00
parent f7ce3a39d0
commit 4c8e66d155
9 changed files with 349 additions and 206 deletions

View file

@ -1,4 +1,5 @@
import frappe
import json
from custom_ui.db_utils import build_error_response, build_success_response
@frappe.whitelist()
@ -54,10 +55,8 @@ def get_contacts_for_address(address_name):
def get_addresses(fields=["*"], filters={}):
"""Get addresses with optional filtering."""
if isinstance(fields, str):
import json
fields = json.loads(fields)
if isinstance(filters, str):
import json
filters = json.loads(filters)
if fields[0] != "*" and len(fields) == 1:
pluck = fields[0]
@ -86,6 +85,16 @@ def create_address(address_data):
address.insert(ignore_permissions=True)
return address
def update_address(address_data):
"""Update an existing address."""
if isinstance(address_data, str):
address_data = json.loads(address_data)
address_doc = check_and_get_address_by_name(address_data.get("name"))
for key, value in address_data.items():
setattr(address_doc, key, value)
address_doc.save(ignore_permissions=True)
return address_doc
def address_exists(address_line1, address_line2, city, state, pincode):
"""Check if an address with the given details already exists."""
filters = {
@ -97,6 +106,16 @@ 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', '')}"

View file

@ -210,6 +210,38 @@ def get_clients_table_data(filters={}, sortings=[], page=1, page_size=10):
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def update_client_info(client_name, data):
"""Update client information for a given client."""
try:
data = json.loads(data)
print("DEBUG: update_client_info called with client_name:", client_name, "and data:", data)
client_doc = check_and_get_client_doc(client_name)
if not client_doc:
return build_error_response(f"Client with name '{client_name}' does not exist.", 404)
address_updates = data.get("addresses", [])
contact_updates = data.get("contacts", [])
customer_updates = data.get("customer", {})
# Update addresses
if address_updates:
for addr_data in address_updates:
update_address(addr_data)
# Update contacts
if contact_updates:
for contact_data in contact_updates:
update_contact(contact_data)
# Update customer/lead
if customer_updates:
for field, value in customer_updates.items():
if hasattr(client_doc, field):
setattr(client_doc, field, value)
client_doc.save(ignore_permissions=True)
frappe.local.message_log = []
return get_client(client_name)
except frappe.ValidationError as ve:
return build_error_response(str(ve), 400)
except Exception as e:
return build_error_response(str(e), 500)
@frappe.whitelist()
def upsert_client(data):

View file

@ -24,6 +24,12 @@ def check_and_get_contact(first_name: str, last_name: str, email: str, phone: st
return get_contact(contact_name)
return None
def check_and_get_contact_by_name(contact_name: str):
"""Check if a contact exists by name and return the contact document if found."""
if frappe.db.exists("Contact", contact_name):
return get_contact(contact_name)
return None
def create_contact(contact_data: dict):
"""Create a new contact."""
contact = frappe.get_doc({

View file

@ -419,7 +419,7 @@ def upsert_estimate(data):
new_estimate = frappe.get_doc({
"doctype": "Quotation",
"custom_requires_half_payment": data.get("requires_half_payment", 0),
"custom_installation_address": data.get("address_name"),
"custom_job_address": data.get("address_name"),
"custom_current_status": "Draft",
"contact_email": data.get("contact_email"),
"party_name": data.get("customer"),