blob: 2e710c75f3d81830959e9827c6163892219c26ef [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05302import frappe
3
4def set_default_role(doc, method):
Manas Solanki5807cd82017-12-22 10:50:10 +05305 '''Set customer, supplier, student, guardian based on email'''
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +05306 if frappe.flags.setting_role or frappe.flags.in_migrate:
7 return
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +05308
9 roles = frappe.get_roles(doc.name)
10
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053011 contact_name = frappe.get_value('Contact', dict(email_id=doc.email))
12 if contact_name:
13 contact = frappe.get_doc('Contact', contact_name)
14 for link in contact.links:
15 frappe.flags.setting_role = True
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053016 if link.link_doctype=='Customer' and 'Customer' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053017 doc.add_roles('Customer')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053018 elif link.link_doctype=='Supplier' and 'Supplier' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053019 doc.add_roles('Supplier')
Makarand Bauskar6b3bc8a2017-05-05 11:53:00 +053020 elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles:
Rushabh Mehta0d6db6c2017-03-31 23:01:45 +053021 doc.add_roles('Student')
Manas Solanki5807cd82017-12-22 10:50:10 +053022 elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles:
23 doc.add_roles('Guardian')