From 70c78d0d6739d531c7897e702a2bfeac72784d3d Mon Sep 17 00:00:00 2001 From: Johannes Obermeier Date: Tue, 14 Mar 2023 10:57:05 +0100 Subject: [PATCH] fix: incorrect EAN validation, EAN can be an EAN8, EAN12 or EAN13 code (#34250) Co-authored-by: Deepesh Garg --- erpnext/stock/doctype/item/item.py | 25 ++++++++++++++++++------- erpnext/stock/doctype/item/test_item.py | 5 +++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py index c06700a99a1..05a37ee4c46 100644 --- a/erpnext/stock/doctype/item/item.py +++ b/erpnext/stock/doctype/item/item.py @@ -377,7 +377,9 @@ class Item(Document): "" if item_barcode.barcode_type not in options else item_barcode.barcode_type ) if item_barcode.barcode_type: - barcode_type = convert_erpnext_to_barcodenumber(item_barcode.barcode_type.upper()) + barcode_type = convert_erpnext_to_barcodenumber( + item_barcode.barcode_type.upper(), item_barcode.barcode + ) if barcode_type in barcodenumber.barcodes(): if not barcodenumber.check_code(barcode_type, item_barcode.barcode): frappe.throw( @@ -982,20 +984,29 @@ class Item(Document): ) -def convert_erpnext_to_barcodenumber(erpnext_number): +def convert_erpnext_to_barcodenumber(erpnext_number, barcode): + if erpnext_number == "EAN": + ean_type = { + 8: "EAN8", + 13: "EAN13", + } + barcode_length = len(barcode) + if barcode_length in ean_type: + return ean_type[barcode_length] + + return erpnext_number + convert = { "UPC-A": "UPCA", "CODE-39": "CODE39", - "EAN": "EAN13", - "EAN-12": "EAN", - "EAN-8": "EAN8", "ISBN-10": "ISBN10", "ISBN-13": "ISBN13", } + if erpnext_number in convert: return convert[erpnext_number] - else: - return erpnext_number + + return erpnext_number def make_item_price(item, price_list_name, item_price): diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index 67ed90d4e75..0c6dc77635d 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -581,8 +581,9 @@ class TestItem(FrappeTestCase): }, {"barcode": "72527273070", "barcode_type": "UPC-A"}, {"barcode": "123456", "barcode_type": "CODE-39"}, - {"barcode": "401268452363", "barcode_type": "EAN-12"}, - {"barcode": "90311017", "barcode_type": "EAN-8"}, + {"barcode": "401268452363", "barcode_type": "EAN"}, + {"barcode": "90311017", "barcode_type": "EAN"}, + {"barcode": "73513537", "barcode_type": "EAN"}, {"barcode": "0123456789012", "barcode_type": "GS1"}, {"barcode": "2211564566668", "barcode_type": "GTIN"}, {"barcode": "0256480249", "barcode_type": "ISBN"},