From 0dde4d4c69f673565696711834f85853dd8673b8 Mon Sep 17 00:00:00 2001 From: Raffael Meyer <14891507+barredterra@users.noreply.github.com> Date: Wed, 7 Jun 2023 18:32:38 +0200 Subject: [PATCH] refactor: use delete_contact_and_address (#34497) Co-authored-by: Deepesh Garg --- erpnext/crm/doctype/lead/lead.py | 31 +++++------------------- erpnext/crm/doctype/prospect/prospect.py | 28 ++++----------------- 2 files changed, 11 insertions(+), 48 deletions(-) diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py index 2a588d8d13..a98886c648 100644 --- a/erpnext/crm/doctype/lead/lead.py +++ b/erpnext/crm/doctype/lead/lead.py @@ -3,7 +3,10 @@ import frappe from frappe import _ -from frappe.contacts.address_and_contact import load_address_and_contact +from frappe.contacts.address_and_contact import ( + delete_contact_and_address, + load_address_and_contact, +) from frappe.email.inbox import link_communication_to_document from frappe.model.mapper import get_mapped_doc from frappe.utils import comma_and, get_link_to_form, has_gravatar, validate_email_address @@ -40,9 +43,8 @@ class Lead(SellingController, CRMNote): self.update_prospect() def on_trash(self): - frappe.db.sql("""update `tabIssue` set lead='' where lead=%s""", self.name) - - self.unlink_dynamic_links() + frappe.db.set_value("Issue", {"lead": self.name}, "lead", None) + delete_contact_and_address(self.doctype, self.name) self.remove_link_from_prospect() def set_full_name(self): @@ -119,27 +121,6 @@ class Lead(SellingController, CRMNote): ) lead_row.db_update() - def unlink_dynamic_links(self): - links = frappe.get_all( - "Dynamic Link", - filters={"link_doctype": self.doctype, "link_name": self.name}, - fields=["parent", "parenttype"], - ) - - for link in links: - linked_doc = frappe.get_doc(link["parenttype"], link["parent"]) - - if len(linked_doc.get("links")) == 1: - linked_doc.delete(ignore_permissions=True) - else: - to_remove = None - for d in linked_doc.get("links"): - if d.link_doctype == self.doctype and d.link_name == self.name: - to_remove = d - if to_remove: - linked_doc.remove(to_remove) - linked_doc.save(ignore_permissions=True) - def remove_link_from_prospect(self): prospects = self.get_linked_prospects() diff --git a/erpnext/crm/doctype/prospect/prospect.py b/erpnext/crm/doctype/prospect/prospect.py index fbb115883f..8b66a83f2a 100644 --- a/erpnext/crm/doctype/prospect/prospect.py +++ b/erpnext/crm/doctype/prospect/prospect.py @@ -2,7 +2,10 @@ # For license information, please see license.txt import frappe -from frappe.contacts.address_and_contact import load_address_and_contact +from frappe.contacts.address_and_contact import ( + delete_contact_and_address, + load_address_and_contact, +) from frappe.model.mapper import get_mapped_doc from erpnext.crm.utils import CRMNote, copy_comments, link_communications, link_open_events @@ -16,7 +19,7 @@ class Prospect(CRMNote): self.link_with_lead_contact_and_address() def on_trash(self): - self.unlink_dynamic_links() + delete_contact_and_address(self.doctype, self.name) def after_insert(self): carry_forward_communication_and_comments = frappe.db.get_single_value( @@ -54,27 +57,6 @@ class Prospect(CRMNote): linked_doc.append("links", {"link_doctype": self.doctype, "link_name": self.name}) linked_doc.save(ignore_permissions=True) - def unlink_dynamic_links(self): - links = frappe.get_all( - "Dynamic Link", - filters={"link_doctype": self.doctype, "link_name": self.name}, - fields=["parent", "parenttype"], - ) - - for link in links: - linked_doc = frappe.get_doc(link["parenttype"], link["parent"]) - - if len(linked_doc.get("links")) == 1: - linked_doc.delete(ignore_permissions=True) - else: - to_remove = None - for d in linked_doc.get("links"): - if d.link_doctype == self.doctype and d.link_name == self.name: - to_remove = d - if to_remove: - linked_doc.remove(to_remove) - linked_doc.save(ignore_permissions=True) - @frappe.whitelist() def make_customer(source_name, target_doc=None):