Merge pull request #31866 from frappe/mergify/bp/version-13-hotfix/pr-31863

fix: not able to issue expired batches (backport #31863)
This commit is contained in:
rohitwaghchaure
2022-08-17 15:43:22 +05:30
committed by GitHub
3 changed files with 46 additions and 3 deletions

View File

@@ -33,6 +33,10 @@ class QualityInspectionNotSubmittedError(frappe.ValidationError):
pass
class BatchExpiredError(frappe.ValidationError):
pass
class StockController(AccountsController):
def validate(self):
super(StockController, self).validate()
@@ -74,6 +78,10 @@ class StockController(AccountsController):
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(
@@ -90,6 +98,9 @@ class StockController(AccountsController):
)
)
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")
@@ -97,7 +108,8 @@ class StockController(AccountsController):
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):

View File

@@ -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)

View File

@@ -5,7 +5,7 @@
import frappe
from frappe.permissions import add_user_permission, remove_user_permission
from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import add_days, flt, nowdate, nowtime
from frappe.utils import add_days, flt, nowdate, nowtime, today
from six import iteritems
from erpnext.accounts.doctype.account.test_account import get_inventory_account
@@ -1546,6 +1546,31 @@ class TestStockEntry(FrappeTestCase):
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)