diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py index 07a8566d4a4..5d5a27f1eab 100644 --- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py +++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py @@ -1,6 +1,7 @@ # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt +from typing import Optional import frappe from frappe import _, msgprint @@ -706,29 +707,43 @@ def get_itemwise_batch(warehouse, posting_date, company, item_code=None): @frappe.whitelist() def get_stock_balance_for( - item_code, warehouse, posting_date, posting_time, batch_no=None, with_valuation_rate=True + item_code: str, + warehouse: str, + posting_date: str, + posting_time: str, + batch_no: Optional[str] = None, + with_valuation_rate: bool = True, ): frappe.has_permission("Stock Reconciliation", "write", throw=True) - item_dict = frappe.db.get_value("Item", item_code, ["has_serial_no", "has_batch_no"], as_dict=1) + item_dict = frappe.get_cached_value( + "Item", item_code, ["has_serial_no", "has_batch_no"], as_dict=1 + ) if not item_dict: # In cases of data upload to Items table msg = _("Item {} does not exist.").format(item_code) frappe.throw(msg, title=_("Missing")) - serial_nos = "" - with_serial_no = True if item_dict.get("has_serial_no") else False + serial_nos = None + has_serial_no = bool(item_dict.get("has_serial_no")) + has_batch_no = bool(item_dict.get("has_batch_no")) + + if not batch_no and has_batch_no: + # Not enough information to fetch data + return {"qty": 0, "rate": 0, "serial_nos": None} + + # TODO: fetch only selected batch's values data = get_stock_balance( item_code, warehouse, posting_date, posting_time, with_valuation_rate=with_valuation_rate, - with_serial_no=with_serial_no, + with_serial_no=has_serial_no, ) - if with_serial_no: + if has_serial_no: qty, rate, serial_nos = data else: qty, rate = data