Merge pull request #51693 from mihir-kandoi/sample-retention-refactor

This commit is contained in:
Mihir Kandoi
2026-01-16 13:53:50 +05:30
committed by GitHub
6 changed files with 105 additions and 34 deletions

View File

@@ -266,7 +266,7 @@ erpnext.stock.PurchaseReceiptController = class PurchaseReceiptController extend
); );
} }
cur_frm.add_custom_button( cur_frm.add_custom_button(
__("Retention Stock Entry"), __("Sample Retention Stock Entry"),
this.make_retention_stock_entry, this.make_retention_stock_entry,
__("Create") __("Create")
); );

View File

@@ -440,12 +440,16 @@ frappe.ui.form.on("Stock Entry", {
if ( if (
frm.doc.docstatus == 1 && frm.doc.docstatus == 1 &&
frm.doc.purpose == "Material Receipt" && ["Material Receipt", "Manufacture"].includes(frm.doc.purpose) &&
frm.get_sum("items", "sample_quantity") frm.get_sum("items", "sample_quantity")
) { ) {
frm.add_custom_button(__("Create Sample Retention Stock Entry"), function () { frm.add_custom_button(
frm.trigger("make_retention_stock_entry"); __("Sample Retention Stock Entry"),
}); function () {
frm.trigger("make_retention_stock_entry");
},
__("Create")
);
} }
frm.trigger("setup_quality_inspection"); frm.trigger("setup_quality_inspection");
@@ -568,10 +572,6 @@ frappe.ui.form.on("Stock Entry", {
if (r.message) { if (r.message) {
var doc = frappe.model.sync(r.message)[0]; var doc = frappe.model.sync(r.message)[0];
frappe.set_route("Form", doc.doctype, doc.name); frappe.set_route("Form", doc.doctype, doc.name);
} else {
frappe.msgprint(
__("Retention Stock Entry already created or Sample Quantity not provided")
);
} }
}, },
}); });
@@ -1054,7 +1054,7 @@ frappe.ui.form.on("Stock Entry Detail", {
var validate_sample_quantity = function (frm, cdt, cdn) { var validate_sample_quantity = function (frm, cdt, cdn) {
var d = locals[cdt][cdn]; var d = locals[cdt][cdn];
if (d.sample_quantity && frm.doc.purpose == "Material Receipt") { if (d.sample_quantity && d.transfer_qty && frm.doc.purpose == "Material Receipt") {
frappe.call({ frappe.call({
method: "erpnext.stock.doctype.stock_entry.stock_entry.validate_sample_quantity", method: "erpnext.stock.doctype.stock_entry.stock_entry.validate_sample_quantity",
args: { args: {

View File

@@ -2570,6 +2570,7 @@ class StockEntry(StockController, SubcontractingInwardController):
"expense_account": expense_account, "expense_account": expense_account,
"cost_center": item.get("buying_cost_center"), "cost_center": item.get("buying_cost_center"),
"is_finished_item": 1, "is_finished_item": 1,
"sample_quantity": item.get("sample_quantity"),
} }
if ( if (
@@ -3103,6 +3104,7 @@ class StockEntry(StockController, SubcontractingInwardController):
se_child.po_detail = item_row.get("po_detail") se_child.po_detail = item_row.get("po_detail")
se_child.sco_rm_detail = item_row.get("sco_rm_detail") se_child.sco_rm_detail = item_row.get("sco_rm_detail")
se_child.scio_detail = item_row.get("scio_detail") se_child.scio_detail = item_row.get("scio_detail")
se_child.sample_quantity = item_row.get("sample_quantity", 0)
for field in [ for field in [
self.subcontract_data.rm_detail_field, self.subcontract_data.rm_detail_field,
@@ -3408,13 +3410,14 @@ class StockEntry(StockController, SubcontractingInwardController):
@frappe.whitelist() @frappe.whitelist()
def move_sample_to_retention_warehouse(company, items): def move_sample_to_retention_warehouse(company, items):
from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import ( from erpnext.stock.serial_batch_bundle import (
get_batch_from_bundle, SerialBatchCreation,
get_batch_nos,
) )
from erpnext.stock.serial_batch_bundle import SerialBatchCreation
if isinstance(items, str): if isinstance(items, str):
items = json.loads(items) items = json.loads(items)
retention_warehouse = frappe.get_single_value("Stock Settings", "sample_retention_warehouse") retention_warehouse = frappe.get_single_value("Stock Settings", "sample_retention_warehouse")
stock_entry = frappe.new_doc("Stock Entry") stock_entry = frappe.new_doc("Stock Entry")
stock_entry.company = company stock_entry.company = company
@@ -3422,38 +3425,64 @@ def move_sample_to_retention_warehouse(company, items):
stock_entry.set_stock_entry_type() stock_entry.set_stock_entry_type()
for item in items: for item in items:
if item.get("sample_quantity") and item.get("serial_and_batch_bundle"): if item.get("sample_quantity") and item.get("serial_and_batch_bundle"):
batch_no = get_batch_from_bundle(item.get("serial_and_batch_bundle")) warehouse = item.get("t_warehouse") or item.get("warehouse")
sample_quantity = validate_sample_quantity( total_qty = 0
item.get("item_code"), cls_obj = SerialBatchCreation(
item.get("sample_quantity"), {
item.get("transfer_qty") or item.get("qty"), "type_of_transaction": "Outward",
batch_no, "serial_and_batch_bundle": item.get("serial_and_batch_bundle"),
"item_code": item.get("item_code"),
"warehouse": warehouse,
"do_not_save": True,
}
) )
sabb = cls_obj.duplicate_package()
if sample_quantity: batches = get_batch_nos(item.get("serial_and_batch_bundle"))
cls_obj = SerialBatchCreation( sabe_list = []
{ for batch_no in batches.keys():
"type_of_transaction": "Outward", sample_quantity = validate_sample_quantity(
"serial_and_batch_bundle": item.get("serial_and_batch_bundle"), item.get("item_code"),
"item_code": item.get("item_code"), item.get("sample_quantity"),
"warehouse": item.get("t_warehouse"), item.get("transfer_qty") or item.get("qty"),
} batch_no,
) )
cls_obj.duplicate_package() sabe = next(item for item in sabb.entries if item.batch_no == batch_no)
if sample_quantity:
if sabb.has_serial_no:
new_sabe = [
entry
for entry in sabb.entries
if entry.batch_no == batch_no
and frappe.db.exists(
"Serial No", {"name": entry.serial_no, "warehouse": warehouse}
)
][: int(sample_quantity)]
sabe_list.extend(new_sabe)
total_qty += len(new_sabe)
else:
total_qty += sample_quantity
sabe.qty = sample_quantity
else:
sabb.entries.remove(sabe)
if total_qty:
if sabe_list:
sabb.entries = sabe_list
sabb.save()
stock_entry.append( stock_entry.append(
"items", "items",
{ {
"item_code": item.get("item_code"), "item_code": item.get("item_code"),
"s_warehouse": item.get("t_warehouse"), "s_warehouse": warehouse,
"t_warehouse": retention_warehouse, "t_warehouse": retention_warehouse,
"qty": item.get("sample_quantity"), "qty": total_qty,
"basic_rate": item.get("valuation_rate"), "basic_rate": item.get("valuation_rate"),
"uom": item.get("uom"), "uom": item.get("uom"),
"stock_uom": item.get("stock_uom"), "stock_uom": item.get("stock_uom"),
"conversion_factor": item.get("conversion_factor") or 1.0, "conversion_factor": item.get("conversion_factor") or 1.0,
"serial_and_batch_bundle": cls_obj.serial_and_batch_bundle, "serial_and_batch_bundle": sabb.name,
}, },
) )
if stock_entry.get("items"): if stock_entry.get("items"):

View File

@@ -190,6 +190,7 @@ def make_stock_entry(**args):
"cost_center": args.cost_center, "cost_center": args.cost_center,
"expense_account": args.expense_account, "expense_account": args.expense_account,
"use_serial_batch_fields": args.use_serial_batch_fields, "use_serial_batch_fields": args.use_serial_batch_fields,
"sample_quantity": frappe.get_value("Item", args.item, "sample_quantity") or 0,
}, },
) )

View File

@@ -2277,6 +2277,46 @@ class TestStockEntry(IntegrationTestCase):
se.save() se.save()
se.submit() se.submit()
@IntegrationTestCase.change_settings(
"Stock Settings", {"sample_retention_warehouse": "_Test Warehouse 1 - _TC"}
)
def test_sample_retention_stock_entry(self):
from erpnext.stock.doctype.stock_entry.stock_entry import move_sample_to_retention_warehouse
warehouse = "_Test Warehouse - _TC"
retain_sample_item = make_item(
"Retain Sample Item",
properties={
"is_stock_item": 1,
"retain_sample": 1,
"sample_quantity": 2,
"has_batch_no": 1,
"has_serial_no": 1,
"create_new_batch": 1,
"batch_number_series": "SAMPLE-RET-.#####",
"serial_no_series": "SAMPLE-RET-SN-.#####",
},
)
material_receipt = make_stock_entry(
item_code=retain_sample_item.item_code, target=warehouse, qty=10, purpose="Material Receipt"
)
source_sabb = frappe.get_doc(
"Serial and Batch Bundle", material_receipt.items[0].serial_and_batch_bundle
)
batch = source_sabb.entries[0].batch_no
serial_nos = [entry.serial_no for entry in source_sabb.entries]
sample_entry = frappe.get_doc(
move_sample_to_retention_warehouse(material_receipt.company, material_receipt.items)
)
sample_entry.submit()
target_sabb = frappe.get_doc("Serial and Batch Bundle", sample_entry.items[0].serial_and_batch_bundle)
self.assertEqual(sample_entry.items[0].transfer_qty, 2)
self.assertEqual(target_sabb.entries[0].batch_no, batch)
self.assertEqual([entry.serial_no for entry in target_sabb.entries], serial_nos[:2])
def make_serialized_item(self, **args): def make_serialized_item(self, **args):
args = frappe._dict(args) args = frappe._dict(args)

View File

@@ -989,9 +989,10 @@ def get_batch_nos(serial_and_batch_bundle):
entries = frappe.get_all( entries = frappe.get_all(
"Serial and Batch Entry", "Serial and Batch Entry",
fields=["batch_no", "qty", "name"], fields=["batch_no", {"SUM": "qty", "as": "qty"}],
filters={"parent": serial_and_batch_bundle, "batch_no": ("is", "set")}, filters={"parent": serial_and_batch_bundle, "batch_no": ("is", "set")},
order_by="idx", order_by="idx",
group_by="batch_no",
) )
if not entries: if not entries:
@@ -1116,7 +1117,7 @@ class SerialBatchCreation:
id = self.serial_and_batch_bundle id = self.serial_and_batch_bundle
package = frappe.get_doc("Serial and Batch Bundle", id) package = frappe.get_doc("Serial and Batch Bundle", id)
new_package = frappe.copy_doc(package) new_package = frappe.copy_doc(package, ignore_no_copy=False)
if self.get("returned_serial_nos"): if self.get("returned_serial_nos"):
self.remove_returned_serial_nos(new_package) self.remove_returned_serial_nos(new_package)