Skip to content

Commit

Permalink
Merge pull request #38061 from frappe/mergify/bp/version-15-hotfix/pr…
Browse files Browse the repository at this point in the history
…-38055

fix(customer): contact creation for companies (backport #38055, #38060)
  • Loading branch information
barredterra authored Nov 12, 2023
2 parents cff56d4 + 6f2b34e commit 6ab2f83
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
59 changes: 43 additions & 16 deletions erpnext/selling/doctype/customer/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from frappe.model.naming import set_name_by_naming_series, set_name_from_naming_options
from frappe.model.utils.rename_doc import update_linked_doctypes
from frappe.utils import cint, cstr, flt, get_formatted_email, today
from frappe.utils.deprecations import deprecated
from frappe.utils.user import get_users_with_role

from erpnext.accounts.party import get_dashboard_info, validate_party_accounts # noqa
Expand Down Expand Up @@ -303,20 +304,22 @@ def set_loyalty_program(self):
)


@deprecated
def create_contact(contact, party_type, party, email):
"""Create contact based on given contact name"""
contact = contact.split(" ")

contact = frappe.get_doc(
first, middle, last = parse_full_name(contact)
doc = frappe.get_doc(
{
"doctype": "Contact",
"first_name": contact[0],
"last_name": len(contact) > 1 and contact[1] or "",
"first_name": first,
"middle_name": middle,
"last_name": last,
"is_primary_contact": 1,
}
)
contact.append("email_ids", dict(email_id=email, is_primary=1))
contact.append("links", dict(link_doctype=party_type, link_name=party))
contact.insert()
doc.append("email_ids", dict(email_id=email, is_primary=1))
doc.append("links", dict(link_doctype=party_type, link_name=party))
return doc.insert()


@frappe.whitelist()
Expand Down Expand Up @@ -636,14 +639,28 @@ def get_credit_limit(customer, company):


def make_contact(args, is_primary_contact=1):
contact = frappe.get_doc(
{
"doctype": "Contact",
"first_name": args.get("customer_name"),
"is_primary_contact": is_primary_contact,
"links": [{"link_doctype": args.get("doctype"), "link_name": args.get("name")}],
}
)
values = {
"doctype": "Contact",
"is_primary_contact": is_primary_contact,
"links": [{"link_doctype": args.get("doctype"), "link_name": args.get("name")}],
}
if args.customer_type == "Individual":
first, middle, last = parse_full_name(args.get("customer_name"))
values.update(
{
"first_name": first,
"middle_name": middle,
"last_name": last,
}
)
else:
values.update(
{
"company_name": args.get("customer_name"),
}
)
contact = frappe.get_doc(values)

if args.get("email_id"):
contact.add_email(args.get("email_id"), is_primary=True)
if args.get("mobile_no"):
Expand Down Expand Up @@ -710,3 +727,13 @@ def get_customer_primary_contact(doctype, txt, searchfield, start, page_len, fil
.where((dlink.link_name == customer) & (con.name.like(f"%{txt}%")))
.run()
)


def parse_full_name(full_name: str) -> tuple[str, str | None, str | None]:
"""Parse full name into first name, middle name and last name"""
names = full_name.split()
first_name = names[0]
middle_name = " ".join(names[1:-1]) if len(names) > 2 else None
last_name = names[-1] if len(names) > 1 else None

return first_name, middle_name, last_name
22 changes: 21 additions & 1 deletion erpnext/selling/doctype/customer/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

from erpnext.accounts.party import get_due_date
from erpnext.exceptions import PartyDisabled, PartyFrozen
from erpnext.selling.doctype.customer.customer import get_credit_limit, get_customer_outstanding
from erpnext.selling.doctype.customer.customer import (
get_credit_limit,
get_customer_outstanding,
parse_full_name,
)
from erpnext.tests.utils import create_test_contact_and_address

test_ignore = ["Price List"]
Expand Down Expand Up @@ -373,6 +377,22 @@ def test_serach_fields_for_customer(self):

frappe.db.set_single_value("Selling Settings", "cust_master_name", "Customer Name")

def test_parse_full_name(self):
first, middle, last = parse_full_name("John")
self.assertEqual(first, "John")
self.assertEqual(middle, None)
self.assertEqual(last, None)

first, middle, last = parse_full_name("John Doe")
self.assertEqual(first, "John")
self.assertEqual(middle, None)
self.assertEqual(last, "Doe")

first, middle, last = parse_full_name("John Michael Doe")
self.assertEqual(first, "John")
self.assertEqual(middle, "Michael")
self.assertEqual(last, "Doe")


def get_customer_dict(customer_name):
return {
Expand Down

0 comments on commit 6ab2f83

Please sign in to comment.