refactor: use delete_contact_and_address (#34497)

Co-authored-by: Deepesh Garg <deepeshgarg6@gmail.com>
This commit is contained in:
Raffael Meyer 2023-06-07 18:32:38 +02:00 committed by GitHub
parent 3b409af9a0
commit 0dde4d4c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 48 deletions

View File

@ -3,7 +3,10 @@
import frappe import frappe
from frappe import _ 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.email.inbox import link_communication_to_document
from frappe.model.mapper import get_mapped_doc from frappe.model.mapper import get_mapped_doc
from frappe.utils import comma_and, get_link_to_form, has_gravatar, validate_email_address 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() self.update_prospect()
def on_trash(self): def on_trash(self):
frappe.db.sql("""update `tabIssue` set lead='' where lead=%s""", self.name) frappe.db.set_value("Issue", {"lead": self.name}, "lead", None)
delete_contact_and_address(self.doctype, self.name)
self.unlink_dynamic_links()
self.remove_link_from_prospect() self.remove_link_from_prospect()
def set_full_name(self): def set_full_name(self):
@ -119,27 +121,6 @@ class Lead(SellingController, CRMNote):
) )
lead_row.db_update() 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): def remove_link_from_prospect(self):
prospects = self.get_linked_prospects() prospects = self.get_linked_prospects()

View File

@ -2,7 +2,10 @@
# For license information, please see license.txt # For license information, please see license.txt
import frappe 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 frappe.model.mapper import get_mapped_doc
from erpnext.crm.utils import CRMNote, copy_comments, link_communications, link_open_events 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() self.link_with_lead_contact_and_address()
def on_trash(self): def on_trash(self):
self.unlink_dynamic_links() delete_contact_and_address(self.doctype, self.name)
def after_insert(self): def after_insert(self):
carry_forward_communication_and_comments = frappe.db.get_single_value( 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.append("links", {"link_doctype": self.doctype, "link_name": self.name})
linked_doc.save(ignore_permissions=True) 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() @frappe.whitelist()
def make_customer(source_name, target_doc=None): def make_customer(source_name, target_doc=None):