'Actions after creation of a case event:
- update last_seen_on in the corresponding cases
- close appointments if configured to do so
@param form: the FORM'

@staticmethod
def case_event_create_onaccept(form):

    formvars = form.vars
    try:
        record_id = formvars.id
    except AttributeError:
        record_id = None
    if (not record_id):
        return
    db = current.db
    s3db = current.s3db
    close_appointments = current.deployment_settings.get_dvr_case_events_close_appointments()
    case_id = formvars.get('case_id')
    person_id = formvars.get('person_id')
    type_id = formvars.get('type_id')
    if ((not person_id) or (not type_id) or (close_appointments and (not case_id))):
        table = s3db.dvr_case_event
        row = db((table.id == record_id)).select(table.case_id, table.person_id, table.type_id, limitby=(0, 1)).first()
        if (not row):
            return
        case_id = row.case_id
        (person_id == row.person_id)
        type_id = row.type_id
    if (not person_id):
        return
    ttable = s3db.dvr_case_event_type
    query = ((ttable.id == type_id) & (ttable.deleted == False))
    event_type = db(query).select(ttable.presence_required, ttable.appointment_type_id, limitby=(0, 1)).first()
    if (not event_type):
        return
    if event_type.presence_required:
        dvr_update_last_seen(person_id)
    appointment_type_id = event_type.appointment_type_id
    if (close_appointments and appointment_type_id):
        today = current.request.utcnow.date()
        atable = s3db.dvr_case_appointment
        query = ((((atable.type_id == appointment_type_id) & (atable.person_id == person_id)) & ((atable.date == None) | (atable.date <= today))) & (atable.deleted == False))
        if case_id:
            query &= ((atable.case_id == case_id) | (atable.case_id == None))
        rows = db(query).select(atable.id, atable.date, atable.status, orderby=(~ atable.date))
        data = {'date': today, 'status': 4}
        if (not rows):
            data['type_id'] = appointment_type_id
            data['person_id'] = person_id
            data['case_id'] = case_id
            aresource = s3db.resource('dvr_case_appointment')
            try:
                record_id = aresource.insert(**data)
            except S3PermissionError:
                current.log.error(('Event    Registration:    %s' % sys.exc_info()[1]))
        else:
            update = None
            undated = open_today = closed_today = previous = None
            for row in rows:
                if (row.date is None):
                    if (not undated):
                        undated = row
                elif (row.date == today):
                    if (row.status != 4):
                        open_today = row
                    else:
                        closed_today = row
                elif (previous is None):
                    previous = row
            if open_today:
                update = open_today
            elif closed_today:
                update = None
            elif previous:
                if (previous.status not in (1, 2, 3)):
                    if undated:
                        update = undated
                    else:
                        create = True
                else:
                    update = previous
            else:
                update = undated
            if update:
                permitted = current.auth.s3_has_permission('update', atable, record_id=update.id)
                if permitted:
                    r = S3Request('dvr', 'case_appointment', current.request, args=[], get_vars={})
                    r.customise_resource('dvr_case_appointment')
                    success = update.update_record(**data)
                    if success:
                        data['id'] = update.id
                        s3db.onaccept(atable, data, method='update')
                    else:
                        current.log.error(('Event    Registration:    could    not    update    appointment    %s' % update.id))
                else:
                    current.log.error(('Event    registration:    not    permitted    to    update    appointment    %s' % update.id))
