Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 1 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | |
| 3 | |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 4 | def set_default_role(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 5 | """Set customer, supplier, student, guardian based on email""" |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 6 | if frappe.flags.setting_role or frappe.flags.in_migrate: |
| 7 | return |
Makarand Bauskar | 6b3bc8a | 2017-05-05 11:53:00 +0530 | [diff] [blame] | 8 | |
| 9 | roles = frappe.get_roles(doc.name) |
| 10 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | contact_name = frappe.get_value("Contact", dict(email_id=doc.email)) |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 12 | if contact_name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | contact = frappe.get_doc("Contact", contact_name) |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 14 | for link in contact.links: |
| 15 | frappe.flags.setting_role = True |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 16 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 20 | |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 21 | |
| 22 | def create_customer_or_supplier(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | """Based on the default Role (Customer, Supplier), create a Customer / Supplier. |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 24 | Called on_session_creation hook. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | """ |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 26 | user = frappe.session.user |
| 27 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | if frappe.db.get_value("User", user, "user_type") != "Website User": |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 29 | return |
| 30 | |
| 31 | user_roles = frappe.get_roles() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 32 | portal_settings = frappe.get_single("Portal Settings") |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 33 | default_role = portal_settings.default_role |
| 34 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | if default_role not in ["Customer", "Supplier"]: |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 36 | 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 Siyad | f900a78 | 2023-10-17 17:05:44 +0530 | [diff] [blame] | 53 | if not doctype == "Customer": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 54 | party.update( |
| 55 | { |
| 56 | "supplier_name": fullname, |
| 57 | "supplier_group": "All Supplier Groups", |
| 58 | "supplier_type": "Individual", |
| 59 | } |
| 60 | ) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 61 | |
| 62 | party.flags.ignore_mandatory = True |
| 63 | party.insert(ignore_permissions=True) |
| 64 | |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 65 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 69 | fullname += "-" + doctype |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 70 | |
| 71 | create_party_contact(doctype, fullname, user, party.name) |
| 72 | |
| 73 | return party |
| 74 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 75 | |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 76 | def create_party_contact(doctype, fullname, user, party_name): |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 77 | contact = frappe.new_doc("Contact") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 78 | contact.update({"first_name": fullname, "email_id": user}) |
| 79 | contact.append("links", dict(link_doctype=doctype, link_name=party_name)) |
kunhi | 15e3b7f | 2022-12-13 12:41:56 +0400 | [diff] [blame] | 80 | contact.append("email_ids", dict(email_id=user, is_primary=True)) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 81 | contact.flags.ignore_mandatory = True |
| 82 | contact.insert(ignore_permissions=True) |
| 83 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 84 | |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 85 | def party_exists(doctype, user): |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 86 | # check if contact exists against party and if it is linked to the doctype |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 87 | contact_name = frappe.db.get_value("Contact", {"email_id": user}) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 88 | if contact_name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | contact = frappe.get_doc("Contact", contact_name) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 90 | doctypes = [d.link_doctype for d in contact.links] |
| 91 | return doctype in doctypes |
| 92 | |
| 93 | return False |