<?php require_once('../Connections/oprmConn.php'); 
if(!isset($_SESSION['user_id']))
{
	header("location: ../index.php");
}
if($_SESSION['role_id']!=2) {
header("Location: ../index.php");
}
  $msg = ""; if(isset($_REQUEST['msg'])) $msg = $_REQUEST['msg']; 
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
	  break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


if (!isset($_GET['timesheets'])) {

$maxRows_getTime = 10;

$pageNum_getTime = 0;

}

if (isset($_GET['pageNum_getTime'])) {

  $pageNum_getTime = $_GET['pageNum_getTime'];

}

$maxRows_getTime = 10;

$pageNum_getTime = 0;

if (isset($_GET['pageNum_getTime'])) {

  $pageNum_getTime = $_GET['pageNum_getTime'];

}

$startRow_getTime = $pageNum_getTime * $maxRows_getTime;

$uid = $_SESSION['user_id'];
//echo date('Y');




mysql_select_db($database_oprmConn, $oprmConn);
$query_getSelecty = "SELECT DISTINCT (task_year), task_year FROM weby_timesheets WHERE user_id = '$uid' AND status in ('A', 'I', 'B', 'Q')";
$getSelecty = mysql_query($query_getSelecty, $oprmConn) or die(mysql_error());
$row_getSelecty = mysql_fetch_assoc($getSelecty);
$totalRows_getSelecty = mysql_num_rows($getSelecty);

mysql_select_db($database_oprmConn, $oprmConn);
$query_getSelectw = "SELECT DISTINCT (task_week), task_year FROM weby_timesheets WHERE user_id = '$uid' AND task_year = '$_GET[year]' AND status in ('A', 'I', 'B', 'Q')";
$getSelectw = mysql_query($query_getSelectw, $oprmConn) or die(mysql_error());
$row_getSelectw = mysql_fetch_assoc($getSelectw);
$totalRows_getSelectw = mysql_num_rows($getSelectw);

	mysql_select_db($database_oprmConn, $oprmConn);
	$query_getTime = "SELECT DISTINCT (task_week), task_year FROM weby_timesheets WHERE user_id = '$uid' AND status in ('A', 'I', 'B', 'Q') ORDER BY task_year DESC, task_week DESC";
	$query_limit_getTime = sprintf("%s LIMIT %d, %d", $query_getTime, $startRow_getTime, $maxRows_getTime);
	$getTime = mysql_query($query_limit_getTime, $oprmConn) or die(mysql_error());
	$row_getTime = mysql_fetch_assoc($getTime);

	
	
	if((isset($_GET['year'])) && ((!isset($_GET['week'])))){
			$week = $_GET['week'];
			$year = $_GET['year'];
			mysql_select_db($database_oprmConn, $oprmConn);
	$query_getTime = "SELECT DISTINCT (task_week), task_year FROM weby_timesheets WHERE user_id = '$uid' AND status in ('A', 'I', 'B', 'Q') AND task_year = '$year' ORDER BY task_week DESC";
	$query_limit_getTime = sprintf("%s LIMIT %d, %d", $query_getTime, $startRow_getTime, $maxRows_getTime);
	$getTime = mysql_query($query_limit_getTime, $oprmConn) or die(mysql_error());
	$row_getTime = mysql_fetch_assoc($getTime);
	
	if($year == '0'){
			$url="timesheet-archive.php";

			header("Location:$url");
			}
	}
	if((isset($_GET['year'])) && ((isset($_GET['week'])))){
			$week = $_GET['week'];
			$year = $_GET['year'];
			mysql_select_db($database_oprmConn, $oprmConn);
			$query_getTime = "SELECT DISTINCT (task_week), task_year FROM weby_timesheets WHERE user_id = '$uid' AND status in ('A', 'I', 'B', 'Q') AND task_year = '$year' AND task_week = '$week' ORDER BY task_week DESC";			
			$query_limit_getTime = sprintf("%s LIMIT %d, %d", $query_getTime, $startRow_getTime, $maxRows_getTime);			
			$getTime = mysql_query($query_limit_getTime, $oprmConn) or die(mysql_error());			
			$row_getTime = mysql_fetch_assoc($getTime);
					
	if($week == '0'){
			$url="timesheet-archive.php?year=$year";

			header("Location:$url");
					
			}
	}
			
	



if (isset($_GET['totalRows_getTime'])) {

  $totalRows_getTime = $_GET['totalRows_getTime'];

} else {

  $all_getTime = mysql_query($query_getTime);

  $totalRows_getTime = mysql_num_rows($all_getTime);

}

$totalPages_getTime = ceil($totalRows_getTime/$maxRows_getTime)-1;



$queryString_getTime = "";

if (!empty($_SERVER['QUERY_STRING'])) {

  $params = explode("&", $_SERVER['QUERY_STRING']);

  $newParams = array();

  foreach ($params as $param) {

    if (stristr($param, "pageNum_getTime") == false && 

        stristr($param, "totalRows_getTime") == false) {

      array_push($newParams, $param);

    }

  }

}


