 CREATE TRIGGER before_insert_timeSheet BEFORE INSERT ON timeSheet
 FOR EACH ROW
 BEGIN
+  DECLARE preferred_tfID INTEGER;
+  DECLARE cbd BIGINT(20);
+  DECLARE cur VARCHAR(3);
   SET NEW.personID = personID();
   SET NEW.status = 'edit';
-  SELECT preferred_tfID INTO @preferred_tfID FROM person WHERE personID = personID();
-  SET NEW.recipient_tfID = @preferred_tfID;
-  SELECT customerBilledDollars,currencyTypeID INTO @cbd,@cur FROM project WHERE projectID = NEW.projectID;
-  SET NEW.customerBilledDollars = @cbd;
-  SET NEW.currencyTypeID = @cur;
+  SELECT preferred_tfID INTO preferred_tfID FROM person WHERE personID = personID();
+  SET NEW.recipient_tfID = preferred_tfID;
+  SELECT customerBilledDollars,currencyTypeID INTO cbd,cur FROM project WHERE projectID = NEW.projectID;
+  SET NEW.customerBilledDollars = cbd;
+  SET NEW.currencyTypeID = cur;
   SET NEW.dateFrom = null;
   SET NEW.dateTo = null;
   SET NEW.approvedByManagerPersonID = null;

DROP PROCEDURE IF EXISTS check_edit_timeSheet $$
CREATE PROCEDURE check_edit_timeSheet(IN id INTEGER)
BEGIN
  SELECT status INTO @timeSheetStatus FROM timeSheet WHERE timeSheetID = id;
  if (neq(@timeSheetStatus, "edit") AND NOT has_perm(personID(),512,"timeSheet")) THEN
    call alloc_error('Time sheet is not editable.');
  END IF;
END

DROP PROCEDURE IF EXISTS change_task_status $$
CREATE PROCEDURE change_task_status(tID INTEGER, new_status varchar(255))
BEGIN
  DECLARE old_status VARCHAR(255);
  SET max_sp_recursion_depth = 10; 
  SET @in_change_task_status = 1;

  SELECT taskStatus INTO old_status FROM task WHERE taskID = tID;
  IF (neq(old_status,new_status)) THEN

    -- If just moved to closed
    IF (neq(SUBSTRING(old_status,1,6),'closed') AND SUBSTRING(new_status,1,6) = 'closed') THEN

      call update_pending_task(tID, "open_notstarted");
      SET @in_change_task_status = 1; 

    -- Else if just re-opened
    ELSEIF (SUBSTRING(old_status,1,6) = 'closed' AND neq(SUBSTRING(new_status,1,6),'closed')) THEN
      UPDATE task SET taskStatus = 'pending_tasks'
       WHERE taskStatus = 'open_notstarted'
         AND taskID IN (SELECT taskID FROM pendingTask WHERE pendingTaskID = tID);
