From 2175d0d5d6fae6c30109fc6afeda318999a7d759 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Fri, 31 Jan 2025 10:26:35 +0530 Subject: [PATCH 1/2] fix: validation to prevent submission if the SABB is not linked to a stock transaction --- erpnext/controllers/selling_controller.py | 6 +- erpnext/controllers/stock_controller.py | 20 ++++- .../serial_and_batch_bundle.py | 27 +++++++ .../test_serial_and_batch_bundle.py | 74 +++++++++++++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 59388e2645e..c62780c5eb4 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -334,7 +334,7 @@ class SellingController(StockController): "batch_no": p.batch_no if self.docstatus == 2 else None, "uom": p.uom, "serial_and_batch_bundle": p.serial_and_batch_bundle - or get_serial_and_batch_bundle(p, self), + or get_serial_and_batch_bundle(p, self, d), "name": d.name, "target_warehouse": p.target_warehouse, "company": self.company, @@ -846,7 +846,7 @@ def set_default_income_account_for_item(obj): set_item_default(d.item_code, obj.company, "income_account", d.income_account) -def get_serial_and_batch_bundle(child, parent): +def get_serial_and_batch_bundle(child, parent, delivery_note_child=None): from erpnext.stock.serial_batch_bundle import SerialBatchCreation if child.get("use_serial_batch_fields"): @@ -866,7 +866,7 @@ def get_serial_and_batch_bundle(child, parent): "warehouse": child.warehouse, "voucher_type": parent.doctype, "voucher_no": parent.name if parent.docstatus < 2 else None, - "voucher_detail_no": child.name, + "voucher_detail_no": delivery_note_child.name if delivery_note_child else child.name, "posting_date": parent.posting_date, "posting_time": parent.posting_time, "qty": child.qty, diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 17d9374cb2b..398f415e43e 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -215,6 +215,10 @@ class StockController(AccountsController): if self.doctype == "Asset Capitalization": table_name = "stock_items" + parent_details = frappe._dict() + if table_name == "packed_items": + parent_details = self.get_parent_details_for_packed_items() + for row in self.get(table_name): if row.serial_and_batch_bundle and (row.serial_no or row.batch_no): self.validate_serial_nos_and_batches_with_bundle(row) @@ -245,13 +249,20 @@ class StockController(AccountsController): } if row.get("qty") or row.get("consumed_qty") or row.get("stock_qty"): - self.update_bundle_details(bundle_details, table_name, row) + self.update_bundle_details(bundle_details, table_name, row, parent_details=parent_details) self.create_serial_batch_bundle(bundle_details, row) if row.get("rejected_qty"): self.update_bundle_details(bundle_details, table_name, row, is_rejected=True) self.create_serial_batch_bundle(bundle_details, row) + def get_parent_details_for_packed_items(self): + parent_details = frappe._dict() + for row in self.get("items"): + parent_details[row.name] = row + + return parent_details + def make_bundle_for_sales_purchase_return(self, table_name=None): if not self.get("is_return"): return @@ -386,7 +397,7 @@ class StockController(AccountsController): return False - def update_bundle_details(self, bundle_details, table_name, row, is_rejected=False): + def update_bundle_details(self, bundle_details, table_name, row, is_rejected=False, parent_details=None): from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos # Since qty field is different for different doctypes @@ -428,6 +439,11 @@ class StockController(AccountsController): warehouse = row.get("target_warehouse") or row.get("warehouse") type_of_transaction = "Outward" + if table_name == "packed_items": + if not warehouse: + warehouse = parent_details[row.parent_detail_docname].warehouse + bundle_details["voucher_detail_no"] = parent_details[row.parent_detail_docname].name + bundle_details.update( { "qty": qty, diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py index d2211f7f5b0..42683d6a3b8 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py @@ -85,6 +85,9 @@ class SerialandBatchBundle(Document): # end: auto-generated types def validate(self): + if self.docstatus == 1 and self.voucher_detail_no: + self.validate_voucher_detail_no() + self.reset_serial_batch_bundle() self.set_batch_no() self.validate_serial_and_batch_no() @@ -108,6 +111,30 @@ class SerialandBatchBundle(Document): self.calculate_qty_and_amount() + def validate_voucher_detail_no(self): + if self.type_of_transaction not in ["Inward", "Outward"] or self.voucher_type in [ + "Installation Note", + "Job Card", + "Maintenance Schedule", + "Pick List", + ]: + return + + if self.voucher_type == "POS Invoice": + if not frappe.db.exists("POS Invoice Item", self.voucher_detail_no): + frappe.throw( + _("The serial and batch bundle {0} not linked to {1} {2}").format( + bold(self.name), self.voucher_type, bold(self.voucher_no) + ) + ) + + elif not frappe.db.exists("Stock Ledger Entry", {"voucher_detail_no": self.voucher_detail_no}): + frappe.throw( + _("The serial and batch bundle {0} not linked to {1} {2}").format( + bold(self.name), self.voucher_type, bold(self.voucher_no) + ) + ) + def allow_existing_serial_nos(self): if self.type_of_transaction == "Outward" or not self.has_serial_no: return diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py index fb1123a59ca..cefa5c62044 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/test_serial_and_batch_bundle.py @@ -755,6 +755,80 @@ class TestSerialandBatchBundle(FrappeTestCase): "Stock Settings", "auto_create_serial_and_batch_bundle_for_outward", original_value ) + def test_voucher_detail_no(self): + item_code = make_item( + "Test Voucher Detail No 1", + properties={ + "is_stock_item": 1, + "has_batch_no": 1, + "create_new_batch": 1, + "batch_number_series": "TST-VDN-.#####", + }, + ).name + + se = make_stock_entry( + item_code=item_code, + qty=10, + target="_Test Warehouse - _TC", + rate=500, + use_serial_batch_fields=True, + do_not_submit=True, + ) + + if not frappe.db.exists("Batch", "TST-ACSBBO-TACSB-00001"): + frappe.get_doc( + { + "doctype": "Batch", + "batch_id": "TST-ACSBBO-TACSB-00001", + "item": item_code, + "company": "_Test Company", + } + ).insert(ignore_permissions=True) + + bundle_doc = make_serial_batch_bundle( + { + "item_code": item_code, + "warehouse": "_Test Warehouse - _TC", + "voucher_type": "Stock Entry", + "posting_date": today(), + "posting_time": nowtime(), + "qty": 10, + "batches": frappe._dict({"TST-ACSBBO-TACSB-00001": 10}), + "type_of_transaction": "Inward", + "do_not_submit": True, + } + ) + + se.append( + "items", + { + "item_code": item_code, + "t_warehouse": "_Test Warehouse - _TC", + "stock_uom": "Nos", + "stock_qty": 10, + "conversion_factor": 1, + "uom": "Nos", + "basic_rate": 500, + "qty": 10, + "use_serial_batch_fields": 0, + "serial_and_batch_bundle": bundle_doc.name, + }, + ) + + se.save() + + bundle_doc = frappe.get_doc("Serial and Batch Bundle", bundle_doc.name) + self.assertEqual(bundle_doc.voucher_detail_no, se.items[1].name) + + se.remove(se.items[1]) + se.save() + self.assertTrue(len(se.items) == 1) + se.submit() + + bundle_doc.reload() + self.assertTrue(bundle_doc.docstatus == 0) + self.assertRaises(frappe.ValidationError, bundle_doc.submit) + def get_batch_from_bundle(bundle): from erpnext.stock.serial_batch_bundle import get_batch_nos -- GitLab From 25229d450b8413a813410179356dc06a2a07769a Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Fri, 31 Jan 2025 16:03:56 +0100 Subject: [PATCH 2/2] fix: revert Dokos specific test --- .../delivery_note/test_delivery_note.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note.py b/erpnext/stock/doctype/delivery_note/test_delivery_note.py index 00efeb43824..dc7f16470e5 100644 --- a/erpnext/stock/doctype/delivery_note/test_delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/test_delivery_note.py @@ -58,7 +58,7 @@ class TestDeliveryNote(FrappeTestCase): self.assertRaises(frappe.ValidationError, frappe.get_doc(si).insert) def test_delivery_note_no_gl_entry(self): - company = frappe.db.get_value("Warehouse", "_Test Warehouse - _TC", "company") + # company = frappe.db.get_value("Warehouse", "_Test Warehouse - _TC", "company") make_stock_entry(target="_Test Warehouse - _TC", qty=5, basic_rate=100) stock_queue = json.loads( @@ -82,7 +82,7 @@ class TestDeliveryNote(FrappeTestCase): self.assertFalse(get_gl_entries("Delivery Note", dn.name)) def test_delivery_note_gl_entry_packing_item(self): - company = frappe.db.get_value("Warehouse", "Stores - TCP1", "company") + # company = frappe.db.get_value("Warehouse", "Stores - TCP1", "company") make_stock_entry(item_code="_Test Item", target="Stores - TCP1", qty=10, basic_rate=100) make_stock_entry( @@ -129,7 +129,7 @@ class TestDeliveryNote(FrappeTestCase): stock_in_hand_account: [0.0, stock_value_diff], "Cost of Goods Sold - TCP1": [stock_value_diff, 0.0], } - for i, gle in enumerate(gl_entries): + for _, gle in enumerate(gl_entries): self.assertEqual([gle.debit, gle.credit], expected_values.get(gle.account)) # check stock in hand balance @@ -866,7 +866,7 @@ class TestDeliveryNote(FrappeTestCase): "Stock In Hand - TCP1": [0.0, stock_value_difference], target_warehouse: [stock_value_difference, 0.0], } - for i, gle in enumerate(gl_entries): + for _, gle in enumerate(gl_entries): self.assertEqual([gle.debit, gle.credit], expected_values.get(gle.account)) # tear down @@ -1074,7 +1074,7 @@ class TestDeliveryNote(FrappeTestCase): "Cost of Goods Sold - TCP1": {"cost_center": cost_center}, stock_in_hand_account: {"cost_center": cost_center}, } - for i, gle in enumerate(gl_entries): + for _, gle in enumerate(gl_entries): self.assertEqual(expected_values[gle.account]["cost_center"], gle.cost_center) def test_delivery_note_cost_center_with_balance_sheet_account(self): @@ -1103,7 +1103,7 @@ class TestDeliveryNote(FrappeTestCase): "Cost of Goods Sold - TCP1": {"cost_center": cost_center}, stock_in_hand_account: {"cost_center": cost_center}, } - for i, gle in enumerate(gl_entries): + for _, gle in enumerate(gl_entries): self.assertEqual(expected_values[gle.account]["cost_center"], gle.cost_center) def test_make_sales_invoice_from_dn_for_returned_qty(self): @@ -1188,11 +1188,8 @@ class TestDeliveryNote(FrappeTestCase): doc = frappe.get_doc("Repost Item Valuation", entry.name) doc.cancel() - # @dokos: Delivery notes cannot be deleted if they are linked to ledger entries - bundle_status = frappe.db.get_value( - "Serial and Batch Bundle", {"voucher_detail_no": packed_name}, "is_cancelled" - ) - self.assertEqual(bundle_status, 1) + bundle = frappe.db.get_value("Serial and Batch Bundle", {"voucher_detail_no": packed_name}, "name") + self.assertFalse(bundle) frappe.db.set_single_value("Stock Settings", "use_serial_batch_fields", 1) -- GitLab