Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - search stock movement in all branch(initial fix) #13

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 84 additions & 47 deletions kenya_compliance/kenya_compliance/apis/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,52 @@ def bulk_register_item(docs_list: str) -> None:
for record in data:
for item in all_items:
if record == item.name:
item=frappe.get_doc("Item", record, for_update=False)
valuation_rate = item.valuation_rate if item.valuation_rate is not None else 0

request_data = {
"name": item.name,
"company_name": frappe.defaults.get_user_default("Company"),
"itemCd": item.custom_item_code_etims,
"itemClsCd": item.custom_item_classification,
"itemTyCd": item.custom_product_type,
"itemNm": item.item_name,
"temStdNm": None,
"orgnNatCd": item.custom_etims_country_of_origin_code,
"pkgUnitCd": item.custom_packaging_unit_code,
"qtyUnitCd": item.custom_unit_of_quantity_code,
"taxTyCd": item.get("custom_taxation_type", "B"),
"btchNo": None,
"bcd": None,
"dftPrc": round(valuation_rate, 2),
"grpPrcL1": None,
"grpPrcL2": None,
"grpPrcL3": None,
"grpPrcL4": None,
"grpPrcL5": None,
"addInfo": None,
"sftyQty": None,
"isrcAplcbYn": "Y",
"useYn": "Y",
"regrId": split_user_email(item.owner),
"regrNm": item.owner,
"modrId": split_user_email(item.modified_by),
"modrNm": item.modified_by,
}
perform_item_registration(request_data=json.dumps(request_data))
process_single_item(record)


@frappe.whitelist()
def process_single_item(record: str) -> None:
"""
Process a single item for registration, construct the payload, and perform registration.

Args:
record (str): Name of the item to process.
"""
item = frappe.get_doc("Item", record, for_update=False)

valuation_rate = item.valuation_rate if item.valuation_rate is not None else 0

request_data = {
"name": item.name,
"company_name": frappe.defaults.get_user_default("Company"),
"itemCd": item.custom_item_code_etims,
"itemClsCd": item.custom_item_classification,
"itemTyCd": item.custom_product_type,
"itemNm": item.item_name,
"temStdNm": None,
"orgnNatCd": item.custom_etims_country_of_origin_code,
"pkgUnitCd": item.custom_packaging_unit_code,
"qtyUnitCd": item.custom_unit_of_quantity_code,
"taxTyCd": item.get("custom_taxation_type", "B"),
"btchNo": None,
"bcd": None,
"dftPrc": round(valuation_rate, 2),
"grpPrcL1": None,
"grpPrcL2": None,
"grpPrcL3": None,
"grpPrcL4": None,
"grpPrcL5": None,
"addInfo": None,
"sftyQty": None,
"isrcAplcbYn": "Y",
"useYn": "Y",
"regrId": split_user_email(item.owner),
"regrNm": item.owner,
"modrId": split_user_email(item.modified_by),
"modrNm": item.modified_by,
}

perform_item_registration(request_data=json.dumps(request_data))


@frappe.whitelist()
Expand Down Expand Up @@ -320,7 +333,7 @@ def create_branch_user() -> None:
doc.system_user = user.email
doc.branch_id = frappe.get_value(
"Branch", {"custom_branch_code": "00"}, ["name"]
) # Created users are assigned to Branch 00
)

doc.save()

Expand Down Expand Up @@ -399,21 +412,21 @@ def perform_import_item_search_all_branches() -> None:
perform_import_item_search(request_data)

@frappe.whitelist()
def perform_purchases_search(request_data: str) -> None:
def perform_purchases_search(request_data: str, vendor: str="OSCU KRA") -> None:
data: dict = json.loads(request_data)

company_name = data["company_name"]

headers = build_headers(company_name)
server_url = get_server_url(company_name)
headers = build_headers(company_name, vendor)
server_url = get_server_url(company_name, vendor)
route_path, last_request_date = get_route_path("TrnsPurchaseSalesReq")

if headers and server_url and route_path:
request_date = last_request_date.strftime("%Y%m%d%H%M%S")

url = f"{server_url}{route_path}"
payload = {"lastReqDt": request_date}

frappe.throw(str(payload))
endpoints_builder.headers = headers
endpoints_builder.url = url
endpoints_builder.payload = payload
Expand All @@ -426,13 +439,13 @@ def perform_purchases_search(request_data: str) -> None:


@frappe.whitelist()
def submit_inventory(request_data: str) -> None:
def submit_inventory(request_data: str, vendor: str="OSCU KRA") -> None:
data: dict = json.loads(request_data)

company_name = frappe.defaults.get_user_default("Company")

headers = build_headers(company_name, data["branch_id"])
server_url = get_server_url(company_name, data["branch_id"])
headers = build_headers(company_name,vendor, data["branch_id"])
server_url = get_server_url(company_name,vendor, data["branch_id"])
route_path, last_request_date = get_route_path("StockMasterSaveReq")

if headers and server_url and route_path:
Expand Down Expand Up @@ -612,7 +625,6 @@ def perform_stock_movement_search(request_data: str, vendor="OSCU KRA") -> None:
if headers and server_url and route_path:
url = f"{server_url}{route_path}"
payload = {"lastReqDt": request_date}

