'Updates the Base Location to be the same as the Address
If the base location hasn\'t yet been set or if this is specifically
requested'

@staticmethod
def pr_address_onaccept(form):

    form_vars = form.vars
    location_id = form_vars.get('location_id')
    if (not location_id):
        return
    try:
        record_id = form_vars['id']
    except:
        return
    db = current.db
    s3db = current.s3db
    atable = db.pr_address
    pe_id = db((atable.id == record_id)).select(atable.pe_id, limitby=(0, 1)).first().pe_id
    requestvars = current.request.form_vars
    settings = current.deployment_settings
    person = None
    ptable = s3db.pr_person
    if (requestvars and ('base_location' in requestvars) and (requestvars.base_location == 'on')):
        S3Tracker()(db.pr_pentity, pe_id).set_base_location(location_id)
        person = db((ptable.pe_id == pe_id)).select(ptable.id, limitby=(0, 1)).first()
    else:
        person = db((ptable.pe_id == pe_id)).select(ptable.id, ptable.location_id, limitby=(0, 1)).first()
        if (person and (not person.location_id)):
            S3Tracker()(db.pr_pentity, pe_id).set_base_location(location_id)
    if (person and (str(form_vars.get('type')) == '1')):
        if settings.has_module('hrm'):
            staff_settings = settings.get_hrm_location_staff()
            staff_person = ('person_id' in staff_settings)
            vol_settings = settings.get_hrm_location_vol()
            vol_person = ('person_id' in vol_settings)
            if (staff_person or vol_person):
                htable = s3db.hrm_human_resource
                query = ((htable.person_id == person.id) & (htable.deleted != True))
                fields = [htable.id]
                if (staff_person and vol_person):
                    fields.append(htable.type)
                    vol_site = ('site_id' == vol_settings[0])
                    staff_site = ('site_id' == staff_settings[0])
                    if (staff_site or vol_site):
                        fields.append(htable.site_id)
                elif vol_person:
                    vol_site = ('site_id' == vol_settings[0])
                    if vol_site:
                        fields.append(htable.site_id)
                    query &= (htable.type == 2)
                elif staff_person:
                    staff_site = ('site_id' == staff_settings[0])
                    if staff_site:
                        fields.append(htable.site_id)
                    query &= (htable.type == 1)
                hrs = db(query).select(*fields)
                for hr in hrs:
                    if (staff_person and vol_person):
                        vol = (hr.type == 2)
                        if (vol and vol_site and hr.site_id):
                            pass
                        elif ((not vol) and staff_site and hr.site_id):
                            pass
                        else:
                            db((htable.id == hr.id)).update(location_id=location_id)
                    elif vol_person:
                        if (vol_site and hr.site_id):
                            pass
                        else:
                            db((htable.id == hr.id)).update(location_id=location_id)
                    elif (staff_site and hr.site_id):
                        pass
                    else:
                        db((htable.id == hr.id)).update(location_id=location_id)
        if settings.has_module('member'):
            mtable = s3db.member_membership
            query = ((mtable.person_id == person.id) & (mtable.deleted != True))
            members = db(query).select(mtable.id)
            for member in members:
                db((mtable.id == member.id)).update(location_id=location_id)
