'When Time is logged, update the Task & Activity'

@staticmethod
def project_time_onaccept(form):

    db = current.db
    titable = db.project_time
    ttable = db.project_task
    atable = db.project_activity
    tatable = db.project_task_activity
    task_id = form.vars.task_id
    if (not task_id):
        query = (titable.id == form.vars.id)
        record = db(query).select(titable.task_id, limitby=(0, 1)).first()
        if record:
            task_id = record.task_id
    query = ((titable.deleted == False) & (titable.task_id == task_id))
    rows = db(query).select(titable.hours)
    hours = 0
    for row in rows:
        if row.hours:
            hours += row.hours
    query = (ttable.id == task_id)
    db(query).update(time_actual=hours)
    query = ((tatable.deleted == False) & (tatable.task_id == task_id))
    activity = db(query).select(tatable.activity_id, limitby=(0, 1)).first()
    if activity:
        activity_id = activity.activity_id
        query = ((((ttable.deleted == False) & (tatable.deleted == False)) & (tatable.task_id == ttable.id)) & (tatable.activity_id == activity_id))
        tasks = db(query).select(ttable.time_actual)
        hours = 0
        for task in tasks:
            hours += (task.time_actual or 0)
        query = (atable.id == activity_id)
        db(query).update(time_actual=hours)
