From 366868dc68a4dab748e0bf055b8926b70efc53c8 Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 18 Jul 2023 10:35:45 +0200 Subject: [PATCH 1/7] fix: removed duplicate contact creation for new users --- erpnext/e_commerce/shopping_cart/cart.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 12ed8277203..a9637812218 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -701,11 +701,24 @@ 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}]}) + first_name = frappe.db.get_value("User", user, "first_name") + last_name = frappe.db.get_value("User", user, "last_name") + if first_name and last_name: + contact = frappe.get_doc( + "Contact", + { + "first_name": first_name, + "last_name": last_name, + } + ) + else: + contact = frappe.new_doc("Contact") + contact.update({"first_name": fullname}) + contact.insert(ignore_permissions=True) + 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.insert(ignore_permissions=True) + contact.save(ignore_permissions=True) return customer -- GitLab From 8e3b130532ea007b14a207b141c8a273c6b1b299 Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 18 Jul 2023 13:27:44 +0200 Subject: [PATCH 2/7] fix: update party function only updates contact first/last name when company type --- erpnext/e_commerce/shopping_cart/cart.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index a9637812218..17653223095 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 -- GitLab From 92446ec6374567d91edc144aa5832960c7f65811 Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 18 Jul 2023 14:19:50 +0200 Subject: [PATCH 3/7] feat: phone added to contact after filling cart address form --- erpnext/e_commerce/shopping_cart/cart.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 17653223095..8405a13ab30 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -360,6 +360,16 @@ def add_new_address(doc): doc.update({"doctype": "Address"}) address = frappe.get_doc(doc) address.save(ignore_permissions=True) + # Add phone number to party contact + if address.phone: + user = frappe.session.user + contact_name = get_contact_name(user) + if frappe.db.exists("Contact", contact_name): + contact = frappe.get_doc("Contact", contact_name) + if contact.mobile_no: + contact.add_phone(address.phone, is_primary_mobile_no=0, autosave=True) + else: + contact.add_phone(address.phone, is_primary_mobile_no=1, autosave=True) return address -- GitLab From dfab01c9268963c9acb05e0483dc430b10d8f9c5 Mon Sep 17 00:00:00 2001 From: Antoine Date: Fri, 28 Jul 2023 14:12:32 +0200 Subject: [PATCH 4/7] Revert "feat: phone added to contact after filling cart address form" This reverts commit 92446ec6374567d91edc144aa5832960c7f65811. --- erpnext/e_commerce/shopping_cart/cart.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 8405a13ab30..17653223095 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -360,16 +360,6 @@ def add_new_address(doc): doc.update({"doctype": "Address"}) address = frappe.get_doc(doc) address.save(ignore_permissions=True) - # Add phone number to party contact - if address.phone: - user = frappe.session.user - contact_name = get_contact_name(user) - if frappe.db.exists("Contact", contact_name): - contact = frappe.get_doc("Contact", contact_name) - if contact.mobile_no: - contact.add_phone(address.phone, is_primary_mobile_no=0, autosave=True) - else: - contact.add_phone(address.phone, is_primary_mobile_no=1, autosave=True) return address -- GitLab From eb82cfe0a6b41390c5f2a5a56ce5a6e8f1cec64a Mon Sep 17 00:00:00 2001 From: Antoine Date: Mon, 31 Jul 2023 11:30:17 +0200 Subject: [PATCH 5/7] reworked code for contact creation --- erpnext/e_commerce/shopping_cart/cart.py | 30 ++++++++---------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 17653223095..52161bdbf77 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -669,7 +669,11 @@ 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") + contact.update({"first_name": frappe.db.get_value('User', user,"first_name"), + "last_name": frappe.db.get_value('User', user,"last_name")}) + contact.insert(ignore_permissions=True) cart_settings = get_shopping_cart_settings() debtors_account = "" @@ -700,25 +704,11 @@ def get_party(user=None): customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) - - first_name = frappe.db.get_value("User", user, "first_name") - last_name = frappe.db.get_value("User", user, "last_name") - if first_name and last_name: - contact = frappe.get_doc( - "Contact", - { - "first_name": first_name, - "last_name": last_name, - } - ) - else: - contact = frappe.new_doc("Contact") - contact.update({"first_name": fullname}) - contact.insert(ignore_permissions=True) - 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) + 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 -- GitLab From aa0175e3164eaa4e366de46a9f596f61e722ae24 Mon Sep 17 00:00:00 2001 From: Antoine Date: Mon, 7 Aug 2023 14:45:39 +0200 Subject: [PATCH 6/7] fix: duplicate contact creation on session creation --- erpnext/portal/utils.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/erpnext/portal/utils.py b/erpnext/portal/utils.py index 4f365420923..6b07081b459 100644 --- a/erpnext/portal/utils.py +++ b/erpnext/portal/utils.py @@ -96,12 +96,23 @@ 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") + contact.update({ + "first_name": frappe.db.get_value('User', user, "first_name"), + "last_name": frappe.db.get_value('User', user, "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): -- GitLab From fa704f9039bf9cc594fc9201042be8f92b7aa665 Mon Sep 17 00:00:00 2001 From: Antoine Date: Mon, 7 Aug 2023 15:50:16 +0200 Subject: [PATCH 7/7] chore: streamlined database request --- erpnext/e_commerce/shopping_cart/cart.py | 7 +++++-- erpnext/portal/utils.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/erpnext/e_commerce/shopping_cart/cart.py b/erpnext/e_commerce/shopping_cart/cart.py index 52161bdbf77..d514a75c7dd 100644 --- a/erpnext/e_commerce/shopping_cart/cart.py +++ b/erpnext/e_commerce/shopping_cart/cart.py @@ -671,8 +671,11 @@ def get_party(user=None): party = contact.links[0].link_name else: contact = frappe.new_doc("Contact") - contact.update({"first_name": frappe.db.get_value('User', user,"first_name"), - "last_name": frappe.db.get_value('User', user,"last_name")}) + 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() diff --git a/erpnext/portal/utils.py b/erpnext/portal/utils.py index 6b07081b459..f61da50824a 100644 --- a/erpnext/portal/utils.py +++ b/erpnext/portal/utils.py @@ -104,9 +104,10 @@ def create_party_contact(doctype, fullname, user, party_name): 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": frappe.db.get_value('User', user, "first_name"), - "last_name": frappe.db.get_value('User', user, "last_name"), + "first_name": first_name, + "last_name": last_name, "email_id": user, }) contact.append("links", dict(link_doctype=doctype, link_name=party_name)) -- GitLab