diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 5fb228375760..6c914770a263 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -1889,6 +1889,10 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe const fields = ["discount_percentage", "discount_amount", "margin_rate_or_amount", "rate_with_margin"]; + if (!item) { + return; + } + if(item.remove_free_item) { let items = []; 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 ef4d475dca4d..956eb08d9ff2 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 @@ -1558,13 +1558,14 @@ def get_serial_nos_based_on_posting_date(kwargs, ignore_serial_nos): serial_nos = set() data = get_stock_ledgers_for_serial_nos(kwargs) + bundle_wise_serial_nos = get_bundle_wise_serial_nos(data) for d in data: if d.serial_and_batch_bundle: - sns = get_serial_nos_from_bundle(d.serial_and_batch_bundle, kwargs.get("serial_nos", [])) - if d.actual_qty > 0: - serial_nos.update(sns) - else: - serial_nos.difference_update(sns) + if sns := bundle_wise_serial_nos.get(d.serial_and_batch_bundle): + if d.actual_qty > 0: + serial_nos.update(sns) + else: + serial_nos.difference_update(sns) elif d.serial_no: sns = get_serial_nos(d.serial_no) @@ -1581,6 +1582,25 @@ def get_serial_nos_based_on_posting_date(kwargs, ignore_serial_nos): return serial_nos +def get_bundle_wise_serial_nos(data): + bundle_wise_serial_nos = defaultdict(list) + bundles = [d.serial_and_batch_bundle for d in data if d.serial_and_batch_bundle] + if not bundles: + return bundle_wise_serial_nos + + bundle_data = frappe.get_all( + "Serial and Batch Entry", + fields=["serial_no", "parent"], + filters={"parent": ("in", bundles), "docstatus": 1, "serial_no": ("is", "set")}, + ) + + for d in bundle_data: + if d.parent: + bundle_wise_serial_nos[d.parent].append(d.serial_no) + + return bundle_wise_serial_nos + + def get_reserved_serial_nos(kwargs) -> list: """Returns a list of `Serial No` reserved in POS Invoice and Stock Reservation Entry."""