From 2f63fae31da75425f01f5c7c8d57d72514b3eb9f Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Thu, 18 Jul 2024 12:14:53 +0530 Subject: [PATCH 1/3] fix: Create Sales Order from Quotation for Prospect --- erpnext/crm/doctype/prospect/test_prospect.py | 20 +++++++++++ .../selling/doctype/customer/customer.json | 9 +++++ erpnext/selling/doctype/customer/customer.py | 1 + .../selling/doctype/quotation/quotation.py | 36 +++++++++++++++---- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/erpnext/crm/doctype/prospect/test_prospect.py b/erpnext/crm/doctype/prospect/test_prospect.py index c3930ee6c93d..286b91e20860 100644 --- a/erpnext/crm/doctype/prospect/test_prospect.py +++ b/erpnext/crm/doctype/prospect/test_prospect.py @@ -27,6 +27,26 @@ def test_add_lead_to_prospect_and_address_linking(self): address_doc.reload() self.assertEqual(address_doc.has_link("Prospect", prospect_doc.name), True) + def test_make_customer_from_prospect(self): + from erpnext.crm.doctype.prospect.prospect import make_customer as make_customer_from_prospect + + frappe.delete_doc_if_exists("Customer", "_Test Prospect") + + prospect = frappe.get_doc({ + "doctype": "Prospect", + "company_name": "_Test Prospect", + "customer_group": "_Test Customer Group", + }) + prospect.insert() + + customer = make_customer_from_prospect("_Test Prospect") + + self.assertEqual(customer.doctype, "Customer") + self.assertEqual(customer.company_name, "_Test Prospect") + self.assertEqual(customer.customer_group, "_Test Customer Group") + + customer.company = "_Test Company" + customer.insert() def make_prospect(**args): args = frappe._dict(args) diff --git a/erpnext/selling/doctype/customer/customer.json b/erpnext/selling/doctype/customer/customer.json index 4ce8d942154f..961a4b3f8b57 100644 --- a/erpnext/selling/doctype/customer/customer.json +++ b/erpnext/selling/doctype/customer/customer.json @@ -21,6 +21,7 @@ "gender", "lead_name", "opportunity_name", + "prospect_name", "account_manager", "image", "defaults_tab", @@ -570,6 +571,14 @@ { "fieldname": "column_break_nwor", "fieldtype": "Column Break" + }, + { + "fieldname": "prospect_name", + "fieldtype": "Link", + "label": "From Prospect", + "no_copy": 1, + "options": "Prospect", + "print_hide": 1 } ], "icon": "fa fa-user", diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 0610c11fa9f7..134ae753421a 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -76,6 +76,7 @@ class Customer(TransactionBase): payment_terms: DF.Link | None portal_users: DF.Table[PortalUser] primary_address: DF.Text | None + prospect_name: DF.Link | None represents_company: DF.Link | None sales_team: DF.Table[SalesTeam] salutation: DF.Link | None diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py index 4381531fc4ba..1d7d3fcfbe00 100644 --- a/erpnext/selling/doctype/quotation/quotation.py +++ b/erpnext/selling/doctype/quotation/quotation.py @@ -513,14 +513,23 @@ def _make_customer(source_name, ignore_permissions=False): if quotation.quotation_to == "Customer": return frappe.get_doc("Customer", quotation.party_name) - # If the Quotation is not to a Customer, it must be to a Lead. - # Check if a Customer already exists for the Lead. - existing_customer_for_lead = frappe.db.get_value("Customer", {"lead_name": quotation.party_name}) - if existing_customer_for_lead: - return frappe.get_doc("Customer", existing_customer_for_lead) + # Check if a Customer already exists for the Lead or Prospect. + existing_customer = None + if quotation.quotation_to == "Lead": + existing_customer = frappe.db.get_value("Customer", {"lead_name": quotation.party_name}) + elif quotation.quotation_to == "Prospect": + existing_customer = frappe.db.get_value("Customer", {"prospect_name": quotation.party_name}) - # If no Customer exists for the Lead, create a new Customer. - return create_customer_from_lead(quotation.party_name, ignore_permissions=ignore_permissions) + if existing_customer: + return frappe.get_doc("Customer", existing_customer) + + # If no Customer exists, create a new Customer or Prospect. + if quotation.quotation_to == "Lead": + return create_customer_from_lead(quotation.party_name, ignore_permissions=ignore_permissions) + elif quotation.quotation_to == "Prospect": + return create_customer_from_prospect(quotation.party_name, ignore_permissions=ignore_permissions) + + return None def create_customer_from_lead(lead_name, ignore_permissions=False): @@ -536,6 +545,19 @@ def create_customer_from_lead(lead_name, ignore_permissions=False): handle_mandatory_error(e, customer, lead_name) +def create_customer_from_prospect(prospect_name, ignore_permissions=False): + from erpnext.crm.doctype.prospect.prospect import make_customer as make_customer_from_prospect + + customer = make_customer_from_prospect(prospect_name) + customer.flags.ignore_permissions = ignore_permissions + + try: + customer.insert() + return customer + except frappe.MandatoryError as e: + handle_mandatory_error(e, customer, prospect_name) + + def handle_mandatory_error(e, customer, lead_name): from frappe.utils import get_link_to_form From 29d50b770e3d8de745a75cecbf18c0d745c4de6f Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Thu, 18 Jul 2024 12:24:52 +0530 Subject: [PATCH 2/3] fix: update the testcase format --- erpnext/crm/doctype/prospect/test_prospect.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/erpnext/crm/doctype/prospect/test_prospect.py b/erpnext/crm/doctype/prospect/test_prospect.py index 286b91e20860..e89bade19a1b 100644 --- a/erpnext/crm/doctype/prospect/test_prospect.py +++ b/erpnext/crm/doctype/prospect/test_prospect.py @@ -32,11 +32,13 @@ def test_make_customer_from_prospect(self): frappe.delete_doc_if_exists("Customer", "_Test Prospect") - prospect = frappe.get_doc({ - "doctype": "Prospect", - "company_name": "_Test Prospect", - "customer_group": "_Test Customer Group", - }) + prospect = frappe.get_doc( + { + "doctype": "Prospect", + "company_name": "_Test Prospect", + "customer_group": "_Test Customer Group", + } + ) prospect.insert() customer = make_customer_from_prospect("_Test Prospect") From ee44022249c39bc17d9cbf29c854c64c41f9a911 Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Thu, 18 Jul 2024 12:26:23 +0530 Subject: [PATCH 3/3] fix: update the testcase format --- erpnext/crm/doctype/prospect/test_prospect.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/crm/doctype/prospect/test_prospect.py b/erpnext/crm/doctype/prospect/test_prospect.py index e89bade19a1b..47e38515cac5 100644 --- a/erpnext/crm/doctype/prospect/test_prospect.py +++ b/erpnext/crm/doctype/prospect/test_prospect.py @@ -50,6 +50,7 @@ def test_make_customer_from_prospect(self): customer.company = "_Test Company" customer.insert() + def make_prospect(**args): args = frappe._dict(args)