$uid = $_SESSION['user_id'];

mysql_select_db($database_oprmConn, $oprmConn);

$query_getProfile = "SELECT weby_profiles.*, weby_users.caeated_date as jdate FROM weby_profiles, weby_users WHERE weby_profiles.user_id = '$uid' AND weby_users.user_id =  weby_profiles.user_id ";

$getProfile = mysql_query($query_getProfile, $oprmConn) or die(mysql_error());

$row_getProfile = mysql_fetch_assoc($getProfile);

$totalRows_getProfile = mysql_num_rows($getProfile);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> SS WEB TECHNOLOGIES - Tool Resource Projects</title>
<link rel="stylesheet" href="../css/style.css" type="text/css">
<link rel="stylesheet" href="../css/style.css" type="text/css">
<style>
.act_timesheet { background: #373938;
  color: #fff;}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="../../js/jquery.flexslider.js"></script>
<script language="JavaScript">
function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;
}
</script>
<script type='text/javascript'>
function redirect1(){
var year = document.getElementById('year').options[document.getElementById('year').selectedIndex].value;
window.location = "timesheet-archive.php?year="+year;
}
function redirect(){
var year = document.getElementById('year').options[document.getElementById('year').selectedIndex].value;
var week = document.getElementById('week').options[document.getElementById('week').selectedIndex].value;
window.location = "timesheet-archive.php?year="+year+"&week="+week;
}
</script>
</head>

<body>
<!-- Wrapper Starts -->
<div class="wrapper">

    <?php  require_once('../includes/header.php'); ?>
    
<?php  require_once('../includes/tools-menu.php'); ?>
  
  <!-- Content Goes here -->  
  <article class="inside_wrapper clear_fix">    
    <!-- Main Content Goes here -->   
   
    <section class="main_content">
    	  <h2>Current Timesheets</h2>
             <?php if(isset($_REQUEST['msg']) ){
			 $msg="Timesheet Updated Successfully"?>
             <div id="hide" class="msg_box"><font color="#3cda52"><?php echo $msg; ?><img src="/RM/images/cross.png" width="12" height="12" alt="delete" onclick="setVisibility('hide', 'none');"></font></div>
              <?php } ?>

      <div class="welcome_box1 welcome_2box">
        
					<div class="welcome_txt"><strong>Select Year: </strong>
                   <select name="year" id="year" onChange="redirect1();">
                    <option value="0">Select Year</option>
                    <?php do { ?>                
                      <option value="<?php echo $row_getSelecty['task_year']; ?>"  <?php if($row_getSelecty['task_year'] == $_GET['year']){ ?> selected="selected" <?php } ?> >
						  <?php echo $row_getSelecty['task_year']; ?>
                          </option>
                      <?php } while ($row_getSelecty = mysql_fetch_assoc($getSelecty)); ?>                 
                      </select>
</div><?php if(isset($_GET['year'])){ ?>
 
					<div class="welcome_txt"><strong>Select Week: </strong>
                   <select name="week" id="week" onChange="redirect();">
                    <option value="0">Select Week</option>
                    <?php do { ?>                    
                      <option value="<?php echo $row_getSelectw['task_week']; ?>"  <?php if($row_getSelectw['task_week'] == $_GET['week']){ ?> selected="selected" <?php } ?> > 
						  <?php echo $row_getSelectw['task_week']; ?>
                          </option>
                      <?php } while ($row_getSelectw = mysql_fetch_assoc($getSelectw)); ?>                     
                      </select>
</div> 
        <?php } ?>
