blob: e8cdc80f93f25bcd61741edf5f0d8cbd0593ba1d [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):
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530109 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Nabin Hait9a380ef2013-07-16 17:24:17 +0530110 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530111 and account_type in (%s)
Nabin Hait9a380ef2013-07-16 17:24:17 +0530112 and group_or_ledger = 'Ledger'
113 and company = %s
114 and `%s` LIKE %s
115 limit %s, %s""" %
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530116 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
117 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
118 start, page_len]))
119 if not tax_accounts:
120 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
121 where tabAccount.docstatus!=2 and group_or_ledger = 'Ledger'
122 and company = %s and `%s` LIKE %s limit %s, %s"""
123 % ("%s", searchfield, "%s", "%s", "%s"),
124 (filters.get("company"), "%%%s%%" % txt, start, page_len))
125
126 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530127
128def item_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530129 from frappe.utils import nowdate
Anand Doshi22c0d782013-11-04 16:23:04 +0530130
Saurabh02875592013-07-08 18:45:55 +0530131 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530132
Anand Doshie9baaa62014-02-26 12:35:33 +0530133 return frappe.db.sql("""select tabItem.name,
Saurabh02875592013-07-08 18:45:55 +0530134 if(length(tabItem.item_name) > 40,
135 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
136 if(length(tabItem.description) > 40, \
Anand Doshi22c0d782013-11-04 16:23:04 +0530137 concat(substr(tabItem.description, 1, 40), "..."), description) as decription
Saurabha29b6922013-07-10 16:06:31 +0530138 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530139 where tabItem.docstatus < 2
140 and (ifnull(tabItem.end_of_life, '') = '' or tabItem.end_of_life > %(today)s)
141 and (tabItem.`{key}` LIKE %(txt)s
142 or tabItem.item_name LIKE %(txt)s)
143 {fcond} {mcond}
144 limit %(start)s, %(page_len)s """.format(key=searchfield,
145 fcond=get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530146 mcond=get_match_cond(doctype)),
Anand Doshi22c0d782013-11-04 16:23:04 +0530147 {
148 "today": nowdate(),
149 "txt": "%%%s%%" % txt,
150 "start": start,
151 "page_len": page_len
152 })
Saurabh02875592013-07-08 18:45:55 +0530153
154def bom(doctype, txt, searchfield, start, page_len, filters):
155 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530156
Anand Doshie9baaa62014-02-26 12:35:33 +0530157 return frappe.db.sql("""select tabBOM.name, tabBOM.item
Saurabh02875592013-07-08 18:45:55 +0530158 from tabBOM
Saurabha29b6922013-07-10 16:06:31 +0530159 where tabBOM.docstatus=1
160 and tabBOM.is_active=1
Nabin Haitcfecd2b2013-07-11 17:49:18 +0530161 and tabBOM.%(key)s like "%(txt)s"
Saurabha29b6922013-07-10 16:06:31 +0530162 %(fcond)s %(mcond)s
163 limit %(start)s, %(page_len)s """ % {'key': searchfield, 'txt': "%%%s%%" % txt,
164 'fcond': get_filters_cond(doctype, filters, conditions),
Rushabh Mehta45418752014-03-06 11:18:37 +0530165 'mcond':get_match_cond(doctype), 'start': start, 'page_len': page_len})
Saurabh02875592013-07-08 18:45:55 +0530166
Saurabh02875592013-07-08 18:45:55 +0530167def get_project_name(doctype, txt, searchfield, start, page_len, filters):
168 cond = ''
169 if filters['customer']:
Saurabhab462d22013-07-09 16:18:52 +0530170 cond = '(`tabProject`.customer = "' + filters['customer'] + '" or ifnull(`tabProject`.customer,"")="") and'
Saurabha29b6922013-07-10 16:06:31 +0530171
Anand Doshie9baaa62014-02-26 12:35:33 +0530172 return frappe.db.sql("""select `tabProject`.name from `tabProject`
Saurabha29b6922013-07-10 16:06:31 +0530173 where `tabProject`.status not in ("Completed", "Cancelled")
174 and %(cond)s `tabProject`.name like "%(txt)s" %(mcond)s
175 order by `tabProject`.name asc
176 limit %(start)s, %(page_len)s """ % {'cond': cond,'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530177 'mcond':get_match_cond(doctype),'start': start, 'page_len': page_len})
Anand Doshi17350b82013-08-01 15:45:23 +0530178
179def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +0530180 return frappe.db.sql("""select `tabDelivery Note`.name, `tabDelivery Note`.customer_name
Anand Doshi17350b82013-08-01 15:45:23 +0530181 from `tabDelivery Note`
Anand Doshi1dadf352013-09-03 16:21:01 +0530182 where `tabDelivery Note`.`%(key)s` like %(txt)s and
183 `tabDelivery Note`.docstatus = 1 %(fcond)s and
Anand Doshi17350b82013-08-01 15:45:23 +0530184 (ifnull((select sum(qty) from `tabDelivery Note Item` where
185 `tabDelivery Note Item`.parent=`tabDelivery Note`.name), 0) >
186 ifnull((select sum(qty) from `tabSales Invoice Item` where
Anand Doshi1dadf352013-09-03 16:21:01 +0530187 `tabSales Invoice Item`.docstatus = 1 and
Anand Doshi17350b82013-08-01 15:45:23 +0530188 `tabSales Invoice Item`.delivery_note=`tabDelivery Note`.name), 0))
189 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc
190 limit %(start)s, %(page_len)s""" % {
191 "key": searchfield,
192 "fcond": get_filters_cond(doctype, filters, []),
193 "mcond": get_match_cond(doctype),
194 "start": "%(start)s", "page_len": "%(page_len)s", "txt": "%(txt)s"
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530195 }, { "start": start, "page_len": page_len, "txt": ("%%%s%%" % txt) })
196
197def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530198 from erpnext.controllers.queries import get_match_cond
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530199
200 if filters.has_key('warehouse'):
Anand Doshie9baaa62014-02-26 12:35:33 +0530201 return frappe.db.sql("""select batch_no from `tabStock Ledger Entry` sle
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530202 where item_code = '%(item_code)s'
203 and warehouse = '%(warehouse)s'
204 and batch_no like '%(txt)s'
205 and exists(select * from `tabBatch`
206 where name = sle.batch_no
207 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
208 and docstatus != 2)
209 %(mcond)s
210 group by batch_no having sum(actual_qty) > 0
211 order by batch_no desc
212 limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
213 'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
Rushabh Mehta45418752014-03-06 11:18:37 +0530214 'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530215 'start': start, 'page_len': page_len})
216 else:
Anand Doshie9baaa62014-02-26 12:35:33 +0530217 return frappe.db.sql("""select name from tabBatch
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530218 where docstatus != 2
219 and item = '%(item_code)s'
220 and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
221 and name like '%(txt)s'
222 %(mcond)s
223 order by name desc
224 limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
225 'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
Rushabh Mehta45418752014-03-06 11:18:37 +0530226 'mcond':get_match_cond(doctype),'start': start,
Anand Doshi1788c8b2013-11-05 12:10:08 +0530227 'page_len': page_len})