diff --git a/kenya_compliance/kenya_compliance/apis/apis.py b/kenya_compliance/kenya_compliance/apis/apis.py index 3398b3e..2314a7e 100644 --- a/kenya_compliance/kenya_compliance/apis/apis.py +++ b/kenya_compliance/kenya_compliance/apis/apis.py @@ -11,6 +11,7 @@ from frappe.model.document import Document from frappe.utils.dateutils import add_to_date + from ..doctype.doctype_names_mapping import ( COUNTRIES_DOCTYPE_NAME, SETTINGS_DOCTYPE_NAME, @@ -23,6 +24,7 @@ get_server_url, make_get_request, split_user_email, + get_first_branch_id, ) from .api_builder import EndpointsBuilder from .remote_response_status_handlers import ( @@ -801,7 +803,7 @@ def create_item(item: dict | frappe._dict) -> Document: @frappe.whitelist() def create_purchase_invoice_from_request(request_data: str) -> None: data = json.loads(request_data) - + bhifd=get_first_branch_id() # Check if supplier exists supplier = None if not frappe.db.exists("Supplier", data["supplier_name"], cache=False): @@ -825,7 +827,7 @@ def create_purchase_invoice_from_request(request_data: str) -> None: purchase_invoice.custom_supplier_branch_id = data["supplier_branch_id"] purchase_invoice.bill_no = data["supplier_invoice_no"] purchase_invoice.bill_date = data["supplier_invoice_date"] - + purchase_invoice.bhifd = bhifd if "currency" in data: # The "currency" key is only available when creating from Imported Item purchase_invoice.currency = data["currency"] diff --git a/kenya_compliance/kenya_compliance/overrides/server/purchase_invoice.py b/kenya_compliance/kenya_compliance/overrides/server/purchase_invoice.py index e5f6ad3..3a4581b 100644 --- a/kenya_compliance/kenya_compliance/overrides/server/purchase_invoice.py +++ b/kenya_compliance/kenya_compliance/overrides/server/purchase_invoice.py @@ -25,6 +25,8 @@ def validate(doc: Document, method: str) -> None: + if not doc.branch: + frappe.throw("Please ensure the branch is set before saving the document") item_taxes = get_itemised_tax_breakup_data(doc) # if not doc.branch: # frappe.throw("Please ensure the branch is set before submitting the document") @@ -51,8 +53,6 @@ def validate(doc: Document, method: str) -> None: def on_submit(doc: Document, method: str) -> None: - if not doc.branch: - frappe.throw("Please ensure the branch is set before submitting the document") validate_item_registration(doc.items) if doc.is_return == 0 and doc.update_stock == 1: # TODO: Handle cases when item tax templates have not been picked @@ -179,13 +179,13 @@ def validate_item_registration(items): validation_message(item_code) def validation_message(item_code): - item_doc = frappe.get_doc("Item", item_code) - - if item_doc.custom_referenced_imported_item and (item_doc.custom_item_registered == 0 or item_doc.custom_imported_item_submitted == 0): - item_link = get_link_to_form("Item", item_doc.name) - frappe.throw(f"Register or submit the item: {item_link}") - - elif not item_doc.custom_referenced_imported_item and item_doc.custom_item_registered == 0: - item_link = get_link_to_form("Item", item_doc.name) - frappe.throw(f"Register the item: {item_link}") + item_doc = frappe.get_doc("Item", item_code) + + if item_doc.custom_referenced_imported_item and (item_doc.custom_item_registered == 0 or item_doc.custom_imported_item_submitted == 0): + item_link = get_link_to_form("Item", item_doc.name) + frappe.throw(f"Register or submit the item: {item_link}") + + elif not item_doc.custom_referenced_imported_item and item_doc.custom_item_registered == 0: + item_link = get_link_to_form("Item", item_doc.name) + frappe.throw(f"Register the item: {item_link}") diff --git a/kenya_compliance/kenya_compliance/overrides/server/sales_invoice.py b/kenya_compliance/kenya_compliance/overrides/server/sales_invoice.py index da1b34a..eb38cf2 100644 --- a/kenya_compliance/kenya_compliance/overrides/server/sales_invoice.py +++ b/kenya_compliance/kenya_compliance/overrides/server/sales_invoice.py @@ -2,11 +2,9 @@ from frappe.model.document import Document from .shared_overrides import generic_invoices_on_submit_override - +from .purchase_invoice import validate def on_submit(doc: Document, method: str) -> None: - if not doc.branch: - frappe.throw("Please ensure the branch is set before submitting the document") """Intercepts submit event for document""" if ( diff --git a/kenya_compliance/kenya_compliance/overrides/server/shared_overrides.py b/kenya_compliance/kenya_compliance/overrides/server/shared_overrides.py index be614ce..3554caa 100644 --- a/kenya_compliance/kenya_compliance/overrides/server/shared_overrides.py +++ b/kenya_compliance/kenya_compliance/overrides/server/shared_overrides.py @@ -74,7 +74,8 @@ def validate(doc: Document, method: str) -> None: doc.custom_scu_id = get_curr_env_etims_settings( frappe.defaults.get_user_default("Company"),vendor, doc.branch ).scu_id - + if not doc.branch: + frappe.throw("Please ensure the branch is set before saving the documents") # item_taxes = get_itemised_tax_breakup_data(doc) # taxes_breakdown = defaultdict(list) diff --git a/kenya_compliance/kenya_compliance/utils.py b/kenya_compliance/kenya_compliance/utils.py index 56fc4a4..0f6c3a1 100644 --- a/kenya_compliance/kenya_compliance/utils.py +++ b/kenya_compliance/kenya_compliance/utils.py @@ -110,26 +110,6 @@ def is_valid_url(url: str) -> bool: return bool(re.match(pattern, url)) -# def get_route_path( -# search_field: str, -# vendor: str, -# routes_table_doctype: str = ROUTES_TABLE_CHILD_DOCTYPE_NAME, -# ) -> tuple[str, str] | None: - -# query = f""" -# SELECT -# url_path, -# last_request_date -# FROM `tab{routes_table_doctype}` -# WHERE url_path_function LIKE '{search_field}' -# AND parent LIKE '{ROUTES_TABLE_DOCTYPE_NAME}' -# LIMIT 1 -# """ - -# results = frappe.db.sql(query, as_dict=True) - -# if results: -# return (results[0].url_path, results[0].last_request_date) def get_route_path( search_field: str, vendor: str="OSCU KRA", @@ -370,6 +350,8 @@ def build_invoice_payload( return payload + + # def build_invoice_payload( # invoice: Document, invoice_type_identifier: Literal["S", "C"], company_name: str # ) -> dict[str, str | int]: @@ -491,13 +473,6 @@ def get_invoice_items_list(invoice: Document) -> list[dict[str, str | int | None items_list = [] for index, item in enumerate(invoice.items): - # taxable_amount = round(int(item_taxes[index]["taxable_amount"]), 2) - # actual_tax_amount = 0 - # tax_head = invoice.taxes[0].description # Fetch tax head from taxes table - - # actual_tax_amount = item_taxes[index][tax_head]["tax_amount"] - - # tax_amount = round(actual_tax_amount, 2) items_list.append( { @@ -705,4 +680,12 @@ def get_taxation_types(doc): } - return taxation_totals \ No newline at end of file + return taxation_totals + +def get_first_branch_id() -> str | None: + settings = frappe.get_all("Navari KRA eTims Settings", filters={"is_active": 1}, fields=["bhfid"], limit=1) + + if settings: + return settings[0].bhfid + + return None \ No newline at end of file