<?php
include("../include/session.php");
if(!$session->logged_in) {
	header("Location: ../main.php");
}
else {
	include("../storeHeader.php");
	
	require_once('classes/tc_calendar.php');
	$date = mysql_real_escape_string($_GET['date']);
	//$cat = mysql_real_escape_string($_GET['cat']);
	
	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) );	 
	
	
	
    $endsearch = date('Y-m-d', strtotime('+1 day', strtotime($end)));

	$productId =  mysql_real_escape_string($_GET['productIdSelect']);	 
	//$productQuery = "SELECT productName, productId FROM products WHERE productId = ". $productId;
	$productQuery = "SELECT productName, category FROM products WHERE productId = ".$productId ;
		  $productResult = execsql($productQuery);
		  $productName = mysql_result($productResult, 0, "productname");
		  $category = mysql_result($productResult, 0, "category");
	
	  
		$displayQuery = "SELECT itemhistory.historyId, itemhistory.productId, itemhistory.details, itemhistory.entry, itemhistory.reference, itemhistory.quantity, itemhistory.total, itemhistory.date, itemhistory.entryDate,  itemhistory.userId, products.productId, products.productName FROM itemhistory, products WHERE products.productId=$productId AND  products.productId = itemhistory.productId AND itemhistory.entryDate >= '$start' AND itemhistory.entryDate <= '$endsearch' ";

		$displayResult = execsql($displayQuery);
		$i = 0;
		$num = mysql_num_rows($displayResult);
		if ($num==0) {
		  $message="No records to show"; 
		   }
		else  {
		  $message="End of list";
		  }
		  
	?> 
    
<link rel="stylesheet" href="../css/dhtmlwindow.css" type="text/css" />
<script type="text/javascript" src="../css/dhtmlwindow.js"></script>
<link rel="stylesheet" href="../css/modal.css" type="text/css" />
<script type="text/javascript" src="../css/modal.js"></script>


<script type="text/javascript" src="../css/tablesort.js"></script>
<style type="text/css">
//<!--
//.style7 {font-size: 9}
//-->
//</style>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<div class="headerbox2">Stock Item History </div>
<p class="main" ><label class="std">History for : <font size='3'><?php echo $productName ?></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
</p>
<br /><br /><br /><br />
<div id="coloredContainer">
<form action="stockMovement.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(2000, 2030);
	 	$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(2000, 2030);
		$myCalendar->writeScript();?>
        &nbsp;&nbsp;
<label>
          <input name="button5" type="submit" class="smalllinkbuttonblue"  id="button5" value="Go" />
          <input name="date" type="hidden" id="date" value="1" />
    </label>
    <input name="productIdSelect" type="hidden" id="productIdSelect" value="<?php echo $productId ?>" />
  </form>
