From 7a8d1931ed90964cfe870f1f96e13ae5cf8671df Mon Sep 17 00:00:00 2001 From: diptanilsaha Date: Sun, 15 Mar 2026 12:58:58 +0530 Subject: [PATCH] fix(serial_and_batch_bundle_selector): handle CSV attachment properly (#53460) --- .../serial_and_batch_bundle.py | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py index fa2a4e9fe1a..b880bb748d2 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py @@ -1715,17 +1715,34 @@ def upload_csv_file(item_code: str, file_path: str): def get_serial_batch_from_csv(item_code, file_path): - if "private" in file_path: - file_path = frappe.get_site_path() + file_path - else: - file_path = frappe.get_site_path() + "/public" + file_path + from frappe.utils.csvutils import read_csv_content serial_nos = [] batch_nos = [] - with open(file_path) as f: - reader = csv.reader(f) - serial_nos, batch_nos = parse_csv_file_to_get_serial_batch(reader) + if not file_path: + return serial_nos, batch_nos + + try: + file = frappe.get_doc("File", {"file_url": file_path}) + except frappe.DoesNotExistError: + frappe.msgprint( + _("File '{0}' not found").format(frappe.bold(file_path)), + alert=True, + indicator="red", + raise_exception=FileNotFoundError, + ) + + if file.file_type != "CSV": + frappe.msgprint( + _("{0} is not a CSV file.").format(frappe.bold(file.file_name)), + alert=True, + indicator="red", + raise_exception=frappe.ValidationError, + ) + + csv_data = read_csv_content(file.get_content()) + serial_nos, batch_nos = parse_csv_file_to_get_serial_batch(csv_data) if serial_nos: make_serial_nos(item_code, serial_nos) @@ -2823,7 +2840,7 @@ def get_auto_batch_nos(kwargs): ) if kwargs.based_on == "Expiry": - available_batches = sorted(available_batches, key=lambda x: (x.expiry_date or getdate("9999-12-31"))) + available_batches = sorted(available_batches, key=lambda x: x.expiry_date or getdate("9999-12-31")) if not kwargs.get("do_not_check_future_batches") and available_batches and kwargs.get("posting_datetime"): filter_zero_near_batches(available_batches, kwargs)