<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php 
include ("../navigation/header.php");

$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {

include ("../navigation/store_sidebar.php");

$category = $_GET['category'];
$subCat = $_GET['subCat'];
$table_name2 = $_SESSION['db_prefix']."_store_items"; 
$tip_reserved_text='';
$reservedTT='';		 
		if ($subCat=="Complete") {
		$items_query = "SELECT * FROM $table_name2 WHERE category!='Old' ORDER BY item_name ASC";
		}
		
		if ($category=="Direct material") {
		$items_query = "SELECT * FROM $table_name2 WHERE category = 'Direct material' ORDER BY item_name ASC";
		}
		
		if ($category=="Stores") {
		$items_query = "SELECT * FROM $table_name2 WHERE category = 'Stores' ORDER BY item_name ASC";
		}
		
		if ($subCat=="Old") {
		$items_query = "SELECT * FROM $table_name2 WHERE category='Old'  ORDER BY item_name ASC";
		}
		
		$items_result = $con->query($items_query);
    	$items_num = mysqli_num_rows($items_result);
		
		
?>

<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />

</head>

<body>
<div class="main_frame">
<div class="page_head_frame">
    <label class="label_page_head">Store Items List <?php echo $subCat ?></label>
  </div>

<div class="page_inside_frame">   
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>     
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 1, "asc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
								{"bSortable": true},
                             	{"iDataSort": 4},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": false}
								 ]
				} );
			} );
</script>			         


  <table width="492" id="scroll_table" class="data_display">
      <thead>
        <tr>
          <th width="181">Product Name</th>
          <th width="23">&nbsp;</th>
          <th width="74">Store group</th>
          <th width="74">Stock qty</th>
          <th width="26">&nbsp;</th>
          <th width="74">Min stock</th>
          <th width="74">&nbsp;</th>
        </tr>
      </thead>
      <tbody>
        <?php 
	     while($row = $items_result->fetch_assoc()) {
           $a1 = $row["store_item_id"];
	       $a2 = $row["item_name"];
	       $a3 = $row["location"];
		   $a4 = $row["subCategory"];
	       $a5 = $row["quantity"];
	       $a6 = $row["minimumStock"];
	       $a7 = $row["price"];
		   $a8 = $row["unit"];
		   $a9 = $row["supplier_id"];
		   
		   $a12 = '0';
		   
	$table_name3 = $_SESSION['db_prefix']."_item_history";
$history_query = "SELECT entry, quantity, total, date FROM $table_name3 WHERE store_item_id = '".$a1."' ORDER BY history_id DESC LIMIT 10";
$history_result = $con->query($history_query);
$history_num = mysqli_num_rows($history_result);
		
$historyTT_body	='';  
$k=0; 
while ($k<$history_num) {
	      while($row = $history_result->fetch_assoc()) {
					$h1 = $row["date"];
		            $h2 = $row["entry"];
		  $h2a='';
		  if ($h2=="5") {$h2a='stock count adjustment';}
		  if ($h2=="10") {$h2a='item added to store';}
		  if ($h2=="11") {$h2a='stock received';}
		  if ($h2=="12") {$h2a='stock count edit';}
		  if ($h2=="13") {$h2a='order delivered';}
		  if ($h2=="14") {$h2a='supplied to employee';}
		  if ($h2=="15") {$h2a='supplied to department';}
		  if ($h2=="18") {$h2a='return from order';}
		  if ($h2=="20") {$h2a='used in production';}
		  if ($h2=="21") {$h2a='produced (no new stock)';}
		  if ($h2=="22") {$h2a='production edited';}
		  if ($h2=="23") {$h2a='used in packing';}
		  if ($h2=="24") {$h2a='products packed';}
		  if ($h2=="25") {$h2a='production edited';}
		  if ($h2=="26") {$h2a='packing edited';}
		  if ($h2=="27") {$h2a='packing edited';}
		  
		  if ($h2=="35") {$h2a='adjustment receive note';}
		  
		  $h3 = $row["quantity"];
		  if (($h2==5) OR ($h2==12)) {if ($h3>0) {$h3='+'.$h3;} }
		  if (($h2==11) OR ($h2==18) OR ($h2==21) OR ($h2==24) ) {$h3='+'.$h3;}
		  if ( ($h2==13) OR ($h2==19) ) {$h3='-'.$h3;}
		  $h4 = $row["total"];
		  
		  //if (($h2=="22") OR ($h2=="23")) { 
//		  			if ($h3<0) {$h3='+'.abs($h3);}
//					else {$h3='-'.abs($h3);}
//		  }

$historyTT_body=$historyTT_body.'<tr><td>'.date("d-m-Y", strtotime($h1)) .'</td><td>'.$h2a.'</td><td align=right>'.$h3.'</td><td align=right>'.$h4.'</td></tr>'; 
		  }
$k++;
}

$historyTT_head='<b>Recent history:</b><br><br><table id=table_tool_tip width=400 border=1 cellspacing=0 cellpadding=0><tr><td width=75>date</td><td width=75>action</td><td width=50>qty</td><td width=50>stock qty</td></tr>';
$historyTT_foot='</table><br><b>Click for full history</b>';		   
$historyTT = $historyTT_head.$historyTT_body.$historyTT_foot; 	   
		   
		   
    ?>
        <tr class="alt">
          <td align="left"><a href="store_item_history.php?store_item_id=<?php echo $a1 ?>&date=0" onMouseover="ddrivetip('<?php echo $historyTT ?>', 400)"; onMouseout="hideddrivetip()"><?php echo $a2 ?></a></td>
          <td align="left"><?php echo $a2 ?></td>
          <td align="left"><?php echo $a4 ?></td>
          <td align="right"><?php
         
			if($a5<0){
				echo "<font color=\"red\">".number_format($a5 , 2, '.', ',')."</font>";
				}
			else {
			 if($a5<$a6){

				echo "<font color=\"orange\">".number_format($a5 , 2, '.', ',')."</font>";
				}
		       else{
			     echo number_format($a5 , 2, '.', ',');
		     }	
		   }
			
			?></td>
          <td align="right"><?php echo $a5 ?></td>
          <td align="right"><?php echo $a6 ?></td>
          <td align="center"><a href="store_item_display.php?store_item_id=<?php echo $a1 ?> " onMouseover="ddrivetip('Click for details', 70)"; onMouseout="hideddrivetip()">   <img src="../images/info_blue.png" width="16" height="16"></a></td>
        </tr>
        <?php 
		}
    	?>
      </tbody>
    </table>
          </div>  

  

</div> 
<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else {  ?>
<script> needToConfirm = false; </script>
<?php
    $_SESSION['denied_return'] = "store/store_main.php?date=0&branch=&subCatSelect=Tiles";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>