</div>
<p align="right">
<table width="589" class="sortable-onload-0r rowstyle-alt no-arrow " id="tableList"  >
	
	<thead>
		<tr>
		  <th colspan="2" class="sortable-numeric">Entry</th>
		  <th width="175" class="sortable-text">Product</th>

			<th width="170" class="sortable-text">Details</th>
			<th width="41" class="sortable-numeric">
		    in/out </th>
			<th width="47" class="sortable-numeric">
		    Bal</th>
			<th width="52" class="sortable-date-dmy">Date</th>
		</tr>
        <tr>
          <th width="32" >&nbsp;</th>
            <th width="40" >&nbsp;</th>
            <th width="175" >(click for spec)</th>
            <th width="170">(click for details)</th>
            <th width="41">&nbsp;</th>
            <th width="47">&nbsp;</th>
            <th width="52">&nbsp;</th>
          <!--        <th>Reserved Stock</th>-->
          </tr>
	</thead>
	<tbody>
	<?php

	while($i < $num){
		$b1 = mysql_result($displayResult, $i, "historyId");
		
		
		$b2 = mysql_result($displayResult, $i, "details");
		$b3 = mysql_result($displayResult, $i, "quantity");
		$b4 = mysql_result($displayResult, $i, "date");
		$b5a = mysql_result($displayResult, $i, "productId");
		  
		$b5 = mysql_result($productResult, 0, "productname");
				
		$b7 = mysql_result($displayResult, $i, "total");
		
		$b10 = mysql_result($displayResult, $i, "entry");
		
		$b8 = mysql_result($displayResult, $i, "reference");
		
		
		if ($b10==0) {
			$b8="";
			$b8a="Radstone";
		}
		
		
		if ($b10==20) {
			  	$supplierQuery = "SELECT account_name FROM rad_suppliers WHERE account_no = '".$b8."' " ;
		  		$supplierResult = execsql($supplierQuery);
		  		$b8a = mysql_result($supplierResult, 0, "account_name");
		}
		if ($b10==34) {
			  	$companyQuery = "SELECT name FROM rad_tocotex WHERE account_no = '".$b8."' " ;
				
		  		$companyResult = execsql($companyQuery);
		  		$b8a = mysql_result($companyResult, 0, "name");
		}
		if ($b10==35) {
			  	$companyQuery = "SELECT name FROM rad_tocotex WHERE account_no = '".$b8."' " ;
				
		  		$companyResult = execsql($companyQuery);
		  		$b8a = mysql_result($companyResult, 0, "name");
		}
		if ($b10==36) {
			  	$b8a = $b8;
		}
		if ($b10==37) {
			  	$empQuery = "SELECT empFName, empLName FROM employees WHERE empId = ".$b8 ;
				$empResult = execsql($empQuery);
				$b8a = mysql_result($empResult, 0, "empFName")." ".mysql_result($empResult, 0, "empLName");
		}
		if ($b10==38) {
			  	$repQuery = "SELECT first_name, last_name FROM rad_representatives WHERE repId = ".$b8 ;
				$repResult = execsql($repQuery);
				$b8a = mysql_result($repResult, 0, "first_name")." ".mysql_result($repResult, 0, "last_name");
		}
		if ($b10==40) {
			  	$orderQuery = "SELECT orderNo FROM orders WHERE orderNo = ".$b8 ;
		  		$orderResult = execsql($orderQuery);
		  		$b8a = mysql_result($orderResult, 0, "orderNo");
		}
		if ($b10==41) {
			  	$empQuery = "SELECT empFName, empLName FROM employees WHERE empId = ".$b8 ;
				$empResult = execsql($empQuery);
				$b8a = mysql_result($empResult, 0, "empFName")." ".mysql_result($empResult, 0, "empLName");
		}
		$b10a="";
		if ($b10=="0") {$b10a="New stock from production";}
		if ($b10=="5") {$b10a="Stock count";}
		if ($b10=="10") {$b10a="Added new stock item.";}
		if ($b10=="20") {$b10a="Stock received from: <a href=displaySupplier.php?account_no=". $b8 ."><b>".  $b8a ."</b> </a> " ;}
		if ($b10=="30") {$b10a="Delivery to:  <a href=viewDelivery.php?DelNoText=". $b8 ."><b>".  $b8 ."</b> </a> " ;}
		if ($b10=="31") {$b10a="Delivered to:  <a href=viewDelivery.php?DelNoText=". $b8 ."><b>".  $b8 ."</b> </a> " ;}
		if ($b10=="34") {$b10a="Diesel delivered to: <a href=displayTocotex.php?account_no=". $b8 ."><b>".  $b8a ."</b> </a> " ;}
		if ($b10=="35") {$b10a="Direct Materials delivered.";}
		if ($b10=="36") {$b10a="Issued to: <b>".  $b8a ."</b> " ;}
		if ($b10=="37") {$b10a="Issued to: <a href=../HR/searchAndDisplayE.php?id=". $b8 ."><b>".  $b8a ."</b> </a> " ;}
		if ($b10=="38") {$b10a="Issued to: <a href=displayRep.php?repId=". $b8 ."><b>".  $b8a ."</b> </a> " ;}
		if ($b10=="40") {$b10a="Returned on order: <a href=displayOrder.php?OrderNoText=". $b8 ."><b>".  $b8a ."</b> </a> " ;}
		if ($b10=="41") {$b10a="Returned by: <a href=../HR/searchAndDisplayE.php?id=". $b8 ."><b>".  $b8a ."</b> </a> " ;}
		if ($b10=="50") {$b10a="Stock Count.";}
		if ($b10=="60") {$b10a="Used in production.";}
		if ($b10=="70") {$b10a="Palletting tiles.";}
		
		
		
		if ($b10=="") {
			$b12=$b2." ".$b8;
		}
		else {
			$b12=$b10a;
		}
		
		
		$b9 = mysql_result($displayResult, $i, "entryDate");
		$b10 = mysql_result($displayResult, $i, "userId");
		?>
		<tr class="alt">
		  <td><a href="#"onmouseover="ddrivetip('<?php echo "Entered on: ". date('d-m-Y  --- H:i', strtotime($b9)) . "<br>by: ". $b10;?>','lightyellow', 180)" onmouseout="hideddrivetip()";><?php echo $b1?></a></td>
		  <td><a href="#"onmouseover="ddrivetip('<?php echo "Entered on: ". date('d-m-Y  --- H:i', strtotime($b9)) . "<br>by: ". $b10;?>','lightyellow', 180)" onmouseout="hideddrivetip()";></a><a href="#"onmouseover="ddrivetip('<?php echo "Entered on: ". date('d-m-Y  --- H:i', strtotime($b9)) . "<br>by: ". $b10;?>','lightyellow', 180)" onmouseout="hideddrivetip()";><?php echo date("d-m-Y", strtotime($b9));?></a></td>
		  
          <td><a href="stockItemDisplay.php?productId=<?php echo $b5a ?>" onmouseover="ddrivetip('<?php echo $b5."<br>(".$b5a.")<br><b>Click to view item details</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()";></a><a href="stockItemDisplay.php?productId=<?php echo $b5a ?>" onmouseover="ddrivetip('<?php echo $b5."<br>(".$b5a.")<br><b>Click to view item details</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()";></a><a href="stockItemDisplay.php?productId=<?php echo $b5a ?>" onmouseover="ddrivetip('<?php echo $b5."<br>(".$b5a.")<br><b>Click to view item details</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()";></a><a href="stockItemDisplay.php?productId=<?php echo $b5a ?>" onmouseover="ddrivetip('<?php echo $b5."<br>(".$b5a.")<br><b>Click to view item details</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()";></a><a href="stockItemDisplay.php?productId=<?php echo $b5a ?>" onmouseover="ddrivetip('<?php echo $b5."<br>(".$b5a.")<br><b>Click to view item details</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()";>
            <?php echo $b5?>
          </a></td>

			<td><?php echo $b12?></td>
			<td align="right"><?php echo $b3?></td>
			<td align="right"><?php echo $b7?></td>
			<td><?php echo date("d-m-Y", strtotime($b4));?></td>
		</tr>
        
		<?php
		$i++;
		  
	}
	
	?>
	</tbody>
	
</table>
<br />
	<b><font color='#FF0000'><?php  echo $message ?></font></b>
	<?php
	include("../footer.php");
}
//

?>