Merge pull request #38077 from anandbaburajan/trans_delete_att_comm
chore: delete comments and unlink attachments on company transactions deletion
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index ece08d8..a470b47 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -365,7 +365,7 @@
{
"item_code": d.item_code,
"warehouse": d.get("from_warehouse"),
- "posting_date": self.get("posting_date") or self.get("transation_date"),
+ "posting_date": self.get("posting_date") or self.get("transaction_date"),
"posting_time": posting_time,
"qty": -1 * flt(d.get("stock_qty")),
"serial_and_batch_bundle": d.get("serial_and_batch_bundle"),
diff --git a/erpnext/setup/doctype/transaction_deletion_record/transaction_deletion_record.py b/erpnext/setup/doctype/transaction_deletion_record/transaction_deletion_record.py
index 481a3a5..d266285 100644
--- a/erpnext/setup/doctype/transaction_deletion_record/transaction_deletion_record.py
+++ b/erpnext/setup/doctype/transaction_deletion_record/transaction_deletion_record.py
@@ -108,7 +108,16 @@
if no_of_docs > 0:
self.delete_version_log(docfield["parent"], docfield["fieldname"])
- self.delete_communications(docfield["parent"], docfield["fieldname"])
+
+ reference_docs = frappe.get_all(
+ docfield["parent"], filters={docfield["fieldname"]: self.company}
+ )
+ reference_doc_names = [r.name for r in reference_docs]
+
+ self.delete_communications(docfield["parent"], reference_doc_names)
+ self.delete_comments(docfield["parent"], reference_doc_names)
+ self.unlink_attachments(docfield["parent"], reference_doc_names)
+
self.populate_doctypes_table(tables, docfield["parent"], no_of_docs)
self.delete_child_tables(docfield["parent"], docfield["fieldname"])
@@ -197,19 +206,49 @@
(versions.ref_doctype == doctype) & (versions.docname.isin(batch))
).run()
- def delete_communications(self, doctype, company_fieldname):
- reference_docs = frappe.get_all(doctype, filters={company_fieldname: self.company})
- reference_doc_names = [r.name for r in reference_docs]
-
+ def delete_communications(self, doctype, reference_doc_names):
communications = frappe.get_all(
"Communication",
filters={"reference_doctype": doctype, "reference_name": ["in", reference_doc_names]},
)
communication_names = [c.name for c in communications]
+ if not communication_names:
+ return
+
for batch in create_batch(communication_names, self.batch_size):
frappe.delete_doc("Communication", batch, ignore_permissions=True)
+ def delete_comments(self, doctype, reference_doc_names):
+ comments = frappe.get_all(
+ "Comment",
+ filters={"reference_doctype": doctype, "reference_name": ["in", reference_doc_names]},
+ )
+ comment_names = [c.name for c in comments]
+
+ if not comment_names:
+ return
+
+ for batch in create_batch(comment_names, self.batch_size):
+ frappe.delete_doc("Comment", batch, ignore_permissions=True)
+
+ def unlink_attachments(self, doctype, reference_doc_names):
+ files = frappe.get_all(
+ "File",
+ filters={"attached_to_doctype": doctype, "attached_to_name": ["in", reference_doc_names]},
+ )
+ file_names = [c.name for c in files]
+
+ if not file_names:
+ return
+
+ file = qb.DocType("File")
+
+ for batch in create_batch(file_names, self.batch_size):
+ qb.update(file).set(file.attached_to_doctype, None).set(file.attached_to_name, None).where(
+ file.name.isin(batch)
+ ).run()
+
@frappe.whitelist()
def get_doctypes_to_be_ignored():