Skip to content

Commit

Permalink
fix: Create Sales Order from Quotation for Prospect
Browse files Browse the repository at this point in the history
(cherry picked from commit 2f63fae)

# Conflicts:
#	erpnext/selling/doctype/quotation/quotation.py
  • Loading branch information
Nihantra-Patel authored and mergify[bot] committed Aug 20, 2024
1 parent a144059 commit f547bef
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
20 changes: 20 additions & 0 deletions erpnext/crm/doctype/prospect/test_prospect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions erpnext/selling/doctype/customer/customer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"gender",
"lead_name",
"opportunity_name",
"prospect_name",
"account_manager",
"image",
"defaults_tab",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions erpnext/selling/doctype/customer/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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
Expand Down
61 changes: 61 additions & 0 deletions erpnext/selling/doctype/quotation/quotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def _make_customer(source_name, ignore_permissions=False, customer_group=None):
if not customer_name:
from erpnext.crm.doctype.lead.lead import _make_customer

<<<<<<< HEAD
customer_doclist = _make_customer(lead_name, ignore_permissions=ignore_permissions)
customer = frappe.get_doc(customer_doclist)
customer.flags.ignore_permissions = ignore_permissions
Expand All @@ -535,6 +536,25 @@ def _make_customer(source_name, ignore_permissions=False, customer_group=None):
except frappe.MandatoryError as e:
mandatory_fields = e.args[0].split(":")[1].split(",")
mandatory_fields = [customer.meta.get_label(field.strip()) for field in mandatory_fields]
=======
# 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 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
>>>>>>> 2f63fae31d (fix: Create Sales Order from Quotation for Prospect)

frappe.local.message_log = []
lead_link = frappe.utils.get_link_to_form("Lead", lead_name)
Expand All @@ -545,8 +565,49 @@ def _make_customer(source_name, ignore_permissions=False, customer_group=None):
message += "<br><ul><li>" + "</li><li>".join(mandatory_fields) + "</li></ul>"
message += _("Please create Customer from Lead {0}.").format(lead_link)

<<<<<<< HEAD
frappe.throw(message, title=_("Mandatory Missing"))
else:
return customer_name
else:
return frappe.get_doc("Customer", quotation.get("party_name"))
=======
def create_customer_from_lead(lead_name, ignore_permissions=False):
from erpnext.crm.doctype.lead.lead import _make_customer

customer = _make_customer(lead_name, ignore_permissions=ignore_permissions)
customer.flags.ignore_permissions = ignore_permissions

try:
customer.insert()
return customer
except frappe.MandatoryError as e:
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

mandatory_fields = e.args[0].split(":")[1].split(",")
mandatory_fields = [customer.meta.get_label(field.strip()) for field in mandatory_fields]

frappe.local.message_log = []
message = _("Could not auto create Customer due to the following missing mandatory field(s):") + "<br>"
message += "<br><ul><li>" + "</li><li>".join(mandatory_fields) + "</li></ul>"
message += _("Please create Customer from Lead {0}.").format(get_link_to_form("Lead", lead_name))

frappe.throw(message, title=_("Mandatory Missing"))
>>>>>>> 2f63fae31d (fix: Create Sales Order from Quotation for Prospect)

0 comments on commit f547bef

Please sign in to comment.