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