endpoints_builder.headers = headers
endpoints_builder.url = url
endpoints_builder.payload = payload
Expand All @@ -627,6 +639,18 @@ def perform_stock_movement_search(request_data: str, vendor="OSCU KRA") -> None:
job_name=token_hex(100),
)

@frappe.whitelist()
def perform_stock_movement_search_all_branches() -> None:
all_credentials = frappe.get_all(
SETTINGS_DOCTYPE_NAME,
["name", "bhfid", "communication_key", "tin", "company"],
)

for credential in all_credentials:
request_data = json.dumps(
{"company_name": credential.company, "branch_id": credential.bhfid}
)
perform_stock_movement_search(request_data)

@frappe.whitelist()
def submit_item_composition(request_data: str, vendor="OSCU KRA") -> None:
Expand Down Expand Up @@ -727,7 +751,6 @@ def create_supplier(supplier_details: dict) -> Document:
@frappe.whitelist()
def create_items_from_fetched_registered_purchases(request_data: str) -> None:
data = json.loads(request_data)

if data["items"]:
items = data["items"]
for item in items:
Expand All @@ -736,7 +759,7 @@ def create_items_from_fetched_registered_purchases(request_data: str) -> None:

def create_item(item: dict | frappe._dict) -> Document:
item_code = item.get("item_code", None)

new_item = frappe.new_doc("Item")
new_item.is_stock_item = 0 # Default to 0
new_item.item_code = item["item_name"]
Expand Down Expand Up @@ -771,7 +794,8 @@ def create_item(item: dict | frappe._dict) -> Document:
new_item.custom_referenced_imported_item = item["imported_item"]

new_item.insert(ignore_mandatory=True, ignore_if_duplicate=True)

if new_item.custom_item_classification is not None:
process_single_item(new_item.name)
return new_item


Expand Down Expand Up @@ -848,14 +872,14 @@ def create_purchase_invoice_from_request(request_data: str) -> None:
"custom_packaging_unit": item["packaging_unit_code"],
"custom_unit_of_quantity": item["quantity_unit_code"],
"custom_taxation_type": item["taxation_type_code"],
"task_code": item["task_code"],
},
)

validate_mapping_and_registration_of_items(data["items"])
purchase_invoice.insert(ignore_mandatory=True)

frappe.msgprint("Purchase Invoices have been created")


@frappe.whitelist()
def ping_server(request_data: str) -> None:
url = json.loads(request_data)["server_url"]
Expand Down Expand Up @@ -918,3 +942,16 @@ def create_stock_entry_from_stock_movement(request_data: str) -> None:
stock_entry.save()

frappe.msgprint(f"Stock Entry {stock_entry.name} created successfully")

def validate_mapping_and_registration_of_items(items):
for item in items:
task_code = item.get("task_code") or item.get("item_name")
items = frappe.get_all("Item",
filters={"custom_referenced_imported_item": task_code},
fields=["name","item_name","item_code"]
)
if items:
item_name = items[0].name
from kenya_compliance.kenya_compliance.overrides.server.purchase_invoice import validation_message
validation_message(item_name)

Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def submit_inventory_on_success(response: dict, document_name: str) -> None:
{"custom_inventory_submitted_successfully": 1},
)


def sales_information_submission_on_success(
response: dict,
invoice_type: str,
Expand Down Expand Up @@ -370,7 +369,13 @@ def create_if_not_exists(doctype: str, code: str) -> str:

for item in items:
doc = frappe.new_doc(REGISTERED_IMPORTED_ITEM_DOCTYPE_NAME)

if frappe.db.exists(
REGISTERED_IMPORTED_ITEM_DOCTYPE_NAME,
{
"task_code": item["taskCd"],
},
):
continue
doc.item_name = item["itemNm"]
doc.task_code = item["taskCd"]
doc.declaration_date = datetime.strptime(item["dclDe"], "%d%m%Y")
Expand Down Expand Up @@ -398,14 +403,14 @@ def create_if_not_exists(doctype: str, code: str) -> str:
doc.invoice_foreign_currency_amount = item["invcFcurAmt"]
doc.invoice_foreign_currency = item["invcFcurCd"]
doc.invoice_foreign_currency_rate = item["invcFcurExcrt"]
doc.rate = item["invcFcurAmt"] / item["qty"]

doc.save()

frappe.msgprint(
"Imported Items Fetched. Go to <b>Navari eTims Registered Imported Item</b> Doctype for more information"
)



def search_branch_request_on_success(response: dict) -> None:
for branch in response["data"]["bhfList"]:
doc = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ frappe.ui.form.on(doctypeName, {
frappe.db.get_value(
'Item',
{ custom_referenced_imported_item: frm.doc.name },
['custom_item_registered'],
['custom_item_registered','name'],
(response) => {
if (parseInt(response.custom_item_registered) === 1) {
frm.add_custom_button(
Expand All @@ -98,6 +98,7 @@ frappe.ui.form.on(doctypeName, {
currency: frm.doc.invoice_foreign_currency,
amount: frm.doc.invoice_foreign_currency_amount,
items: item,
task_code: frm.doc.task_code,
},
},
callback: (response) => {
Expand Down
Loading
Loading