'After DB I/O'

@staticmethod
def req_onaccept(form):

    db = current.db
    s3db = current.s3db
    request = current.request
    settings = current.deployment_settings
    tablename = 'req_req'
    table = s3db.req_req
    form_vars = form.vars
    id = form_vars.id
    if form_vars.get('is_template', None):
        is_template = True
        f = 'req_template'
    else:
        is_template = False
        f = 'req'
        if settings.get_req_use_req_number():
            record = db((table.id == id)).select(table.req_ref, table.site_id, limitby=(0, 1)).first()
            if (not record.req_ref):
                code = s3db.supply_get_shipping_code(settings.get_req_shortname(), record.site_id, table.req_ref)
                db((table.id == id)).update(req_ref=code)
    req_status = form_vars.get('req_status', None)
    if (req_status is not None):
        req_status = int(req_status)
        if (req_status == REQ_STATUS_PARTIAL):
            record = db((table.id == id)).select(table.commit_status, table.fulfil_status, limitby=(0, 1)).first()
            data = dict(cancel=False)
            if (record.commit_status != REQ_STATUS_COMPLETE):
                data['commit_status'] = REQ_STATUS_PARTIAL
            if (record.fulfil_status == REQ_STATUS_COMPLETE):
                data['fulfil_status'] = REQ_STATUS_PARTIAL
            db((table.id == id)).update(**data)
        elif (req_status == REQ_STATUS_COMPLETE):
            db((table.id == id)).update(fulfil_status=REQ_STATUS_COMPLETE, cancel=False)
        elif (req_status == REQ_STATUS_CANCEL):
            db((table.id == id)).update(cancel=True)
        elif (req_status == REQ_STATUS_NONE):
            db((table.id == id)).update(commit_status=REQ_STATUS_NONE, fulfil_status=REQ_STATUS_NONE, cancel=False)
    if settings.get_req_requester_to_site():
        requester_id = form_vars.get('requester_id', None)
        if requester_id:
            site_id = form_vars.get('site_id', None)
            hrtable = s3db.hrm_human_resource
            query = (hrtable.person_id == requester_id)
            exists = db(query).select(hrtable.id, hrtable.organisation_id, hrtable.site_id, hrtable.site_contact, limitby=(0, 1)).first()
            if exists:
                if (site_id and (not exists.site_id)):
                    stable = s3db.org_site
                    site = db((stable.site_id == site_id)).select(stable.organisation_id, limitby=(0, 1)).first()
                    if (site and (site.organisation_id == exists.organisation_id)):
                        exists.update(site_id=site_id)
                        s3db.hrm_human_resource_onaccept(exists)
            elif site_id:
                stable = s3db.org_site
                site = db((stable.site_id == site_id)).select(stable.organisation_id, limitby=(0, 1)).first()
                ltable = s3db.hrm_human_resource_site
                query = ((ltable.site_id == site_id) & (ltable.site_contact == True))
                already = db(query).select(ltable.id, limitby=(0, 1)).first()
                if already:
                    site_contact = False
                else:
                    site_contact = True
                hr_id = hrtable.insert(person_id=requester_id, organisation_id=site.organisation_id, site_id=site_id, site_contact=site_contact)
                s3db.hrm_human_resource_onaccept(Storage(id=hr_id))
    inline_forms = settings.get_req_inline_forms()
    if (inline_forms and is_template):
        s3db.configure(tablename, create_next=URL(c='req', f=f, args=['[id]', 'job']), update_next=URL(c='req', f=f, args=['[id]', 'job']))
    elif (not inline_forms):
        if table.type.default:
            type = table.type.default
        elif ('type' in form_vars):
            type = int(form_vars.type)
        else:
            type = 1
        if ((type == 1) and settings.has_module('inv')):
            s3db.configure(tablename, create_next=URL(c='req', f=f, args=['[id]', 'req_item']), update_next=URL(c='req', f=f, args=['[id]', 'req_item']))
        elif ((type == 2) and settings.has_module('asset')):
            s3db.configure(tablename, create_next=URL(c='req', f=f, args=['[id]', 'req_asset']), update_next=URL(c='req', f=f, args=['[id]', 'req_asset']))
        elif ((type == 3) and settings.has_module('hrm')):
            s3db.configure(tablename, create_next=URL(c='req', f=f, args=['[id]', 'req_skill']), update_next=URL(c='req', f=f, args=['[id]', 'req_skill']))
        elif ((type == 4) and settings.has_module('cr')):
            s3db.configure(tablename, create_next=URL(c='req', f=f, args=['[id]', 'req_shelter']), update_next=URL(c='req', f=f, args=['[id]', 'req_shelter']))
