refactor: move scan api to stock utils; add item_info
This commit is contained in:
parent
9bf427985f
commit
47f27a5171
@ -21,9 +21,7 @@ erpnext.utils.BarcodeScanner = class BarcodeScanner {
|
|||||||
// batch_no: "LOT12", // present if batch was scanned
|
// batch_no: "LOT12", // present if batch was scanned
|
||||||
// serial_no: "987XYZ", // present if serial no was scanned
|
// serial_no: "987XYZ", // present if serial no was scanned
|
||||||
// }
|
// }
|
||||||
this.scan_api =
|
this.scan_api = opts.scan_api || "erpnext.stock.utils.scan_barcode";
|
||||||
opts.scan_api ||
|
|
||||||
"erpnext.selling.page.point_of_sale.point_of_sale.search_for_serial_or_batch_or_barcode_number";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process_scan() {
|
process_scan() {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ from frappe.utils.nestedset import get_root_of
|
|||||||
|
|
||||||
from erpnext.accounts.doctype.pos_invoice.pos_invoice import get_stock_availability
|
from erpnext.accounts.doctype.pos_invoice.pos_invoice import get_stock_availability
|
||||||
from erpnext.accounts.doctype.pos_profile.pos_profile import get_child_nodes, get_item_groups
|
from erpnext.accounts.doctype.pos_profile.pos_profile import get_child_nodes, get_item_groups
|
||||||
|
from erpnext.stock.utils import scan_barcode
|
||||||
|
|
||||||
|
|
||||||
def search_by_term(search_term, warehouse, price_list):
|
def search_by_term(search_term, warehouse, price_list):
|
||||||
@ -152,38 +153,7 @@ def get_items(start, page_length, price_list, item_group, pos_profile, search_te
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def search_for_serial_or_batch_or_barcode_number(search_value: str) -> Dict[str, Optional[str]]:
|
def search_for_serial_or_batch_or_barcode_number(search_value: str) -> Dict[str, Optional[str]]:
|
||||||
|
return scan_barcode(search_value)
|
||||||
# search barcode no
|
|
||||||
barcode_data = frappe.db.get_value(
|
|
||||||
"Item Barcode",
|
|
||||||
{"barcode": search_value},
|
|
||||||
["barcode", "parent as item_code"],
|
|
||||||
as_dict=True,
|
|
||||||
)
|
|
||||||
if barcode_data:
|
|
||||||
return barcode_data
|
|
||||||
|
|
||||||
# search serial no
|
|
||||||
serial_no_data = frappe.db.get_value(
|
|
||||||
"Serial No",
|
|
||||||
search_value,
|
|
||||||
["name as serial_no", "item_code", "batch_no"],
|
|
||||||
as_dict=True,
|
|
||||||
)
|
|
||||||
if serial_no_data:
|
|
||||||
return serial_no_data
|
|
||||||
|
|
||||||
# search batch no
|
|
||||||
batch_no_data = frappe.db.get_value(
|
|
||||||
"Batch",
|
|
||||||
search_value,
|
|
||||||
["name as batch_no", "item as item_code"],
|
|
||||||
as_dict=True,
|
|
||||||
)
|
|
||||||
if batch_no_data:
|
|
||||||
return batch_no_data
|
|
||||||
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def get_conditions(search_term):
|
def get_conditions(search_term):
|
||||||
|
|||||||
31
erpnext/stock/tests/test_utils.py
Normal file
31
erpnext/stock/tests/test_utils.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import frappe
|
||||||
|
from frappe.tests.utils import FrappeTestCase
|
||||||
|
|
||||||
|
from erpnext.stock.doctype.item.test_item import make_item
|
||||||
|
from erpnext.stock.utils import scan_barcode
|
||||||
|
|
||||||
|
|
||||||
|
class TestStockUtilities(FrappeTestCase):
|
||||||
|
def test_barcode_scanning(self):
|
||||||
|
simple_item = make_item(properties={"barcodes": [{"barcode": "12399"}]})
|
||||||
|
self.assertEqual(scan_barcode("12399")["item_code"], simple_item.name)
|
||||||
|
|
||||||
|
batch_item = make_item(properties={"has_batch_no": 1, "create_new_batch": 1})
|
||||||
|
batch = frappe.get_doc(doctype="Batch", item=batch_item.name).insert()
|
||||||
|
|
||||||
|
batch_scan = scan_barcode(batch.name)
|
||||||
|
self.assertEqual(batch_scan["item_code"], batch_item.name)
|
||||||
|
self.assertEqual(batch_scan["batch_no"], batch.name)
|
||||||
|
self.assertEqual(batch_scan["has_batch_no"], 1)
|
||||||
|
self.assertEqual(batch_scan["has_serial_no"], 0)
|
||||||
|
|
||||||
|
serial_item = make_item(properties={"has_serial_no": 1})
|
||||||
|
serial = frappe.get_doc(
|
||||||
|
doctype="Serial No", item_code=serial_item.name, serial_no=frappe.generate_hash()
|
||||||
|
).insert()
|
||||||
|
|
||||||
|
serial_scan = scan_barcode(serial.name)
|
||||||
|
self.assertEqual(serial_scan["item_code"], serial_item.name)
|
||||||
|
self.assertEqual(serial_scan["serial_no"], serial.name)
|
||||||
|
self.assertEqual(serial_scan["has_batch_no"], 0)
|
||||||
|
self.assertEqual(serial_scan["has_serial_no"], 1)
|
||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
@ -548,3 +549,51 @@ def check_pending_reposting(posting_date: str, throw_error: bool = True) -> bool
|
|||||||
)
|
)
|
||||||
|
|
||||||
return bool(reposting_pending)
|
return bool(reposting_pending)
|
||||||
|
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def scan_barcode(search_value: str) -> Dict[str, Optional[str]]:
|
||||||
|
|
||||||
|
# search barcode no
|
||||||
|
barcode_data = frappe.db.get_value(
|
||||||
|
"Item Barcode",
|
||||||
|
{"barcode": search_value},
|
||||||
|
["barcode", "parent as item_code"],
|
||||||
|
as_dict=True,
|
||||||
|
)
|
||||||
|
if barcode_data:
|
||||||
|
return _update_item_info(barcode_data)
|
||||||
|
|
||||||
|
# search serial no
|
||||||
|
serial_no_data = frappe.db.get_value(
|
||||||
|
"Serial No",
|
||||||
|
search_value,
|
||||||
|
["name as serial_no", "item_code", "batch_no"],
|
||||||
|
as_dict=True,
|
||||||
|
)
|
||||||
|
if serial_no_data:
|
||||||
|
return _update_item_info(serial_no_data)
|
||||||
|
|
||||||
|
# search batch no
|
||||||
|
batch_no_data = frappe.db.get_value(
|
||||||
|
"Batch",
|
||||||
|
search_value,
|
||||||
|
["name as batch_no", "item as item_code"],
|
||||||
|
as_dict=True,
|
||||||
|
)
|
||||||
|
if batch_no_data:
|
||||||
|
return _update_item_info(batch_no_data)
|
||||||
|
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def _update_item_info(scan_result: Dict[str, Optional[str]]) -> Dict[str, Optional[str]]:
|
||||||
|
if item_code := scan_result.get("item_code"):
|
||||||
|
if item_info := frappe.get_cached_value(
|
||||||
|
"Item",
|
||||||
|
item_code,
|
||||||
|
["has_batch_no", "has_serial_no"],
|
||||||
|
as_dict=True,
|
||||||
|
):
|
||||||
|
scan_result.update(item_info)
|
||||||
|
return scan_result
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user