blob: 1f8077fc8d6c920fa71b1060ca898236ff448058 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Saurabh02875592013-07-08 18:45:55 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe.widgets.reportview import get_match_cond
Rushabh Mehta2e7ad892014-03-06 12:28:47 +05307from frappe.model.db_query import DatabaseQuery
Saurabh02875592013-07-08 18:45:55 +05308
9def get_filters_cond(doctype, filters, conditions):
10 if filters:
11 if isinstance(filters, dict):
Saurabhf52dc072013-07-10 13:07:49 +053012 filters = filters.items()
13 flt = []
14 for f in filters:
Anand Doshi17350b82013-08-01 15:45:23 +053015 if isinstance(f[1], basestring) and f[1][0] == '!':
Saurabhf52dc072013-07-10 13:07:49 +053016 flt.append([doctype, f[0], '!=', f[1][1:]])
17 else:
18 flt.append([doctype, f[0], '=', f[1]])
Saurabh02875592013-07-08 18:45:55 +053019
Rushabh Mehta2e7ad892014-03-06 12:28:47 +053020 query = DatabaseQuery(doctype)
21 query.filters = flt
22 query.conditions = conditions
23 query.build_filter_conditions()
24
25 cond = ' and ' + ' and '.join(query.conditions)
Saurabh02875592013-07-08 18:45:55 +053026 else:
27 cond = ''
28 return cond
29
Saurabh02875592013-07-08 18:45:55 +053030 # searches for active employees
31def employee_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053032 return frappe.db.sql("""select name, employee_name from `tabEmployee`
Saurabha29b6922013-07-10 16:06:31 +053033 where status = 'Active'
34 and docstatus < 2
35 and (%(key)s like "%(txt)s"
36 or employee_name like "%(txt)s")
37 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053038 order by
39 case when name like "%(txt)s" then 0 else 1 end,
40 case when employee_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053041 name
42 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +053043 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +053044
45 # searches for leads which are not converted
46def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053047 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Saurabha29b6922013-07-10 16:06:31 +053048 where docstatus < 2
49 and ifnull(status, '') != 'Converted'
50 and (%(key)s like "%(txt)s"
51 or lead_name like "%(txt)s"
52 or company_name like "%(txt)s")
53 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053054 order by
55 case when name like "%(txt)s" then 0 else 1 end,
56 case when lead_name like "%(txt)s" then 0 else 1 end,
57 case when company_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053058 lead_name asc
59 limit %(start)s, %(page_len)s""" % {'key': searchfield, 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +053060 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +053061
62 # searches for customer
63def customer_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053064 cust_master_name = frappe.defaults.get_user_default("cust_master_name")
Saurabhf52dc072013-07-10 13:07:49 +053065
Saurabh02875592013-07-08 18:45:55 +053066 if cust_master_name == "Customer Name":
67 fields = ["name", "customer_group", "territory"]
68 else:
69 fields = ["name", "customer_name", "customer_group", "territory"]
Saurabhf52dc072013-07-10 13:07:49 +053070
Saurabh02875592013-07-08 18:45:55 +053071 fields = ", ".join(fields)
72
Anand Doshie9baaa62014-02-26 12:35:33 +053073 return frappe.db.sql("""select %(field)s from `tabCustomer`
Saurabha29b6922013-07-10 16:06:31 +053074 where docstatus < 2
75 and (%(key)s like "%(txt)s"
76 or customer_name like "%(txt)s")
77 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +053078 order by
79 case when name like "%(txt)s" then 0 else 1 end,
80 case when customer_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +053081 name, customer_name
82 limit %(start)s, %(page_len)s""" % {'field': fields,'key': searchfield,
Rushabh Mehta45418752014-03-06 11:18:37 +053083 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Saurabh02875592013-07-08 18:45:55 +053084 'start': start, 'page_len': page_len})
85
86# searches for supplier
87def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053088 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Saurabh02875592013-07-08 18:45:55 +053089 if supp_master_name == "Supplier Name":
90 fields = ["name", "supplier_type"]
91 else:
92 fields = ["name", "supplier_name", "supplier_type"]
93 fields = ", ".join(fields)
94
Anand Doshie9baaa62014-02-26 12:35:33 +053095 return frappe.db.sql("""select %(field)s from `tabSupplier`
Saurabha29b6922013-07-10 16:06:31 +053096 where docstatus < 2
97 and (%(key)s like "%(txt)s"
98 or supplier_name like "%(txt)s")
99 %(mcond)s
Saurabh02875592013-07-08 18:45:55 +0530100 order by
101 case when name like "%(txt)s" then 0 else 1 end,
102 case when supplier_name like "%(txt)s" then 0 else 1 end,
Saurabha29b6922013-07-10 16:06:31 +0530103 name, supplier_name
104 limit %(start)s, %(page_len)s """ % {'field': fields,'key': searchfield,
Rushabh Mehta45418752014-03-06 11:18:37 +0530105 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype), 'start': start,
Saurabh02875592013-07-08 18:45:55 +0530106 'page_len': page_len})
Nabin Hait9a380ef2013-07-16 17:24:17 +0530107
108def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530109 return frappe.db.sql("""select name, parent_account, debit_or_credit
Nabin Hait9a380ef2013-07-16 17:24:17 +0530110 from tabAccount
111 where tabAccount.docstatus!=2
112 and (account_type in (%s) or
113 (ifnull(is_pl_account, 'No') = 'Yes' and debit_or_credit = %s) )
114 and group_or_ledger = 'Ledger'
115 and company = %s
116 and `%s` LIKE %s
117 limit %s, %s""" %
118 (", ".join(['%s']*len(filters.get("account_type"))),
119 "%s", "%s", searchfield, "%s", "%s", "%s"),
120 tuple(filters.get("account_type") + [filters.get("debit_or_credit"),
121 filters.get("company"), "%%%s%%" % txt, start, page_len]))
Saurabh02875592013-07-08 18:45:55 +0530122
123def item_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530124 from frappe.utils import nowdate
Anand Doshi22c0d782013-11-04 16:23:04 +0530125
Saurabh02875592013-07-08 18:45:55 +0530126 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530127
Anand Doshie9baaa62014-02-26 12:35:33 +0530128 return frappe.db.sql("""select tabItem.name,
Saurabh02875592013-07-08 18:45:55 +0530129 if(length(tabItem.item_name) > 40,
130 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
131 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530132 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530133 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530134 where tabItem.docstatus < 2
135 and (ifnull(tabItem.end_of_life, '') = '' or tabItem.end_of_life > %(today)s)
136 and (tabItem.`{key}` LIKE %(txt)s
137 or tabItem.item_name LIKE %(txt)s)
138 {fcond} {mcond}
139 limit %(start)s, %(page_len)s """.format(key=searchfield,
140 fcond=get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530141 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530142 {
143 "today": nowdate(),
144 "txt": "%%%s%%" % txt,
145 "start": start,
146 "page_len": page_len
147 })
Saurabh02875592013-07-08 18:45:55 +0530148
149def bom(doctype, txt, searchfield, start, page_len, filters):
150 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530151
Anand Doshie9baaa62014-02-26 12:35:33 +0530152 return frappe.db.sql("""select tabBOM.name, tabBOM.item
Saurabh02875592013-07-08 18:45:55 +0530153 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530154 where tabBOM.docstatus=1
155 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530156 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530157 %(fcond)s %(mcond)s
158 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
159 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530160 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530161
Saurabh02875592013-07-08 18:45:55 +0530162def get_project_name(doctype, txt, searchfield, start, page_len, filters):
163 cond = ''
164 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530165 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530166
Anand Doshie9baaa62014-02-26 12:35:33 +0530167 return frappe.db.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530168 where `tabProject`.status not in ("Completed", "Cancelled")
169 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
170 order by `tabProject`.name asc
171 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530172 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
Anand Doshi17350b82013-08-01 15:45:23 +0530173
174def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530175 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshi17350b82013-08-01 15:45:23 +0530176 from `tabDelivery Note`
Anand Doshi1dadf352013-09-03 16:21:01 +0530177 where `tabDelivery Note`.`%(key)s` like %(txt)s and
178 `tabDelivery Note`.docstatus = 1 %(fcond)s and
Anand Doshi17350b82013-08-01 15:45:23 +0530179 (ifnull((select sum(qty) from `tabDelivery Note Item` where
180 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
181 ifnull((select sum(qty) from `tabSales Invoice Item` where
Anand Doshi1dadf352013-09-03 16:21:01 +0530182 `tabSales Invoice Item`.docstatus = 1 and
Anand Doshi17350b82013-08-01 15:45:23 +0530183 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
184 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
185 limit %(start)s, %(page_len)s""" % {
186 "key": searchfield,
187 "fcond": get_filters_cond(doctype, filters, []),
188 "mcond": get_match_cond(doctype),
189 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530190 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
191
192def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530193 from erpnext.controllers.queries import get_match_cond
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530194
195 if filters.has_key('warehouse'):
Anand Doshie9baaa62014-02-26 12:35:33 +0530196 return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530197 where item_code = '%(item_code)s'
198 and warehouse = '%(warehouse)s'
199 and batch_no like '%(txt)s'
200 and exists(select * from `tabBatch`
201 where name = sle.batch_no
202 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
203 and docstatus != 2)
204 %(mcond)s
205 group by batch_no having sum(actual_qty) > 0
206 order by batch_no desc
207 limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
208 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
Rushabh Mehta45418752014-03-06 11:18:37 +0530209 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530210 'start': start, 'page_len': page_len})
211 else:
Anand Doshie9baaa62014-02-26 12:35:33 +0530212 return frappe.db.sql("""select name from tabBatch
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530213 where docstatus != 2
214 and item = '%(item_code)s'
215 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
216 and name like '%(txt)s'
217 %(mcond)s
218 order by name desc
219 limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
220 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530221 'mcond':get_match_cond(doctype),'start': start,
Anand Doshi1788c8b2013-11-05 12:10:08 +0530222 'page_len': page_len})