blob: 903d4a6196c1059d3a8b9fe5f7b2f3230e1b1e68 [file] [log] [blame]
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05301import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05302
3
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05304def set_default_role(doc, method):
Ankush Menat494bd9e2022-03-28 18:52:46 +05305 """Set customer, supplier, student, guardian based on email"""
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05306 if frappe.flags.setting_role or frappe.flags.in_migrate:
7 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +05308
9 roles = frappe.get_roles(doc.name)
10
Ankush Menat494bd9e2022-03-28 18:52:46 +053011 contact_name = frappe.get_value("Contact", dict(email_id=doc.email))
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053012 if contact_name:
Ankush Menat494bd9e2022-03-28 18:52:46 +053013 contact = frappe.get_doc("Contact", contact_name)
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053014 for link in contact.links:
15 frappe.flags.setting_role = True
Ankush Menat494bd9e2022-03-28 18:52:46 +053016 if link.link_doctype == "Customer" and "Customer" not in roles:
17 doc.add_roles("Customer")
18 elif link.link_doctype == "Supplier" and "Supplier" not in roles:
19 doc.add_roles("Supplier")
Ankush Menat494bd9e2022-03-28 18:52:46 +053020
Faris Ansari38ac7f72019-10-09 11:41:33 +053021
22def create_customer_or_supplier():
Ankush Menat494bd9e2022-03-28 18:52:46 +053023 """Based on the default Role (Customer, Supplier), create a Customer / Supplier.
Faris Ansari38ac7f72019-10-09 11:41:33 +053024 Called on_session_creation hook.
Ankush Menat494bd9e2022-03-28 18:52:46 +053025 """
Faris Ansari38ac7f72019-10-09 11:41:33 +053026 user = frappe.session.user
27
Ankush Menat494bd9e2022-03-28 18:52:46 +053028 if frappe.db.get_value("User", user, "user_type") != "Website User":
Faris Ansari38ac7f72019-10-09 11:41:33 +053029 return
30
31 user_roles = frappe.get_roles()
Ankush Menat494bd9e2022-03-28 18:52:46 +053032 portal_settings = frappe.get_single("Portal Settings")
Faris Ansari38ac7f72019-10-09 11:41:33 +053033 default_role = portal_settings.default_role
34
Ankush Menat494bd9e2022-03-28 18:52:46 +053035 if default_role not in ["Customer", "Supplier"]:
Faris Ansari38ac7f72019-10-09 11:41:33 +053036 return
37
38 # create customer / supplier if the user has that role
39 if portal_settings.default_role and portal_settings.default_role in user_roles:
40 doctype = portal_settings.default_role
41 else:
42 doctype = None
43
44 if not doctype:
45 return
46
47 if party_exists(doctype, user):
48 return
49
50 party = frappe.new_doc(doctype)
51 fullname = frappe.utils.get_fullname(user)
52
Sabu Siyadf900a782023-10-17 17:05:44 +053053 if not doctype == "Customer":
Ankush Menat494bd9e2022-03-28 18:52:46 +053054 party.update(
55 {
56 "supplier_name": fullname,
57 "supplier_group": "All Supplier Groups",
58 "supplier_type": "Individual",
59 }
60 )
Faris Ansari38ac7f72019-10-09 11:41:33 +053061
62 party.flags.ignore_mandatory = True
63 party.insert(ignore_permissions=True)
64
Marica3994aa82020-07-22 19:49:54 +053065 alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
66
67 if party_exists(alternate_doctype, user):
68 # if user is both customer and supplier, alter fullname to avoid contact name duplication
Ankush Menat494bd9e2022-03-28 18:52:46 +053069 fullname += "-" + doctype
Marica3994aa82020-07-22 19:49:54 +053070
71 create_party_contact(doctype, fullname, user, party.name)
72
73 return party
74
Ankush Menat494bd9e2022-03-28 18:52:46 +053075
Marica3994aa82020-07-22 19:49:54 +053076def create_party_contact(doctype, fullname, user, party_name):
Faris Ansari38ac7f72019-10-09 11:41:33 +053077 contact = frappe.new_doc("Contact")
Ankush Menat494bd9e2022-03-28 18:52:46 +053078 contact.update({"first_name": fullname, "email_id": user})
79 contact.append("links", dict(link_doctype=doctype, link_name=party_name))
kunhi15e3b7f2022-12-13 12:41:56 +040080 contact.append("email_ids", dict(email_id=user, is_primary=True))
Faris Ansari38ac7f72019-10-09 11:41:33 +053081 contact.flags.ignore_mandatory = True
82 contact.insert(ignore_permissions=True)
83
Ankush Menat494bd9e2022-03-28 18:52:46 +053084
Faris Ansari38ac7f72019-10-09 11:41:33 +053085def party_exists(doctype, user):
Marica3994aa82020-07-22 19:49:54 +053086 # check if contact exists against party and if it is linked to the doctype
Faris Ansari38ac7f72019-10-09 11:41:33 +053087 contact_name = frappe.db.get_value("Contact", {"email_id": user})
Faris Ansari38ac7f72019-10-09 11:41:33 +053088 if contact_name:
Ankush Menat494bd9e2022-03-28 18:52:46 +053089 contact = frappe.get_doc("Contact", contact_name)
Faris Ansari38ac7f72019-10-09 11:41:33 +053090 doctypes = [d.link_doctype for d in contact.links]
91 return doctype in doctypes
92
93 return False