blob: d6d446942030f2fcc98dc0d37d4fc4d6bda320f4 [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05302import frappe
Faris Ansari38ac7f72019-10-09 11:41:33 +05303from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import get_shopping_cart_settings
4from erpnext.shopping_cart.cart import get_debtors_account
5from frappe.utils.nestedset import get_root_of
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05306
7def set_default_role(doc, method):
Manas Solanki5807cd82017-12-22 10:50:10 +05308 '''Set customer, supplier, student, guardian based on email'''
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05309 if frappe.flags.setting_role or frappe.flags.in_migrate:
10 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053011
12 roles = frappe.get_roles(doc.name)
13
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053014 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 Bauskar6b3bc8a2017-05-05 11:53:00 +053019 if link.link_doctype=='Customer' and 'Customer' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053020 doc.add_roles('Customer')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053021 elif link.link_doctype=='Supplier' and 'Supplier' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053022 doc.add_roles('Supplier')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053023 elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053024 doc.add_roles('Student')
Manas Solanki5807cd82017-12-22 10:50:10 +053025 elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles:
26 doc.add_roles('Guardian')
Faris Ansari38ac7f72019-10-09 11:41:33 +053027
28def 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
Marica3994aa82020-07-22 19:49:54 +053091 alternate_doctype = "Customer" if doctype == "Supplier" else "Supplier"
92
93 if party_exists(alternate_doctype, user):
94 # if user is both customer and supplier, alter fullname to avoid contact name duplication
95 fullname += "-" + doctype
96
97 create_party_contact(doctype, fullname, user, party.name)
98
99 return party
100
101def create_party_contact(doctype, fullname, user, party_name):
Faris Ansari38ac7f72019-10-09 11:41:33 +0530102 contact = frappe.new_doc("Contact")
103 contact.update({
104 "first_name": fullname,
105 "email_id": user
106 })
Marica3994aa82020-07-22 19:49:54 +0530107 contact.append('links', dict(link_doctype=doctype, link_name=party_name))
108 contact.append('email_ids', dict(email_id=user))
Faris Ansari38ac7f72019-10-09 11:41:33 +0530109 contact.flags.ignore_mandatory = True
110 contact.insert(ignore_permissions=True)
111
Faris Ansari38ac7f72019-10-09 11:41:33 +0530112def party_exists(doctype, user):
Marica3994aa82020-07-22 19:49:54 +0530113 # check if contact exists against party and if it is linked to the doctype
Faris Ansari38ac7f72019-10-09 11:41:33 +0530114 contact_name = frappe.db.get_value("Contact", {"email_id": user})
Faris Ansari38ac7f72019-10-09 11:41:33 +0530115 if contact_name:
116 contact = frappe.get_doc('Contact', contact_name)
117 doctypes = [d.link_doctype for d in contact.links]
118 return doctype in doctypes
119
120 return False