From b059c6f630f175bc2150d66ecb4b50898cc5a31a Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 28 Jun 2019 12:36:27 +0530 Subject: [PATCH] fix: Fixes related to customer/lead merging (#18075) --- erpnext/controllers/selling_controller.py | 21 +++++++++++++++---- erpnext/controllers/tests/test_mapper.py | 2 +- .../crm/doctype/opportunity/opportunity.py | 4 ++-- erpnext/demo/user/sales.py | 2 +- .../v11_1/set_missing_title_for_quotation.py | 1 + .../setup_wizard/operations/sample_data.py | 2 +- erpnext/templates/utils.py | 6 +++--- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index a9883017cfa..f16d40c4ea8 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -55,14 +55,28 @@ class SellingController(StockController): self.set_price_list_and_item_details(for_validate=for_validate) def set_missing_lead_customer_details(self): + customer, lead = None, None if getattr(self, "customer", None): + customer = self.customer + elif self.doctype == "Opportunity" and self.party_name: + if self.opportunity_from == "Customer": + customer = self.party_name + else: + lead = self.party_name + elif self.doctype == "Quotation" and self.party_name: + if self.quotation_to == "Customer": + customer = self.party_name + else: + lead = self.party_name + + if customer: from erpnext.accounts.party import _get_party_details fetch_payment_terms_template = False if (self.get("__islocal") or self.company != frappe.db.get_value(self.doctype, self.name, 'company')): fetch_payment_terms_template = True - party_details = _get_party_details(self.customer, + party_details = _get_party_details(customer, ignore_permissions=self.flags.ignore_permissions, doctype=self.doctype, company=self.company, fetch_payment_terms_template=fetch_payment_terms_template, @@ -71,10 +85,9 @@ class SellingController(StockController): party_details.pop("sales_team") self.update_if_missing(party_details) - elif getattr(self, "lead", None): + elif lead: from erpnext.crm.doctype.lead.lead import get_lead_details - self.update_if_missing(get_lead_details( - self.lead, + self.update_if_missing(get_lead_details(lead, posting_date=self.get('transaction_date') or self.get('posting_date'), company=self.company)) diff --git a/erpnext/controllers/tests/test_mapper.py b/erpnext/controllers/tests/test_mapper.py index 14738c5ff2b..d02308d8f21 100644 --- a/erpnext/controllers/tests/test_mapper.py +++ b/erpnext/controllers/tests/test_mapper.py @@ -43,7 +43,7 @@ class TestMapper(unittest.TestCase): qtn = frappe.get_doc({ "doctype": "Quotation", "quotation_to": "Customer", - "customer": customer, + "party_name": customer, "order_type": "Sales", "transaction_date" : nowdate(), "valid_till" : add_months(nowdate(), 1) diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index dad249213c7..973b3412b8b 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -320,11 +320,11 @@ def make_opportunity_from_communication(communication, ignore_communication_link if not lead: lead = make_lead_from_communication(communication, ignore_communication_links=True) - enquiry_from = "Lead" + opportunity_from = "Lead" opportunity = frappe.get_doc({ "doctype": "Opportunity", - "enquiry_from": enquiry_from, + "opportunity_from": opportunity_from, "lead": lead }).insert(ignore_permissions=True) diff --git a/erpnext/demo/user/sales.py b/erpnext/demo/user/sales.py index 3809c1f0924..d5b0133f21b 100644 --- a/erpnext/demo/user/sales.py +++ b/erpnext/demo/user/sales.py @@ -99,7 +99,7 @@ def make_quotation(domain): "creation": frappe.flags.current_date, "doctype": "Quotation", "quotation_to": "Customer", - "customer": customer, + "party_name": customer, "currency": party_account_currency or company_currency, "conversion_rate": exchange_rate, "order_type": "Sales", diff --git a/erpnext/patches/v11_1/set_missing_title_for_quotation.py b/erpnext/patches/v11_1/set_missing_title_for_quotation.py index e86b8320c31..e2ef3433d3e 100644 --- a/erpnext/patches/v11_1/set_missing_title_for_quotation.py +++ b/erpnext/patches/v11_1/set_missing_title_for_quotation.py @@ -1,6 +1,7 @@ import frappe def execute(): + frappe.reload_doctype("Quotation") # update customer_name from Customer document if quotation_to is set to Customer frappe.db.sql(''' update tabQuotation, tabCustomer diff --git a/erpnext/setup/setup_wizard/operations/sample_data.py b/erpnext/setup/setup_wizard/operations/sample_data.py index e21c9bd1089..64c669363d3 100644 --- a/erpnext/setup/setup_wizard/operations/sample_data.py +++ b/erpnext/setup/setup_wizard/operations/sample_data.py @@ -34,7 +34,7 @@ def make_opportunity(items, customer): b = frappe.get_doc({ "doctype": "Opportunity", "opportunity_from": "Customer", - "customer": customer, + "party_name": customer, "opportunity_type": _("Sales"), "with_items": 1 }) diff --git a/erpnext/templates/utils.py b/erpnext/templates/utils.py index 97e2a7f7235..743657de322 100644 --- a/erpnext/templates/utils.py +++ b/erpnext/templates/utils.py @@ -36,11 +36,11 @@ def send_message(subject="Website Query", message="", sender="", status="Open"): )) if customer: - opportunity.customer = customer[0][0] + opportunity.party_name = customer[0][0] elif lead: - opportunity.lead = lead + opportunity.party_name = lead else: - opportunity.lead = new_lead.name + opportunity.party_name = new_lead.name opportunity.insert(ignore_permissions=True)