/**
  * @see ProjectTaskInterface::create_status_process()
  * @param integer $project_id
  * @param integer $user_id
  * @param string $comment
  * @param string $start_date
  * @param string $start_time
  * @param string $end_date
  * @param string $end_time
  * @param bool $whole_day
  * @param integer $end_status_id
  * @param bool $finalise
  * @param bool $auto_connect
  * @return integer
  * @throws ProjectTaskCreateException
  * @throws ProjectTaskCreateAttachException
  */
 public function create_status_process($project_id, $user_id, $comment, $start_date, $start_time, $end_date, $end_time, $whole_day, $end_status_id, $finalise, $auto_connect)
 {
     global $transaction;
     if (is_numeric($project_id) and is_numeric($user_id) and $end_date and ($end_time or $whole_day == true) and $end_status_id) {
         $transaction_id = $transaction->begin();
         $project = new Project($project_id);
         $project_task_point = new ProjectTaskPoint($project_id);
         $start_status_id = $project->get_current_status_id();
         if ($auto_connect == true) {
             // Get all autoconnected tasks of the project
             $task_array = ProjectTask_Access::list_auto_connected_entries_by_project_id($project_id);
             // Create Task
             $current_task_object = new ProjectTask_Access(null);
             $current_task_id = $current_task_object->create(1, $project_id, $user_id, $comment, $start_date, $start_time, $end_date, $end_time, $whole_day, true);
             if ($current_task_id) {
                 $current_task_type_object = new ProjectTaskStatusProcess_Access(null);
                 if ($current_task_type_object->create($current_task_id, $start_status_id, $end_status_id, $project_task_point->get_current_achieved_points(null), $finalise) == null) {
                     if ($transaction_id != null) {
                         $transaction->rollback($transaction_id);
                     }
                     throw new ProjectTaskCreateException("Could not create Status Task");
                 }
                 if (is_array($task_array) and count($task_array) >= 1) {
                     foreach ($task_array as $key => $value) {
                         $task_object = new ProjectTask_Access($value);
                         if ($task_object->get_type_id() == 1) {
                             $task_type_object = new ProjectTaskStatusProcess_Access($value);
                             $project_start_status_relation = new ProjectStatusRelation($project_id, $task_type_object->get_begin_status_id());
                             $project_end_status_relation = new ProjectStatusRelation($project_id, $task_type_object->get_end_status_id());
                             if ($project_end_status_relation->get_next() == $start_status_id) {
                                 // Case 1: End-status of an existing task will be start-status of the new task
                                 // Attach task directly
                                 $project_task_has_previous_task = new ProjectTaskHasPreviousTask_Access(null, null);
                                 if ($project_task_has_previous_task->create($current_task_id, $value) == null) {
                                     if ($transaction_id != null) {
                                         $transaction->rollback($transaction_id);
                                     }
                                     return null;
                                 }
                             } else {
                                 // Case 2: Start-status is inside an existing task
                                 // Case 2a: New task will be included after an existing task, existing task will be shortened
                                 // (e.g. include a task with the same begin like an existing task and choose a later status)
                                 $object_datetime_handler = new DatetimeHandler($task_object->get_end_date());
                                 $current_datetime_handler = new DatetimeHandler($end_date);
                                 if ($project_end_status_relation->is_less($end_status_id) and $current_datetime_handler->distance($object_datetime_handler) < 0) {
                                     // Check if interlinked
                                     $next_task_array = ProjectTaskHasPreviousTask_Access::list_tasks_by_previous_task_id($value);
                                     $connect_task = true;
                                     if (is_array($next_task_array) and count($next_task_array) >= 1) {
                                         foreach ($next_task_array as $next_key => $next_value) {
                                             $check_task_object = new ProjectTask_Access($next_value);
                                             if ($check_task_object->get_type_id() == 1) {
                                                 $check_task_type_object = new ProjectTaskStatusProcess_Access($value);
                                                 $check_project_start_status_relation = new ProjectStatusRelation($project_id, $check_task_type_object->get_begin_status_id());
                                                 $check_project_end_status_relation = new ProjectStatusRelation($project_id, $check_task_type_object->get_end_status_id());
                                                 if ($check_task_object->get_end_time() != null) {
                                                     $check_object_datetime_handler = new DatetimeHandler($check_task_object->get_end_date() . " " . $check_task_object->get_end_time());
                                                 } else {
                                                     $check_object_datetime_handler = new DatetimeHandler($check_task_object->get_end_date() . " 23:59:59");
                                                 }
                                                 if ($whole_day == false and $end_time) {
                                                     $check_current_datetime_handler = new DatetimeHandler($end_date . " " . $end_time);
                                                 } else {
                                                     $check_current_datetime_handler = new DatetimeHandler($end_date . " 23:59:59");
                                                 }
                                                 if ($check_project_end_status_relation->is_less($end_status_id) and $check_current_datetime_handler->distance($check_object_datetime_handler) < 0) {
                                                     $connect_task = false;
                                                 }
                                             }
                                         }
                                     }
                                     if ($connect_task == true) {
                                         if ($task_object->get_whole_day() == true) {
                                             $new_start_datetime_handler = new DatetimeHandler($task_object->get_end_date());
                                             $new_start_datetime_handler->add_day(1);
                                             if ($current_task_object->set_start_date($new_start_datetime_handler->get_formatted_string("Y-m-d")) == false) {
                                                 if ($transaction_id != null) {
                                                     $transaction->rollback($transaction_id);
                                                 }
                                                 throw new ProjectTaskCreateAttachException("Could not set start date (whole day)");
                                             }
                                         } else {
                                             $new_start_datetime_handler = new DatetimeHandler($task_object->get_end_date() . " " . $task_object->get_end_time());
                                             $new_start_datetime_handler->add_second(1);
                                             if ($current_task_object->set_start_date($new_start_datetime_handler->get_formatted_string("Y-m-d")) == false) {
                                                 if ($transaction_id != null) {
                                                     $transaction->rollback($transaction_id);
                                                 }
                                                 throw new ProjectTaskCreateAttachException("Could not set start date (time)");
                                             }
                                             if ($current_task_object->set_start_time($new_start_datetime_handler->get_formatted_string("H:i:s")) == false) {
                                                 if ($transaction_id != null) {
                                                     $transaction->rollback($transaction_id);
                                                 }
                                                 throw new ProjectTaskCreateAttachException("Could not set start time (time)");
                                             }
                                         }
                                         if ($current_task_type_object->set_begin_status_id($task_type_object->get_end_status_id()) == false) {
                                             if ($transaction_id != null) {
                                                 $transaction->rollback($transaction_id);
                                             }
                                             throw new ProjectTaskCreateAttachException("Could not set start status id");
                                         }
                                         // Change later tasks
                                         $next_task_array = ProjectTaskHasPreviousTask_Access::list_tasks_by_previous_task_id($value);
                                         if (is_array($next_task_array) and count($next_task_array) >= 1) {
                                             foreach ($next_task_array as $next_key => $next_value) {
                                                 $project_task_has_previous_task = new ProjectTaskHasPreviousTask_Access($next_value, $value);
                                                 if ($project_task_has_previous_task->set_previous_task_id($current_task_id) == null) {
                                                     if ($transaction_id != null) {
                                                         $transaction->rollback($transaction_id);
                                                     }
                                                     throw new ProjectTaskCreateAttachException("Could not set previous task id");
                                                 }
                                             }
                                         }
                                         // Connect Tasks
                                         $project_task_has_previous_task = new ProjectTaskHasPreviousTask_Access(null, null);
                                         if ($project_task_has_previous_task->create($current_task_id, $value) == null) {
                                             if ($transaction_id != null) {
                                                 $transaction->rollback($transaction_id);
                                             }
                                             throw new ProjectTaskCreateAttachException("Could not create task link");
                                         }
                                     }
                                 }
                                 // Case 2b: New task will be included before an existing task
                                 if (($project_start_status_relation->is_less($end_status_id) or $task_type_object->get_begin_status_id() == $end_status_id) and $project_end_status_relation->is_more($end_status_id) and $current_datetime_handler->distance($object_datetime_handler) >= 0) {
                                     // Change Date and Status
                                     if ($whole_day == true) {
                                         $new_end_datetime_handler = new DatetimeHandler($end_date);
                                         $new_end_datetime_handler->add_day(1);
                                         if ($task_object->set_start_date($new_end_datetime_handler->get_formatted_string("Y-m-d")) == false) {
                                             if ($transaction_id != null) {
                                                 $transaction->rollback($transaction_id);
                                             }
                                             throw new ProjectTaskCreateAttachException("Could not set start date (whole day)");
                                         }
                                     } else {
                                         $new_end_datetime_handler = new DatetimeHandler($end_date . " " . $end_time);
                                         $new_end_datetime_handler->add_second(1);
                                         if ($task_object->set_start_date($new_end_datetime_handler->get_formatted_string("Y-m-d")) == false) {
                                             if ($transaction_id != null) {
                                                 $transaction->rollback($transaction_id);
                                             }
                                             throw new ProjectTaskCreateAttachException("Could not set start date (time)");
                                         }
                                         if ($task_object->set_start_time($new_end_datetime_handler->get_formatted_string("H:i:s")) == false) {
                                             if ($transaction_id != null) {
                                                 $transaction->rollback($transaction_id);
                                             }
                                             throw new ProjectTaskCreateAttachException("Could not set start time (time)");
                                         }
                                     }
                                     if ($task_type_object->set_begin_status_id($end_status_id) == false) {
                                         if ($transaction_id != null) {
                                             $transaction->rollback($transaction_id);
                                         }
                                         throw new ProjectTaskCreateAttachException("Could not start status id");
                                     }
                                     // Change previous tasks
                                     $previous_task_array = ProjectTaskHasPreviousTask_Access::list_previous_tasks_by_task_id($value);
                                     if (is_array($previous_task_array) and count($previous_task_array) >= 1) {
                                         foreach ($previous_task_array as $previous_key => $previous_value) {
                                             $project_task_has_previous_task = new ProjectTaskHasPreviousTask_Access($value, $previous_value);
                                             if ($project_task_has_previous_task->set_task_id($current_task_id) == false) {
                                                 if ($transaction_id != null) {
                                                     $transaction->rollback($transaction_id);
                                                 }
                                                 throw new ProjectTaskCreateAttachException("Could not set previous task");
                                             }
                                         }
                                     }
                                     // Connect Tasks
                                     $project_task_has_previous_task = new ProjectTaskHasPreviousTask_Access(null, null);
                                     if ($project_task_has_previous_task->create($value, $current_task_id) == null) {
                                         if ($transaction_id != null) {
                                             $transaction->rollback($transaction_id);
                                         }
                                         throw new ProjectTaskCreateAttachException("Could not create task link");
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if ($transaction_id != null) {
                     $transaction->commit($transaction_id);
                 }
                 return $current_task_id;
             } else {
                 if ($transaction_id != null) {
                     $transaction->rollback($transaction_id);
                 }
                 throw new ProjectTaskCreateException("Could not create parent Task");
             }
         } else {
             $current_task_object = new ProjectTask_Access(null);
             if (($current_task_id = $current_task_object->create(1, $project_id, $user_id, $comment, $start_date, $start_time, $end_date, $end_time, $whole_day, false)) != null) {
                 $current_task_type_object = new ProjectTaskStatusProcess_Access(null);
                 if ($current_task_type_object->create($current_task_id, $start_status_id, $end_status_id, $project_task_point->get_current_achieved_points(), $finalise) != null) {
                     if ($transaction_id != null) {
                         $transaction->commit($transaction_id);
                     }
                     return $current_task_id;
                 } else {
                     if ($transaction_id != null) {
                         $transaction->rollback($transaction_id);
                     }
                     throw new ProjectTaskCreateException("Could not create Status Task");
                 }
             } else {
                 if ($transaction_id != null) {
                     $transaction->rollback($transaction_id);
                 }
                 throw new ProjectTaskCreateException("Could not create parent Task");
             }
         }
     } else {
         throw new ProjectTaskCreateException("Information Missing");
     }
 }