<?php
// Key of Project ans Task in List
$key_task = $values['tasks_task_id'];

$sql_0="select projects_project_id from tasks where task_id = $key_task";
$resql_0 = DB::Query($sql_0);
$row_0 = $resql_0->fetchAssoc();
$key_project = $row_0['projects_project_id'];
$_SESSION['project_id']= $key_project;

// datum of task LOG
$task_hours = $values['hours'];
$task_percent_complete = $values['percent_complete'];
$task_end_date = $values['task_end_date'];

// Calc hours worked
$sql_0="select sum(hours) hours from task_log where tasks_task_id = $key_task";
$resql_0 = DB::Query($sql_0);
$row_0 = $resql_0->fetchAssoc();
$task_hours = $row_0['hours'];

// Calc Task  and Project  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

if ($task_percent_complete == 100) {
		$task_end = ", actual_end_date = '$task_end_date'";
		} else {
		$task_end = ", actual_end_date = NULL";
		}


$sql_1="
update tasks
set
percent_complete=$task_percent_complete,
hours_worked= $task_hours
$task_end
where `task_id` = $key_task
";
DB::Exec($sql_1);

// Update Tasks dependence if close task
$update_dates_by_dependent =  $_SESSION['Config']['update_dates_by_dependent'];
if ($update_dates_by_dependent == 'yes' && $task_percent_complete == 100) {
		
		$sql_1="SELECT `task_id`, `start_date`, `duration`, `duration_type`, `end_date` FROM tasks where dependent = $key_task";
		$resql_1 = DB::Query($sql_1);
		while ($values = $resql_1->fetchAssoc()) {
		$values['start_date'] = $task_end_date.' '; // New date of end Task
		
		require_once ('MyCode/date_settings.php'); // Calc dates of Task
		
		$t_start_date = $values['start_date'];
		$t_end_date = $values['end_date'];
		$t_task_id =  $values['task_id'];

		$sql_2="
		update tasks
		set
		start_date = '$t_start_date',
		end_date = '$t_end_date'
		where task_id = $t_task_id
		";
		DB::Exec($sql_2);
}

}

require_once ("MyCode/calc_task_dymanic_and_project.php");

?>