blob: df7b7c528eba8e1889455490bf9b4b87e7c527fc [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Saurabh02875592013-07-08 18:45:55 +05303
4from __future__ import unicode_literals
5import webnotes
6
7def get_filters_cond(doctype, filters, conditions):
8 if filters:
9 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053010 filters = filters.items()
11 flt = []
12 for f in filters:
Anand Doshi17350b82013-08-01 15:45:23 +053013 if isinstance(f[1], basestring) and f[1][0] == '!':
Saurabhf52dc072013-07-10 13:07:49 +053014 flt.append([doctype, f[0], '!=', f[1][1:]])
15 else:
16 flt.append([doctype, f[0], '=', f[1]])
Saurabh02875592013-07-08 18:45:55 +053017
18 from webnotes.widgets.reportview import build_filter_conditions
Saurabh2deca5f2013-07-10 14:31:29 +053019 build_filter_conditions(flt, conditions)
Saurabh02875592013-07-08 18:45:55 +053020 cond = ' and ' + ' and '.join(conditions)
21 else:
22 cond = ''
23 return cond
24
25def get_match_cond(doctype, searchfield = 'name'):
Saurabh02875592013-07-08 18:45:55 +053026 from webnotes.widgets.reportview import build_match_conditions
Anand Doshi7180eb62013-07-30 14:40:44 +053027 cond = build_match_conditions(doctype)
Saurabhf52dc072013-07-10 13:07:49 +053028
Saurabh02875592013-07-08 18:45:55 +053029 if cond:
30 cond = ' and ' + cond
31 else:
32 cond = ''
33 return cond
34
35 # searches for enabled profiles
36def profile_query(doctype, txt, searchfield, start, page_len, filters):
37 return webnotes.conn.sql("""select name, concat_ws(' ', first_name, middle_name, last_name)
Saurabha29b6922013-07-10 16:06:31 +053038 from `tabProfile`
39 where ifnull(enabled, 0)=1
40 and docstatus < 2
41 and name not in ('Administrator', 'Guest')
42 and (%(key)s like "%(txt)s"
43 or concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s")
44 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053045 order by
46 case when name like "%(txt)s" then 0 else 1 end,
47 case when concat_ws(' ', first_name, middle_name, last_name) like "%(txt)s"
48 then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053049 name asc
50 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053051 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
52
53 # searches for active employees
54def employee_query(doctype, txt, searchfield, start, page_len, filters):
55 return webnotes.conn.sql("""select name, employee_name from `tabEmployee`
Saurabha29b6922013-07-10 16:06:31 +053056 where status = 'Active'
57 and docstatus < 2
58 and (%(key)s like "%(txt)s"
59 or employee_name like "%(txt)s")
60 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053061 order by
62 case when name like "%(txt)s" then 0 else 1 end,
63 case when employee_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053064 name
65 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053066 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
67
68 # searches for leads which are not converted
69def lead_query(doctype, txt, searchfield, start, page_len, filters):
70 return webnotes.conn.sql("""select name, lead_name, company_name from `tabLead`
Saurabha29b6922013-07-10 16:06:31 +053071 where docstatus < 2
72 and ifnull(status, '') != 'Converted'
73 and (%(key)s like "%(txt)s"
74 or lead_name like "%(txt)s"
75 or company_name like "%(txt)s")
76 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053077 order by
78 case when name like "%(txt)s" then 0 else 1 end,
79 case when lead_name like "%(txt)s" then 0 else 1 end,
80 case when company_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053081 lead_name asc
82 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +053083 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
84
85 # searches for customer
86def customer_query(doctype, txt, searchfield, start, page_len, filters):
87 cust_master_name = webnotes.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053088
Saurabh02875592013-07-08 18:45:55 +053089 if cust_master_name == "Customer Name":
90 fields = ["name", "customer_group", "territory"]
91 else:
92 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053093
Saurabh02875592013-07-08 18:45:55 +053094 fields = ", ".join(fields)
95
Saurabha29b6922013-07-10 16:06:31 +053096 return webnotes.conn.sql("""select %(field)s from `tabCustomer`
97 where docstatus < 2
98 and (%(key)s like "%(txt)s"
99 or customer_name like "%(txt)s")
100 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530101 order by
102 case when name like "%(txt)s" then 0 else 1 end,
103 case when customer_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530104 name, customer_name
105 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530106 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
107 'start': start, 'page_len': page_len})
108
109# searches for supplier
110def supplier_query(doctype, txt, searchfield, start, page_len, filters):
111 supp_master_name = webnotes.defaults.get_user_default("supp_master_name")
112 if supp_master_name == "Supplier Name":
113 fields = ["name", "supplier_type"]
114 else:
115 fields = ["name", "supplier_name", "supplier_type"]
116 fields = ", ".join(fields)
117
Saurabha29b6922013-07-10 16:06:31 +0530118 return webnotes.conn.sql("""select %(field)s from `tabSupplier`
119 where docstatus < 2
120 and (%(key)s like "%(txt)s"
121 or supplier_name like "%(txt)s")
122 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530123 order by
124 case when name like "%(txt)s" then 0 else 1 end,
125 case when supplier_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530126 name, supplier_name
127 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Saurabh02875592013-07-08 18:45:55 +0530128 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield), 'start': start,
129 'page_len': page_len})
Nabin Hait9a380ef2013-07-16 17:24:17 +0530130
131def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
132 return webnotes.conn.sql("""select name, parent_account, debit_or_credit
133 from tabAccount
134 where tabAccount.docstatus!=2
135 and (account_type in (%s) or
136 (ifnull(is_pl_account, 'No') = 'Yes' and debit_or_credit = %s) )
137 and group_or_ledger = 'Ledger'
138 and company = %s
139 and `%s` LIKE %s
140 limit %s, %s""" %
141 (", ".join(['%s']*len(filters.get("account_type"))),
142 "%s", "%s", searchfield, "%s", "%s", "%s"),
143 tuple(filters.get("account_type") + [filters.get("debit_or_credit"),
144 filters.get("company"), "%%%s%%" % txt, start, page_len]))
Saurabh02875592013-07-08 18:45:55 +0530145
146def item_query(doctype, txt, searchfield, start, page_len, filters):
147 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530148
Saurabh02875592013-07-08 18:45:55 +0530149 return webnotes.conn.sql("""select tabItem.name,
150 if(length(tabItem.item_name) > 40,
151 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
152 if(length(tabItem.description) > 40, \
153 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530154 from tabItem
Anand Doshi9eb28682013-07-29 13:07:30 +0530155 where tabItem.docstatus<2
Saurabha29b6922013-07-10 16:06:31 +0530156 and (tabItem.%(key)s LIKE "%(txt)s"
157 or tabItem.item_name LIKE "%(txt)s")
158 %(fcond)s %(mcond)s
159 limit %(start)s,%(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
Saurabh02875592013-07-08 18:45:55 +0530160 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi9eb28682013-07-29 13:07:30 +0530161 'mcond': get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530162
163def bom(doctype, txt, searchfield, start, page_len, filters):
164 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530165
Saurabh02875592013-07-08 18:45:55 +0530166 return webnotes.conn.sql("""select tabBOM.name, tabBOM.item
167 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530168 where tabBOM.docstatus=1
169 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530170 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530171 %(fcond)s %(mcond)s
172 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
173 'fcond': get_filters_cond(doctype, filters, conditions),
174 'mcond':get_match_cond(doctype, searchfield), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530175
Saurabh02875592013-07-08 18:45:55 +0530176def get_project_name(doctype, txt, searchfield, start, page_len, filters):
177 cond = ''
178 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530179 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530180
Saurabh02875592013-07-08 18:45:55 +0530181 return webnotes.conn.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530182 where `tabProject`.status not in ("Completed", "Cancelled")
183 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
184 order by `tabProject`.name asc
185 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Nabin Haitda46a372013-07-15 17:52:10 +0530186 'mcond':get_match_cond(doctype, searchfield),'start': start, 'page_len': page_len})
187
188def get_price_list_currency(doctype, txt, searchfield, start, page_len, filters):
189 return webnotes.conn.sql("""select ref_currency from `tabItem Price`
190 where price_list_name = %s and buying_or_selling = %s
191 and `%s` like %s order by ref_currency asc limit %s, %s""" %
192 ("%s", "%s", searchfield, "%s", "%s", "%s"),
193 (filters["price_list_name"], filters['buying_or_selling'], "%%%s%%" % txt,
Anand Doshi17350b82013-08-01 15:45:23 +0530194 start, page_len))
195
196def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
197 return webnotes.conn.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
198 from `tabDelivery Note`
199 where `tabDelivery Note`.`%(key)s` like %(txt)s %(fcond)s and
200 (ifnull((select sum(qty) from `tabDelivery Note Item` where
201 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
202 ifnull((select sum(qty) from `tabSales Invoice Item` where
203 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
204 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
205 limit %(start)s, %(page_len)s""" % {
206 "key": searchfield,
207 "fcond": get_filters_cond(doctype, filters, []),
208 "mcond": get_match_cond(doctype),
209 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
210 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) }, debug=True)