mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-02 19:59:12 +00:00
Merge pull request #42826 from frappe/mergify/bp/version-15-hotfix/pr-42379
fix: Create Sales Order from Quotation for Prospect (backport #42379)
This commit is contained in:
@@ -27,6 +27,29 @@ class TestProspect(unittest.TestCase):
|
|||||||
address_doc.reload()
|
address_doc.reload()
|
||||||
self.assertEqual(address_doc.has_link("Prospect", prospect_doc.name), True)
|
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):
|
def make_prospect(**args):
|
||||||
args = frappe._dict(args)
|
args = frappe._dict(args)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
"gender",
|
"gender",
|
||||||
"lead_name",
|
"lead_name",
|
||||||
"opportunity_name",
|
"opportunity_name",
|
||||||
|
"prospect_name",
|
||||||
"account_manager",
|
"account_manager",
|
||||||
"image",
|
"image",
|
||||||
"defaults_tab",
|
"defaults_tab",
|
||||||
@@ -570,6 +571,14 @@
|
|||||||
{
|
{
|
||||||
"fieldname": "column_break_nwor",
|
"fieldname": "column_break_nwor",
|
||||||
"fieldtype": "Column Break"
|
"fieldtype": "Column Break"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldname": "prospect_name",
|
||||||
|
"fieldtype": "Link",
|
||||||
|
"label": "From Prospect",
|
||||||
|
"no_copy": 1,
|
||||||
|
"options": "Prospect",
|
||||||
|
"print_hide": 1
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"icon": "fa fa-user",
|
"icon": "fa fa-user",
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ class Customer(TransactionBase):
|
|||||||
payment_terms: DF.Link | None
|
payment_terms: DF.Link | None
|
||||||
portal_users: DF.Table[PortalUser]
|
portal_users: DF.Table[PortalUser]
|
||||||
primary_address: DF.Text | None
|
primary_address: DF.Text | None
|
||||||
|
prospect_name: DF.Link | None
|
||||||
represents_company: DF.Link | None
|
represents_company: DF.Link | None
|
||||||
sales_team: DF.Table[SalesTeam]
|
sales_team: DF.Table[SalesTeam]
|
||||||
salutation: DF.Link | None
|
salutation: DF.Link | None
|
||||||
|
|||||||
@@ -347,8 +347,8 @@ def make_sales_order(source_name: str, target_doc=None):
|
|||||||
return _make_sales_order(source_name, target_doc)
|
return _make_sales_order(source_name, target_doc)
|
||||||
|
|
||||||
|
|
||||||
def _make_sales_order(source_name, target_doc=None, customer_group=None, ignore_permissions=False):
|
def _make_sales_order(source_name, target_doc=None, ignore_permissions=False):
|
||||||
customer = _make_customer(source_name, ignore_permissions, customer_group)
|
customer = _make_customer(source_name, ignore_permissions)
|
||||||
ordered_items = frappe._dict(
|
ordered_items = frappe._dict(
|
||||||
frappe.db.get_all(
|
frappe.db.get_all(
|
||||||
"Sales Order Item",
|
"Sales Order Item",
|
||||||
@@ -502,51 +502,71 @@ def _make_sales_invoice(source_name, target_doc=None, ignore_permissions=False):
|
|||||||
return doclist
|
return doclist
|
||||||
|
|
||||||
|
|
||||||
def _make_customer(source_name, ignore_permissions=False, customer_group=None):
|
def _make_customer(source_name, ignore_permissions=False):
|
||||||
quotation = frappe.db.get_value(
|
quotation = frappe.db.get_value(
|
||||||
"Quotation", source_name, ["order_type", "party_name", "customer_name"], as_dict=1
|
"Quotation",
|
||||||
|
source_name,
|
||||||
|
["order_type", "quotation_to", "party_name", "customer_name"],
|
||||||
|
as_dict=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
if quotation and quotation.get("party_name"):
|
if quotation.quotation_to == "Customer":
|
||||||
if not frappe.db.exists("Customer", quotation.get("party_name")):
|
return frappe.get_doc("Customer", quotation.party_name)
|
||||||
lead_name = quotation.get("party_name")
|
|
||||||
customer_name = frappe.db.get_value(
|
|
||||||
"Customer", {"lead_name": lead_name}, ["name", "customer_name"], as_dict=True
|
|
||||||
)
|
|
||||||
if not customer_name:
|
|
||||||
from erpnext.crm.doctype.lead.lead import _make_customer
|
|
||||||
|
|
||||||
customer_doclist = _make_customer(lead_name, ignore_permissions=ignore_permissions)
|
# Check if a Customer already exists for the Lead or Prospect.
|
||||||
customer = frappe.get_doc(customer_doclist)
|
existing_customer = None
|
||||||
customer.flags.ignore_permissions = ignore_permissions
|
if quotation.quotation_to == "Lead":
|
||||||
customer.customer_group = customer_group
|
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})
|
||||||
|
|
||||||
try:
|
if existing_customer:
|
||||||
customer.insert()
|
return frappe.get_doc("Customer", existing_customer)
|
||||||
return customer
|
|
||||||
except frappe.NameError:
|
|
||||||
if frappe.defaults.get_global_default("cust_master_name") == "Customer Name":
|
|
||||||
customer.run_method("autoname")
|
|
||||||
customer.name += "-" + lead_name
|
|
||||||
customer.insert()
|
|
||||||
return customer
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
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]
|
|
||||||
|
|
||||||
frappe.local.message_log = []
|
# If no Customer exists, create a new Customer or Prospect.
|
||||||
lead_link = frappe.utils.get_link_to_form("Lead", lead_name)
|
if quotation.quotation_to == "Lead":
|
||||||
message = (
|
return create_customer_from_lead(quotation.party_name, ignore_permissions=ignore_permissions)
|
||||||
_("Could not auto create Customer due to the following missing mandatory field(s):")
|
elif quotation.quotation_to == "Prospect":
|
||||||
+ "<br>"
|
return create_customer_from_prospect(quotation.party_name, ignore_permissions=ignore_permissions)
|
||||||
)
|
|
||||||
message += "<br><ul><li>" + "</li><li>".join(mandatory_fields) + "</li></ul>"
|
|
||||||
message += _("Please create Customer from Lead {0}.").format(lead_link)
|
|
||||||
|
|
||||||
frappe.throw(message, title=_("Mandatory Missing"))
|
return None
|
||||||
else:
|
|
||||||
return customer_name
|
|
||||||
else:
|
def create_customer_from_lead(lead_name, ignore_permissions=False):
|
||||||
return frappe.get_doc("Customer", quotation.get("party_name"))
|
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"))
|
||||||
|
|||||||
Reference in New Issue
Block a user