[fix] connect customer to Quotation, Opportunity if created from lead, fixes #6051
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 40441c8..8ab34de 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -328,3 +328,4 @@
erpnext.patches.v7_1.move_sales_invoice_from_parent_to_child_timesheet
execute:frappe.db.sql("update `tabTimesheet` ts, `tabEmployee` emp set ts.employee_name = emp.employee_name where emp.name = ts.employee and ts.employee_name is null and ts.employee is not null")
erpnext.patches.v7_1.update_lead_source
+erpnext.patches.v7_1.fix_link_for_customer_from_lead
\ No newline at end of file
diff --git a/erpnext/patches/v7_1/fix_link_for_customer_from_lead.py b/erpnext/patches/v7_1/fix_link_for_customer_from_lead.py
new file mode 100644
index 0000000..cbb3ea4
--- /dev/null
+++ b/erpnext/patches/v7_1/fix_link_for_customer_from_lead.py
@@ -0,0 +1,6 @@
+import frappe
+
+def execute():
+ for c in frappe.db.sql('select name from tabCustomer where ifnull(lead_name,"")!=""'):
+ customer = frappe.get_doc('Customer', c[0])
+ customer.update_lead_status()
\ No newline at end of file
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index ee3f6e6..ad43a76 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -40,14 +40,24 @@
return self.customer_name
+ def after_insert(self):
+ '''If customer created from Lead, update customer id in quotations, opportunities'''
+ self.update_lead_status()
+
def validate(self):
self.flags.is_new_doc = self.is_new()
validate_party_accounts(self)
self.status = get_party_status(self)
def update_lead_status(self):
+ '''If Customer created from Lead, update lead status to "Converted"
+ update Customer link in Quotation, Opportunity'''
if self.lead_name:
- frappe.db.sql("update `tabLead` set status='Converted' where name = %s", self.lead_name)
+ frappe.db.set_value('Lead', self.lead_name, 'status', 'Converted', update_modified=False)
+
+ for doctype in ('Opportunity', 'Quotation'):
+ for d in frappe.get_all(doctype, {'lead': self.lead_name}):
+ frappe.db.set_value(doctype, d.name, 'customer', self.name, update_modified=False)
def update_address(self):
frappe.db.sql("""update `tabAddress` set customer_name=%s, modified=NOW()
@@ -81,7 +91,6 @@
def on_update(self):
self.validate_name_with_customer_group()
- self.update_lead_status()
self.update_address()
self.update_contact()