From e35dbca01bfc4c2949128b432e60445506382cad Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 19 Sep 2016 15:03:57 +0530 Subject: [PATCH] [fix] connect customer to Quotation, Opportunity if created from lead, fixes #6051 --- erpnext/patches.txt | 1 + .../patches/v7_1/fix_link_for_customer_from_lead.py | 6 ++++++ erpnext/selling/doctype/customer/customer.py | 13 +++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 erpnext/patches/v7_1/fix_link_for_customer_from_lead.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 40441c8e22..8ab34dee97 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -328,3 +328,4 @@ erpnext.patches.v7_0.update_conversion_factor_in_supplier_quotation_item 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 0000000000..cbb3ea4092 --- /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 ee3f6e6503..ad43a76b15 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -40,14 +40,24 @@ class Customer(TransactionBase): 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 @@ class Customer(TransactionBase): def on_update(self): self.validate_name_with_customer_group() - self.update_lead_status() self.update_address() self.update_contact()