'Returns a filter condition in the format:
ifnull(`tabDocType`.`fieldname`, fallback) operator "value"'

def prepare_filter_condition(self, f):

    f = get_filter(self.doctype, f)
    tname = ((u'`tab' + f.doctype) + u'`')
    if (not (tname in self.tables)):
        self.append_table(tname)
    if (u'ifnull(' in f.fieldname):
        column_name = f.fieldname
    else:
        column_name = u'{tname}.{fname}'.format(tname=tname, fname=f.fieldname)
    can_be_null = True
    if (f.operator.lower() in (u'in', u'not    in')):
        values = (f.value or u'')
        if (not isinstance(values, (list, tuple))):
            values = values.split(u',')
        fallback = u"''"
        value = (frappe.db.escape((v or u'').strip(), percent=False) for v in values)
        value = u'("{0}")'.format(u'",    "'.join(value))
    else:
        df = frappe.get_meta(f.doctype).get(u'fields', {u'fieldname': f.fieldname})
        df = (df[0] if df else None)
        if (df and (df.fieldtype in (u'Check', u'Float', u'Int', u'Currency', u'Percent'))):
            can_be_null = False
        if ((f.operator.lower() == u'between') and ((f.fieldname in (u'creation', u'modified')) or (df and ((df.fieldtype == u'Date') or (df.fieldtype == u'Datetime'))))):
            from_date = None
            to_date = None
            if (f.value and isinstance(f.value, (list, tuple))):
                if (len(f.value) >= 1):
                    from_date = f.value[0]
                if (len(f.value) >= 2):
                    to_date = f.value[1]
            value = (u"'%s'    AND    '%s'" % (add_to_date(get_datetime(from_date), days=(-1)).strftime(u'%Y-%m-%d    %H:%M:%S.%f'), get_datetime(to_date).strftime(u'%Y-%m-%d    %H:%M:%S.%f')))
            fallback = u"'0000-00-00    00:00:00'"
        elif (df and (df.fieldtype == u'Date')):
            value = getdate(f.value).strftime(u'%Y-%m-%d')
            fallback = u"'0000-00-00'"
        elif ((df and (df.fieldtype == u'Datetime')) or isinstance(f.value, datetime)):
            value = get_datetime(f.value).strftime(u'%Y-%m-%d    %H:%M:%S.%f')
            fallback = u"'0000-00-00    00:00:00'"
        elif (df and (df.fieldtype == u'Time')):
            value = get_time(f.value).strftime(u'%H:%M:%S.%f')
            fallback = u"'00:00:00'"
        elif ((f.operator.lower() in (u'like', u'not    like')) or (isinstance(f.value, basestring) and ((not df) or (df.fieldtype not in [u'Float', u'Int', u'Currency', u'Percent', u'Check'])))):
            value = (u'' if (f.value == None) else f.value)
            fallback = u'""'
            if ((f.operator.lower() in (u'like', u'not    like')) and isinstance(value, basestring)):
                value = value.replace(u'\\', u'\\\\').replace(u'%', u'%%')
        else:
            value = flt(f.value)
            fallback = 0
        if (isinstance(value, basestring) and (not (f.operator.lower() == u'between'))):
            value = u'"{0}"'.format(frappe.db.escape(value, percent=False))
    if (self.ignore_ifnull or (not can_be_null) or (f.value and (f.operator.lower() in (u'=', u'like'))) or (u'ifnull(' in column_name.lower())):
        condition = u'{column_name}    {operator}    {value}'.format(column_name=column_name, operator=f.operator, value=value)
    else:
        condition = u'ifnull({column_name},    {fallback})    {operator}    {value}'.format(column_name=column_name, fallback=fallback, operator=f.operator, value=value)
    return condition
