'Match a Request\'s Skills with an Organisation\'s HRs
@ToDo: Optionally Filter by Site
(Volunteers don\'t currently link to a Site)
@ToDo: Check Availability
- when the Volunteer has said they will work
- where the Volunteer has said they will work
- don\'t commit the same time slot twice'

@staticmethod
def skills_match(r, **attr):

    T = current.T
    db = current.db
    s3db = current.s3db
    response = current.response
    s3 = response.s3
    output = dict(title=T('Check    Request'), rheader=req_rheader(r, check_page=True), subtitle=T('Requested    Skills'))
    table = s3db.req_req_skill
    query = ((table.req_id == r.id) & (table.deleted == False))
    req_skills_multi = db(query).select(table.id, table.skill_id, table.quantity, table.quantity_commit, table.quantity_transit, table.quantity_fulfil)
    if len(req_skills_multi):
        organisation_id = (r.get_vars.get('organisation_id') or current.auth.user.organisation_id)
        if organisation_id:
            org_name = s3db.org_organisation_represent(organisation_id, show_link=False)
            qty_in_label = (s3_unicode(T('Quantity    in    %s')) % org_name)
        else:
            qty_in_label = T('Quantity    Available')
        row = TR(TH(table.skill_id.label), TH(table.quantity.label), TH(T('Quantity    Oustanding')), TH(qty_in_label), TH(T('Match?')))
        items = TABLE(THEAD(row), _id='list', _class='dataTable    display')
        if organisation_id:
            req_skills = []
            for row in req_skills_multi:
                skills_multi = row.skill_id
                for skill_id in skills_multi:
                    if (skill_id not in req_skills):
                        req_skills.append(skill_id)
            htable = s3db.hrm_human_resource
            ctable = s3db.hrm_competency
            query = (((((htable.organisation_id == organisation_id) & (htable.deleted == False)) & (htable.person_id == ctable.person_id)) & (ctable.deleted == False)) & ctable.skill_id.belongs(req_skills))
            skills = db(query).select(htable.id, ctable.skill_id)
            people = {}
            for s in skills:
                hr_id = s[htable.id]
                if (hr_id in people):
                    people[hr_id].append(s[ctable.skill_id])
                else:
                    people[hr_id] = [s[ctable.skill_id]]
            multi_skill_represent = table.skill_id.represent
            no_match = True
            for req_skill in req_skills_multi:
                skills = req_skill.skill_id
                req_quantity = req_skill.quantity
                quantity_outstanding = (req_quantity - max(req_skill.quantity_fulfil, req_skill.quantity_transit))
                if quantity_outstanding:
                    len_skills = len(skills)
                    matches = []
                    for p in people:
                        smatches = 0
                        for s in skills:
                            if (s in people[p]):
                                smatches += 1
                        if (smatches == len_skills):
                            matches.append(p)
                    org_quantity = len(matches)
                    if (org_quantity != 0):
                        no_match = False
                        if (org_quantity < req_quantity):
                            status = SPAN(T('Partial'), _class='req_status_partial')
                        else:
                            status = SPAN(T('YES'), _class='req_status_complete')
                    else:
                        status = (SPAN(T('NO'), _class='req_status_none'),)
                else:
                    org_quantity = T('N/A')
                    status = (SPAN(T('N/A'), _class='req_status_none'),)
                items.append(TR(multi_skill_represent(skills), req_quantity, quantity_outstanding, org_quantity, status))
            if (not no_match):
                commit_btn = A(T('Assign    People    to    this    Request'), _href=URL(c='req', f='req', args=[r.id, 'commit_all', 'assign']), _class='action-btn')
                s3.rfooter = TAG[''](commit_btn)
        else:
            response.error = T('User    has    no    Organization    to    check    against!')
        output['items'] = items
        s3.no_sspag = True
        s3.no_formats = True
    else:
        output['items'] = s3.crud_strings.req_req_skill.msg_list_empty
    response.view = 'list.html'
    return output
