'Link a user account to an organisation group
@param user: the user account record
@param person_id: the person record ID associated with this user'

def s3_link_to_org_group(self, user, person_id):

    db = current.db
    s3db = current.s3db
    org_group_id = user.get('org_group_id')
    if ((not org_group_id) or (not person_id)):
        return None
    stable = s3db.org_group_person_status
    query = ((stable.name.lower() == 'member') & (stable.deleted != True))
    row = db(query).select(stable.id, limitby=(0, 1)).first()
    if row:
        status_id = row.id
    else:
        status_id = None
    ltable = s3db.org_group_person
    query = (((ltable.person_id == person_id) & (ltable.org_group_id == org_group_id)) & (ltable.deleted != True))
    row = db(query).select(ltable.id, limitby=(0, 1)).first()
    if (not row):
        ptable = s3db.pr_person
        gtable = s3db.org_group
        if (ptable[person_id] and gtable[org_group_id]):
            ltable.insert(person_id=person_id, org_group_id=org_group_id, status_id=status_id)
    return org_group_id
