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