Skip to content

Commit

Permalink
Merge pull request #17 from navariltd/fix26
Browse files Browse the repository at this point in the history
fix - validate branch before save on both invoices
  • Loading branch information
maniamartial authored Dec 16, 2024
2 parents ae2b990 + 48c2994 commit f1e16aa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 45 deletions.
6 changes: 4 additions & 2 deletions kenya_compliance/kenya_compliance/apis/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down Expand Up @@ -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):
Expand All @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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}")

Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 11 additions & 28 deletions kenya_compliance/kenya_compliance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -705,4 +680,12 @@ def get_taxation_types(doc):
}


return taxation_totals
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

0 comments on commit f1e16aa

Please sign in to comment.