Skip to content

Commit

Permalink
fix: manufacturing date issue in the batch (#42034)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure authored Jun 26, 2024
1 parent 1a9899b commit eca3e02
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
16 changes: 15 additions & 1 deletion erpnext/stock/doctype/batch/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,25 @@ def set_batchwise_valuation(self):
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(
Expand Down
31 changes: 30 additions & 1 deletion erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3001,6 +3001,35 @@ def test_purchase_return_from_accepted_and_rejected_warehouse(self):
),
)

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
Expand Down

0 comments on commit eca3e02

Please sign in to comment.