custom_ui/custom_ui/services/client_service.py
2026-01-15 08:36:08 -06:00

71 lines
No EOL
3.6 KiB
Python

import frappe
from .db_service import DbService
from erpnext.crm.doctype.lead.lead import make_customer
from .address_service import AddressService
from .contact_service import ContactService
from .estimate_service import EstimateService
from .onsite_meeting_service import OnSiteMeetingService
class ClientService:
@staticmethod
def get_client_doctype(client_name: str) -> str:
"""Determine if the client is a Customer or Lead."""
if DbService.exists("Customer", client_name):
return "Customer"
elif DbService.exists("Lead", client_name):
return "Lead"
else:
raise ValueError(f"Client with name {client_name} does not exist as Customer or Lead.")
@staticmethod
def set_primary_contact(client_name: str, contact_name: str):
"""Set the primary contact for a client (Customer or Lead)."""
print(f"DEBUG: Setting primary contact for client {client_name} to contact {contact_name}")
client_doctype = ClientService.get_client_doctype(client_name)
frappe.db.set_value(client_doctype, client_name, "primary_contact", contact_name)
print(f"DEBUG: Set primary contact for client {client_name} to contact {contact_name}")
@staticmethod
def append_link(client_name: str, field: str, link_doctype: str, link_name: str):
"""Set a link field for a client (Customer or Lead)."""
print(f"DEBUG: Setting link field {field} for client {client_name} to {link_doctype} {link_name}")
client_doctype = ClientService.get_client_doctype(client_name)
client_doc = frappe.get_doc(client_doctype, client_name)
client_doc.append(field, {
link_doctype.lower(): link_name
})
client_doc.save(ignore_permissions=True)
print(f"DEBUG: Set link field {field} for client {client_doc.get('name')} to {link_doctype} {link_name}")
@staticmethod
def convert_lead_to_customer(
lead_name: str,
update_quotations: bool = True,
update_addresses: bool = True,
update_contacts: bool = True,
update_onsite_meetings: bool = True
):
"""Convert a Lead to a Customer."""
print(f"DEBUG: Converting Lead {lead_name} to Customer")
lead_doc = DbService.get_or_throw("Lead", lead_name)
customer_doc = make_customer(lead_doc.name)
customer_doc.insert(ignore_permissions=True)
if update_addresses:
for address in lead_doc.get("addresses", []):
address_doc = AddressService.get_or_throw(address.get("address"))
AddressService.link_address_to_customer(address_doc, "Customer", customer_doc.name)
if update_contacts:
for contact in lead_doc.get("contacts", []):
contact_doc = ContactService.get_or_throw(contact.get("contact"))
ContactService.link_contact_to_customer(contact_doc, "Customer", customer_doc.name)
if update_quotations:
for quotation in lead_doc.get("quotations", []):
quotation_doc = EstimateService.get_or_throw(quotation.get("quotation"))
EstimateService.link_estimate_to_customer(quotation_doc, "Customer", customer_doc.name)
if update_onsite_meetings:
for meeting in lead_doc.get("onsite_meetings", []):
meeting_doc = OnSiteMeetingService.get_or_throw(meeting.get("onsite_meeting"))
OnSiteMeetingService.link_onsite_meeting_to_customer(meeting_doc, "Customer", customer_doc.name)
print(f"DEBUG: Converted Lead {lead_name} to Customer {customer_doc.name}")
return customer_doc