@staticmethod
    def case_event_create_onaccept(form):
        """
            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
        """

        formvars = form.vars
        try:
            record_id = formvars.id
        except __HOLE__:
            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 \
           close_appointments and (not case_id or not type_id):
            # Reload the record
            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

        # Update last_seen
        dvr_update_last_seen(person_id)

        # Close appointments
        if close_appointments and type_id:

            atable = s3db.dvr_case_appointment
            ttable = s3db.dvr_case_event_type

            today = current.request.utcnow.date()

            left = atable.on((atable.type_id == ttable.appointment_type_id) & \
                             (atable.person_id == person_id) & \
                             ((atable.date == None) | (atable.date <= today)) & \
                             (atable.deleted != True)
                             )

            query = (ttable.id == type_id) & \
                    (ttable.appointment_type_id != None) & \
                    (ttable.deleted != True)

            if case_id:
                query &= (atable.case_id == case_id) | \
                         (atable.case_id == None)
            rows = db(query).select(ttable.appointment_type_id,
                                    atable.id,
                                    atable.date,
                                    atable.status,
                                    left = left,
                                    orderby = ~atable.date,
                                    )

            data = {"date": today, "status": 4}
            if rows:
                update = None
                create = False

                first = rows[0].dvr_case_appointment
                if first.id is None:
                    # No appointment of this type yet
                    create = True

                else:
                    # Find key dates
                    undated = open_today = closed_today = previous = None
                    for row in rows:
                        appointment = row.dvr_case_appointment
                        if appointment.date is None:
                            if not undated:
                                # An appointment without date
                                undated = appointment
                        elif appointment.date == today:
                            if appointment.status != 4:
                                # An open or cancelled appointment today
                                open_today = appointment
                            else:
                                # A closed appointment today
                                closed_today = appointment
                        elif previous is None:
                            # The last appointment before today
                            previous = appointment

                    if open_today:
                        # If we have an open appointment for today, update it
                        update = open_today
                    elif closed_today:
                        # If we already have a closed appointment for today,
                        # do nothing
                        update = None
                    elif previous:
                        if previous.status not in (1, 2, 3):
                            # Last appointment before today is closed
                            # => create a new one unless there is an undated one
                            if undated:
                                update = undated
                            else:
                                create = True
                        else:
                            # Last appointment before today is still open
                            # => update it
                            update = previous
                    else:
                        update = undated

                if create:
                    # Create a new closed appointment
                    data["type_id"] = rows[0].dvr_case_event_type.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])

                elif update:
                    # Update the appointment
                    permitted = current.auth.s3_has_permission("update",
                                                               atable,
                                                               record_id=update.id,
                                                               )
                    if permitted:
                        # Customise appointment resource
                        r = S3Request("dvr", "case_appointment",
                                      current.request,
                                      args = [],
                                      get_vars = {},
                                      )
                        r.customise_resource("dvr_case_appointment")
                        # Update 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)

    # -------------------------------------------------------------------------
dataset/ETHPy150Open sahana/eden/modules/s3db/dvr.py/DVRCaseEventModel.case_event_create_onaccept