blob: 7b4a4c92ad1ade379d5f0b2221cd68339ec1208e [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe 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
Rushabh Mehtab92087c2017-01-13 18:53:11 +05306from frappe.desk.reportview import get_match_cond, get_filters_cond
Nabin Hait2ed71ba2015-03-20 15:06:30 +05307from frappe.utils import nowdate
suyashphadtare750a0672017-01-18 15:35:01 +05308from collections import defaultdict
Saurabh02875592013-07-08 18:45:55 +05309
Saurabh02875592013-07-08 18:45:55 +053010
Saurabh02875592013-07-08 18:45:55 +053011 # searches for active employees
12def employee_query(doctype, txt, searchfield, start, page_len, filters):
Kanchan Chauhan7652b852016-11-16 15:29:01 +053013 conditions = []
Anand Doshibd67e872014-04-11 16:51:27 +053014 return frappe.db.sql("""select name, employee_name from `tabEmployee`
15 where status = 'Active'
16 and docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +053017 and ({key} like %(txt)s
18 or employee_name like %(txt)s)
Kanchan Chauhan7652b852016-11-16 15:29:01 +053019 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053020 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053021 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
22 if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053023 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053024 name, employee_name
Anand Doshi48d3b542014-07-09 13:15:03 +053025 limit %(start)s, %(page_len)s""".format(**{
26 'key': searchfield,
Kanchan Chauhan7652b852016-11-16 15:29:01 +053027 'fcond': get_filters_cond(doctype, filters, conditions),
Anand Doshi48d3b542014-07-09 13:15:03 +053028 'mcond': get_match_cond(doctype)
29 }), {
30 'txt': "%%%s%%" % txt,
31 '_txt': txt.replace("%", ""),
32 'start': start,
33 'page_len': page_len
34 })
Saurabh02875592013-07-08 18:45:55 +053035
36 # searches for leads which are not converted
Anand Doshibd67e872014-04-11 16:51:27 +053037def lead_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshie9baaa62014-02-26 12:35:33 +053038 return frappe.db.sql("""select name, lead_name, company_name from `tabLead`
Anand Doshibd67e872014-04-11 16:51:27 +053039 where docstatus < 2
40 and ifnull(status, '') != 'Converted'
Anand Doshi48d3b542014-07-09 13:15:03 +053041 and ({key} like %(txt)s
42 or lead_name like %(txt)s
43 or company_name like %(txt)s)
44 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053045 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053046 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
47 if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999),
48 if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053049 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +053050 name, lead_name
Anand Doshi48d3b542014-07-09 13:15:03 +053051 limit %(start)s, %(page_len)s""".format(**{
52 'key': searchfield,
53 'mcond':get_match_cond(doctype)
54 }), {
55 'txt': "%%%s%%" % txt,
56 '_txt': txt.replace("%", ""),
57 'start': start,
58 'page_len': page_len
59 })
Saurabh02875592013-07-08 18:45:55 +053060
61 # searches for customer
62def customer_query(doctype, txt, searchfield, start, page_len, filters):
KanchanChauhan4b888b92017-07-25 14:03:01 +053063 conditions = []
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"]
Rushabh Mehtab92087c2017-01-13 18:53:11 +053070
Maxwell Morais35572612016-07-21 23:42:59 -030071 meta = frappe.get_meta("Customer")
Console Admin86231662017-06-23 20:32:52 +030072 searchfields = meta.get_search_fields()
73 searchfields = searchfields + [f for f in [searchfield or "name", "customer_name"] \
74 if not f in searchfields]
75 fields = fields + [f for f in searchfields if not f in fields]
Saurabhf52dc072013-07-10 13:07:49 +053076
Anand Doshibd67e872014-04-11 16:51:27 +053077 fields = ", ".join(fields)
Console Admin86231662017-06-23 20:32:52 +030078 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
Saurabh02875592013-07-08 18:45:55 +053079
Anand Doshi48d3b542014-07-09 13:15:03 +053080 return frappe.db.sql("""select {fields} from `tabCustomer`
Anand Doshibd67e872014-04-11 16:51:27 +053081 where docstatus < 2
Console Admin86231662017-06-23 20:32:52 +030082 and ({scond}) and disabled=0
KanchanChauhan4b888b92017-07-25 14:03:01 +053083 {fcond} {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +053084 order by
Anand Doshi48d3b542014-07-09 13:15:03 +053085 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
86 if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +053087 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +053088 name, customer_name
Anand Doshi48d3b542014-07-09 13:15:03 +053089 limit %(start)s, %(page_len)s""".format(**{
90 "fields": fields,
Console Admin86231662017-06-23 20:32:52 +030091 "scond": searchfields,
KanchanChauhan4b888b92017-07-25 14:03:01 +053092 "mcond": get_match_cond(doctype),
93 "fcond": get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
Anand Doshi48d3b542014-07-09 13:15:03 +053094 }), {
95 'txt': "%%%s%%" % txt,
96 '_txt': txt.replace("%", ""),
97 'start': start,
98 'page_len': page_len
99 })
Saurabh02875592013-07-08 18:45:55 +0530100
101# searches for supplier
102def supplier_query(doctype, txt, searchfield, start, page_len, filters):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530103 supp_master_name = frappe.defaults.get_user_default("supp_master_name")
Anand Doshibd67e872014-04-11 16:51:27 +0530104 if supp_master_name == "Supplier Name":
Zlash652e080982018-04-19 18:37:53 +0530105 fields = ["name", "supplier_group"]
Anand Doshibd67e872014-04-11 16:51:27 +0530106 else:
Zlash652e080982018-04-19 18:37:53 +0530107 fields = ["name", "supplier_name", "supplier_group"]
Anand Doshibd67e872014-04-11 16:51:27 +0530108 fields = ", ".join(fields)
Saurabh02875592013-07-08 18:45:55 +0530109
Anand Doshi48d3b542014-07-09 13:15:03 +0530110 return frappe.db.sql("""select {field} from `tabSupplier`
Anand Doshibd67e872014-04-11 16:51:27 +0530111 where docstatus < 2
Anand Doshi48d3b542014-07-09 13:15:03 +0530112 and ({key} like %(txt)s
shreyas29b565f2016-01-25 17:30:49 +0530113 or supplier_name like %(txt)s) and disabled=0
Anand Doshi48d3b542014-07-09 13:15:03 +0530114 {mcond}
Anand Doshibd67e872014-04-11 16:51:27 +0530115 order by
Anand Doshi48d3b542014-07-09 13:15:03 +0530116 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
117 if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530118 idx desc,
Anand Doshibd67e872014-04-11 16:51:27 +0530119 name, supplier_name
Anand Doshi48d3b542014-07-09 13:15:03 +0530120 limit %(start)s, %(page_len)s """.format(**{
121 'field': fields,
122 'key': searchfield,
123 'mcond':get_match_cond(doctype)
124 }), {
125 'txt': "%%%s%%" % txt,
126 '_txt': txt.replace("%", ""),
127 'start': start,
128 'page_len': page_len
129 })
Anand Doshibd67e872014-04-11 16:51:27 +0530130
Nabin Hait9a380ef2013-07-16 17:24:17 +0530131def tax_account_query(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530132 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
133 where tabAccount.docstatus!=2
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530134 and account_type in (%s)
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530135 and is_group = 0
Nabin Hait9a380ef2013-07-16 17:24:17 +0530136 and company = %s
137 and `%s` LIKE %s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530138 order by idx desc, name
Anand Doshibd67e872014-04-11 16:51:27 +0530139 limit %s, %s""" %
140 (", ".join(['%s']*len(filters.get("account_type"))), "%s", searchfield, "%s", "%s", "%s"),
141 tuple(filters.get("account_type") + [filters.get("company"), "%%%s%%" % txt,
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530142 start, page_len]))
143 if not tax_accounts:
Anand Doshibd67e872014-04-11 16:51:27 +0530144 tax_accounts = frappe.db.sql("""select name, parent_account from tabAccount
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530145 where tabAccount.docstatus!=2 and is_group = 0
Anand Doshibd67e872014-04-11 16:51:27 +0530146 and company = %s and `%s` LIKE %s limit %s, %s"""
147 % ("%s", searchfield, "%s", "%s", "%s"),
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530148 (filters.get("company"), "%%%s%%" % txt, start, page_len))
Anand Doshibd67e872014-04-11 16:51:27 +0530149
Nabin Hait0c21e2a2014-03-21 11:14:49 +0530150 return tax_accounts
Saurabh02875592013-07-08 18:45:55 +0530151
Rushabh Mehta203cc962016-04-07 15:25:43 +0530152def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
Saurabh02875592013-07-08 18:45:55 +0530153 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530154
marination3dbef9d2019-10-28 15:48:10 +0530155 #Get searchfields from meta and use in Item Link field query
marination1e754b12019-10-30 18:33:44 +0530156 meta = frappe.get_meta("Item", cached=True)
marination3dbef9d2019-10-28 15:48:10 +0530157 searchfields = meta.get_search_fields()
158
marination1e754b12019-10-30 18:33:44 +0530159 if "description" in searchfields:
160 searchfields.remove("description")
marination3dbef9d2019-10-28 15:48:10 +0530161
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530162 columns = ''
163 extra_searchfields = [field for field in searchfields
164 if not field in ["name", "item_group", "description"]]
165
166 if extra_searchfields:
167 columns = ", " + ", ".join(extra_searchfields)
marination1e754b12019-10-30 18:33:44 +0530168
169 searchfields = searchfields + [field for field in[searchfield or "name", "item_code", "item_group", "item_name"]
170 if not field in searchfields]
marination3dbef9d2019-10-28 15:48:10 +0530171 searchfields = " or ".join([field + " like %(txt)s" for field in searchfields])
172
Rushabh Mehtad5f9ebd2018-04-02 23:37:33 +0530173 description_cond = ''
174 if frappe.db.count('Item', cache=True) < 50000:
175 # scan description only if items are less than 50000
176 description_cond = 'or tabItem.description LIKE %(txt)s'
177
Prateeksha Singh984a7a72018-05-17 17:29:36 +0530178 return frappe.db.sql("""select tabItem.name,
Anand Doshibd67e872014-04-11 16:51:27 +0530179 if(length(tabItem.item_name) > 40,
180 concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name,
Prateeksha Singh984a7a72018-05-17 17:29:36 +0530181 tabItem.item_group,
Saurabh02875592013-07-08 18:45:55 +0530182 if(length(tabItem.description) > 40, \
Rohit Waghchaurec42312e2019-11-19 19:05:23 +0530183 concat(substr(tabItem.description, 1, 40), "..."), description) as description
marination1e754b12019-10-30 18:33:44 +0530184 {columns}
Anand Doshibd67e872014-04-11 16:51:27 +0530185 from tabItem
Anand Doshi22c0d782013-11-04 16:23:04 +0530186 where tabItem.docstatus < 2
Anand Doshi602e8252015-11-16 19:05:46 +0530187 and tabItem.has_variants=0
Anand Doshi21e09a22015-10-29 12:21:41 +0530188 and tabItem.disabled=0
Rushabh Mehta864d1ea2014-06-23 12:20:12 +0530189 and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00')
marination3dbef9d2019-10-28 15:48:10 +0530190 and ({scond} or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s)
Rohit Waghchaure2bfb0632019-03-02 21:47:55 +0530191 {description_cond})
Anand Doshi22c0d782013-11-04 16:23:04 +0530192 {fcond} {mcond}
Anand Doshi652bc072014-04-16 15:21:46 +0530193 order by
194 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
195 if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999),
Rushabh Mehta3574b372016-03-11 14:33:04 +0530196 idx desc,
Anand Doshi652bc072014-04-16 15:21:46 +0530197 name, item_name
Rushabh Mehtabc4e2cd2017-10-17 12:30:34 +0530198 limit %(start)s, %(page_len)s """.format(
199 key=searchfield,
marination1e754b12019-10-30 18:33:44 +0530200 columns=columns,
marination3dbef9d2019-10-28 15:48:10 +0530201 scond=searchfields,
Nabin Haitc6285062016-03-30 13:10:25 +0530202 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
Rushabh Mehtad5f9ebd2018-04-02 23:37:33 +0530203 mcond=get_match_cond(doctype).replace('%', '%%'),
204 description_cond = description_cond),
Anand Doshi22c0d782013-11-04 16:23:04 +0530205 {
206 "today": nowdate(),
207 "txt": "%%%s%%" % txt,
Anand Doshi652bc072014-04-16 15:21:46 +0530208 "_txt": txt.replace("%", ""),
Anand Doshi22c0d782013-11-04 16:23:04 +0530209 "start": start,
210 "page_len": page_len
Rushabh Mehta203cc962016-04-07 15:25:43 +0530211 }, as_dict=as_dict)
Saurabh02875592013-07-08 18:45:55 +0530212
Saurabh022ab632017-11-10 15:06:02 +0530213def bom(doctype, txt, searchfield, start, page_len, filters):
Anand Doshibd67e872014-04-11 16:51:27 +0530214 conditions = []
Saurabhf52dc072013-07-10 13:07:49 +0530215
Anand Doshibd67e872014-04-11 16:51:27 +0530216 return frappe.db.sql("""select tabBOM.name, tabBOM.item
217 from tabBOM
218 where tabBOM.docstatus=1
219 and tabBOM.is_active=1
Nabin Hait62211172016-03-16 16:22:03 +0530220 and tabBOM.`{key}` like %(txt)s
221 {fcond} {mcond}
222 order by
Rushabh Mehta3574b372016-03-11 14:33:04 +0530223 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
224 idx desc, name
Nabin Hait62211172016-03-16 16:22:03 +0530225 limit %(start)s, %(page_len)s """.format(
Mangesh-Khairnar6a796912019-07-08 10:40:40 +0530226 fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'),
Karthikeyan S747c2622019-07-19 22:49:21 +0530227 mcond=get_match_cond(doctype).replace('%', '%%'),
228 key=searchfield),
Mangesh-Khairnar6a796912019-07-08 10:40:40 +0530229 {
Karthikeyan S747c2622019-07-19 22:49:21 +0530230 'txt': '%' + txt + '%',
Rushabh Mehta3574b372016-03-11 14:33:04 +0530231 '_txt': txt.replace("%", ""),
Saurabh022ab632017-11-10 15:06:02 +0530232 'start': start or 0,
233 'page_len': page_len or 20
Rushabh Mehta3574b372016-03-11 14:33:04 +0530234 })
Saurabh02875592013-07-08 18:45:55 +0530235
Saurabh02875592013-07-08 18:45:55 +0530236def get_project_name(doctype, txt, searchfield, start, page_len, filters):
237 cond = ''
Nabin Haitf71011a2014-08-21 11:34:31 +0530238 if filters.get('customer'):
Suraj Shetty6ea3de92018-09-26 18:15:53 +0530239 cond = """(`tabProject`.customer = %s or
rohitwaghchauree3304722018-08-27 11:43:57 +0530240 ifnull(`tabProject`.customer,"")="") and""" %(frappe.db.escape(filters.get("customer")))
Anand Doshibd67e872014-04-11 16:51:27 +0530241
242 return frappe.db.sql("""select `tabProject`.name from `tabProject`
243 where `tabProject`.status not in ("Completed", "Cancelled")
Rushabh Mehta3574b372016-03-11 14:33:04 +0530244 and {cond} `tabProject`.name like %(txt)s {match_cond}
245 order by
246 if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
247 idx desc,
248 `tabProject`.name asc
249 limit {start}, {page_len}""".format(
250 cond=cond,
251 match_cond=get_match_cond(doctype),
252 start=start,
253 page_len=page_len), {
254 "txt": "%{0}%".format(txt),
Nabin Haitdf4deba2016-03-16 11:16:31 +0530255 "_txt": txt.replace('%', '')
Rushabh Mehta3574b372016-03-11 14:33:04 +0530256 })
Anand Doshibd67e872014-04-11 16:51:27 +0530257
tundebabzyf6d738b2017-09-18 12:40:09 +0100258
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530259def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict):
260 return frappe.db.sql("""
261 select `tabDelivery Note`.name, `tabDelivery Note`.customer, `tabDelivery Note`.posting_date
Anand Doshibd67e872014-04-11 16:51:27 +0530262 from `tabDelivery Note`
263 where `tabDelivery Note`.`%(key)s` like %(txt)s and
tundebabzyf6d738b2017-09-18 12:40:09 +0100264 `tabDelivery Note`.docstatus = 1
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530265 and status not in ("Stopped", "Closed") %(fcond)s
tundebabzyf6d738b2017-09-18 12:40:09 +0100266 and (
267 (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100)
268 or `tabDelivery Note`.grand_total = 0
269 or (
270 `tabDelivery Note`.is_return = 1
271 and return_against in (select name from `tabDelivery Note` where per_billed < 100)
272 )
273 )
rohitwaghchaured07a3e12019-05-15 07:46:28 +0530274 %(mcond)s order by `tabDelivery Note`.`%(key)s` asc limit %(start)s, %(page_len)s
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530275 """ % {
276 "key": searchfield,
277 "fcond": get_filters_cond(doctype, filters, []),
278 "mcond": get_match_cond(doctype),
rohitwaghchaured07a3e12019-05-15 07:46:28 +0530279 "start": start,
280 "page_len": page_len,
Nabin Hait1e2d7b32017-05-17 13:52:21 +0530281 "txt": "%(txt)s"
tundebabzyf6d738b2017-09-18 12:40:09 +0100282 }, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict)
283
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530284
285def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530286 cond = ""
287 if filters.get("posting_date"):
Nabin Hait7918b922018-01-31 15:30:03 +0530288 cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)"
Rushabh Mehtab6398be2015-08-26 10:50:16 +0530289
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530290 batch_nos = None
291 args = {
292 'item_code': filters.get("item_code"),
293 'warehouse': filters.get("warehouse"),
294 'posting_date': filters.get('posting_date'),
Anand Doshi0dc79f42015-04-06 12:59:34 +0530295 'txt': "%{0}%".format(txt),
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530296 "start": start,
297 "page_len": page_len
298 }
299
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530300 having_clause = "having sum(sle.actual_qty) > 0"
301 if filters.get("is_return"):
302 having_clause = ""
303
Anand Doshi0dc79f42015-04-06 12:59:34 +0530304 if args.get('warehouse'):
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +0530305 batch_nos = frappe.db.sql("""select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom,
306 concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date)
307 from `tabStock Ledger Entry` sle
308 INNER JOIN `tabBatch` batch on sle.batch_no = batch.name
309 where
310 batch.disabled = 0
311 and sle.item_code = %(item_code)s
312 and sle.warehouse = %(warehouse)s
313 and (sle.batch_no like %(txt)s
314 or batch.manufacturing_date like %(txt)s)
315 and batch.docstatus < 2
316 {cond}
317 {match_conditions}
318 group by batch_no {having_clause}
319 order by batch.expiry_date, sle.batch_no desc
320 limit %(start)s, %(page_len)s""".format(
321 cond=cond,
322 match_conditions=get_match_cond(doctype),
323 having_clause = having_clause
324 ), args)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530325
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530326 return batch_nos
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530327 else:
sivankar621740e2018-02-12 14:33:40 +0530328 return frappe.db.sql("""select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date) from `tabBatch` batch
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800329 where batch.disabled = 0
330 and item = %(item_code)s
sivankar621740e2018-02-12 14:33:40 +0530331 and (name like %(txt)s
332 or manufacturing_date like %(txt)s)
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530333 and docstatus < 2
Neil Trini Lasradoebb60f52015-07-08 14:36:09 +0530334 {0}
Anand Doshi0dc79f42015-04-06 12:59:34 +0530335 {match_conditions}
336 order by expiry_date, name desc
Nabin Haite52ee552015-09-02 10:55:32 +0530337 limit %(start)s, %(page_len)s""".format(cond, match_conditions=get_match_cond(doctype)), args)
Nabin Haitea4aa042014-05-28 12:56:28 +0530338
339def get_account_list(doctype, txt, searchfield, start, page_len, filters):
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530340 filter_list = []
Nabin Haitea4aa042014-05-28 12:56:28 +0530341
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530342 if isinstance(filters, dict):
343 for key, val in filters.items():
344 if isinstance(val, (list, tuple)):
345 filter_list.append([doctype, key, val[0], val[1]])
346 else:
347 filter_list.append([doctype, key, "=", val])
bhupeshg2e2e973f2015-04-14 22:15:24 +0530348 elif isinstance(filters, list):
349 filter_list.extend(filters)
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530350
Rushabh Mehta38c6b522015-04-23 13:14:17 +0530351 if "is_group" not in [d[1] for d in filter_list]:
352 filter_list.append(["Account", "is_group", "=", "0"])
Nabin Haite1b2b3e2014-06-14 15:26:10 +0530353
354 if searchfield and txt:
355 filter_list.append([doctype, searchfield, "like", "%%%s%%" % txt])
356
Rushabh Mehtac0bb4532014-09-09 16:15:35 +0530357 return frappe.desk.reportview.execute("Account", filters = filter_list,
Nabin Haitea4aa042014-05-28 12:56:28 +0530358 fields = ["name", "parent_account"],
359 limit_start=start, limit_page_length=page_len, as_list=True)
Anand Doshifaefeaa2014-06-24 18:53:04 +0530360
Nabin Haitafd14f62015-10-19 11:55:28 +0530361
362@frappe.whitelist()
363def get_income_account(doctype, txt, searchfield, start, page_len, filters):
364 from erpnext.controllers.queries import get_match_cond
365
366 # income account can be any Credit account,
367 # but can also be a Asset account with account_type='Income Account' in special circumstances.
368 # Hence the first condition is an "OR"
369 if not filters: filters = {}
370
Anand Doshi21e09a22015-10-29 12:21:41 +0530371 condition = ""
Nabin Haitafd14f62015-10-19 11:55:28 +0530372 if filters.get("company"):
373 condition += "and tabAccount.company = %(company)s"
Anand Doshi21e09a22015-10-29 12:21:41 +0530374
Nabin Haitafd14f62015-10-19 11:55:28 +0530375 return frappe.db.sql("""select tabAccount.name from `tabAccount`
376 where (tabAccount.report_type = "Profit and Loss"
377 or tabAccount.account_type in ("Income Account", "Temporary"))
378 and tabAccount.is_group=0
379 and tabAccount.`{key}` LIKE %(txt)s
Rushabh Mehta3574b372016-03-11 14:33:04 +0530380 {condition} {match_condition}
381 order by idx desc, name"""
Nabin Haitafd14f62015-10-19 11:55:28 +0530382 .format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), {
Suraj Shetty4b404c42018-09-27 15:39:34 +0530383 'txt': '%' + txt + '%',
Nabin Haitafd14f62015-10-19 11:55:28 +0530384 'company': filters.get("company", "")
Anand Doshi21e09a22015-10-29 12:21:41 +0530385 })
Nabin Hait3a15c922016-03-04 12:30:46 +0530386
387
388@frappe.whitelist()
389def get_expense_account(doctype, txt, searchfield, start, page_len, filters):
390 from erpnext.controllers.queries import get_match_cond
Rushabh Mehta203cc962016-04-07 15:25:43 +0530391
Nabin Hait3a15c922016-03-04 12:30:46 +0530392 if not filters: filters = {}
393
394 condition = ""
395 if filters.get("company"):
396 condition += "and tabAccount.company = %(company)s"
Rushabh Mehta203cc962016-04-07 15:25:43 +0530397
Nabin Hait3a15c922016-03-04 12:30:46 +0530398 return frappe.db.sql("""select tabAccount.name from `tabAccount`
399 where (tabAccount.report_type = "Profit and Loss"
Mangesh-Khairnar5619db22019-08-21 14:49:24 +0530400 or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed", "Capital Work in Progress"))
Nabin Hait3a15c922016-03-04 12:30:46 +0530401 and tabAccount.is_group=0
402 and tabAccount.docstatus!=2
403 and tabAccount.{key} LIKE %(txt)s
404 {condition} {match_condition}"""
Suraj Shettybfc195d2018-09-21 10:20:52 +0530405 .format(condition=condition, key=searchfield,
Nabin Hait3a15c922016-03-04 12:30:46 +0530406 match_condition=get_match_cond(doctype)), {
Neil Trini Lasrado30b97b02016-03-31 23:10:13 +0530407 'company': filters.get("company", ""),
Suraj Shetty4b404c42018-09-27 15:39:34 +0530408 'txt': '%' + txt + '%'
Maxwell Morais35572612016-07-21 23:42:59 -0300409 })
suyashphadtare049a88c2017-01-12 17:49:37 +0530410
411
412@frappe.whitelist()
413def warehouse_query(doctype, txt, searchfield, start, page_len, filters):
414 # Should be used when item code is passed in filters.
suyashphadtare750a0672017-01-18 15:35:01 +0530415 conditions, bin_conditions = [], []
416 filter_dict = get_doctype_wise_filters(filters)
417
418 sub_query = """ select round(`tabBin`.actual_qty, 2) from `tabBin`
suyashphadtare34ab1362017-01-31 15:14:44 +0530419 where `tabBin`.warehouse = `tabWarehouse`.name
420 {bin_conditions} """.format(
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530421 bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"),
Nabin Hait4e6ff8c2017-05-09 15:09:10 +0530422 bin_conditions, ignore_permissions=True))
suyashphadtare750a0672017-01-18 15:35:01 +0530423
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530424 query = """select `tabWarehouse`.name,
suyashphadtare34ab1362017-01-31 15:14:44 +0530425 CONCAT_WS(" : ", "Actual Qty", ifnull( ({sub_query}), 0) ) as actual_qty
426 from `tabWarehouse`
427 where
Suraj Shetty6ea3de92018-09-26 18:15:53 +0530428 `tabWarehouse`.`{key}` like {txt}
suyashphadtare34ab1362017-01-31 15:14:44 +0530429 {fcond} {mcond}
430 order by
431 `tabWarehouse`.name desc
432 limit
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530433 {start}, {page_len}
suyashphadtare34ab1362017-01-31 15:14:44 +0530434 """.format(
435 sub_query=sub_query,
Suraj Shettybfc195d2018-09-21 10:20:52 +0530436 key=searchfield,
suyashphadtare34ab1362017-01-31 15:14:44 +0530437 fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions),
Rushabh Mehta7e506af2017-08-24 15:23:33 +0530438 mcond=get_match_cond(doctype),
439 start=start,
440 page_len=page_len,
441 txt=frappe.db.escape('%{0}%'.format(txt))
442 )
443
444 return frappe.db.sql(query)
suyashphadtare750a0672017-01-18 15:35:01 +0530445
446
447def get_doctype_wise_filters(filters):
448 # Helper function to seperate filters doctype_wise
449 filter_dict = defaultdict(list)
450 for row in filters:
451 filter_dict[row[0]].append(row)
452 return filter_dict
tundebabzy2a4fefc2017-11-29 06:23:09 +0100453
454
455@frappe.whitelist()
456def get_batch_numbers(doctype, txt, searchfield, start, page_len, filters):
rohitwaghchaure9fbed562018-01-12 16:22:33 +0530457 query = """select batch_id from `tabBatch`
Doridel Cahanap59e4c322018-08-06 17:15:18 +0800458 where disabled = 0
459 and (expiry_date >= CURDATE() or expiry_date IS NULL)
Suraj Shettybfc195d2018-09-21 10:20:52 +0530460 and name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt)))
tundebabzy2a4fefc2017-11-29 06:23:09 +0100461
rohitwaghchaure9fbed562018-01-12 16:22:33 +0530462 if filters and filters.get('item'):
Suraj Shettybfc195d2018-09-21 10:20:52 +0530463 query += " and item = {item}".format(item = frappe.db.escape(filters.get('item')))
tundebabzy2a4fefc2017-11-29 06:23:09 +0100464
Sachin Mane64f48db2018-01-08 17:57:32 +0530465 return frappe.db.sql(query, filters)
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530466
467@frappe.whitelist()
468def item_manufacturer_query(doctype, txt, searchfield, start, page_len, filters):
Maricabac4b932019-09-16 19:44:28 +0530469 item_filters = [
470 ['manufacturer', 'like', '%' + txt + '%'],
471 ['item_code', '=', filters.get("item_code")]
472 ]
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530473
Maricabac4b932019-09-16 19:44:28 +0530474 item_manufacturers = frappe.get_all(
475 "Item Manufacturer",
476 fields=["manufacturer", "manufacturer_part_no"],
477 filters=item_filters,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530478 limit_start=start,
479 limit_page_length=page_len,
480 as_list=1
481 )
Maricabac4b932019-09-16 19:44:28 +0530482 return item_manufacturers
Saqibd9956092019-11-18 11:46:55 +0530483
484@frappe.whitelist()
485def get_purchase_receipts(doctype, txt, searchfield, start, page_len, filters):
486 query = """
487 select pr.name
488 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pritem
489 where pr.docstatus = 1 and pritem.parent = pr.name
490 and pr.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt)))
491
492 if filters and filters.get('item_code'):
493 query += " and pritem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code')))
494
495 return frappe.db.sql(query, filters)
496
497@frappe.whitelist()
498def get_purchase_invoices(doctype, txt, searchfield, start, page_len, filters):
499 query = """
500 select pi.name
501 from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` piitem
502 where pi.docstatus = 1 and piitem.parent = pi.name
503 and pi.name like {txt}""".format(txt = frappe.db.escape('%{0}%'.format(txt)))
504
505 if filters and filters.get('item_code'):
506 query += " and piitem.item_code = {item_code}".format(item_code = frappe.db.escape(filters.get('item_code')))
507
508 return frappe.db.sql(query, filters)