From 6e5484ea031027de4e6a897b02bd0f2e7b8adda9 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Tue, 2 Jan 2024 12:54:18 +0530 Subject: [PATCH] fix: on cancellation of document cancel the serial and batch bundle (#39076) --- .../doctype/work_order/test_work_order.py | 2 -- .../serial_and_batch_bundle.py | 16 +++++++++++- .../test_serial_and_batch_bundle.py | 25 +++++++++++++++++-- erpnext/stock/serial_batch_bundle.py | 6 +++++ 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/erpnext/manufacturing/doctype/work_order/test_work_order.py b/erpnext/manufacturing/doctype/work_order/test_work_order.py index 07c253b232..aa5db57fa8 100644 --- a/erpnext/manufacturing/doctype/work_order/test_work_order.py +++ b/erpnext/manufacturing/doctype/work_order/test_work_order.py @@ -921,11 +921,9 @@ class TestWorkOrder(FrappeTestCase): "Test RM Item 2 for Scrap Item Test", ] - from_time = add_days(now(), -1) job_cards = frappe.get_all( "Job Card Time Log", fields=["distinct parent as name", "docstatus"], - filters={"from_time": (">", from_time)}, order_by="creation asc", ) 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 774e5c657d..218406f56f 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,7 @@ class SerialandBatchBundle(Document): # end: auto-generated types def validate(self): + self.reset_serial_batch_bundle() self.set_batch_no() self.validate_serial_and_batch_no() self.validate_duplicate_serial_and_batch_no() @@ -100,6 +101,15 @@ class SerialandBatchBundle(Document): self.set_incoming_rate() self.calculate_qty_and_amount() + def reset_serial_batch_bundle(self): + if self.is_new() and self.amended_from: + for field in ["is_cancelled", "is_rejected"]: + if self.get(field): + self.set(field, 0) + + if self.voucher_detail_no: + self.voucher_detail_no = None + def set_batch_no(self): if self.has_serial_no and self.has_batch_no: serial_nos = [d.serial_no for d in self.entries if d.serial_no] @@ -914,7 +924,11 @@ def upload_csv_file(item_code, file_path): def get_serial_batch_from_csv(item_code, file_path): - file_path = frappe.get_site_path() + file_path + if "private" in file_path: + file_path = frappe.get_site_path() + file_path + else: + file_path = frappe.get_site_path() + "/public" + file_path + serial_nos = [] batch_nos = [] 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 478cfa4d1c..19757479a5 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 @@ -427,11 +427,12 @@ class TestSerialandBatchBundle(FrappeTestCase): from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt item = make_item( + "Test Serial and Batch Bundle Company Item", properties={ "has_serial_no": 1, "serial_no_series": "TT-SER-VAL-.#####", - } - ) + }, + ).name pr = make_purchase_receipt( item_code=item, @@ -460,6 +461,26 @@ class TestSerialandBatchBundle(FrappeTestCase): sn_doc = add_serial_batch_ledgers(entries, item_row, pr, "_Test Warehouse - _TC") self.assertEqual(sn_doc.company, "_Test Company") + def test_auto_cancel_serial_and_batch(self): + item_code = make_item( + properties={"has_serial_no": 1, "serial_no_series": "ATC-TT-SER-VAL-.#####"} + ).name + + se = make_stock_entry( + item_code=item_code, + target="_Test Warehouse - _TC", + qty=5, + rate=500, + ) + + bundle = se.items[0].serial_and_batch_bundle + docstatus = frappe.db.get_value("Serial and Batch Bundle", bundle, "docstatus") + self.assertEqual(docstatus, 1) + + se.cancel() + docstatus = frappe.db.get_value("Serial and Batch Bundle", bundle, "docstatus") + self.assertEqual(docstatus, 2) + def get_batch_from_bundle(bundle): from erpnext.stock.serial_batch_bundle import get_batch_nos diff --git a/erpnext/stock/serial_batch_bundle.py b/erpnext/stock/serial_batch_bundle.py index a1874b84dc..39df2279cd 100644 --- a/erpnext/stock/serial_batch_bundle.py +++ b/erpnext/stock/serial_batch_bundle.py @@ -242,6 +242,12 @@ class SerialBatchBundle: if self.item_details.has_batch_no == 1: self.update_batch_qty() + if self.sle.is_cancelled and self.sle.serial_and_batch_bundle: + self.cancel_serial_and_batch_bundle() + + def cancel_serial_and_batch_bundle(self): + frappe.get_cached_doc("Serial and Batch Bundle", self.sle.serial_and_batch_bundle).cancel() + def submit_serial_and_batch_bundle(self): doc = frappe.get_doc("Serial and Batch Bundle", self.sle.serial_and_batch_bundle) self.validate_actual_qty(doc)