<?php
include ("../include/session.php");
if (!$session->isRadstoneStoreUser() && !$session->isRadstoneReceptionUser() && !$session->isAdmin()) {
	header("Location: ../main.php");
} else {
	include("../storeHeader.php");
	
	require_once('classes/tc_calendar.php');
	$date = mysql_real_escape_string($_GET['date']);
	
		
	if ($date==1) {
	$start = mysql_real_escape_string($_GET['date1']);
    $end = mysql_real_escape_string($_GET['date2']);
	}
	else {
	$start = date("Y-m-01");
	$end = date("Y-m-d");
	}
	
		
	if ($date!=1) {
	   $ds = "1"; }
	   else {
		$ds = date("d", strtotime($start) );
	   }
	$ms = date("m", strtotime($start) );
	$ys = date("Y", strtotime($start) );
	
	
	$de = date("d", strtotime($end) );
	$me = date("m", strtotime($end) );
	$ye = date("Y", strtotime($end) );	
	
	$empId = mysql_real_escape_string($_GET['search_select']);
		
	if ($empId!="0") {
		$loanQuery = "SELECT * FROM loan WHERE date >= '$start' AND date <= '$end' AND empId=".  $empId ."  ORDER BY date DESC";
	}
	else {
		$loanQuery = "SELECT * FROM loan WHERE date >= '$start' AND date <= '$end' AND empId<>0   ORDER BY date DESC";
	   	
	}
		
    $loanResult =  execsql($loanQuery);
	$loanResultNum = mysql_num_rows($loanResult);
	
	 
	$q = "SELECT empId, empFName, empLName FROM employees ORDER BY empFName ASC";
	$arrId = array();
	$arrName = array();
	$result = execsql($q);
	$num = mysql_num_rows($result);
	$b = 0;
	while($b < $num){
		$arrId[$b] = mysql_result($result, $b, "empId");
		$arrName[$b] = mysql_result($result, $b, "empFName") ." ". mysql_result($result, $b, "empLName");
		$b++;
	}	
	
	if ($empId=="0") {
	    $forName = "All"; }
	else {
	    $nameQuery = "SELECT empFName, empLName FROM employees WHERE empId = $empId";
		$nameResult =  execsql
		($nameQuery); 
		$forLName = mysql_result($nameResult, 0, "empLName");
		$forFName = mysql_result($nameResult, 0, "empFName");
		$forName = $forFName . " " . $forLName;
	    }
	
?>

<link	href="calendar.css" rel="stylesheet" type="text/css" />
<script	language="javascript" src="../include/calendar.js"></script>
<script type="text/javascript" src="../css/tablesort.js"></script>
<div class="headerbox2">Issues to employees </div>
<br /><br />




<div id="coloredContainer">
  <form action="outgoingDisplay.php" method="get" name="dateform" class="today" id="dateform">
    from:
    &nbsp;&nbsp;
    <?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2009, 2020);
	 	$myCalendar->writeScript();?>
    &nbsp;&nbsp;to:&nbsp;&nbsp;
    <?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2009, 2020);
		$myCalendar->writeScript();?>
    &nbsp;&nbsp;
    <label>
      <input name="date" type="hidden" id="date" value="1" />
      <br />
    </label>
    Select employee:
    <select name="search_select" id="search_select" >
      <option value="<?php echo $empId ?>" selected="selected"><?php echo $forName  ?></option>
      <option value="0">All</option>
      <?php
    	for ($i = 0; $i < count($arrId); $i++) {
		echo "<option value=" . "\"" . $arrId[$i] . "\"" . ">" . $arrName[$i] . "</option>";
		$_SESSION['CustomerName'] = $arrName[$i];
		//foreach($arrId as $key=>$value){
			//echo "<option value=\"delivery.php?productId=".$arrId[$i]."&cat=$cat&formEnabled2=1&date=$date" . "\"" . ">" . $arrName[$i] . "</option>";	
	    }
     ?>
    </select>
    </span>
    <input name="button" type="submit" class="smalllinkbuttonblue" id="button" value="Go" />
  </form>
