'Create/update linked hrm_experience record for assignment
@param form: the form'

@staticmethod
def deploy_assignment_onaccept(form):

    db = current.db
    s3db = current.s3db
    form_vars = form.vars
    assignment_id = form_vars.id
    fields = ('human_resource_id', 'mission_id', 'job_title', 'job_title_id', 'start_date', 'end_date')
    if any(((key not in form_vars) for key in fields)):
        atable = db.deploy_assignment
        query = (atable.id == assignment_id)
        qfields = [atable[f] for f in fields]
        row = db(query).select(limitby=(0, 1), *qfields).first()
        if row:
            data = dict(((k, row[k]) for k in fields))
        else:
            return
    else:
        data = dict(((k, form_vars[k]) for k in fields))
    hr = mission = None
    human_resource_id = data.pop('human_resource_id')
    if human_resource_id:
        hrtable = s3db.hrm_human_resource
        hr = db((hrtable.id == human_resource_id)).select(hrtable.person_id, hrtable.type, limitby=(0, 1)).first()
        if hr:
            person_id = hr.person_id
            data['person_id'] = person_id
            data['employment_type'] = hr.type
            ltable = s3db.pr_person_user
            ptable = s3db.pr_person
            query = ((ptable.id == person_id) & (ptable.pe_id == ltable.pe_id))
            link = db(query).select(ltable.user_id, limitby=(0, 1)).first()
            if link:
                data['owned_by_user'] = link.user_id
    mission_id = data.pop('mission_id')
    if mission_id:
        mtable = db.deploy_mission
        mission = db((mtable.id == mission_id)).select(mtable.location_id, mtable.organisation_id, limitby=(0, 1)).first()
        if mission:
            data['location_id'] = mission.location_id
            data['organisation_id'] = mission.organisation_id
    if (hr and mission):
        etable = s3db.hrm_experience
        ltable = s3db.deploy_assignment_experience
        query = (ltable.assignment_id == assignment_id)
        link = db(query).select(ltable.experience_id, limitby=(0, 1)).first()
        if link:
            db((etable.id == link.experience_id)).update(**data)
        else:
            experience_id = etable.insert(**data)
            ltable.insert(assignment_id=assignment_id, experience_id=experience_id)
    return
