'Get the person record for a PE Label (or ID code), search only
for persons with an open DVR case.
@param pe_label: the PE label (or a scanned ID code as string)'

@classmethod
def get_person(cls, pe_label):

    s3db = current.s3db
    person = None
    fields = ['id', 'pe_id', 'pe_label', 'first_name', 'middle_name', 'last_name', 'date_of_birth', 'gender']
    data = cls.parse_code(pe_label)
    def person_(label):
        '    Helper    function    to    find    a    person    by    pe_label    '
        query = ((((FS('pe_label') == pe_label) & (FS('dvr_case.id') != None)) & (FS('dvr_case.archived') != True)) & (FS('dvr_case.status_id$is_closed') != True))
        presource = s3db.resource('pr_person', filter=query)
        rows = presource.select(fields, start=0, limit=1, as_rows=True)
        return (rows[0] if rows else None)
    pe_label = data['label'].strip()
    if pe_label:
        person = person_(pe_label)
    if person:
        data_match = True
    else:
        family = data.get('family')
        if family:
            person = person_(family)
            data_match = False
    if person:
        (first_name, last_name) = (None, None)
        if ('first_name' in data):
            first_name = s3_unicode(data['first_name']).lower()
            if (s3_unicode(person.first_name).lower() != first_name):
                data_match = False
        if ('last_name' in data):
            last_name = s3_unicode(data['last_name']).lower()
            if (s3_unicode(person.last_name).lower() != last_name):
                data_match = False
        if (not data_match):
            ptable = s3db.pr_person
            query = current.auth.s3_accessible_query('read', ptable)
            gtable = s3db.pr_group
            mtable = s3db.pr_group_membership
            otable = mtable.with_alias('family')
            ctable = s3db.dvr_case
            stable = s3db.dvr_case_status
            left = [gtable.on(((gtable.id == mtable.group_id) & (gtable.group_type == 7))), otable.on((((otable.group_id == gtable.id) & (otable.person_id != mtable.person_id)) & (otable.deleted != True))), ptable.on(((ptable.id == otable.person_id) & (ptable.pe_label != None))), ctable.on(((ctable.person_id == otable.person_id) & (ctable.archived != True))), stable.on((stable.id == ctable.status_id))]
            query &= (((((mtable.person_id == person.id) & (ctable.id != None)) & (stable.is_closed != True)) & (mtable.deleted != True)) & (ptable.deleted != True))
            if first_name:
                query &= (ptable.first_name.lower() == first_name)
            if last_name:
                query &= (ptable.last_name.lower() == last_name)
            if ('date_of_birth' in data):
                (dob, error) = IS_UTC_DATE()(data['date_of_birth'])
                if ((not error) and dob):
                    query &= (ptable.date_of_birth == dob)
            fields_ = [ptable[fn] for fn in fields]
            rows = current.db(query).select(left=left, limitby=(0, 2), *fields_)
            if (len(rows) == 1):
                person = rows[0]
    elif (('first_name' in data) and ('last_name' in data)):
        first_name = s3_unicode(data['first_name']).lower()
        last_name = s3_unicode(data['last_name']).lower()
        query = (FS('pe_label') != None)
        if first_name:
            query &= (FS('first_name').lower() == first_name)
        if last_name:
            query &= (FS('last_name').lower() == last_name)
        if ('date_of_birth' in data):
            (dob, error) = IS_UTC_DATE()(data['date_of_birth'])
            if ((not error) and dob):
                query &= (FS('date_of_birth') == dob)
        query &= (((FS('dvr_case.id') != None) & (FS('dvr_case.archived') != True)) & (FS('dvr_case.status_id$is_closed') != True))
        presource = s3db.resource('pr_person', filter=query)
        rows = presource.select(fields, start=0, limit=2, as_rows=True)
        if (len(rows) == 1):
            person = rows[0]
    return person
