blob: 974b51ef0a51df2d456d4c17ee4e0b0540e92302 [file] [log] [blame]
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05301import frappe
Faris Ansari38ac7f72019-10-09 11:41:33 +05302from frappe.utils.nestedset import get_root_of
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05303
Chillar Anand915b3432021-09-02 16:44:59 +05304from erpnext.shopping_cart.cart import get_debtors_account
5from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import (
6 get_shopping_cart_settings,
7)
8
9
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053010def set_default_role(doc, method):
Manas Solanki5807cd82017-12-22 10:50:10 +053011 '''Set customer, supplier, student, guardian based on email'''
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053012 if frappe.flags.setting_role or frappe.flags.in_migrate:
13 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053014
15 roles = frappe.get_roles(doc.name)
16
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053017 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 Bauskar6b3bc8a2017-05-05 11:53:00 +053022 if link.link_doctype=='Customer' and 'Customer' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053023 doc.add_roles('Customer')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053024 elif link.link_doctype=='Supplier' and 'Supplier' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053025 doc.add_roles('Supplier')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053026 elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053027 doc.add_roles('Student')
Manas Solanki5807cd82017-12-22 10:50:10 +053028 elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles:
29 doc.add_roles('Guardian')
Faris Ansari38ac7f72019-10-09 11:41:33 +053030
31def 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
Marica3994aa82020-07-22 19:49:54 +053094 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
104def create_party_contact(doctype, fullname, user, party_name):
Faris Ansari38ac7f72019-10-09 11:41:33 +0530105 contact = frappe.new_doc("Contact")
106 contact.update({
107 "first_name": fullname,
108 "email_id": user
109 })
Marica3994aa82020-07-22 19:49:54 +0530110 contact.append('links', dict(link_doctype=doctype, link_name=party_name))
111 contact.append('email_ids', dict(email_id=user))
Faris Ansari38ac7f72019-10-09 11:41:33 +0530112 contact.flags.ignore_mandatory = True
113 contact.insert(ignore_permissions=True)
114
Faris Ansari38ac7f72019-10-09 11:41:33 +0530115def party_exists(doctype, user):
Marica3994aa82020-07-22 19:49:54 +0530116 # check if contact exists against party and if it is linked to the doctype
Faris Ansari38ac7f72019-10-09 11:41:33 +0530117 contact_name = frappe.db.get_value("Contact", {"email_id": user})
Faris Ansari38ac7f72019-10-09 11:41:33 +0530118 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