blob: 12ceb5b61b072f2e8f8dc1956e86dda0f34a444c [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
6
7def load_address_and_contact(doc, key):
8 """Loads address list and contact list in `__onload`"""
Rushabh Mehtab92087c2017-01-13 18:53:11 +05309 from frappe.geo.doctype.address.address import get_address_display
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +053010
Rushabh Mehtab92087c2017-01-13 18:53:11 +053011 address_list = [frappe.get_value('Address', a.parent, '*')
12 for a in frappe.get_all('Dynamic Link', fields='parent',
13 filters=dict(parenttype='Address', link_doctype=doc.doctype, link_name=doc.name))]
14
15 address_list = [a.update({"display": get_address_display(a)})
16 for a in address_list]
17
18 address_list = sorted(address_list,
19 lambda a, b:
20 (int(a.is_primary_address - b.is_primary_address)) or
21 (1 if a.modified - b.modified else 0))
22
23 doc.set_onload('addr_list', address_list)
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +053024
25 if doc.doctype != "Lead":
Rushabh Mehtab92087c2017-01-13 18:53:11 +053026 contact_list = [frappe.get_value('Contact', a.parent, '*')
27 for a in frappe.get_all('Dynamic Link', fields='parent',
28 filters=dict(parenttype='Contact', link_doctype=doc.doctype, link_name=doc.name))]
29
30 contact_list = sorted(contact_list,
31 lambda a, b:
32 (int(a.is_primary_contact - b.is_primary_contact)) or
33 (1 if a.modified - b.modified else 0))
34
35 doc.set_onload('contact_list', contact_list)
Anand Doshi89b8d112015-09-29 20:06:53 +053036
Rushabh Mehta95439db2017-01-14 00:25:22 +053037def set_default_role(doc, method):
38 '''Set customer, supplier, student based on email'''
Rushabh Mehtad14a6c02017-01-17 18:46:37 +053039 if frappe.flags.setting_role:
40 return
Rushabh Mehta95439db2017-01-14 00:25:22 +053041 contact_name = frappe.get_value('Contact', dict(email_id=doc.email))
42 if contact_name:
43 contact = frappe.get_doc('Contact', contact_name)
44 for link in contact.links:
Rushabh Mehtad14a6c02017-01-17 18:46:37 +053045 frappe.flags.setting_role = True
Rushabh Mehta95439db2017-01-14 00:25:22 +053046 if link.link_doctype=='Customer':
47 doc.add_roles('Customer')
48 elif link.link_doctype=='Supplier':
49 doc.add_roles('Supplier')
50 elif frappe.get_value('Student', dict(student_email_id=doc.email)):
51 doc.add_roles('Student')
52
Anand Doshi89b8d112015-09-29 20:06:53 +053053def has_permission(doc, ptype, user):
54 links = get_permitted_and_not_permitted_links(doc.doctype)
55 if not links.get("not_permitted_links"):
56 # optimization: don't determine permissions based on link fields
57 return True
58
59 # True if any one is True or all are empty
60 names = []
61 for df in (links.get("permitted_links") + links.get("not_permitted_links")):
62 doctype = df.options
63 name = doc.get(df.fieldname)
Anand Doshi89b8d112015-09-29 20:06:53 +053064 names.append(name)
65
66 if name and frappe.has_permission(doctype, ptype, doc=name):
67 return True
68
69 if not any(names):
70 return True
Serpent Consulting Services Pvt Ltd8b231f72016-10-18 17:30:24 +053071 return False
Anand Doshi89b8d112015-09-29 20:06:53 +053072
73def get_permission_query_conditions_for_contact(user):
74 return get_permission_query_conditions("Contact")
75
76def get_permission_query_conditions_for_address(user):
77 return get_permission_query_conditions("Address")
78
79def get_permission_query_conditions(doctype):
80 links = get_permitted_and_not_permitted_links(doctype)
81
82 if not links.get("not_permitted_links"):
83 # when everything is permitted, don't add additional condition
84 return ""
Rushabh Mehtab92087c2017-01-13 18:53:11 +053085
Nabin Haitdace2b62015-10-01 13:54:46 +053086 elif not links.get("permitted_links"):
87 conditions = []
Rushabh Mehtab92087c2017-01-13 18:53:11 +053088
Nabin Haitdace2b62015-10-01 13:54:46 +053089 # when everything is not permitted
90 for df in links.get("not_permitted_links"):
91 # like ifnull(customer, '')='' and ifnull(supplier, '')=''
92 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
Rushabh Mehtab92087c2017-01-13 18:53:11 +053093
Nabin Haitdace2b62015-10-01 13:54:46 +053094 return "( " + " and ".join(conditions) + " )"
Anand Doshi89b8d112015-09-29 20:06:53 +053095
96 else:
97 conditions = []
98
99 for df in links.get("permitted_links"):
100 # like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530101 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
Anand Doshi89b8d112015-09-29 20:06:53 +0530102
103 return "( " + " or ".join(conditions) + " )"
104
105def get_permitted_and_not_permitted_links(doctype):
106 permitted_links = []
107 not_permitted_links = []
108
109 meta = frappe.get_meta(doctype)
110
111 for df in meta.get_link_fields():
RobertSchoutenf28a5da2016-06-07 17:03:41 +0800112 if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"):
Anand Doshi89b8d112015-09-29 20:06:53 +0530113 continue
114
115 if frappe.has_permission(df.options):
116 permitted_links.append(df)
117 else:
118 not_permitted_links.append(df)
119
120 return {
121 "permitted_links": permitted_links,
122 "not_permitted_links": not_permitted_links
123 }
Rushabh Mehta95439db2017-01-14 00:25:22 +0530124
125def delete_contact_and_address(doctype, name):
126 for parenttype in ('Contact', 'Address'):
127 items = frappe.db.sql("""select parent from `tabDynamic Link`
Rushabh Mehta8d39fd92017-01-16 13:06:07 +0530128 where parenttype=%s and link_doctype=%s and link_name=%s""",
Rushabh Mehta95439db2017-01-14 00:25:22 +0530129 (parenttype, doctype, name))
130
131 for name in items:
132 doc = frappe.get_doc(parenttype, name)
133 if len(doc.links)==1:
134 doc.delete()