2017-03-31 17:31:45 +00:00
|
|
|
import frappe
|
2019-10-09 06:11:33 +00:00
|
|
|
from frappe.utils.nestedset import get_root_of
|
2017-03-31 17:31:45 +00:00
|
|
|
|
2021-02-16 13:15:36 +00:00
|
|
|
from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
|
2021-09-02 11:14:59 +00:00
|
|
|
get_shopping_cart_settings,
|
|
|
|
)
|
2022-01-31 19:09:14 +00:00
|
|
|
from erpnext.e_commerce.shopping_cart.cart import get_debtors_account
|
2021-09-02 11:14:59 +00:00
|
|
|
|
|
|
|
|
2017-03-31 17:31:45 +00:00
|
|
|
def set_default_role(doc, method):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Set customer, supplier, student, guardian based on email"""
|
2017-03-31 17:31:45 +00:00
|
|
|
if frappe.flags.setting_role or frappe.flags.in_migrate:
|
|
|
|
return
|
2017-05-05 06:23:00 +00:00
|
|
|
|
|
|
|
roles = frappe.get_roles(doc.name)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
|
2017-03-31 17:31:45 +00:00
|
|
|
if contact_name:
|
2022-03-28 13:22:46 +00:00
|
|
|
contact = frappe.get_doc("Contact", contact_name)
|
2017-03-31 17:31:45 +00:00
|
|
|
for link in contact.links:
|
|
|
|
frappe.flags.setting_role = True
|
2022-03-28 13:22:46 +00:00
|
|
|
if link.link_doctype == "Customer" and "Customer" not in roles:
|
|
|
|
doc.add_roles("Customer")
|
|
|
|
elif link.link_doctype == "Supplier" and "Supplier" not in roles:
|
|
|
|
doc.add_roles("Supplier")
|
|
|
|
elif frappe.get_value("Student", dict(student_email_id=doc.email)) and "Student" not in roles:
|
|
|
|
doc.add_roles("Student")
|
|
|
|
elif frappe.get_value("Guardian", dict(email_address=doc.email)) and "Guardian" not in roles:
|
|
|
|
doc.add_roles("Guardian")
|
|
|
|
|
2019-10-09 06:11:33 +00:00
|
|
|
|
|
|
|
def create_customer_or_supplier():
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Based on the default Role (Customer, Supplier), create a Customer / Supplier.
|
2019-10-09 06:11:33 +00:00
|
|
|
Called on_session_creation hook.
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2019-10-09 06:11:33 +00:00
|
|
|
user = frappe.session.user
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if frappe.db.get_value("User", user, "user_type") != "Website User":
|
2019-10-09 06:11:33 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
user_roles = frappe.get_roles()
|
2022-03-28 13:22:46 +00:00
|
|
|
portal_settings = frappe.get_single("Portal Settings")
|
2019-10-09 06:11:33 +00:00
|
|
|
default_role = portal_settings.default_role
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if default_role not in ["Customer", "Supplier"]:
|
2019-10-09 06:11:33 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# create customer / supplier if the user has that role
|
|
|
|
if portal_settings.default_role and portal_settings.default_role in user_roles:
|
|
|
|
doctype = portal_settings.default_role
|
|
|
|
else:
|
|
|
|
doctype = None
|
|
|
|
|
|
|
|
if not doctype:
|
|
|
|
return
|
|
|
|
|
|
|
|
if party_exists(doctype, user):
|
|
|
|
return
|
|
|
|
|
|
|
|
party = frappe.new_doc(doctype)
|
|
|
|
fullname = frappe.utils.get_fullname(user)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if doctype == "Customer":
|
2019-10-09 06:11:33 +00:00
|
|
|
cart_settings = get_shopping_cart_settings()
|
|
|
|
|
|
|
|
if cart_settings.enable_checkout:
|
|
|
|
debtors_account = get_debtors_account(cart_settings)
|
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
debtors_account = ""
|
2019-10-09 06:11:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
party.update(
|
|
|
|
{
|
|
|
|
"customer_name": fullname,
|
|
|
|
"customer_type": "Individual",
|
|
|
|
"customer_group": cart_settings.default_customer_group,
|
|
|
|
"territory": get_root_of("Territory"),
|
|
|
|
}
|
|
|
|
)
|
2019-10-09 06:11:33 +00:00
|
|
|
|
|
|
|
if debtors_account:
|
2022-03-28 13:22:46 +00:00
|
|
|
party.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]})
|
2019-10-09 06:11:33 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
party.update(
|
|
|
|
{
|
|
|
|
"supplier_name": fullname,
|
|
|
|
"supplier_group": "All Supplier Groups",
|
|
|
|
"supplier_type": "Individual",
|
|
|
|
}
|
|
|
|
)
|
2019-10-09 06:11:33 +00:00
|
|
|
|
|
|
|
party.flags.ignore_mandatory = True
|
|
|
|
party.insert(ignore_permissions=True)
|
|
|
|
|
2020-07-22 14:19:54 +00:00
|
|
|
alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
|
|
|
|
|
|
|
|
if party_exists(alternate_doctype, user):
|
|
|
|
# if user is both customer and supplier, alter fullname to avoid contact name duplication
|
2022-03-28 13:22:46 +00:00
|
|
|
fullname += "-" + doctype
|
2020-07-22 14:19:54 +00:00
|
|
|
|
|
|
|
create_party_contact(doctype, fullname, user, party.name)
|
|
|
|
|
|
|
|
return party
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-22 14:19:54 +00:00
|
|
|
def create_party_contact(doctype, fullname, user, party_name):
|
2019-10-09 06:11:33 +00:00
|
|
|
contact = frappe.new_doc("Contact")
|
2022-03-28 13:22:46 +00:00
|
|
|
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))
|
2019-10-09 06:11:33 +00:00
|
|
|
contact.flags.ignore_mandatory = True
|
|
|
|
contact.insert(ignore_permissions=True)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-10-09 06:11:33 +00:00
|
|
|
def party_exists(doctype, user):
|
2020-07-22 14:19:54 +00:00
|
|
|
# check if contact exists against party and if it is linked to the doctype
|
2019-10-09 06:11:33 +00:00
|
|
|
contact_name = frappe.db.get_value("Contact", {"email_id": user})
|
|
|
|
if contact_name:
|
2022-03-28 13:22:46 +00:00
|
|
|
contact = frappe.get_doc("Contact", contact_name)
|
2019-10-09 06:11:33 +00:00
|
|
|
doctypes = [d.link_doctype for d in contact.links]
|
|
|
|
return doctype in doctypes
|
|
|
|
|
|
|
|
return False
|