   -- if this task is at pending_tasks, and you try and change it to a different status, but we're still
   -- waiting on other tasks to complete, then bomb out with an error
   IF (OLD.taskStatus = 'pending_tasks' AND neq(NEW.taskStatus, OLD.taskStatus)) THEN
-    SELECT count(pendingTask.taskID) INTO @num_pending_tasks FROM pendingTask
+    SELECT count(pendingTask.taskID) INTO num_pending_tasks FROM pendingTask
  LEFT JOIN task ON task.taskID = pendingTask.pendingTaskID
      WHERE pendingTask.taskID = NEW.taskID
        AND SUBSTRING(task.taskStatus,1,6) != 'closed';
 
-    IF (@num_pending_tasks > 0 AND neq(@in_change_task_status,1)) THEN
+    IF (num_pending_tasks > 0 AND neq(@in_change_task_status,1)) THEN
       call alloc_error('Task has pending tasks.');
     END IF;
   END IF;
-
 
   IF (NEW.taskStatus = 'open_inprogress' AND neq(NEW.taskStatus, OLD.taskStatus) AND empty(NEW.dateActualStart)) THEN
     SET NEW.dateActualStart = current_date();

CREATE TRIGGER before_insert_timeSheetItem BEFORE INSERT ON timeSheetItem
FOR EACH ROW
BEGIN
  call check_edit_timeSheet(NEW.timeSheetID);

  SET NEW.timeSheetItemCreatedUser = personID();
  SET NEW.timeSheetItemCreatedTime = current_timestamp();

  SELECT DATE(NEW.dateTimeSheetItem) INTO @validDate;
  IF (@validDate = '0000-00-00') THEN
    call alloc_error("Invalid date.");
    -- SET NEW.dateTimeSheetItem = NULL;
  END IF;

  SET NEW.personID = personID();
  SELECT projectID INTO @projectID FROM timeSheet WHERE timeSheet.timeSheetID = NEW.timeSheetID;
  SELECT rate,rateUnitID INTO @rate,@rateUnitID FROM projectPerson WHERE projectID = @projectID AND personID = personID();

  -- if rate is neq project-person rate AND they're don't have perm halt with error about rate
  IF (neq(NEW.rate,@rate) AND NOT can_edit_rate(personID(),@projectID)) THEN
    call alloc_error("Time sheet's rate is not editable.");
  END IF;

  IF (NEW.rate IS NULL AND @rate) THEN
    SET NEW.rate = @rate;
    SET NEW.timeSheetItemDurationUnitID = @rateUnitID;
  END IF;

  IF (NEW.taskID) THEN
    SELECT taskName INTO @taskName FROM task WHERE taskID = NEW.taskID;
    SET NEW.description = @taskName;
  END IF;

END

DROP PROCEDURE IF EXISTS check_edit_task $$
CREATE PROCEDURE check_edit_task(IN id INTEGER)
BEGIN
  DECLARE count_project INTEGER;
  SELECT count(project.projectID) INTO count_project
    FROM project LEFT JOIN projectPerson ON projectPerson.projectID = project.projectID 
   WHERE project.projectID = id AND projectPerson.personID = personID();

  IF (id AND count_project = 0 AND NOT has_perm(personID(),15,"task")) THEN
    call alloc_error('Task is not editable: user not a project member.');
  END IF;

