blob: bae8f353b356cf087603c1ad7a2e9e225f004e90 [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05303import frappe
Faris Ansari38ac7f72019-10-09 11:41:33 +05304from frappe.utils.nestedset import get_root_of
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05305
Chillar Anand915b3432021-09-02 16:44:59 +05306from erpnext.shopping_cart.cart import get_debtors_account
7from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import (
8 get_shopping_cart_settings,
9)
10
11
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053012def set_default_role(doc, method):
Manas Solanki5807cd82017-12-22 10:50:10 +053013 '''Set customer, supplier, student, guardian based on email'''
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053014 if frappe.flags.setting_role or frappe.flags.in_migrate:
15 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053016
17 roles = frappe.get_roles(doc.name)
18
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053019 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 Bauskar6b3bc8a2017-05-05 11:53:00 +053024 if link.link_doctype=='Customer' and 'Customer' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053025 doc.add_roles('Customer')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053026 elif link.link_doctype=='Supplier' and 'Supplier' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053027 doc.add_roles('Supplier')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053028 elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053029 doc.add_roles('Student')
Manas Solanki5807cd82017-12-22 10:50:10 +053030 elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles:
31 doc.add_roles('Guardian')
Faris Ansari38ac7f72019-10-09 11:41:33 +053032
33def 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
Marica3994aa82020-07-22 19:49:54 +053096 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
106def create_party_contact(doctype, fullname, user, party_name):
Faris Ansari38ac7f72019-10-09 11:41:33 +0530107 contact = frappe.new_doc("Contact")
108 contact.update({
109 "first_name": fullname,
110 "email_id": user
111 })
Marica3994aa82020-07-22 19:49:54 +0530112 contact.append('links', dict(link_doctype=doctype, link_name=party_name))
113 contact.append('email_ids', dict(email_id=user))
Faris Ansari38ac7f72019-10-09 11:41:33 +0530114 contact.flags.ignore_mandatory = True
115 contact.insert(ignore_permissions=True)
116
Faris Ansari38ac7f72019-10-09 11:41:33 +0530117def party_exists(doctype, user):
Marica3994aa82020-07-22 19:49:54 +0530118 # check if contact exists against party and if it is linked to the doctype
Faris Ansari38ac7f72019-10-09 11:41:33 +0530119 contact_name = frappe.db.get_value("Contact", {"email_id": user})
Faris Ansari38ac7f72019-10-09 11:41:33 +0530120 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