fix: list index out of range (#21613)

This commit is contained in:
rohitwaghchaure 2020-05-07 12:06:47 +05:30 committed by GitHub
parent 9513171d3f
commit 9de26f6648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.naming import make_autoname, revert_series_if_last
from frappe.utils import flt, cint
from frappe.utils import flt, cint, get_link_to_form
from frappe.utils.jinja import render_template
from frappe.utils.data import add_days
from six import string_types
@ -124,7 +124,7 @@ class Batch(Document):
if has_expiry_date and not self.expiry_date:
frappe.throw(msg=_("Please set {0} for Batched Item {1}, which is used to set {2} on Submit.") \
.format(frappe.bold("Shelf Life in Days"),
frappe.utils.get_link_to_form("Item", self.item),
get_link_to_form("Item", self.item),
frappe.bold("Batch Expiry Date")),
title=_("Expiry Date Mandatory"))
@ -264,16 +264,20 @@ def get_batch_no(item_code, warehouse, qty=1, throw=False, serial_no=None):
def get_batches(item_code, warehouse, qty=1, throw=False, serial_no=None):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
cond = ''
if serial_no:
if serial_no and frappe.get_cached_value('Item', item_code, 'has_batch_no'):
serial_nos = get_serial_nos(serial_no)
batch = frappe.get_all("Serial No",
fields = ["distinct batch_no"],
filters= {
"item_code": item_code,
"warehouse": warehouse,
"name": ("in", get_serial_nos(serial_no))
"name": ("in", serial_nos)
}
)
if not batch:
validate_serial_no_with_batch(serial_nos, item_code)
if batch and len(batch) > 1:
return []
@ -288,4 +292,15 @@ def get_batches(item_code, warehouse, qty=1, throw=False, serial_no=None):
and (`tabBatch`.expiry_date >= CURDATE() or `tabBatch`.expiry_date IS NULL) {0}
group by batch_id
order by `tabBatch`.expiry_date ASC, `tabBatch`.creation ASC
""".format(cond), (item_code, warehouse), as_dict=True)
""".format(cond), (item_code, warehouse), as_dict=True)
def validate_serial_no_with_batch(serial_nos, item_code):
if frappe.get_cached_value("Serial No", serial_nos[0], "item_code") != item_code:
frappe.throw(_("The serial no {0} does not belong to item {1}")
.format(get_link_to_form("Serial No", serial_nos[0]), get_link_to_form("Item", item_code)))
serial_no_link = ','.join([get_link_to_form("Serial No", sn) for sn in serial_nos])
message = "Serial Nos" if len(serial_nos) > 1 else "Serial No"
frappe.throw(_("There is no batch found against the {0}: {1}")
.format(message, serial_no_link))