Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 1 | import frappe |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 2 | from frappe.utils.nestedset import get_root_of |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 3 | |
marination | eef9cf1 | 2021-02-16 18:45:36 +0530 | [diff] [blame] | 4 | from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import ( |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | get_shopping_cart_settings, |
| 6 | ) |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 7 | from erpnext.e_commerce.shopping_cart.cart import get_debtors_account |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
| 9 | |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 10 | def set_default_role(doc, method): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | """Set customer, supplier, student, guardian based on email""" |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 12 | if frappe.flags.setting_role or frappe.flags.in_migrate: |
| 13 | return |
Makarand Bauskar | 6b3bc8a | 2017-05-05 11:53:00 +0530 | [diff] [blame] | 14 | |
| 15 | roles = frappe.get_roles(doc.name) |
| 16 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 17 | contact_name = frappe.get_value("Contact", dict(email_id=doc.email)) |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 18 | if contact_name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | contact = frappe.get_doc("Contact", contact_name) |
Rushabh Mehta | 0d6db6c | 2017-03-31 23:01:45 +0530 | [diff] [blame] | 20 | for link in contact.links: |
| 21 | frappe.flags.setting_role = True |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | 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") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 27 | |
| 28 | def create_customer_or_supplier(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 29 | """Based on the default Role (Customer, Supplier), create a Customer / Supplier. |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 30 | Called on_session_creation hook. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 31 | """ |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 32 | user = frappe.session.user |
| 33 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 34 | if frappe.db.get_value("User", user, "user_type") != "Website User": |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 35 | return |
| 36 | |
| 37 | user_roles = frappe.get_roles() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 38 | portal_settings = frappe.get_single("Portal Settings") |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 39 | default_role = portal_settings.default_role |
| 40 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 41 | if default_role not in ["Customer", "Supplier"]: |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 42 | return |
| 43 | |
| 44 | # create customer / supplier if the user has that role |
| 45 | if portal_settings.default_role and portal_settings.default_role in user_roles: |
| 46 | doctype = portal_settings.default_role |
| 47 | else: |
| 48 | doctype = None |
| 49 | |
| 50 | if not doctype: |
| 51 | return |
| 52 | |
| 53 | if party_exists(doctype, user): |
| 54 | return |
| 55 | |
| 56 | party = frappe.new_doc(doctype) |
| 57 | fullname = frappe.utils.get_fullname(user) |
| 58 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | if doctype == "Customer": |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 60 | cart_settings = get_shopping_cart_settings() |
| 61 | |
| 62 | if cart_settings.enable_checkout: |
| 63 | debtors_account = get_debtors_account(cart_settings) |
| 64 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 65 | debtors_account = "" |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 66 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 67 | party.update( |
| 68 | { |
| 69 | "customer_name": fullname, |
| 70 | "customer_type": "Individual", |
| 71 | "customer_group": cart_settings.default_customer_group, |
| 72 | "territory": get_root_of("Territory"), |
| 73 | } |
| 74 | ) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 75 | |
| 76 | if debtors_account: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 77 | party.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]}) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 78 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | party.update( |
| 80 | { |
| 81 | "supplier_name": fullname, |
| 82 | "supplier_group": "All Supplier Groups", |
| 83 | "supplier_type": "Individual", |
| 84 | } |
| 85 | ) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 86 | |
| 87 | party.flags.ignore_mandatory = True |
| 88 | party.insert(ignore_permissions=True) |
| 89 | |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 90 | alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier" |
| 91 | |
| 92 | if party_exists(alternate_doctype, user): |
| 93 | # 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] | 94 | fullname += "-" + doctype |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 95 | |
| 96 | create_party_contact(doctype, fullname, user, party.name) |
| 97 | |
| 98 | return party |
| 99 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 100 | |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 101 | def create_party_contact(doctype, fullname, user, party_name): |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 102 | contact = frappe.new_doc("Contact") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 103 | contact.update({"first_name": fullname, "email_id": user}) |
| 104 | contact.append("links", dict(link_doctype=doctype, link_name=party_name)) |
| 105 | contact.append("email_ids", dict(email_id=user)) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 106 | contact.flags.ignore_mandatory = True |
| 107 | contact.insert(ignore_permissions=True) |
| 108 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 109 | |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 110 | def party_exists(doctype, user): |
Marica | 3994aa8 | 2020-07-22 19:49:54 +0530 | [diff] [blame] | 111 | # 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] | 112 | contact_name = frappe.db.get_value("Contact", {"email_id": user}) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 113 | if contact_name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | contact = frappe.get_doc("Contact", contact_name) |
Faris Ansari | 38ac7f7 | 2019-10-09 11:41:33 +0530 | [diff] [blame] | 115 | doctypes = [d.link_doctype for d in contact.links] |
| 116 | return doctype in doctypes |
| 117 | |
| 118 | return False |