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: duplicate entry for serial / batch creation #39188

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
Original file line number Diff line number Diff line change
Expand Up @@ -1011,13 +1011,17 @@ def make_serial_nos(item_code, serial_nos):
item = frappe.get_cached_value("Item", item_code, ["description", "item_code"], as_dict=1)

serial_nos = [d.get("serial_no") for d in serial_nos if d.get("serial_no")]
existing_serial_nos = frappe.get_all("Serial No", filters={"name": ("in", serial_nos)})

existing_serial_nos = [d.get("name") for d in existing_serial_nos if d.get("name")]
serial_nos = list(set(serial_nos) - set(existing_serial_nos))

if not serial_nos:
return

serial_nos_details = []
user = frappe.session.user
for serial_no in serial_nos:
if frappe.db.exists("Serial No", serial_no):
continue

serial_nos_details.append(
(
serial_no,
Expand Down Expand Up @@ -1053,9 +1057,16 @@ def make_serial_nos(item_code, serial_nos):

def make_batch_nos(item_code, batch_nos):
item = frappe.get_cached_value("Item", item_code, ["description", "item_code"], as_dict=1)

batch_nos = [d.get("batch_no") for d in batch_nos if d.get("batch_no")]

existing_batches = frappe.get_all("Batch", filters={"name": ("in", batch_nos)})

existing_batches = [d.get("name") for d in existing_batches if d.get("name")]

batch_nos = list(set(batch_nos) - set(existing_batches))
if not batch_nos:
return

batch_nos_details = []
user = frappe.session.user
for batch_no in batch_nos:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from erpnext.stock.doctype.item.test_item import make_item
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import (
add_serial_batch_ledgers,
make_batch_nos,
make_serial_nos,
)
from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry

Expand Down Expand Up @@ -481,6 +483,38 @@ def test_auto_cancel_serial_and_batch(self):
docstatus = frappe.db.get_value("Serial and Batch Bundle", bundle, "docstatus")
self.assertEqual(docstatus, 2)

def test_batch_duplicate_entry(self):
item_code = make_item(properties={"has_batch_no": 1}).name

batch_id = "TEST-BATTCCH-VAL-00001"
batch_nos = [{"batch_no": batch_id, "qty": 1}]

make_batch_nos(item_code, batch_nos)
self.assertTrue(frappe.db.exists("Batch", batch_id))

batch_id = "TEST-BATTCCH-VAL-00001"
batch_nos = [{"batch_no": batch_id, "qty": 1}]

# Shouldn't throw duplicate entry error
make_batch_nos(item_code, batch_nos)
self.assertTrue(frappe.db.exists("Batch", batch_id))

def test_serial_no_duplicate_entry(self):
item_code = make_item(properties={"has_serial_no": 1}).name

serial_no_id = "TEST-SNID-VAL-00001"
serial_nos = [{"serial_no": serial_no_id, "qty": 1}]

make_serial_nos(item_code, serial_nos)
self.assertTrue(frappe.db.exists("Serial No", serial_no_id))

serial_no_id = "TEST-SNID-VAL-00001"
serial_nos = [{"batch_no": serial_no_id, "qty": 1}]

# Shouldn't throw duplicate entry error
make_serial_nos(item_code, serial_nos)
self.assertTrue(frappe.db.exists("Serial No", serial_no_id))


def get_batch_from_bundle(bundle):
from erpnext.stock.serial_batch_bundle import get_batch_nos
Expand Down
Loading