diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js index b5c1c38729f..81c1b147697 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js @@ -266,7 +266,7 @@ erpnext.stock.PurchaseReceiptController = class PurchaseReceiptController extend ); } cur_frm.add_custom_button( - __("Retention Stock Entry"), + __("Sample Retention Stock Entry"), this.make_retention_stock_entry, __("Create") ); diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index d106d097256..7d2ae01357a 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -440,12 +440,16 @@ frappe.ui.form.on("Stock Entry", { if ( frm.doc.docstatus == 1 && - frm.doc.purpose == "Material Receipt" && + ["Material Receipt", "Manufacture"].includes(frm.doc.purpose) && frm.get_sum("items", "sample_quantity") ) { - frm.add_custom_button(__("Create Sample Retention Stock Entry"), function () { - frm.trigger("make_retention_stock_entry"); - }); + frm.add_custom_button( + __("Sample Retention Stock Entry"), + function () { + frm.trigger("make_retention_stock_entry"); + }, + __("Create") + ); } frm.trigger("setup_quality_inspection"); @@ -568,10 +572,6 @@ frappe.ui.form.on("Stock Entry", { if (r.message) { var doc = frappe.model.sync(r.message)[0]; 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 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({ method: "erpnext.stock.doctype.stock_entry.stock_entry.validate_sample_quantity", args: { diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 8c200715397..fbcc43231dd 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -2570,6 +2570,7 @@ class StockEntry(StockController, SubcontractingInwardController): "expense_account": expense_account, "cost_center": item.get("buying_cost_center"), "is_finished_item": 1, + "sample_quantity": item.get("sample_quantity"), } if ( @@ -3103,6 +3104,7 @@ class StockEntry(StockController, SubcontractingInwardController): se_child.po_detail = item_row.get("po_detail") se_child.sco_rm_detail = item_row.get("sco_rm_detail") se_child.scio_detail = item_row.get("scio_detail") + se_child.sample_quantity = item_row.get("sample_quantity", 0) for field in [ self.subcontract_data.rm_detail_field, @@ -3408,13 +3410,14 @@ class StockEntry(StockController, SubcontractingInwardController): @frappe.whitelist() def move_sample_to_retention_warehouse(company, items): - from erpnext.stock.doctype.serial_and_batch_bundle.test_serial_and_batch_bundle import ( - get_batch_from_bundle, + from erpnext.stock.serial_batch_bundle import ( + SerialBatchCreation, + get_batch_nos, ) - from erpnext.stock.serial_batch_bundle import SerialBatchCreation if isinstance(items, str): items = json.loads(items) + retention_warehouse = frappe.get_single_value("Stock Settings", "sample_retention_warehouse") stock_entry = frappe.new_doc("Stock Entry") stock_entry.company = company @@ -3422,38 +3425,64 @@ def move_sample_to_retention_warehouse(company, items): stock_entry.set_stock_entry_type() for item in items: if item.get("sample_quantity") and item.get("serial_and_batch_bundle"): - batch_no = get_batch_from_bundle(item.get("serial_and_batch_bundle")) - sample_quantity = validate_sample_quantity( - item.get("item_code"), - item.get("sample_quantity"), - item.get("transfer_qty") or item.get("qty"), - batch_no, + warehouse = item.get("t_warehouse") or item.get("warehouse") + total_qty = 0 + cls_obj = SerialBatchCreation( + { + "type_of_transaction": "Outward", + "serial_and_batch_bundle": item.get("serial_and_batch_bundle"), + "item_code": item.get("item_code"), + "warehouse": warehouse, + "do_not_save": True, + } ) - - if sample_quantity: - cls_obj = SerialBatchCreation( - { - "type_of_transaction": "Outward", - "serial_and_batch_bundle": item.get("serial_and_batch_bundle"), - "item_code": item.get("item_code"), - "warehouse": item.get("t_warehouse"), - } + sabb = cls_obj.duplicate_package() + batches = get_batch_nos(item.get("serial_and_batch_bundle")) + sabe_list = [] + for batch_no in batches.keys(): + sample_quantity = validate_sample_quantity( + item.get("item_code"), + item.get("sample_quantity"), + 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( "items", { "item_code": item.get("item_code"), - "s_warehouse": item.get("t_warehouse"), + "s_warehouse": warehouse, "t_warehouse": retention_warehouse, - "qty": item.get("sample_quantity"), + "qty": total_qty, "basic_rate": item.get("valuation_rate"), "uom": item.get("uom"), "stock_uom": item.get("stock_uom"), "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"): diff --git a/erpnext/stock/doctype/stock_entry/stock_entry_utils.py b/erpnext/stock/doctype/stock_entry/stock_entry_utils.py index 576d129ee2d..6e54fd4e3b9 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry_utils.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry_utils.py @@ -190,6 +190,7 @@ def make_stock_entry(**args): "cost_center": args.cost_center, "expense_account": args.expense_account, "use_serial_batch_fields": args.use_serial_batch_fields, + "sample_quantity": frappe.get_value("Item", args.item, "sample_quantity") or 0, }, ) diff --git a/erpnext/stock/doctype/stock_entry/test_stock_entry.py b/erpnext/stock/doctype/stock_entry/test_stock_entry.py index cf1b12a8f28..a0ac4f180b2 100644 --- a/erpnext/stock/doctype/stock_entry/test_stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/test_stock_entry.py @@ -2277,6 +2277,46 @@ class TestStockEntry(IntegrationTestCase): se.save() 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): args = frappe._dict(args) diff --git a/erpnext/stock/serial_batch_bundle.py b/erpnext/stock/serial_batch_bundle.py index 13d7b100855..50603eb609d 100644 --- a/erpnext/stock/serial_batch_bundle.py +++ b/erpnext/stock/serial_batch_bundle.py @@ -989,9 +989,10 @@ def get_batch_nos(serial_and_batch_bundle): entries = frappe.get_all( "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")}, order_by="idx", + group_by="batch_no", ) if not entries: @@ -1116,7 +1117,7 @@ class SerialBatchCreation: id = self.serial_and_batch_bundle 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"): self.remove_returned_serial_nos(new_package)