'JSON lookup method for S3AddPersonWidget2'

@staticmethod
def pr_person_check_duplicates(r, **attr):

    settings = current.deployment_settings
    post_vars = current.request.post_vars
    dob = post_vars.get('dob', None)
    if dob:
        (dob, error) = s3_validate(current.s3db.pr_person, 'date_of_birth', dob)
        if (not error):
            dob = dob.isoformat()
        else:
            dob = None
    gender = post_vars.get('sex')
    father_name = post_vars.get('father_name')
    grandfather_name = post_vars.get('grandfather_name')
    occupation = post_vars.get('occupation')
    mobile_phone = post_vars.get('mphone')
    home_phone = post_vars.get('hphone')
    email = post_vars.get('email')
    separate_name_fields = settings.get_pr_separate_name_fields()
    if separate_name_fields:
        middle_name_field = (separate_name_fields == 3)
        first_name = post_vars.get('first_name')
        middle_name = post_vars.get('middle_name')
        last_name = post_vars.get('last_name')
        query = ((FS('first_name').lower().like((first_name + '%')) | FS('middle_name').lower().like((first_name + '%'))) | FS('last_name').lower().like((first_name + '%')))
        if middle_name:
            query |= ((FS('first_name').lower().like((middle_name + '%')) | FS('middle_name').lower().like((middle_name + '%'))) | FS('last_name').lower().like((middle_name + '%')))
        if last_name:
            query |= ((FS('first_name').lower().like((last_name + '%')) | FS('middle_name').lower().like((last_name + '%'))) | FS('last_name').lower().like((last_name + '%')))
    else:
        name_format = settings.get_pr_name_format()
        middle_name = ('middle_name' in name_format)
        value = post_vars.get('name')
        query = (FS('first_name').lower().like((value + '%')) | FS('last_name').lower().like((value + '%')))
        if middle_name:
            query |= FS('middle_name').lower().like((value + '%'))
        if ('    ' in value):
            (value1, value2) = value.split('    ', 1)
            query |= ((FS('first_name').lower().like((value1 + '%')) & FS('last_name').lower().like((value2 + '%'))) | (FS('first_name').lower().like((value2 + '%')) & FS('last_name').lower().like((value1 + '%'))))
            if middle_name:
                query |= ((((FS('first_name').lower().like((value1 + '%')) & FS('middle_name').lower().like((value2 + '%'))) | (FS('first_name').lower().like((value2 + '%')) & FS('middle_name').lower().like((value1 + '%')))) | (FS('middle_name').lower().like((value1 + '%')) & FS('last_name').lower().like((value2 + '%')))) | (FS('middle_name').lower().like((value2 + '%')) & FS('last_name').lower().like((value1 + '%'))))
            if ('    ' in value2):
                (value21, value3) = value2.split('    ', 1)
                value12 = ('%s    %s' % (value1, value21))
                query |= ((FS('first_name').lower().like((value12 + '%')) & FS('last_name').lower().like((value3 + '%'))) | (FS('first_name').lower().like((value3 + '%')) & FS('last_name').lower().like((value12 + '%'))))
                if middle_name:
                    query |= (((((FS('first_name').lower().like((value1 + '%')) & FS('middle_name').lower().like((value21 + '%'))) & FS('last_name').lower().like((value3 + '%'))) | ((FS('first_name').lower().like((value1 + '%')) & FS('last_name').lower().like((value21 + '%'))) & FS('middle_name').lower().like((value3 + '%')))) | ((FS('last_name').lower().like((value1 + '%')) & FS('middle_name').lower().like((value21 + '%'))) & FS('first_name').lower().like((value3 + '%')))) | ((FS('last_name').lower().like((value1 + '%')) & FS('first_name').lower().like((value21 + '%'))) & FS('middle_name').lower().like((value3 + '%'))))
                if ('    ' in value3):
                    (value31, value4) = value3.split('    ', 1)
                    value13 = ('%s    %s    %s' % (value1, value21, value31))
                    value22 = ('%s    %s' % (value21, value31))
                    query |= ((FS('first_name').lower().like((value13 + '%')) & FS('last_name').lower().like((value4 + '%'))) | (FS('first_name').lower().like((value4 + '%')) & FS('last_name').lower().like((value13 + '%'))))
                    if middle_name:
                        query |= (((((((((FS('first_name').lower().like((value1 + '%')) & FS('middle_name').lower().like((value22 + '%'))) & FS('last_name').lower().like((value4 + '%'))) | ((FS('first_name').lower().like((value1 + '%')) & FS('last_name').lower().like((value22 + '%'))) & FS('middle_name').lower().like((value4 + '%')))) | ((FS('last_name').lower().like((value1 + '%')) & FS('middle_name').lower().like((value22 + '%'))) & FS('first_name').lower().like((value4 + '%')))) | ((FS('last_name').lower().like((value1 + '%')) & FS('first_name').lower().like((value22 + '%'))) & FS('middle_name').lower().like((value4 + '%')))) | ((FS('first_name').lower().like((value12 + '%')) & FS('middle_name').lower().like((value31 + '%'))) & FS('last_name').lower().like((value4 + '%')))) | ((FS('first_name').lower().like((value12 + '%')) & FS('last_name').lower().like((value31 + '%'))) & FS('middle_name').lower().like((value4 + '%')))) | ((FS('last_name').lower().like((value12 + '%')) & FS('middle_name').lower().like((value31 + '%'))) & FS('first_name').lower().like((value4 + '%')))) | ((FS('last_name').lower().like((value12 + '%')) & FS('first_name').lower().like((value31 + '%'))) & FS('middle_name').lower().like((value4 + '%'))))
    resource = r.resource
    resource.add_filter(query)
    fields = ['id', 'first_name', 'middle_name', 'last_name', 'date_of_birth', 'gender', 'person_details.father_name', 'person_details.grandfather_name', 'person_details.occupation', 'image.image']
    MAX_SEARCH_RESULTS = settings.get_search_max_results()
    show_hr = settings.get_pr_search_shows_hr_details()
    if show_hr:
        fields.append('human_resource.job_title_id$name')
        show_orgs = settings.get_hrm_show_organisation()
        if show_orgs:
            fields.append('human_resource.organisation_id$name')
    rows = resource.select(fields=fields, start=0, limit=MAX_SEARCH_RESULTS)['rows']
    if (not len(rows)):
        if dob:
            resource.rfilter.filters.pop()
            resource.rfilter.query = None
            query = (FS('date_of_birth') == dob)
            resource.add_filter(query)
            rows = resource.select(fields=fields, start=0, limit=MAX_SEARCH_RESULTS)['rows']
        if ((not len(rows)) and email):
            resource.rfilter.filters.pop()
            resource.rfilter.query = None
            query = ((FS('contact.value') == email) & (FS('contact.contact_method') == 'EMAIL'))
            resource.add_filter(query)
            rows = resource.select(fields=fields, start=0, limit=MAX_SEARCH_RESULTS)['rows']
        if ((not len(rows)) and mobile_phone):
            resource.rfilter.filters.pop()
            resource.rfilter.query = None
            query = ((FS('contact.value') == mobile_phone) & (FS('contact.contact_method') == 'SMS'))
            resource.add_filter(query)
            rows = resource.select(fields=fields, start=0, limit=MAX_SEARCH_RESULTS)['rows']
        if ((not len(rows)) and home_phone):
            resource.rfilter.filters.pop()
            resource.rfilter.query = None
            query = ((FS('contact.value') == home_phone) & (FS('contact.contact_method') == 'HOME_PHONE'))
            resource.add_filter(query)
            rows = resource.select(fields=fields, start=0, limit=MAX_SEARCH_RESULTS)['rows']
    items = []
    iappend = items.append
    for row in rows:
        item = {'id': row['pr_person.id']}
        if separate_name_fields:
            item['first_name'] = row['pr_person.first_name']
            item['last_name'] = row['pr_person.last_name']
            if middle_name_field:
                middle_name = row['pr_person.middle_name']
                if middle_name:
                    item['middle_name'] = middle_name
        else:
            name = Storage(first_name=row['pr_person.first_name'], middle_name=row['pr_person.middle_name'], last_name=row['pr_person.last_name'])
            name = s3_fullname(name)
            item['name'] = name
        date_of_birth = row.get('pr_person.date_of_birth')
        if date_of_birth:
            item['dob'] = date_of_birth.isoformat()
        gender = row.get('pr_person.gender')
        if (gender in (2, 3, 4)):
            item['sex'] = gender
        father_name = row.get('pr_person_details.father_name')
        if father_name:
            item['father_name'] = father_name
        grandfather_name = row.get('pr_person_details.grandfather_name')
        if grandfather_name:
            item['grandfather_name'] = grandfather_name
        occupation = row.get('pr_person_details.occupation')
        if occupation:
            item['job'] = occupation
        email = row.get('pr_contact.email')
        if email:
            item['email'] = email
        phone = row.get('pr_contact.phone')
        if phone:
            item['mphone'] = phone
        image = row.get('pr_image.image')
        if image:
            item['image'] = image
        if show_hr:
            job_title = row.get('hrm_job_title.name')
            if job_title:
                item['job'] = job_title
            if show_orgs:
                org = row.get('org_organisation.name')
                if org:
                    item['org'] = org
        iappend(item)
    output = json.dumps(items, separators=SEPARATORS)
    current.response.headers['Content-Type'] = 'application/json'
    return output
