def get_batch_no(doctype, txt, searchfield, start, page_len, filters): cond = "" if filters.get("posting_date"): cond = "and (batch.expiry_date is null or batch.expiry_date >= %(posting_date)s)" batch_nos = None args = { 'item_code': filters.get("item_code"), 'warehouse': filters.get("warehouse"), 'posting_date': filters.get('posting_date'), 'txt': "%{0}%".format(txt), "start": start, "page_len": page_len } if args.get('warehouse'): batch_nos = dataent.db.sql( """select sle.batch_no, round(sum(sle.actual_qty),2), sle.stock_uom, concat('MFG-',batch.manufacturing_date), concat('EXP-',batch.expiry_date) from `tabStock Ledger Entry` sle INNER JOIN `tabBatch` batch on sle.batch_no = batch.name where batch.disabled = 0 and sle.item_code = %(item_code)s and sle.warehouse = %(warehouse)s and (sle.batch_no like %(txt)s or batch.manufacturing_date like %(txt)s) and batch.docstatus < 2 {0} {match_conditions} group by batch_no having sum(sle.actual_qty) > 0 order by batch.expiry_date, sle.batch_no desc limit %(start)s, %(page_len)s""".format( cond, match_conditions=get_match_cond(doctype)), args) return batch_nos else: return dataent.db.sql( """select name, concat('MFG-', manufacturing_date), concat('EXP-',expiry_date) from `tabBatch` batch where batch.disabled = 0 and item = %(item_code)s and (name like %(txt)s or manufacturing_date like %(txt)s) and docstatus < 2 {0} {match_conditions} order by expiry_date, name desc limit %(start)s, %(page_len)s""".format( cond, match_conditions=get_match_cond(doctype)), args)
def employee_query(doctype, txt, searchfield, start, page_len, filters): conditions = [] return dataent.db.sql( """select name, employee_name from `tabEmployee` where status = 'Active' and docstatus < 2 and ({key} like %(txt)s or employee_name like %(txt)s) {fcond} {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), if(locate(%(_txt)s, employee_name), locate(%(_txt)s, employee_name), 99999), idx desc, name, employee_name limit %(start)s, %(page_len)s""".format( **{ 'key': searchfield, 'fcond': get_filters_cond(doctype, filters, conditions), 'mcond': get_match_cond(doctype) }), { 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len })
def item_query(doctype, txt, searchfield, start, page_len, filters): if filters.get("from"): from dataent.desk.reportview import get_match_cond mcond = get_match_cond(filters["from"]) cond, qi_condition = "", "and (quality_inspection is null or quality_inspection = '')" if filters.get('from') in ['Purchase Invoice Item', 'Purchase Receipt Item']: cond = """and item_code in (select name from `tabItem` where inspection_required_before_purchase = 1)""" elif filters.get('from') in ['Sales Invoice Item', 'Delivery Note Item']: cond = """and item_code in (select name from `tabItem` where inspection_required_before_delivery = 1)""" elif filters.get('from') == 'Stock Entry Detail': cond = """and s_warehouse is null""" if filters.get('from') in ['Supplier Quotation Item']: qi_condition = "" return dataent.db.sql(""" select item_code from `tab{doc}` where parent=%(parent)s and docstatus < 2 and item_code like %(txt)s {qi_condition} {cond} {mcond} order by item_code limit {start}, {page_len}""".format(doc=filters.get('from'), parent=filters.get('parent'), cond = cond, mcond = mcond, start = start, page_len = page_len, qi_condition = qi_condition), {'parent': filters.get('parent'), 'txt': "%%%s%%" % txt})
def supplier_query(doctype, txt, searchfield, start, page_len, filters): supp_master_name = dataent.defaults.get_user_default("supp_master_name") if supp_master_name == "Supplier Name": fields = ["name", "supplier_group"] else: fields = ["name", "supplier_name", "supplier_group"] fields = ", ".join(fields) return dataent.db.sql( """select {field} from `tabSupplier` where docstatus < 2 and ({key} like %(txt)s or supplier_name like %(txt)s) and disabled=0 {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), if(locate(%(_txt)s, supplier_name), locate(%(_txt)s, supplier_name), 99999), idx desc, name, supplier_name limit %(start)s, %(page_len)s """.format(**{ 'field': fields, 'key': searchfield, 'mcond': get_match_cond(doctype) }), { 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len })
def get_users_for_project(doctype, txt, searchfield, start, page_len, filters): conditions = [] return dataent.db.sql( """select name, concat_ws(' ', first_name, middle_name, last_name) from `tabUser` where enabled=1 and name not in ("Guest", "Administrator") and ({key} like %(txt)s or full_name like %(txt)s) {fcond} {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), if(locate(%(_txt)s, full_name), locate(%(_txt)s, full_name), 99999), idx desc, name, full_name limit %(start)s, %(page_len)s""".format( **{ 'key': searchfield, 'fcond': get_filters_cond(doctype, filters, conditions), 'mcond': get_match_cond(doctype) }), { 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len })
def get_income_account(doctype, txt, searchfield, start, page_len, filters): from epaas.controllers.queries import get_match_cond # income account can be any Credit account, # but can also be a Asset account with account_type='Income Account' in special circumstances. # Hence the first condition is an "OR" if not filters: filters = {} condition = "" if filters.get("company"): condition += "and tabAccount.company = %(company)s" return dataent.db.sql( """select tabAccount.name from `tabAccount` where (tabAccount.report_type = "Profit and Loss" or tabAccount.account_type in ("Income Account", "Temporary")) and tabAccount.is_group=0 and tabAccount.`{key}` LIKE %(txt)s {condition} {match_condition} order by idx desc, name""".format(condition=condition, match_condition=get_match_cond(doctype), key=searchfield), { 'txt': "%%%s%%" % dataent.db.escape(txt), 'company': filters.get("company", "") })
def warehouse_query(doctype, txt, searchfield, start, page_len, filters): # Should be used when item code is passed in filters. conditions, bin_conditions = [], [] filter_dict = get_doctype_wise_filters(filters) sub_query = """ select round(`tabBin`.actual_qty, 2) from `tabBin` where `tabBin`.warehouse = `tabWarehouse`.name {bin_conditions} """.format( bin_conditions=get_filters_cond(doctype, filter_dict.get("Bin"), bin_conditions, ignore_permissions=True)) query = """select `tabWarehouse`.name, CONCAT_WS(" : ", "Actual Qty", ifnull( ({sub_query}), 0) ) as actual_qty from `tabWarehouse` where `tabWarehouse`.`{key}` like '{txt}' {fcond} {mcond} order by `tabWarehouse`.name desc limit {start}, {page_len} """.format(sub_query=sub_query, key=dataent.db.escape(searchfield), fcond=get_filters_cond(doctype, filter_dict.get("Warehouse"), conditions), mcond=get_match_cond(doctype), start=start, page_len=page_len, txt=dataent.db.escape('%{0}%'.format(txt))) return dataent.db.sql(query)
def user_query(doctype, txt, searchfield, start, page_len, filters): from dataent.desk.reportview import get_match_cond user_type_condition = "and user_type = 'System User'" if filters and filters.get('ignore_user_type'): user_type_condition = '' txt = "%{}%".format(txt) return dataent.db.sql( """select name, concat_ws(' ', first_name, middle_name, last_name) from `tabUser` where enabled=1 {user_type_condition} and docstatus < 2 and name not in ({standard_users}) and ({key} like %(txt)s or concat_ws(' ', first_name, middle_name, last_name) like %(txt)s) {mcond} order by case when name like %(txt)s then 0 else 1 end, case when concat_ws(' ', first_name, middle_name, last_name) like %(txt)s then 0 else 1 end, name asc limit %(start)s, %(page_len)s""".format( user_type_condition=user_type_condition, standard_users=", ".join([ "'{0}'".format(dataent.db.escape(u)) for u in STANDARD_USERS ]), key=searchfield, mcond=get_match_cond(doctype)), dict(start=start, page_len=page_len, txt=txt))
def get_delivery_notes_to_be_billed(doctype, txt, searchfield, start, page_len, filters, as_dict): return dataent.db.sql(""" select `tabDelivery Note`.name, `tabDelivery Note`.customer, `tabDelivery Note`.posting_date from `tabDelivery Note` where `tabDelivery Note`.`%(key)s` like %(txt)s and `tabDelivery Note`.docstatus = 1 and status not in ("Stopped", "Closed") %(fcond)s and ( (`tabDelivery Note`.is_return = 0 and `tabDelivery Note`.per_billed < 100) or `tabDelivery Note`.grand_total = 0 or ( `tabDelivery Note`.is_return = 1 and return_against in (select name from `tabDelivery Note` where per_billed < 100) ) ) %(mcond)s order by `tabDelivery Note`.`%(key)s` asc limit %(start)s, %(page_len)s """ % { "key": searchfield, "fcond": get_filters_cond(doctype, filters, []), "mcond": get_match_cond(doctype), "start": start, "page_len": page_len, "txt": "%(txt)s" }, {"txt": ("%%%s%%" % txt)}, as_dict=as_dict)
def item_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False): conditions = [] description_cond = '' if dataent.db.count('Item', cache=True) < 50000: # scan description only if items are less than 50000 description_cond = 'or tabItem.description LIKE %(txt)s' return dataent.db.sql("""select tabItem.name, if(length(tabItem.item_name) > 40, concat(substr(tabItem.item_name, 1, 40), "..."), item_name) as item_name, tabItem.item_group, if(length(tabItem.description) > 40, \ concat(substr(tabItem.description, 1, 40), "..."), description) as decription from tabItem where tabItem.docstatus < 2 and tabItem.has_variants=0 and tabItem.disabled=0 and (tabItem.end_of_life > %(today)s or ifnull(tabItem.end_of_life, '0000-00-00')='0000-00-00') and (tabItem.`{key}` LIKE %(txt)s or tabItem.item_code LIKE %(txt)s or tabItem.item_group LIKE %(txt)s or tabItem.item_name LIKE %(txt)s or tabItem.item_code IN (select parent from `tabItem Barcode` where barcode LIKE %(txt)s) {description_cond}) {fcond} {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), if(locate(%(_txt)s, item_name), locate(%(_txt)s, item_name), 99999), idx desc, name, item_name limit %(start)s, %(page_len)s """.format( key=searchfield, fcond=get_filters_cond(doctype, filters, conditions).replace('%', '%%'), mcond=get_match_cond(doctype).replace('%', '%%'), description_cond=description_cond), { "today": nowdate(), "txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len }, as_dict=as_dict)
def get_program_courses(doctype, txt, searchfield, start, page_len, filters): if filters.get('program'): return dataent.db.sql("""select course, course_name from `tabProgram Course` where parent = %(program)s and course like %(txt)s {match_cond} order by if(locate(%(_txt)s, course), locate(%(_txt)s, course), 99999), idx desc, `tabProgram Course`.course asc limit {start}, {page_len}""".format( match_cond=get_match_cond(doctype), start=start, page_len=page_len), { "txt": "%{0}%".format(txt), "_txt": txt.replace('%', ''), "program": filters['program'] })
def customer_query(doctype, txt, searchfield, start, page_len, filters): conditions = [] cust_master_name = dataent.defaults.get_user_default("cust_master_name") if cust_master_name == "Customer Name": fields = ["name", "customer_group", "territory"] else: fields = ["name", "customer_name", "customer_group", "territory"] meta = dataent.get_meta("Customer") searchfields = meta.get_search_fields() searchfields = searchfields + [f for f in [searchfield or "name", "customer_name"] \ if not f in searchfields] fields = fields + [f for f in searchfields if not f in fields] fields = ", ".join(fields) searchfields = " or ".join( [field + " like %(txt)s" for field in searchfields]) return dataent.db.sql( """select {fields} from `tabCustomer` where docstatus < 2 and ({scond}) and disabled=0 {fcond} {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), if(locate(%(_txt)s, customer_name), locate(%(_txt)s, customer_name), 99999), idx desc, name, customer_name limit %(start)s, %(page_len)s""".format( **{ "fields": fields, "scond": searchfields, "mcond": get_match_cond(doctype), "fcond": get_filters_cond(doctype, filters, conditions).replace( '%', '%%'), }), { 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len })
def get_leave_from(doctype, txt, searchfield, start, page_len, filters): docname = filters['docname'] query = '''select io.service_unit from `tabInpatient Occupancy` io, `tabInpatient Record` ir where io.parent = '{docname}' and io.parentfield = 'inpatient_occupancies' and io.left!=1 and io.parent = ir.name''' return dataent.db.sql( query.format( **{ "docname": docname, "searchfield": searchfield, "mcond": get_match_cond(doctype) }), { 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len })
def get_project_name(doctype, txt, searchfield, start, page_len, filters): cond = '' if filters.get('customer'): cond = """(`tabProject`.customer = '%s' or ifnull(`tabProject`.customer,"")="") and""" % (dataent.db.escape( filters.get("customer"))) return dataent.db.sql( """select `tabProject`.name from `tabProject` where `tabProject`.status not in ("Completed", "Cancelled") and {cond} `tabProject`.name like %(txt)s {match_cond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), idx desc, `tabProject`.name asc limit {start}, {page_len}""".format(cond=cond, match_cond=get_match_cond(doctype), start=start, page_len=page_len), { "txt": "%{0}%".format(txt), "_txt": txt.replace('%', '') })
def address_query(doctype, txt, searchfield, start, page_len, filters): from dataent.desk.reportview import get_match_cond link_doctype = filters.pop('link_doctype') link_name = filters.pop('link_name') condition = "" for fieldname, value in iteritems(filters): condition += " and {field}={value}".format(field=fieldname, value=value) return dataent.db.sql( """select `tabAddress`.name, `tabAddress`.city, `tabAddress`.country from `tabAddress`, `tabDynamic Link` where `tabDynamic Link`.parent = `tabAddress`.name and `tabDynamic Link`.parenttype = 'Address' and `tabDynamic Link`.link_doctype = %(link_doctype)s and `tabDynamic Link`.link_name = %(link_name)s and ifnull(`tabAddress`.disabled, 0) = 0 and `tabAddress`.`{key}` like %(txt)s {mcond} {condition} order by if(locate(%(_txt)s, `tabAddress`.name), locate(%(_txt)s, `tabAddress`.name), 99999), `tabAddress`.idx desc, `tabAddress`.name limit %(start)s, %(page_len)s """.format(mcond=get_match_cond(doctype), key=dataent.db.escape(searchfield), condition=condition or ""), { 'txt': "%%%s%%" % dataent.db.escape(txt), '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len, 'link_name': link_name, 'link_doctype': link_doctype })
def get_expense_account(doctype, txt, searchfield, start, page_len, filters): from epaas.controllers.queries import get_match_cond if not filters: filters = {} condition = "" if filters.get("company"): condition += "and tabAccount.company = %(company)s" return dataent.db.sql( """select tabAccount.name from `tabAccount` where (tabAccount.report_type = "Profit and Loss" or tabAccount.account_type in ("Expense Account", "Fixed Asset", "Temporary", "Asset Received But Not Billed")) and tabAccount.is_group=0 and tabAccount.docstatus!=2 and tabAccount.{key} LIKE %(txt)s {condition} {match_condition}""".format( condition=condition, key=dataent.db.escape(searchfield), match_condition=get_match_cond(doctype)), { 'company': filters.get("company", ""), 'txt': "%%%s%%" % dataent.db.escape(txt) })
def bom(doctype, txt, searchfield, start, page_len, filters): conditions = [] return dataent.db.sql( """select tabBOM.name, tabBOM.item from tabBOM where tabBOM.docstatus=1 and tabBOM.is_active=1 and tabBOM.`{key}` like %(txt)s {fcond} {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), idx desc, name limit %(start)s, %(page_len)s """.format(fcond=get_filters_cond( doctype, filters, conditions), mcond=get_match_cond(doctype), key=dataent.db.escape(searchfield)), { 'txt': "%%%s%%" % dataent.db.escape(txt), '_txt': txt.replace("%", ""), 'start': start or 0, 'page_len': page_len or 20 })
def lead_query(doctype, txt, searchfield, start, page_len, filters): return dataent.db.sql( """select name, lead_name, company_name from `tabLead` where docstatus < 2 and ifnull(status, '') != 'Converted' and ({key} like %(txt)s or lead_name like %(txt)s or company_name like %(txt)s) {mcond} order by if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999), if(locate(%(_txt)s, lead_name), locate(%(_txt)s, lead_name), 99999), if(locate(%(_txt)s, company_name), locate(%(_txt)s, company_name), 99999), idx desc, name, lead_name limit %(start)s, %(page_len)s""".format(**{ 'key': searchfield, 'mcond': get_match_cond(doctype) }), { 'txt': "%%%s%%" % txt, '_txt': txt.replace("%", ""), 'start': start, 'page_len': page_len })