From eca3e02f8da4b5d7d1b15a3c42de225168f3bf5e Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Wed, 26 Jun 2024 09:11:20 +0530 Subject: [PATCH] fix: manufacturing date issue in the batch (#42034) --- erpnext/stock/doctype/batch/batch.py | 16 +++++++++- .../purchase_receipt/test_purchase_receipt.py | 31 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/batch/batch.py b/erpnext/stock/doctype/batch/batch.py index 0be85e46015..e490badfc40 100644 --- a/erpnext/stock/doctype/batch/batch.py +++ b/erpnext/stock/doctype/batch/batch.py @@ -161,11 +161,25 @@ class Batch(Document): self.use_batchwise_valuation = 1 def before_save(self): + self.set_expiry_date() + + def set_expiry_date(self): has_expiry_date, shelf_life_in_days = frappe.db.get_value( "Item", self.item, ["has_expiry_date", "shelf_life_in_days"] ) + if not self.expiry_date and has_expiry_date and shelf_life_in_days: - self.expiry_date = add_days(self.manufacturing_date, shelf_life_in_days) + if ( + not self.manufacturing_date + and self.reference_doctype in ["Stock Entry", "Purchase Receipt", "Purchase Invoice"] + and self.reference_name + ): + self.manufacturing_date = frappe.db.get_value( + self.reference_doctype, self.reference_name, "posting_date" + ) + + if self.manufacturing_date: + self.expiry_date = add_days(self.manufacturing_date, shelf_life_in_days) if has_expiry_date and not self.expiry_date: frappe.throw( diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py index 605d16e1589..c68fc865a80 100644 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py @@ -3,7 +3,7 @@ import frappe from frappe.tests.utils import FrappeTestCase, change_settings -from frappe.utils import add_days, cint, cstr, flt, nowtime, today +from frappe.utils import add_days, cint, cstr, flt, getdate, nowtime, today from pypika import functions as fn import erpnext @@ -3001,6 +3001,35 @@ class TestPurchaseReceipt(FrappeTestCase): ), ) + def test_manufacturing_and_expiry_date_for_batch(self): + item = make_item( + "_Test Manufacturing and Expiry Date For Batch", + { + "is_purchase_item": 1, + "is_stock_item": 1, + "has_batch_no": 1, + "create_new_batch": 1, + "batch_number_series": "B-MEBATCH.#####", + "has_expiry_date": 1, + "shelf_life_in_days": 5, + }, + ) + + pr = make_purchase_receipt( + qty=10, + rate=100, + item_code=item.name, + posting_date=today(), + ) + + pr.reload() + self.assertTrue(pr.items[0].serial_and_batch_bundle) + + batch_no = get_batch_from_bundle(pr.items[0].serial_and_batch_bundle) + batch = frappe.get_doc("Batch", batch_no) + self.assertEqual(batch.manufacturing_date, getdate(today())) + self.assertEqual(batch.expiry_date, getdate(add_days(today(), 5))) + def prepare_data_for_internal_transfer(): from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_internal_supplier