diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 12ed827720347753024443e22aba3035ffc467b7..d514a75c7dde892687c9d7d867c33bfc80504eeb 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -552,8 +552,8 @@ def update_party(fullname, company_name=None, mobile_no=None, phone=None): contact_name = frappe.db.get_value("Contact", {"email_id": frappe.session.user}) contact = frappe.get_doc("Contact", contact_name) - contact.first_name = fullname - contact.last_name = None + contact.first_name = fullname if party.customer_type == "Company" else contact.first_name + contact.last_name = None if party.customer_type == "Company" else contact.last_name contact.customer_name = party.customer_name contact.mobile_no = mobile_no contact.phone = phone @@ -669,7 +669,14 @@ def get_party(user=None): if contact.links: party_doctype = contact.links[0].link_doctype party = contact.links[0].link_name - + else: + contact = frappe.new_doc("Contact") + first_name, last_name = frappe.db.get_value("User", user, ["first_name", "last_name"]) + contact.update({ + "first_name": first_name, + "last_name": last_name + }) + contact.insert(ignore_permissions=True) cart_settings = get_shopping_cart_settings() debtors_account = "" @@ -700,12 +707,11 @@ def get_party(user=None): customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) - - contact = frappe.new_doc("Contact") - contact.update({"first_name": fullname, "email_ids": [{"email_id": user, "is_primary": 1}]}) - contact.append("links", dict(link_doctype="Customer", link_name=customer.name)) - contact.flags.ignore_mandatory = True - contact.insert(ignore_permissions=True) + if contact: + contact.update({"email_ids": [{"email_id": user, "is_primary": 1}]}) + contact.append("links", dict(link_doctype="Customer", link_name=customer.name)) + contact.flags.ignore_mandatory = True + contact.save(ignore_permissions=True) return customer diff --git a/erpnext/portal/utils.py b/erpnext/portal/utils.py index 4f365420923c3a2b2fa32ab70a0b9d57ebb39278..f61da50824a21dbedd9f06f323bf0597733127c1 100644 --- a/erpnext/portal/utils.py +++ b/erpnext/portal/utils.py @@ -96,12 +96,24 @@ def create_customer_or_supplier(): def create_party_contact(doctype, fullname, user, party_name): - contact = frappe.new_doc("Contact") - contact.update({"first_name": fullname, "email_id": user}) - contact.append("links", dict(link_doctype=doctype, link_name=party_name)) - contact.append("email_ids", dict(email_id=user, is_primary=True)) - contact.flags.ignore_mandatory = True - contact.insert(ignore_permissions=True) + if frappe.db.exists("Contact", {"email_id": user}): + contact_name = frappe.db.get_value("Contact", {"email_id": user}) + contact = frappe.get_doc("Contact", contact_name) + contact.append("links", dict(link_doctype=doctype, link_name=party_name)) + contact.save(ignore_permissions=True) + return + else: + contact = frappe.new_doc("Contact") + first_name, last_name = frappe.db.get_value("User", user, ["first_name", "last_name"]) + contact.update({ + "first_name": first_name, + "last_name": last_name, + "email_id": user, + }) + contact.append("links", dict(link_doctype=doctype, link_name=party_name)) + contact.append("email_ids", dict(email_id=user, is_primary=True)) + contact.flags.ignore_mandatory = True + contact.insert(ignore_permissions=True) def party_exists(doctype, user):