From 03bf5296223407d850daea7cff953052579177ff Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Thu, 9 Jul 2015 18:40:38 +0530 Subject: [PATCH] Validation added to prevent adding items with expired batch numbers to stock ledger entry --- erpnext/stock/doctype/stock_entry/stock_entry.py | 6 ++++-- .../doctype/stock_ledger_entry/stock_ledger_entry.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 93fd325d885..d30e7f2eda3 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -730,8 +730,10 @@ class StockEntry(StockController): if self.purpose in ["Material Transfer for Manufacture", "Manufacture", "Repack", "Subcontract"]: for item in self.get("items"): if item.batch_no: - if getdate(self.posting_date) > getdate(frappe.db.get_value("Batch", item.batch_no, "expiry_date")): - frappe.throw(_("Batch {0} of Item {1} has expired.").format(item.batch_no, item.item_code)) + expiry_date = getdate(frappe.db.get_value("Batch", item.batch_no, "expiry_date")) + if expiry_date: + if getdate(self.posting_date) > expiry_date: + frappe.throw(_("Batch {0} of Item {1} has expired.").format(item.batch_no, item.item_code)) @frappe.whitelist() def get_party_details(ref_dt, ref_dn): diff --git a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py index de6b3a6950f..848a00d5547 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py +++ b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.py @@ -18,6 +18,7 @@ class StockLedgerEntry(Document): from erpnext.stock.utils import validate_warehouse_company self.validate_mandatory() self.validate_item() + self.validate_batch() validate_warehouse_company(self.warehouse, self.company) self.scrub_posting_time() @@ -96,6 +97,13 @@ class StockLedgerEntry(Document): def scrub_posting_time(self): if not self.posting_time or self.posting_time == '00:0': self.posting_time = '00:00' + + def validate_batch(self): + if self.batch_no and self.voucher_type != "Stock Entry": + expiry_date = getdate(frappe.db.get_value("Batch", self.batch_no, "expiry_date")) + if expiry_date: + if getdate(self.posting_date) > expiry_date: + frappe.throw(_("Batch {0} of Item {1} has expired.").format(self.batch_no, self.item_code)) def on_doctype_update(): if not frappe.db.sql("""show index from `tabStock Ledger Entry`