blob: eaa71f9b1fc5c72144ef6caae6785be0b5886ea8 [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'''
39 contact_name = frappe.get_value('Contact', dict(email_id=doc.email))
40 if contact_name:
41 contact = frappe.get_doc('Contact', contact_name)
42 for link in contact.links:
43 if link.link_doctype=='Customer':
44 doc.add_roles('Customer')
45 elif link.link_doctype=='Supplier':
46 doc.add_roles('Supplier')
47 elif frappe.get_value('Student', dict(student_email_id=doc.email)):
48 doc.add_roles('Student')
49
Anand Doshi89b8d112015-09-29 20:06:53 +053050def has_permission(doc, ptype, user):
51 links = get_permitted_and_not_permitted_links(doc.doctype)
52 if not links.get("not_permitted_links"):
53 # optimization: don't determine permissions based on link fields
54 return True
55
56 # True if any one is True or all are empty
57 names = []
58 for df in (links.get("permitted_links") + links.get("not_permitted_links")):
59 doctype = df.options
60 name = doc.get(df.fieldname)
Anand Doshi89b8d112015-09-29 20:06:53 +053061 names.append(name)
62
63 if name and frappe.has_permission(doctype, ptype, doc=name):
64 return True
65
66 if not any(names):
67 return True
Serpent Consulting Services Pvt Ltd8b231f72016-10-18 17:30:24 +053068 return False
Anand Doshi89b8d112015-09-29 20:06:53 +053069
70def get_permission_query_conditions_for_contact(user):
71 return get_permission_query_conditions("Contact")
72
73def get_permission_query_conditions_for_address(user):
74 return get_permission_query_conditions("Address")
75
76def get_permission_query_conditions(doctype):
77 links = get_permitted_and_not_permitted_links(doctype)
78
79 if not links.get("not_permitted_links"):
80 # when everything is permitted, don't add additional condition
81 return ""
Rushabh Mehtab92087c2017-01-13 18:53:11 +053082
Nabin Haitdace2b62015-10-01 13:54:46 +053083 elif not links.get("permitted_links"):
84 conditions = []
Rushabh Mehtab92087c2017-01-13 18:53:11 +053085
Nabin Haitdace2b62015-10-01 13:54:46 +053086 # when everything is not permitted
87 for df in links.get("not_permitted_links"):
88 # like ifnull(customer, '')='' and ifnull(supplier, '')=''
89 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
Rushabh Mehtab92087c2017-01-13 18:53:11 +053090
Nabin Haitdace2b62015-10-01 13:54:46 +053091 return "( " + " and ".join(conditions) + " )"
Anand Doshi89b8d112015-09-29 20:06:53 +053092
93 else:
94 conditions = []
95
96 for df in links.get("permitted_links"):
97 # like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
Rushabh Mehtab92087c2017-01-13 18:53:11 +053098 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
Anand Doshi89b8d112015-09-29 20:06:53 +053099
100 return "( " + " or ".join(conditions) + " )"
101
102def get_permitted_and_not_permitted_links(doctype):
103 permitted_links = []
104 not_permitted_links = []
105
106 meta = frappe.get_meta(doctype)
107
108 for df in meta.get_link_fields():
RobertSchoutenf28a5da2016-06-07 17:03:41 +0800109 if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"):
Anand Doshi89b8d112015-09-29 20:06:53 +0530110 continue
111
112 if frappe.has_permission(df.options):
113 permitted_links.append(df)
114 else:
115 not_permitted_links.append(df)
116
117 return {
118 "permitted_links": permitted_links,
119 "not_permitted_links": not_permitted_links
120 }
Rushabh Mehta95439db2017-01-14 00:25:22 +0530121
122def delete_contact_and_address(doctype, name):
123 for parenttype in ('Contact', 'Address'):
124 items = frappe.db.sql("""select parent from `tabDynamic Link`
125 where parenttype=%s and link_type=%s and link_name=%s""",
126 (parenttype, doctype, name))
127
128 for name in items:
129 doc = frappe.get_doc(parenttype, name)
130 if len(doc.links)==1:
131 doc.delete()