blob: 09d100708e353595b330d4ad96c248f999e81454 [file] [log] [blame]
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05301import frappe
Faris Ansari38ac7f72019-10-09 11:41:33 +05302from frappe.utils.nestedset import get_root_of
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05303
marinationeef9cf12021-02-16 18:45:36 +05304from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
Chillar Anand915b3432021-09-02 16:44:59 +05305 get_shopping_cart_settings,
6)
marination9fb61ef2022-02-01 00:39:14 +05307from erpnext.e_commerce.shopping_cart.cart import get_debtors_account
Chillar Anand915b3432021-09-02 16:44:59 +05308
9
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053010def set_default_role(doc, method):
Ankush Menat494bd9e2022-03-28 18:52:46 +053011 """Set customer, supplier, student, guardian based on email"""
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053012 if frappe.flags.setting_role or frappe.flags.in_migrate:
13 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053014
15 roles = frappe.get_roles(doc.name)
16
Ankush Menat494bd9e2022-03-28 18:52:46 +053017 contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053018 if contact_name:
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 contact = frappe.get_doc("Contact", contact_name)
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053020 for link in contact.links:
21 frappe.flags.setting_role = True
Ankush Menat494bd9e2022-03-28 18:52:46 +053022 if link.link_doctype == "Customer" and "Customer" not in roles:
23 doc.add_roles("Customer")
24 elif link.link_doctype == "Supplier" and "Supplier" not in roles:
25 doc.add_roles("Supplier")
26 elif frappe.get_value("Student", dict(student_email_id=doc.email)) and "Student" not in roles:
27 doc.add_roles("Student")
28 elif frappe.get_value("Guardian", dict(email_address=doc.email)) and "Guardian" not in roles:
29 doc.add_roles("Guardian")
30
Faris Ansari38ac7f72019-10-09 11:41:33 +053031
32def create_customer_or_supplier():
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 """Based on the default Role (Customer, Supplier), create a Customer / Supplier.
Faris Ansari38ac7f72019-10-09 11:41:33 +053034 Called on_session_creation hook.
Ankush Menat494bd9e2022-03-28 18:52:46 +053035 """
Faris Ansari38ac7f72019-10-09 11:41:33 +053036 user = frappe.session.user
37
Ankush Menat494bd9e2022-03-28 18:52:46 +053038 if frappe.db.get_value("User", user, "user_type") != "Website User":
Faris Ansari38ac7f72019-10-09 11:41:33 +053039 return
40
41 user_roles = frappe.get_roles()
Ankush Menat494bd9e2022-03-28 18:52:46 +053042 portal_settings = frappe.get_single("Portal Settings")
Faris Ansari38ac7f72019-10-09 11:41:33 +053043 default_role = portal_settings.default_role
44
Ankush Menat494bd9e2022-03-28 18:52:46 +053045 if default_role not in ["Customer", "Supplier"]:
Faris Ansari38ac7f72019-10-09 11:41:33 +053046 return
47
48 # create customer / supplier if the user has that role
49 if portal_settings.default_role and portal_settings.default_role in user_roles:
50 doctype = portal_settings.default_role
51 else:
52 doctype = None
53
54 if not doctype:
55 return
56
57 if party_exists(doctype, user):
58 return
59
60 party = frappe.new_doc(doctype)
61 fullname = frappe.utils.get_fullname(user)
62
Ankush Menat494bd9e2022-03-28 18:52:46 +053063 if doctype == "Customer":
Faris Ansari38ac7f72019-10-09 11:41:33 +053064 cart_settings = get_shopping_cart_settings()
65
66 if cart_settings.enable_checkout:
67 debtors_account = get_debtors_account(cart_settings)
68 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +053069 debtors_account = ""
Faris Ansari38ac7f72019-10-09 11:41:33 +053070
Ankush Menat494bd9e2022-03-28 18:52:46 +053071 party.update(
72 {
73 "customer_name": fullname,
74 "customer_type": "Individual",
75 "customer_group": cart_settings.default_customer_group,
76 "territory": get_root_of("Territory"),
77 }
78 )
Faris Ansari38ac7f72019-10-09 11:41:33 +053079
80 if debtors_account:
Ankush Menat494bd9e2022-03-28 18:52:46 +053081 party.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]})
Faris Ansari38ac7f72019-10-09 11:41:33 +053082 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +053083 party.update(
84 {
85 "supplier_name": fullname,
86 "supplier_group": "All Supplier Groups",
87 "supplier_type": "Individual",
88 }
89 )
Faris Ansari38ac7f72019-10-09 11:41:33 +053090
91 party.flags.ignore_mandatory = True
92 party.insert(ignore_permissions=True)
93
Marica3994aa82020-07-22 19:49:54 +053094 alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
95
96 if party_exists(alternate_doctype, user):
97 # if user is both customer and supplier, alter fullname to avoid contact name duplication
Ankush Menat494bd9e2022-03-28 18:52:46 +053098 fullname += "-" + doctype
Marica3994aa82020-07-22 19:49:54 +053099
100 create_party_contact(doctype, fullname, user, party.name)
101
102 return party
103
Ankush Menat494bd9e2022-03-28 18:52:46 +0530104
Marica3994aa82020-07-22 19:49:54 +0530105def create_party_contact(doctype, fullname, user, party_name):
Faris Ansari38ac7f72019-10-09 11:41:33 +0530106 contact = frappe.new_doc("Contact")
Ankush Menat494bd9e2022-03-28 18:52:46 +0530107 contact.update({"first_name": fullname, "email_id": user})
108 contact.append("links", dict(link_doctype=doctype, link_name=party_name))
109 contact.append("email_ids", dict(email_id=user))
Faris Ansari38ac7f72019-10-09 11:41:33 +0530110 contact.flags.ignore_mandatory = True
111 contact.insert(ignore_permissions=True)
112
Ankush Menat494bd9e2022-03-28 18:52:46 +0530113
Faris Ansari38ac7f72019-10-09 11:41:33 +0530114def party_exists(doctype, user):
Marica3994aa82020-07-22 19:49:54 +0530115 # check if contact exists against party and if it is linked to the doctype
Faris Ansari38ac7f72019-10-09 11:41:33 +0530116 contact_name = frappe.db.get_value("Contact", {"email_id": user})
Faris Ansari38ac7f72019-10-09 11:41:33 +0530117 if contact_name:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530118 contact = frappe.get_doc("Contact", contact_name)
Faris Ansari38ac7f72019-10-09 11:41:33 +0530119 doctypes = [d.link_doctype for d in contact.links]
120 return doctype in doctypes
121
122 return False