<?php
#ini_set('display_errors',1);
#error_reporting(E_ALL);
#include ('global.class.php');
class Timesheet extends Base {
#private $queue;
var $id;
var $user_id;
var $year;
var $month;
public function __construct($id){
$this->id = $id;
}
function set_userID($user_id){
$this->user_id = $user_id;
return $this;
}
function set_year($year){
$this->year = $year;
return $this;
}
function set_month($month){
$this->month = $month;
return $this;
}
function getProjectsFromSheet($user_index,$year,$month){
$query=" select t.hours,t.project_id,day(t.date),p.proj_type from timesteps as t,projects as p where t.project_id = p.id and t.user_id=$user_index and year(t.date)=$year and month(t.date)=$month and t.hours!=0";
foreach (parent::dbQuery($query) as $key=>$value){
$projects[$value['proj_type']][$value['project_id']][$value['day(t.date)']]= $value['hours'];
}
return $projects;
}
function getTasksFromSheet($user_index,$year,$month){
$query=" select t.hours,t.task_id,day(t.date),ts.project_id from tasksteps as t,tasks as ts where t.task_id = ts.id and t.user_id=$user_index and year(t.date)=$year and month(t.date)=$month and t.hours!=0";
foreach (parent::dbQuery($query) as $key=>$value){
$projects[$value['project_id']][$value['task_id']][$value['day(t.date)']]= $value['hours'];
}
return $projects;
}
function getTasksFromBase($user_index,$year,$month){
$task_query="SELECT tasks.project_id,users_tasks.task_id from users_tasks,tasks where users_tasks.task_id=tasks.id and users_tasks.user_id=$user_index and users_tasks.status='Y'";
foreach (parent::dbQuery($task_query) as $key=>$value){
$result[$value['project_id']][$value['task_id']] = array();
$hours_query="SELECT day(date),hours from tasksteps where task_id=$value[task_id] and user_id=$user_index and year(date)=$year and month(date)=$month and hours!=0 and hours!=''";
foreach (parent::dbQuery($hours_query) as $days=>$hour){
$result[$value['project_id']][$value['task_id']][$hour['day(date)']]= $hour['hours'];
}
}
return $result;
}
function getProjectsFromBase($user_index,$year,$month){
$active_projects="SELECT p.proj_type,up.project_id FROM users_projects AS up, projects AS p WHERE p.id = up.project_id AND up.user_id=$user_index AND up.status = 'Y'";
foreach (parent::dbQuery($active_projects) as $key=>$value){
$result[$value['proj_type']][$value['project_id']] = array();
$hours="SELECT day(date),hours from timesteps where project_id=$value[project_id] and user_id=$user_index and year(date)=$year and month(date)=$month and hours!=0 and hours!=''";
foreach (parent::dbQuery($hours) as $days=>$hour){
$result[$value['proj_type']][$value['project_id']][$hour['day(date)']]= $hour['hours'];
}
}
return $result;
}
function getDays($year,$month){
$daysMonth = date( 't', mktime(01,01,01,$month,01,$year));
$max_days = '31';
$emptyCells = $max_days - $daysMonth;
return $answer=array('emptyCells'=>$emptyCells,'days'=>$daysMonth);
}
function get_notes($user_index,$year,$month){
$query="select day(t.date) as day,t.project_id, t.hours,t.note,p.proj_name from timesteps as t,projects as p where p.id=t.project_id and user_id='$user_index' and (hours!='' and hours!='0')and (month(date)=$month and year(date)=$year) ORDER BY day(date) ASC;";
foreach (parent::dbQuery($query) as $key=>$value){
$project_notes[$value['day']][$value['project_id']]['project']= array('note'=>$value['note'],'proj_name'=>$value['proj_name'],'hours'=>$value['hours']);
}
$query="select day(ts.date) as day,ts.task_id,ts.hours,ts.note,t.task_name,t.project_id from tasksteps as ts,tasks as t where t.id=ts.task_id and user_id='$user_index' and (hours!='' and hours!='0')and (month(date)=$month and year(date)=$year) ORDER BY day(date) ASC;";
foreach (parent::dbQuery($query) as $key=>$value){
$task_notes[$value['day']][$value['project_id']][$value['task_id']]= array('note'=>$value['note'],'task_name'=>$value['task_name'],'hours'=>$value['hours']);
}
foreach($project_notes as $day=>$value){
foreach ($project_notes[$day] as $project_id=>$data){
if (isset($task_notes[$day][$project_id])){
foreach ($task_notes[$day][$project_id]as $task_index=>$tasks){
$project_notes[$day][$project_id]['task'][$task_index]= array('note'=>$tasks['note'],'hours'=>$tasks['hours'],'task_name'=>$tasks['task_name']);
}
}
}
}
return $project_notes;
}
function setNotes($user_index,$array,$year,$month)
{
foreach($array as $key=>$value)
{
if($key == 'project_notes'){
foreach ($value as $project_id=>$data){
foreach ($data as $day=>$note){
$query = "UPDATE timesteps SET note='$note' WHERE user_id='$user_index' AND project_id='$project_id' AND year(date)='$year' and month(date)='$month' and day(date)='$day'";
parent::dbExec($query);
}
}
}elseif($key == 'task_notes'){
foreach ($value as $project_id=>$data){
foreach ($data as $day=>$note){
$query = "UPDATE tasksteps SET note='$note' WHERE user_id='$user_index' AND task_id='$project_id' AND year(date)='$year' and month(date)='$month' and day(date)='$day'";
parent::dbExec($query);
}
}
}
}
}
function setHours($user_index,$array,$year,$month){
foreach($array as $key=>$value)
{
if($key == 'tasks'){
foreach ($value as $project_id=>$data){
foreach ($data as $task_id=>$date_hours){
foreach ($date_hours as $day=>$hour){
$query = "INSERT INTO `timesheet`.`tasksteps` ( `user_id` , `task_id`,parent_id, `confirm` , `date` , `hours` ) VALUES ('$user_index','$task_id','$project_id','Y','$year-$month-$day','$hour') ON DUPLICATE KEY UPDATE hours = '$hour'";
parent::dbExec($query);
$update_projects[$project_id][$day]['hours']= $update_projects[$project_id][$day]['hours'] +$hour;
}
}
}
foreach($update_projects as $project_id=>$value){
foreach ($value as $day=>$hour){
$query = "INSERT INTO `timesheet`.`timesteps` ( `user_id` , `project_id`,`confirm` , `date` , `hours` ) VALUES ('$user_index','$project_id','Y','$year-$month-$day','$hour[hours]') ON DUPLICATE KEY UPDATE `timesteps`.`hours` = '$hour[hours]'";
parent::dbExec($query);
}
}
}elseif($key == 'projects'){
foreach ($value as $project_id=>$data){
foreach ($data as $day=>$hours){
$query = "INSERT INTO `timesheet`.`timesteps` ( `user_id` , `project_id`,`confirm` , `date` , `hours` ) VALUES ('$user_index','$project_id','Y','$year-$month-$day','$hours') ON DUPLICATE KEY UPDATE `timesteps`.`hours` = '$hours'";
parent::dbExec($query);
}
}
}
}
}
}
?>