<?php
include("includes/config.php");
include("includes/func.php");
include("templates/header.php");


if (($_SESSION['id'] == ""))
	{
		redirect("login");
	}
$start_date = '';
$end_date = '';
$selecteduser = '';
$excel_icon = '';

if($_SESSION['usertype'] == 'Admin')
{
$ReportBody = "";
$ReportBody .= '<table id="tabling1" cellpadding="0" cellspacing="0">';
$ReportBody .= '<tr>';
      $ReportBody .= '<td nowrap width="25%" align="left" ><span class="blue">User Name</span></td>';
	  $ReportBody .= '<td nowrap width="22%" align="left" ><span class="blue">Date</span></td>';
	  $ReportBody .= '<td nowrap width="22%" align="left" ><span class="blue">Hours</span></td>';
	  $ReportBody .= '<td nowrap width="22%" align="left" ><span class="blue">All Day</span></td>';
	  $ReportBody .= '<td nowrap width="20%" align="left" ><span class="blue">On Leave</span></td>';
$ReportBody .= '</tr>';
if(isset($_POST['stock']))
{
	$equalSign = " = ";
	$start_date = $_POST['start_date'];
	$end_date = $_POST['end_date'];
	$select_user = $_POST['select_user'];
	$selecteduser = $select_user;
	if($select_user == 0)
	{
		$AllUserIDs = '';
		$UserSelectQuery = "Select id from users where usertypeid != 1";
		$UserSelectExecute = mysql_query($UserSelectQuery);
		while($UserSelectRecords = mysql_fetch_array($UserSelectExecute))
		{
			$AllUserIDs .= $UserSelectRecords['id'].",";
		}
		$AllUserIDs = rtrim($AllUserIDs, ",");
		$select_user = "IN($AllUserIDs)";
	}else{
		$equalSign .= $select_user;
		$select_user = $equalSign;
	}
	

	$dayManageQuery = "Select day_managment.*, users.username 
						FROM day_managment
						Inner Join users ON day_managment.userid = users.id
						Where date between '$start_date' AND '$end_date' AND userid $select_user";
						//echo $dayManageQuery;
	$dayManageExecute = mysql_query($dayManageQuery);
	$dayManageNumRows = mysql_num_rows($dayManageExecute);

//	echo $dayManageNumRows;
	if($dayManageNumRows < 1)
	{
		$ReportBody .= '<tr><td colspan="5" align="center">'."No Records found.".'</td></tr>';
		$excel_icon = '';
	}else{
		$excel_icon =1;
	}
	while($dayManageRecords = mysql_fetch_array($dayManageExecute))
	{
		$allDay = $dayManageRecords["allday"];
		$onLeave = $dayManageRecords["onleave"];
		if($allDay == 1){
			$allDay = 'Yes';
		}else{
			$allDay = 'No';
		}
		if($onLeave == 1){
			$onLeave = 'Yes';
		}else{
			$onLeave = 'No';
		}
		$ReportBody .= '<tr>';
		$ReportBody .= '<td>'.$dayManageRecords["username"].'</td>';
		$ReportBody .= '<td>'.$dayManageRecords["date"].'</td>';
		$ReportBody .= '<td>'.$dayManageRecords["hours"].'</td>';
		$ReportBody .= '<td>'.$allDay.'</td>';
		$ReportBody .= '<td>'.$onLeave.'</td>';
		$ReportBody .= '</tr>';
	}
}
$ReportBody .= '<table>';

}

?>

<link rel="stylesheet" href="calender/development-bundle/themes/base/jquery.ui.all.css" />
<script src="calender/development-bundle/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="calender/development-bundle/ui/jquery.ui.widget.js"></script>
<link rel="stylesheet" href="calender/development-bundle/demos/demos.css">
<!--------------------------->

<script type="text/javascript" src="javascripts/jquery.maskedinput-1.3.js"></script>
<script type="text/javascript">
 $(document).ready(function(){

 });

$(function() {
		$('#start_date').datepicker({ dateFormat: 'yy-mm-dd' });
		$('#end_date').datepicker({ dateFormat: 'yy-mm-dd' });
});


function stockcheck(Currentstock)
{
	var quantity='order_quantity_'+Currentstock;
	var quantitysold = 'current_stock_'+Currentstock;
	var error = 'stock_error_'+Currentstock;
	var qtyele = document.getElementById(quantity);
	var qty = document.getElementById(quantity).value;

	var qtysold = document.getElementById(quantitysold).value;
	if(parseInt(qty) > parseInt(qtysold))
	{
		qtyele.value='0';
		qtyele.focus();
		document.getElementById(error).style.display='inline';
	}else{
		document.getElementById(error).style.display='none';
	}
}


</script>
<style type="text/css">
.excel{
	background:url(images/exportExcel.png) no-repeat;
	display:block;
	height:40px;
	width:37px;
}
</style>

<div id="mainPanel">
<h2 style="margin-bottom:10px;">Hours Management Report</h2>
<?php if($excel_icon  != '') {?>
<a href="exportExcel.php?exportExcel&start_date=<?php echo $start_date;?>&end_date=<?php echo $end_date;?>&user=<?php echo $selecteduser;?>" class="excel" title="Export"></a>
<?php }?>
<form action="" method="POST" name="day_report_form" id="day_report_form">
  
  <!--------------------------------------------------------------------------------------------------->
  <table width="100%" cellpadding="0" cellspacing="0" border="0" id="tabling1">
    <tr>
      <td nowrap width="13%" align="right" ><label>Start Date:</label></td>
      <td nowrap><input type="text" name="start_date" id="start_date" value="<?php echo date('Y-m-d'); ?>" /></td>
      <input type="hidden" name="start_date_hidden" id="start_date_hidden" value=""/>
      <td nowrap width="13%" align="right" ><label>End Date:</label></td>
      <input type="hidden" name="start_date_hidden" id="start_date_hidden" value=""/>
      <td nowrap><input type="text" name="end_date" id="end_date" value="<?php echo date('Y-m-d'); ?>" /></td>
      <td nowrap width="13%" align="right" ><label>User:</label></td>
      <td nowrap>
      		<select name="select_user" id="select_user">
            <option value="0">All</option>
             <?php
			  $GetUsersQuery = "Select id, username From users WHERE usertypeid != 1";
			  $GetUsersExecute = mysql_query($GetUsersQuery);
			  while($GetUsersRows = mysql_fetch_array($GetUsersExecute))
			  {
				  echo '<option value="'.$GetUsersRows['id'].'">'.$GetUsersRows['username'].'</option>';
			  }
			  ?>
            </select>
      </td>
      <input type="hidden" name="start_date_hidden" id="start_date_hidden" value=""/>
      <td nowrap><input type="submit" class="form_button" value="Enter" id="stock" name="stock"></td>
    </tr>
  </table>
</form>

<?php
echo $ReportBody;
?>