diff --git a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py index 160a887d851..d0e1cac2b23 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -242,7 +242,7 @@ class RepostItemValuation(Document): def clear_attachment(self): if attachments := get_attachments(self.doctype, self.name): attachment = attachments[0] - frappe.delete_doc("File", attachment.name, ignore_permissions=True) + frappe.delete_doc("File", attachment.name, ignore_permissions=True, force=True) if self.reposting_data_file: self.db_set("reposting_data_file", None) diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py index a27c8d49ea2..a941b64856a 100644 --- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py @@ -515,6 +515,34 @@ class TestRepostItemValuation(ERPNextTestSuite, StockTestMixin): ) ) + def test_clear_attachment_skips_referenced_data_file(self): + riv = frappe.get_doc( + { + "doctype": "Repost Item Valuation", + "based_on": "Item and Warehouse", + "company": "_Test Company", + "item_code": "_Test Item", + "warehouse": "_Test Warehouse - _TC", + "posting_date": today(), + } + ).insert(ignore_permissions=True) + + attached = frappe.get_doc( + { + "doctype": "File", + "file_name": "repost_data.json.gz", + "content": "test", + "attached_to_doctype": riv.doctype, + "attached_to_name": riv.name, + } + ).insert(ignore_permissions=True) + riv.db_set("reposting_data_file", attached.file_url) + + riv.clear_attachment() + + self.assertFalse(frappe.db.exists("File", attached.name)) + self.assertIsNone(frappe.db.get_value("Repost Item Valuation", riv.name, "reposting_data_file")) + @ERPNextTestSuite.change_settings( "Stock Reposting Settings", {"item_based_reposting": 1, "enable_parallel_reposting": 1, "no_of_parallel_reposting": 2},