<?php require_once('../Connections/oprmConn.php');

if(isset($_REQUEST['styear'])) {
$syear = $_REQUEST['styear'];	
$eyear = $_REQUEST['edyear'];
$sweek = $_REQUEST['stweek'];	
$eweek = $_REQUEST['edweek'];

//echo $syear.$eyear.$sweek.$eweek;

Header("Content-Disposition: attachment; filename=timesheets.csv");
echo 'Year:'.$syear.' Week:'.$sweek.'  TO  Year:'.$eyear.' Week:'.$eweek; ?>,
Week Number,Start Date,Resource,Projects(Hrs.),Total Hours,Status
<?php mysql_select_db($database_oprmConn, $oprmConn);
$query_getresult = "SELECT * FROM weby_timesheets WHERE status IN ('A','Q') AND task_year >= '$syear' AND task_year <= '$eyear' AND task_week >= '$sweek' AND task_week <= '$eweek' GROUP BY user_id,task_week";
$getresult = mysql_query($query_getresult, $oprmConn) or die(mysql_error());
$row_getresult = mysql_fetch_assoc($getresult);
$totalRows_getresult = mysql_num_rows($getresult);
if($totalRows_getresult>0){ do{ ?>
<?php echo $row_getresult['task_week']; ?>,<?php $weeknum = $row_getresult['task_week'];
if($weeknum<10){ $weeknum = '0'.$weeknum; } else { $weeknum = $weeknum; }
$weekyear = $row_getresult['task_year'];
echo date("Y-m-d", strtotime($weekyear.'W'.$weeknum)); ?>,<?php //get resource name
mysql_select_db($database_oprmConn, $oprmConn);
$query_getresource = "SELECT * FROM weby_profiles WHERE status = 'A' AND user_id = '$row_getresult[user_id]'";
$getresource = mysql_query($query_getresource, $oprmConn) or die(mysql_error());
$row_getresource = mysql_fetch_assoc($getresource);
$totalRows_getresource = mysql_num_rows($getresource);	
echo $row_getresource['full_name']; ?>,<?php //get project name and hrs
	mysql_select_db($database_oprmConn, $oprmConn);
$query_getProjects = "SELECT weby_projects.project_name,SUM(`total_hrs`) FROM weby_timesheets,weby_projects WHERE weby_timesheets.status IN ('A','Q') AND weby_timesheets.task_year = '$row_getresult[task_year]' AND weby_timesheets.task_week = '$row_getresult[task_week]' AND weby_timesheets.user_id = '$row_getresult[user_id]' AND weby_timesheets.project_id=weby_projects.projetc_id GROUP BY weby_timesheets.user_id,weby_timesheets.task_week,weby_timesheets.project_id ORDER BY weby_projects.project_name ASC";
$getProjects = mysql_query($query_getProjects, $oprmConn) or die(mysql_error());
$row_getProjects = mysql_fetch_assoc($getProjects);
$totalRows_getProjects = mysql_num_rows($getProjects); $i=1;
do{ echo $i.'. '.$row_getProjects['project_name'].' ('.$row_getProjects['SUM(`total_hrs`)'].' hrs)  '; $i++;
}while($row_getProjects = mysql_fetch_assoc($getProjects)); ?>,<?php //get project total hrs
	mysql_select_db($database_oprmConn, $oprmConn);
$query_gettotalhrs = "SELECT SUM(`total_hrs`) FROM weby_timesheets,weby_projects WHERE weby_timesheets.status IN ('A','Q') AND weby_timesheets.task_year = '$row_getresult[task_year]' AND weby_timesheets.task_week = '$row_getresult[task_week]' AND weby_timesheets.user_id = '$row_getresult[user_id]' AND weby_timesheets.project_id=weby_projects.projetc_id GROUP BY weby_timesheets.user_id,weby_timesheets.task_week";
$gettotalhrs = mysql_query($query_gettotalhrs, $oprmConn) or die(mysql_error());
$row_gettotalhrs = mysql_fetch_assoc($gettotalhrs);
$totalRows_gettotalhrs = mysql_num_rows($gettotalhrs);
echo $row_gettotalhrs['SUM(`total_hrs`)']; ?>,<?php if($row_getresult['status']=='Q'){ echo 'Approve'; } else if($row_getresult['status']=='A'){ echo 'Pending'; } ?>

<?php } while($row_getresult = mysql_fetch_assoc($getresult)); }} ?>