'JSON lookup method for S3AddPersonWidget2'

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

    id = r.id
    if (not id):
        output = current.xml.json_message(False, 400, 'No    id    provided!')
        raise HTTP(400, body=output)
    db = current.db
    s3db = current.s3db
    settings = current.deployment_settings
    request_dob = settings.get_pr_request_dob()
    request_gender = settings.get_pr_request_gender()
    home_phone = settings.get_pr_request_home_phone()
    htable = db.hrm_human_resource
    ptable = db.pr_person
    ctable = s3db.pr_contact
    fields = [htable.organisation_id, ptable.pe_id]
    separate_name_fields = settings.get_pr_separate_name_fields()
    if separate_name_fields:
        middle_name = (separate_name_fields == 3)
        fields += [ptable.first_name, ptable.middle_name, ptable.last_name]
    left = None
    if request_dob:
        fields.append(ptable.date_of_birth)
    if request_gender:
        fields.append(ptable.gender)
    if (current.request.controller == 'vol'):
        dtable = s3db.pr_person_details
        fields.append(dtable.occupation)
        left = dtable.on((dtable.person_id == ptable.id))
    query = ((htable.id == id) & (ptable.id == htable.person_id))
    row = db(query).select(left=left, *fields).first()
    if left:
        occupation = row['pr_person_details.occupation']
    else:
        occupation = None
    organisation_id = row['hrm_human_resource.organisation_id']
    row = row['pr_person']
    if request_dob:
        date_of_birth = row.date_of_birth
    else:
        date_of_birth = None
    if request_gender:
        gender = row.gender
    else:
        gender = None
    if separate_name_fields:
        first_name = row.first_name
        last_name = row.last_name
        if middle_name:
            middle_name = row.middle_name
    else:
        first_name = None
        middle_name = None
        last_name = None
    if home_phone:
        contact_methods = ('SMS', 'EMAIL', 'HOME_PHONE')
    else:
        contact_methods = ('SMS', 'EMAIL')
    query = ((ctable.pe_id == row.pe_id) & ctable.contact_method.belongs(contact_methods))
    rows = db(query).select(ctable.contact_method, ctable.value, orderby=ctable.priority)
    email = mobile_phone = None
    if home_phone:
        home_phone = None
        for row in rows:
            if ((not email) and (row.contact_method == 'EMAIL')):
                email = row.value
            elif ((not mobile_phone) and (row.contact_method == 'SMS')):
                mobile_phone = row.value
            elif ((not home_phone) and (row.contact_method == 'HOME_PHONE')):
                home_phone = row.value
            if (email and mobile_phone and home_phone):
                break
    else:
        for row in rows:
            if ((not email) and (row.contact_method == 'EMAIL')):
                email = row.value
            elif ((not mobile_phone) and (row.contact_method == 'SMS')):
                mobile_phone = row.value
            if (email and mobile_phone):
                break
    item = {}
    if first_name:
        item['first_name'] = first_name
    if middle_name:
        item['middle_name'] = middle_name
    if last_name:
        item['last_name'] = last_name
    if email:
        item['email'] = email
    if mobile_phone:
        item['mphone'] = mobile_phone
    if home_phone:
        item['hphone'] = home_phone
    if gender:
        item['sex'] = gender
    if date_of_birth:
        item['dob'] = date_of_birth
    if occupation:
        item['occupation'] = occupation
    if organisation_id:
        item['org_id'] = organisation_id
    output = json.dumps(item, separators=SEPARATORS)
    current.response.headers['Content-Type'] = 'application/json'
    return output