</div><br />

<div id="coloredContainer"><font size="3"><b>Issues to <?php echo $forName ?>.</b></font><br />
<b>Period from:&nbsp;&nbsp;&nbsp; <?php echo date("d-m-Y", strtotime($start )) ?>&nbsp;&nbsp;&nbsp; to&nbsp;&nbsp;&nbsp; <?php echo date("d-m-Y", strtotime($end )) ?></b></div>
<br />
	
	
	
    	<table id="tableList" class="sortable-onload-5r rowstyle-alt no-arrow " >
    <thead>
      <tr>
        <th width="166" class="sortable-text"><div align="left">Product</div></th>
        <th width="177" class="sortable-text"><div align="left">Employee</div></th>
        <th width="49" class="sortable-numeric"><div align="left">Qty</div></th>
        <th width="89" class="sortable-numeric">Loan/give</th>
        <th width="110" class="sortable-numeric"><div align="left">Cost</div></th>
        <th width="110" class="sortable-date-dmy"><div align="left">Date</div></th>
      </tr>
    </thead>
    <tbody>
    <?php


	$k = 0;
	$b9="0.000";
	while ($k < $loanResultNum) {
//		
		$b2 = mysql_result($loanResult, $k, "productId");
		  $productQuery = "SELECT productId, productName,category FROM products WHERE productID = $b2";
		  $productResult = execsql($productQuery);
		  $b2a = mysql_result($productResult, 0, "productName");
		  $b2c = mysql_result($productResult, 0, "category");
		  
		$b3 = mysql_result($loanResult, $k, "empId");
		  $employeeQuery = "SELECT empId, empFName, empLName FROM employees WHERE empId = $b3";
		 
	      $employeeResult = execsql($employeeQuery);
		  $emp_num = mysql_num_rows($employeeResult);
		  if ($emp_num==1) {
		   $b3a = mysql_result($employeeResult, 0, "empLName");
		   $b3b = mysql_result($employeeResult, 0, "empFName");
		  }
		  else {
			 $b3a = "unknown";
			 $b3b = "";
		  }
		   
		$b4 = mysql_result($loanResult, $k, "quantity");
		$b10 = mysql_result($loanResult, $k, "returned");
		$b5 = mysql_result($loanResult, $k, "price");
		$b6 = mysql_result($loanResult, $k, "date");
		$b7 = date("d-m-Y", strtotime($b6));
        $b8 = $b4*$b5;
		$b9 += $b8;

?>

        <tr class="alt">
        <td><div align="left"><a href="stockMovement.php?monthdisplay=0&cat=<?php echo $b2c ?>&productIdSelect=<?php echo $b2 ?>" onmouseover="ddrivetip('<?php echo $b2a."<br>(".$b2.")<br>".$b2c."<br><b>Click for item history</b>" ?>','lightyellow', 150)" onmouseout="hideddrivetip()";>
         <?php echo $b2a?>
       </a></div></td>
        <td><a href="../HR/searchAndDisplayE.php?id=<?php echo $b3?>"><?php echo $b3a.", ".$b3b ?></a></td>
        <td><div align="right">
          <?php echo $b4?>
        </div></td>
        <td><?php if ($b10==3) {echo "given";} else {echo "on loan";} ?> </td>
        <td><div align="right"><?php echo number_format($b8, 2, '.', ',')?></div></td>
        <td><?php echo $b7?></td>
        </tr>
		<?php
  $k++;
	}
?>
       <tfoot>
	    <tr class="alt">
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><div align="right"></div></td>
          <td>&nbsp;</td>
          <td><div align="right"><?php echo number_format($b9, 2, '.', ',')?></div></td>
          <td>&nbsp;</td>
        </tr> 
		</tfoot>
</tbody>
</table>
<?php


	include ("../footer.php");
}
?>