Merge pull request #38753 from barredterra/contact-from-customer-creation

fix: address and contact from customer creation (backport #38055, #37386, #38060)
This commit is contained in:
Deepesh Garg
2023-12-15 10:14:58 +05:30
committed by GitHub
2 changed files with 77 additions and 21 deletions

View File

@@ -307,18 +307,19 @@ class Customer(TransactionBase):
def create_contact(contact, party_type, party, email): def create_contact(contact, party_type, party, email):
"""Create contact based on given contact name""" """Create contact based on given contact name"""
contact = contact.split(" ") first, middle, last = parse_full_name(contact)
doc = frappe.get_doc(
contact = frappe.get_doc(
{ {
"doctype": "Contact", "doctype": "Contact",
"first_name": contact[0], "first_name": first,
"last_name": len(contact) > 1 and contact[1] or "", "middle_name": middle,
"last_name": last,
"is_primary_contact": 1,
} }
) )
contact.append("email_ids", dict(email_id=email, is_primary=1)) doc.append("email_ids", dict(email_id=email, is_primary=1))
contact.append("links", dict(link_doctype=party_type, link_name=party)) doc.append("links", dict(link_doctype=party_type, link_name=party))
contact.insert() return doc.insert()
@frappe.whitelist() @frappe.whitelist()
@@ -684,24 +685,42 @@ def get_credit_limit(customer, company):
def make_contact(args, is_primary_contact=1): def make_contact(args, is_primary_contact=1):
contact = frappe.get_doc( values = {
{ "doctype": "Contact",
"doctype": "Contact", "is_primary_contact": is_primary_contact,
"first_name": args.get("name"), "links": [{"link_doctype": args.get("doctype"), "link_name": args.get("name")}],
"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"): if args.get("email_id"):
contact.add_email(args.get("email_id"), is_primary=True) contact.add_email(args.get("email_id"), is_primary=True)
if args.get("mobile_no"): if args.get("mobile_no"):
contact.add_phone(args.get("mobile_no"), is_primary_mobile_no=True) contact.add_phone(args.get("mobile_no"), is_primary_mobile_no=True)
contact.insert()
if flags := args.get("flags"):
contact.insert(ignore_permissions=flags.get("ignore_permissions"))
else:
contact.insert()
return contact return contact
def make_address(args, is_primary_address=1): def make_address(args, is_primary_address=1, is_shipping_address=1):
reqd_fields = [] reqd_fields = []
for field in ["city", "country"]: for field in ["city", "country"]:
if not args.get(field): if not args.get(field):
@@ -717,16 +736,23 @@ def make_address(args, is_primary_address=1):
address = frappe.get_doc( address = frappe.get_doc(
{ {
"doctype": "Address", "doctype": "Address",
"address_title": args.get("name"), "address_title": args.get("customer_name"),
"address_line1": args.get("address_line1"), "address_line1": args.get("address_line1"),
"address_line2": args.get("address_line2"), "address_line2": args.get("address_line2"),
"city": args.get("city"), "city": args.get("city"),
"state": args.get("state"), "state": args.get("state"),
"pincode": args.get("pincode"), "pincode": args.get("pincode"),
"country": args.get("country"), "country": args.get("country"),
"is_primary_address": is_primary_address,
"is_shipping_address": is_shipping_address,
"links": [{"link_doctype": args.get("doctype"), "link_name": args.get("name")}], "links": [{"link_doctype": args.get("doctype"), "link_name": args.get("name")}],
} }
).insert() )
if flags := args.get("flags"):
address.insert(ignore_permissions=flags.get("ignore_permissions"))
else:
address.insert()
return address return address
@@ -747,3 +773,13 @@ def get_customer_primary_contact(doctype, txt, searchfield, start, page_len, fil
.where((dlink.link_name == customer) & (con.name.like(f"%{txt}%"))) .where((dlink.link_name == customer) & (con.name.like(f"%{txt}%")))
.run() .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

View File

@@ -10,7 +10,11 @@ from frappe.utils import flt
from erpnext.accounts.party import get_due_date from erpnext.accounts.party import get_due_date
from erpnext.exceptions import PartyDisabled, PartyFrozen 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 from erpnext.tests.utils import create_test_contact_and_address
test_ignore = ["Price List"] test_ignore = ["Price List"]
@@ -373,6 +377,22 @@ class TestCustomer(FrappeTestCase):
frappe.db.set_value("Selling Settings", None, "cust_master_name", "Customer Name") frappe.db.set_value("Selling Settings", None, "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): def get_customer_dict(customer_name):
return { return {