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: barcode not fetched on selection of item (backport #40814) #41027

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion erpnext/stock/doctype/item/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
test_dependencies = ["Warehouse", "Item Group", "Item Tax Template", "Brand", "Item Attribute"]


def make_item(item_code=None, properties=None, uoms=None):
def make_item(item_code=None, properties=None, uoms=None, barcode=None):
if not item_code:
item_code = frappe.generate_hash(length=16)

Expand Down Expand Up @@ -61,6 +61,14 @@ def make_item(item_code=None, properties=None, uoms=None):
for uom in uoms:
item.append("uoms", uom)

if barcode:
item.append(
"barcodes",
{
"barcode": barcode,
},
)

item.insert()

return item
Expand Down
14 changes: 13 additions & 1 deletion erpnext/stock/doctype/stock_entry/stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
OpeningEntryAccountError,
)
from erpnext.stock.get_item_details import (
get_barcode_data,
get_bin_details,
get_conversion_factor,
get_default_cost_center,
Expand Down Expand Up @@ -354,7 +355,14 @@ def validate_item(self):
for field in reset_fields:
item.set(field, item_details.get(field))

update_fields = ("uom", "description", "expense_account", "cost_center", "conversion_factor")
update_fields = (
"uom",
"description",
"expense_account",
"cost_center",
"conversion_factor",
"barcode",
)

for field in update_fields:
if not item.get(field):
Expand Down Expand Up @@ -1439,6 +1447,10 @@ def get_item_details(self, args=None, for_update=False):
if subcontract_items and len(subcontract_items) == 1:
ret["subcontracted_item"] = subcontract_items[0].main_item_code

barcode_data = get_barcode_data(item_code=item.name)
if barcode_data and len(barcode_data.get(item.name)) == 1:
ret["barcode"] = barcode_data.get(item.name)[0]

return ret

@frappe.whitelist()
Expand Down
6 changes: 6 additions & 0 deletions erpnext/stock/doctype/stock_entry/test_stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def test_auto_material_request(self):
self._test_auto_material_request("_Test Item")
self._test_auto_material_request("_Test Item", material_request_type="Transfer")

def test_barcode_item_stock_entry(self):
item_code = make_item("_Test Item Stock Entry For Barcode", barcode="BDD-1234567890")

se = make_stock_entry(item_code=item_code, target="_Test Warehouse - _TC", qty=1, basic_rate=100)
self.assertEqual(se.items[0].barcode, "BDD-1234567890")

def test_auto_material_request_for_variant(self):
fields = [{"field_name": "reorder_levels"}]
set_item_variant_settings(fields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ frappe.ui.form.on("Stock Reconciliation", {
if (frm.doc.company) {
frm.trigger("toggle_display_account_head");
}

frm.events.set_fields_onload_for_line_item(frm);
},

set_fields_onload_for_line_item(frm) {
if (frm.is_new() && frm.doc?.items && cint(frappe.user_defaults?.use_serial_batch_fields) === 1) {
frm.doc.items.forEach((item) => {
if (!item.serial_and_batch_bundle) {
frappe.model.set_value(item.doctype, item.name, "use_serial_batch_fields", 1);
}
});
}
},

scan_barcode: function (frm) {
Expand Down Expand Up @@ -127,6 +139,9 @@ frappe.ui.form.on("Stock Reconciliation", {

item.qty = item.qty || 0;
item.valuation_rate = item.valuation_rate || 0;
item.use_serial_batch_fields = cint(
frappe.user_defaults?.use_serial_batch_fields
);
});
frm.refresh_field("items");
},
Expand Down Expand Up @@ -267,6 +282,10 @@ frappe.ui.form.on("Stock Reconciliation Item", {
if (!item.warehouse && frm.doc.set_warehouse) {
frappe.model.set_value(cdt, cdn, "warehouse", frm.doc.set_warehouse);
}

if (item.docstatus === 0 && cint(frappe.user_defaults?.use_serial_batch_fields) === 1) {
frappe.model.set_value(item.doctype, item.name, "use_serial_batch_fields", 1);
}
},
});

Expand Down
11 changes: 10 additions & 1 deletion erpnext/stock/get_item_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,21 @@ def update_barcode_value(out):
out["barcode"] = barcode_data.get(out.item_code)[0]


def get_barcode_data(items_list):
def get_barcode_data(items_list=None, item_code=None):
# get itemwise batch no data
# exmaple: {'LED-GRE': [Batch001, Batch002]}
# where LED-GRE is item code, SN0001 is serial no and Pune is warehouse

itemwise_barcode = {}
if not items_list and item_code:
_dict_item_code = frappe._dict(
{
"item_code": item_code,
}
)

items_list = [frappe._dict(_dict_item_code)]

for item in items_list:
barcodes = frappe.db.get_all("Item Barcode", filters={"parent": item.item_code}, fields="barcode")

Expand Down