</div>     
<div class="clear-fix">&nbsp;</div>       

    		<!-- Timesheets Goes here -->
      	   	 <div class="timesheet_table clear_fix">
             	<div class="column_title">
                	<span class="item_wide">Week Number</span>
                    <span class="x_item_wide">Week start Date</span>
                    <span class="item_wide">Resource</span>                    
                    <span class="item_small">Status</span>
                    <span class="item_small">Total Hours</span>
                    <span class="item_small">Action</span>
                   </div>

               <?php $n =0; if($totalRows_getTime>0) { do { 
			   
				mysql_select_db($database_oprmConn, $oprmConn);
$query_getWeka = "SELECT * FROM weby_timesheets WHERE task_week = '$row_getTime[task_week]' and task_year = '$row_getTime[task_year]' and user_id = '$_SESSION[user_id]' and status != 'D'";
$getWeka = mysql_query($query_getWeka, $oprmConn) or die(mysql_error());
$row_getWeka = mysql_fetch_assoc($getWeka);
$totalRows_getWeka = mysql_num_rows($getWeka);
			   
			   
?>

                 <div class="row <?php if($n==0) { echo "yellow"; $n=1; } else $n=0; ?>">
                 <span class="item_wide"><?php echo $row_getTime['task_week']; ?></span>
                 
                 <span class="x_item_wide"><?php echo date('d/M/Y',strtotime($row_getWeka['created_date'])); ?></span>
                 
                 <span class="item_wide"><?php mysql_select_db($database_oprmConn, $oprmConn);
			$query_getDraft = "SELECT * FROM weby_profiles WHERE user_id = '$row_getWeka[user_id]'";
			$getDraft = mysql_query($query_getDraft, $oprmConn) or die(mysql_error());
			$row_getDraft = mysql_fetch_assoc($getDraft);
			$totalRows_getDraft = mysql_num_rows($getDraft);

				 echo $row_getDraft['full_name']; ?></span>
                 
                <span class="item_small"><?php if($row_getWeka['status'] == 'I'){echo 'In Draft';} if($row_getWeka['status'] == 'B'){echo 'Rejected';} if($row_getWeka['status'] == 'A'){echo 'Pending';}if($row_getWeka['status'] == 'Q'){echo 'Approved';} ?></span>
                
                 <span class="item_small"><?php 
				 mysql_select_db($database_oprmConn, $oprmConn);
$query_getHrs = "SELECT  SUM(`total_hrs`) FROM weby_timesheets WHERE task_week = '$row_getTime[task_week]' and task_year = '$row_getTime[task_year]' and user_id = '$_SESSION[user_id]' and status != 'D'";
$getHrs = mysql_query($query_getHrs, $oprmConn) or die(mysql_error());
$row_getHrs = mysql_fetch_assoc($getHrs);
$totalRows_getHrs = mysql_num_rows($getHrs);

				 echo $row_getHrs['SUM(`total_hrs`)']; ?></span>
                  <span class="item_small"><?php if(($row_getWeka['status'] == 'I') || ($row_getWeka['status'] == 'B')){ ?><a href="timesheet-edit.php?week=<?php if($row_getWeka['task_week'] < 10){ echo '0'.$row_getWeka['task_week'];} else {echo $row_getWeka['task_week'];} ?>&year=<?php echo $row_getWeka['task_year']; ?>">Edit</a><?php } if(($row_getWeka['status'] == 'A') || ($row_getWeka['status'] == 'Q')){?><a href="timesheet-view-week.php?week=<?php if($row_getWeka['task_week'] < 10){ echo '0'.$row_getWeka['task_week'];} else {echo $row_getWeka['task_week'];} ?>&year=<?php echo $row_getWeka['task_year']; ?>">View</a><?php } ?></span>
                  
                     </div>
                  <?php } while ($row_getTime = mysql_fetch_assoc($getTime)); ?>
<div class="h-line"></div>
<div class="nav-bar">
<div class="nav-right">
<table width="300" border="0" align="center" cellpadding="2" style="clear:both">     
<tr>
<td>
<?php echo ($startRow_getTime + 1) ?> to <?php echo min($startRow_getTime + $maxRows_getTime, $totalRows_getTime) ?> of <?php echo $totalRows_getTime ?>
</td>
</tr>     
          <tr><td align="center"><?php if ($pageNum_getTime > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_getTime=%d%s", $currentPage, 0, $queryString_getTime); ?><?php if(isset($_GET['year'])){ echo "&year=".$_GET['year'];}?>">&lt;&lt; First</a>
                <?php } // Show if not first page ?></td>
            <td align="center"><?php if ($pageNum_getTime > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_getTime=%d%s", $currentPage, max(0, $pageNum_getTime - 1), $queryString_getTime); ?><?php if(isset($_GET['year'])){ echo "&year=".$_GET['year'];}?>">&lt; Previous</a>
                <?php } // Show if not first page ?></td>
            <td align="center"><?php if ($pageNum_getTime < $totalPages_getTime) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_getTime=%d%s", $currentPage, min($totalPages_getTime, $pageNum_getTime + 1), $queryString_getTime); ?><?php if(isset($_GET['year'])){ echo "&year=".$_GET['year'];}?>">Next &gt;</a>
                <?php } // Show if not last page ?></td>
            <td align="center"><?php if ($pageNum_getTime < $totalPages_getTime) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_getTime=%d%s", $currentPage, $totalPages_getTime, $queryString_getTime); ?><?php if(isset($_GET['year'])){ echo "&year=".$_GET['year'];}?>">Last &gt;&gt;</a>
                <?php } // Show if not last page ?></td>
          </tr>
        </table>
</div>
<div class="clear-fix"></div>
        </div>           
                  <?php } else echo '<div  class="no-rows">Please select Year and Week.</div>';?>
             </div>		
    </section>   
    <!-- Sidebar Goes here -->    
    <aside class="sidebar">
      
      <!--<h3>Recent Activity</h3>-->
      <?php #include("recent-ac.php"); ?>
    </aside>
    <div class="clear-fix"></div>
  </article> 
    <div class="clear-fix">&nbsp;</div>
  </article>
  <div class="clear-fix">&nbsp;</div>  
  <!-- Footer Goes here -->  
   <?php  require_once('../includes/footer.php'); ?>
</div>
</body>
</html>
