Merge pull request #38060 from barredterra/refactor-fullname-parsing
refactor: parse full name
This commit is contained in:
commit
8062d2be3b
@ -309,20 +309,19 @@ class Customer(TransactionBase):
|
||||
|
||||
def create_contact(contact, party_type, party, email):
|
||||
"""Create contact based on given contact name"""
|
||||
names = contact.split(" ")
|
||||
|
||||
contact = frappe.get_doc(
|
||||
first, middle, last = parse_full_name(contact)
|
||||
doc = frappe.get_doc(
|
||||
{
|
||||
"doctype": "Contact",
|
||||
"first_name": names[0],
|
||||
"middle_name": len(names) > 2 and " ".join(names[1:-1]) or "",
|
||||
"last_name": len(names) > 1 and names[-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()
|
||||
@ -648,12 +647,12 @@ def make_contact(args, is_primary_contact=1):
|
||||
"links": [{"link_doctype": args.get("doctype"), "link_name": args.get("name")}],
|
||||
}
|
||||
if args.customer_type == "Individual":
|
||||
names = args.get("customer_name").split(" ")
|
||||
first, middle, last = parse_full_name(args.get("customer_name"))
|
||||
values.update(
|
||||
{
|
||||
"first_name": names[0],
|
||||
"middle_name": len(names) > 2 and " ".join(names[1:-1]) or "",
|
||||
"last_name": len(names) > 1 and names[-1] or "",
|
||||
"first_name": first,
|
||||
"middle_name": middle,
|
||||
"last_name": last,
|
||||
}
|
||||
)
|
||||
else:
|
||||
@ -730,3 +729,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
|
||||
|
@ -10,7 +10,11 @@ from frappe.utils import flt
|
||||
|
||||
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"]
|
||||
@ -373,6 +377,22 @@ class TestCustomer(FrappeTestCase):
|
||||
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user