From fc09d757f089afc78b2ab544321d57aaa4b5b532 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sun, 12 Nov 2023 13:21:18 +0100 Subject: [PATCH 1/5] fix(customer): contact creation for companies (#38055) (cherry picked from commit 9fde7824035580a3d8bd0b48a2e1a3d2ffc46082) # Conflicts: # erpnext/selling/doctype/customer/customer.py --- erpnext/selling/doctype/customer/customer.py | 39 +++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 432ad9f38368..587a1a1e1f2e 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -305,13 +305,20 @@ def set_loyalty_program(self): def create_contact(contact, party_type, party, email): """Create contact based on given contact name""" - contact = contact.split(" ") + names = contact.split(" ") contact = frappe.get_doc( { "doctype": "Contact", +<<<<<<< HEAD "first_name": contact[0], "last_name": len(contact) > 1 and contact[1] or "", +======= + "first_name": names[0], + "middle_name": len(names) > 2 and " ".join(names[1:-1]) or "", + "last_name": len(names) > 1 and names[-1] or "", + "is_primary_contact": 1, +>>>>>>> 9fde782403 (fix(customer): contact creation for companies (#38055)) } ) contact.append("email_ids", dict(email_id=email, is_primary=1)) @@ -636,14 +643,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": + names = args.get("customer_name").split(" ") + 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 "", + } + ) + 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"): From 0142a9308bc515f64bd856a005358f03d6d91478 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Sun, 12 Nov 2023 19:43:01 +0100 Subject: [PATCH 2/5] chore: resolve conflicts --- erpnext/selling/doctype/customer/customer.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 587a1a1e1f2e..5d41cf1041dd 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -310,15 +310,10 @@ def create_contact(contact, party_type, party, email): contact = frappe.get_doc( { "doctype": "Contact", -<<<<<<< HEAD - "first_name": contact[0], - "last_name": len(contact) > 1 and contact[1] or "", -======= "first_name": names[0], "middle_name": len(names) > 2 and " ".join(names[1:-1]) or "", "last_name": len(names) > 1 and names[-1] or "", "is_primary_contact": 1, ->>>>>>> 9fde782403 (fix(customer): contact creation for companies (#38055)) } ) contact.append("email_ids", dict(email_id=email, is_primary=1)) From f61b476be1993f940610c71cba59fea4d03c7512 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Sun, 12 Nov 2023 14:48:18 +0100 Subject: [PATCH 3/5] refactor: parse full name --- erpnext/selling/doctype/customer/customer.py | 35 ++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 5d41cf1041dd..12d5de362e12 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -305,20 +305,19 @@ def set_loyalty_program(self): 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() @@ -644,12 +643,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: @@ -726,3 +725,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 From d3d10231b995fe402741d31d9b44ff1e34a5ca02 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Sun, 12 Nov 2023 14:51:40 +0100 Subject: [PATCH 4/5] test: parse full name --- .../selling/doctype/customer/test_customer.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py index 6e737e4b55de..29dbd4f32146 100644 --- a/erpnext/selling/doctype/customer/test_customer.py +++ b/erpnext/selling/doctype/customer/test_customer.py @@ -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"] @@ -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 { From 6f2b34e7d52e5cdfffccddecff9c0408cdb168c1 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Sun, 12 Nov 2023 20:01:10 +0100 Subject: [PATCH 5/5] fix: deprecate unused create_contact --- erpnext/selling/doctype/customer/customer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py index 12d5de362e12..27bb8845dd6d 100644 --- a/erpnext/selling/doctype/customer/customer.py +++ b/erpnext/selling/doctype/customer/customer.py @@ -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 @@ -303,6 +304,7 @@ def set_loyalty_program(self): ) +@deprecated def create_contact(contact, party_type, party, email): """Create contact based on given contact name""" first, middle, last = parse_full_name(contact)