blob: 93fe5da693b5b7c4fae75faaa39e5b15f3d8983f [file] [log] [blame]
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05301import frappe
2
3def set_default_role(doc, method):
Manas Solanki5807cd82017-12-22 10:50:10 +05304 '''Set customer, supplier, student, guardian based on email'''
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05305 if frappe.flags.setting_role or frappe.flags.in_migrate:
6 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +05307
8 roles = frappe.get_roles(doc.name)
9
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053010 contact_name = frappe.get_value('Contact', dict(email_id=doc.email))
11 if contact_name:
12 contact = frappe.get_doc('Contact', contact_name)
13 for link in contact.links:
14 frappe.flags.setting_role = True
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053015 if link.link_doctype=='Customer' and 'Customer' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053016 doc.add_roles('Customer')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053017 elif link.link_doctype=='Supplier' and 'Supplier' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053018 doc.add_roles('Supplier')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053019 elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053020 doc.add_roles('Student')
Manas Solanki5807cd82017-12-22 10:50:10 +053021 elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles:
22 doc.add_roles('Guardian')