diff --git a/custom_ui/services/__init__.py b/custom_ui/services/__init__.py new file mode 100644 index 0000000..959e02b --- /dev/null +++ b/custom_ui/services/__init__.py @@ -0,0 +1,3 @@ +from .address_service import AddressService + +from .db_service import DbService \ No newline at end of file diff --git a/custom_ui/services/address_service.py b/custom_ui/services/address_service.py new file mode 100644 index 0000000..4ee3eca --- /dev/null +++ b/custom_ui/services/address_service.py @@ -0,0 +1,81 @@ +import frappe + +class AddressService: + + @staticmethod + def exists(address_name: str) -> bool: + """Check if an address with the given name exists.""" + print(f"DEBUG: Checking existence of Address with name: {address_name}") + result = frappe.db.exists("Address", address_name) is not None + print(f"DEBUG: Address existence: {result}") + return result + + @staticmethod + def get(address_name: str): + """Retrieve an address document by name. Returns None if not found.""" + print(f"DEBUG: Retrieving Address document with name: {address_name}") + if AddressService.exists(address_name): + address_doc = frappe.get_doc("Address", address_name) + print("DEBUG: Address document found.") + return address_doc + print("DEBUG: Address document not found.") + return None + + @staticmethod + def get_or_throw(address_name: str): + """Retrieve an address document by name or throw an error if not found.""" + address_doc = AddressService.get(address_name) + if address_doc: + return address_doc + raise ValueError(f"Address with name {address_name} does not exist.") + + @staticmethod + def update_value(docname: str, fieldname: str, value, save: bool = True) -> frappe._dict: + """Update a specific field value of a document.""" + print(f"DEBUG: Updating Address {docname}, setting {fieldname} to {value}") + if AddressService.exists(docname) is False: + raise ValueError(f"Address with name {docname} does not exist.") + if save: + print("DEBUG: Saving updated Address document.") + address_doc = AddressService.get_or_throw(docname) + setattr(address_doc, fieldname, value) + address_doc.save(ignore_permissions=True) + else: + print("DEBUG: Not saving Address document as save=False.") + frappe.db.set_value("Address", docname, fieldname, value) + print(f"DEBUG: Updated Address {docname}: set {fieldname} to {value}") + return address_doc + + @staticmethod + def get_value(docname: str, fieldname: str): + """Get a specific field value of a document. Returns None if document does not exist.""" + print(f"DEBUG: Getting value of field {fieldname} from Address {docname}") + if not AddressService.exists(docname): + print("DEBUG: Value cannot be retrieved; Address does not exist.") + return None + value = frappe.db.get_value("Address", docname, fieldname) + print(f"DEBUG: Retrieved value: {value}") + return value + + @staticmethod + def get_value_or_throw(docname: str, fieldname: str): + """Get a specific field value of a document or throw an error if document does not exist.""" + value = AddressService.get_value(docname, fieldname) + if value is not None: + return value + raise ValueError(f"Address with name {docname} does not exist.") + + @staticmethod + def create(address_data: dict): + """Create a new address.""" + print("DEBUG: Creating new Address with data:", address_data) + address = frappe.get_doc({ + "doctype": "Address", + **address_data + }) + address.insert(ignore_permissions=True) + print("DEBUG: Created new Address:", address.as_dict()) + return address + + + \ No newline at end of file diff --git a/custom_ui/services/db_service.py b/custom_ui/services/db_service.py new file mode 100644 index 0000000..6035bdb --- /dev/null +++ b/custom_ui/services/db_service.py @@ -0,0 +1,91 @@ +import frappe + +class DbService: + + @staticmethod + def exists(doctype: str, name: str) -> bool: + """Check if a document exists by doctype and name.""" + result = frappe.db.exists(doctype, name) is not None + print(f"DEBUG: {doctype} existence for {name}: {result}") + return result + + @staticmethod + def get(doctype: str, name: str): + """Retrieve a document by doctype and name. Returns None if not found.""" + print(f"DEBUG: Retrieving {doctype} document with name: {name}") + if DbService.exists(doctype, name): + doc = frappe.get_doc(doctype, name) + print(f"DEBUG: {doctype} document found.") + return doc + print(f"DEBUG: {doctype} document not found.") + return None + + @staticmethod + def get_or_throw(doctype: str, name: str): + """Retrieve a document by doctype and name or throw an error if not found.""" + doc = DbService.get(doctype, name) + if doc: + return doc + raise ValueError(f"{doctype} document with name {name} does not exist.") + + @staticmethod + def get_value(doctype: str, name: str, fieldname: str): + """Get a specific field value of a document. Returns None if document does not exist.""" + print(f"DEBUG: Getting value of field {fieldname} from {doctype} {name}") + if not DbService.exists(doctype, name): + print("DEBUG: Value cannot be retrieved; document does not exist.") + return None + value = frappe.db.get_value(doctype, name, fieldname) + print(f"DEBUG: Retrieved value: {value}") + return value + + @staticmethod + def get_value_or_throw(doctype: str, name: str, fieldname: str): + """Get a specific field value of a document or throw an error if document does not exist.""" + value = DbService.get_value(doctype, name, fieldname) + if value is not None: + return value + raise ValueError(f"{doctype} document with name {name} does not exist.") + + @staticmethod + def create(doctype: str, data: dict): + """Create a new document of the specified doctype.""" + print(f"DEBUG: Creating new {doctype} document with data: {data}") + doc = frappe.get_doc({ + "doctype": doctype, + **data + }) + doc.insert(ignore_permissions=True) + print(f"DEBUG: Created new {doctype} document with name: {doc.name}") + return doc + + @staticmethod + def set_value(doctype: str, name: str, fieldname: str, value: any, save: bool = True): + """Set a specific field value of a document.""" + print(f"DEBUG: Setting value of field {fieldname} in {doctype} {name} to {value}") + if save: + print("DEBUG: Saving updated document.") + doc = DbService.get_or_throw(doctype, name) + setattr(doc, fieldname, value) + doc.save(ignore_permissions=True) + return doc + else: + print("DEBUG: Not saving document as save=False.") + frappe.db.set_value(doctype, name, fieldname, value) + return None + + @staticmethod + def update(doctype: str, name: str, update_data: dict, save: bool = True): + """Update an existing document of the specified doctype.""" + print(f"DEBUG: Updating {doctype} {name}") + doc = DbService.get_or_throw(doctype, name) + for key, value in update_data.items(): + setattr(doc, key, value) + if save: + doc.save(ignore_permissions=True) + else: + DbService.set_value(doctype, name, key, value, save=False) + print(f"DEBUG: Updated {doctype} document: {doc.as_dict()}") + return doc + + \ No newline at end of file diff --git a/custom_ui/services/estimate_service.py b/custom_ui/services/estimate_service.py new file mode 100644 index 0000000..63a734e --- /dev/null +++ b/custom_ui/services/estimate_service.py @@ -0,0 +1,87 @@ +import frappe + +class EstimateService: + + @staticmethod + def exists(estimate_name: str) -> bool: + """Check if a Quotation document exists by name.""" + print(f"DEBUG: Checking existence of Quotation document with name: {estimate_name}") + result = frappe.db.exists("Quotation", estimate_name) is not None + print(f"DEBUG: Quotation document existence: {result}") + return result + + @staticmethod + def get(estimate_name: str) -> frappe._dict: + """Retrieve a Quotation document by name. Returns None if not found.""" + print(f"DEBUG: Retrieving Quotation document with name: {estimate_name}") + if EstimateService.exists(estimate_name): + estimate_doc = frappe.get_doc("Quotation", estimate_name) + print("DEBUG: Quotation document found.") + return estimate_doc + print("DEBUG: Quotation document not found.") + return None + + @staticmethod + def get_or_throw(estimate_name: str) -> frappe._dict: + """Retrieve a Quotation document by name or throw an error if not found.""" + estimate_doc = EstimateService.get(estimate_name) + if estimate_doc: + return estimate_doc + raise ValueError(f"Quotation document with name {estimate_name} does not exist.") + + @staticmethod + def update_value(docname: str, fieldname: str, value, save: bool = True) -> None: + """Update a specific field value of an Quotation document.""" + print(f"DEBUG: Updating Quotation {docname}, setting {fieldname} to {value}") + if save: + print("DEBUG: Saving updated Quotation document.") + estimate_doc = EstimateService.get_or_throw(docname) + setattr(estimate_doc, fieldname, value) + estimate_doc.save(ignore_permissions=True) + else: + print("DEBUG: Not saving Quotation document as save=False.") + frappe.db.set_value("Quotation", docname, fieldname, value) + print(f"DEBUG: Updated Quotation {docname}: set {fieldname} to {value}") + + @staticmethod + def get_value(docname: str, fieldname: str) -> any: + """Get a specific field value of a Quotation document. Returns None if document does not exist.""" + print(f"DEBUG: Getting value of field {fieldname} from Quotation {docname}") + if not EstimateService.exists(docname): + print("DEBUG: Value cannot be retrieved; Quotation document does not exist.") + return None + value = frappe.db.get_value("Quotation", docname, fieldname) + print(f"DEBUG: Retrieved value: {value}") + return value + + @staticmethod + def get_value_or_throw(docname: str, fieldname: str) -> any: + """Get a specific field value of a Quotation document or throw an error if document does not exist.""" + value = EstimateService.get_value(docname, fieldname) + if value is not None: + return value + raise ValueError(f"Quotation document with name {docname} does not exist.") + + @staticmethod + def update(estimate_name: str, update_data: dict) -> frappe._dict: + """Update an existing Quotation document.""" + print(f"DEBUG: Updating Quotation {estimate_name} with data: {update_data}") + estimate_doc = EstimateService.get_or_throw(estimate_name) + for key, value in update_data.items(): + setattr(estimate_doc, key, value) + estimate_doc.save(ignore_permissions=True) + print(f"DEBUG: Updated Quotation document: {estimate_doc.as_dict()}") + return estimate_doc + + @staticmethod + def create(estimate_data: dict) -> frappe._dict: + """Create a new Quotation document.""" + print(f"DEBUG: Creating new Quotation with data: {estimate_data}") + estimate_doc = frappe.get_doc({ + "doctype": "Quotation", + **estimate_data + }) + estimate_doc.insert(ignore_permissions=True) + print(f"DEBUG: Created Quotation document: {estimate_doc.as_dict()}") + return estimate_doc + \ No newline at end of file