Skip to content

Commit

Permalink
fix: not able to issue expired batches
Browse files Browse the repository at this point in the history
(cherry picked from commit 795c943)

# Conflicts:
#	erpnext/stock/doctype/stock_entry/test_stock_entry.py
  • Loading branch information
rohitwaghchaure authored and mergify[bot] committed Aug 17, 2022
1 parent 3274e76 commit 7ee75ff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
14 changes: 13 additions & 1 deletion erpnext/controllers/stock_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class QualityInspectionNotSubmittedError(frappe.ValidationError):
pass


class BatchExpiredError(frappe.ValidationError):
pass


class StockController(AccountsController):
def validate(self):
super(StockController, self).validate()
Expand Down Expand Up @@ -74,6 +78,10 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):
def validate_serialized_batch(self):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

is_material_issue = False
if self.doctype == "Stock Entry" and self.purpose == "Material Issue":
is_material_issue = True

for d in self.get("items"):
if hasattr(d, "serial_no") and hasattr(d, "batch_no") and d.serial_no and d.batch_no:
serial_nos = frappe.get_all(
Expand All @@ -90,14 +98,18 @@ def validate_serialized_batch(self):
)
)

if is_material_issue:
continue

if flt(d.qty) > 0.0 and d.get("batch_no") and self.get("posting_date") and self.docstatus < 2:
expiry_date = frappe.get_cached_value("Batch", d.get("batch_no"), "expiry_date")

if expiry_date and getdate(expiry_date) < getdate(self.posting_date):
frappe.throw(
_("Row #{0}: The batch {1} has already expired.").format(
d.idx, get_link_to_form("Batch", d.get("batch_no"))
)
),
BatchExpiredError,
)

def clean_serial_nos(self):
Expand Down
8 changes: 7 additions & 1 deletion erpnext/stock/doctype/batch/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,14 @@ def make_new_batch(**args):
"doctype": "Batch",
"batch_id": args.batch_id,
"item": args.item_code,
"expiry_date": args.expiry_date,
}
).insert()
)

if args.expiry_date:
batch.expiry_date = args.expiry_date

batch.insert()

except frappe.DuplicateEntryError:
batch = frappe.get_doc("Batch", args.batch_id)
Expand Down
29 changes: 29 additions & 0 deletions erpnext/stock/doctype/stock_entry/test_stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import frappe
from frappe.permissions import add_user_permission, remove_user_permission
from frappe.tests.utils import FrappeTestCase, change_settings
<<<<<<< HEAD
from frappe.utils import add_days, flt, nowdate, nowtime
from six import iteritems
=======
from frappe.utils import add_days, flt, nowdate, nowtime, today
>>>>>>> 795c94384a (fix: not able to issue expired batches)

from erpnext.accounts.doctype.account.test_account import get_inventory_account
from erpnext.stock.doctype.item.test_item import (
Expand Down Expand Up @@ -1546,6 +1550,31 @@ def test_reposting_for_depedent_warehouse(self):
self.assertEqual(obj.items[index].basic_rate, 200)
self.assertEqual(obj.items[index].basic_amount, 2000)

def test_batch_expiry(self):
from erpnext.controllers.stock_controller import BatchExpiredError
from erpnext.stock.doctype.batch.test_batch import make_new_batch

item_code = "Test Batch Expiry Test Item - 001"
item_doc = create_item(item_code=item_code, is_stock_item=1, valuation_rate=10)

item_doc.has_batch_no = 1
item_doc.save()

batch = make_new_batch(
batch_id=frappe.generate_hash("", 5), item_code=item_doc.name, expiry_date=add_days(today(), -1)
)

se = make_stock_entry(
item_code=item_code,
purpose="Material Receipt",
qty=4,
to_warehouse="_Test Warehouse - _TC",
batch_no=batch.name,
do_not_save=True,
)

self.assertRaises(BatchExpiredError, se.save)


def make_serialized_item(**args):
args = frappe._dict(args)
Expand Down

0 comments on commit 7ee75ff

Please sign in to comment.