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