<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../jscript/tablesort.js"></script>


<?php
include ("../navigation/header.php");

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

if ($permission_num==1) {

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


$table_name2 = $_SESSION['db_prefix']."_store_items";
$tiles_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Tiles' ORDER BY item_name ASC";
$tiles_result = execsql($tiles_query);
$tiles_num = mysql_num_rows($tiles_result);		
		
$table_name2 = $_SESSION['db_prefix']."_store_items";		
$accessories_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Accessories' ORDER BY item_name ASC";
$accessories_result = execsql($accessories_query);
$accessories_num = mysql_num_rows($accessories_result);		
?>
</head>
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<body>

<div class="main_frame">
 
<div class="page_inside_frame"> 
<label class="label_std" style="font-size:16px;">Tiles</label>  
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>     
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table1').dataTable( {
					"sScrollY": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 1, "asc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
                             	{"iDataSort": 3},
								{"bVisible": false},
								{"iDataSort": 6},
								{"bVisible": false},
								{"iDataSort": 8},
								{"bVisible": false},
								{"iDataSort": 10},
								{"bVisible": false}
								 ]
				} );
			} );
</script>			         


  <table width="662" id="scroll_table1" class="data_display">
      <thead>
        <tr>
          <th width="181">Product Name</th>
          <th width="23">&nbsp;</th>
          <th width="74">Stock qty</th>
          <th width="26">&nbsp;</th>
          <th width="77">Reserved</th>
          <th width="77">&nbsp;</th>
          <th width="91">Available</th>
          <th width="91">&nbsp;</th>
          <th width="74">Min stock</th>
          <th width="74">&nbsp;</th>
        </tr>
      </thead>
      <tbody>
        <?php 
    $i=0;
    while($i < $tiles_num){
	       $a1 = mysql_result($tiles_result, $i, "store_item_id");
	       $a2 = mysql_result($tiles_result, $i, "item_name");
	       $a3 = mysql_result($tiles_result, $i, "location");
		   $a4 = mysql_result($tiles_result, $i, "subCategory");
	       $a5 = mysql_result($tiles_result, $i, "quantity");
	       $a6 = mysql_result($tiles_result, $i, "minimumStock");
	       $a7 = mysql_result($tiles_result, $i, "price");
		   $a8 = mysql_result($tiles_result, $i, "unit");
		   $a9 = mysql_result($tiles_result, $i, "supplier_id");
		   
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$reserved_query = "SELECT SUM(quantity-delivered+returned-cancelled) AS reserved FROM $table_name WHERE store_item_id = '".$a1."' AND (quantity-delivered+returned-cancelled) > 0  GROUP BY store_item_id ";
	//echo '<br>*****'.$reserved_query;
	$reserved_result = mySQL_query($reserved_query);
	$reserved_num = mysql_num_rows($reserved_result);
	if ($reserved_num>0) {
	$a11 = mysql_result($reserved_result, 0, "reserved");
	}
	else {
		$a11=0;
	}
	
	
	$reservedTT_line='';
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$order_query = "SELECT order_no, order_reference, cust_id, (quantity-delivered+returned-cancelled) AS reserved FROM $table_name WHERE store_item_id = '".$a1."' AND (quantity-delivered+returned-cancelled) > 0 ";
	//echo '<br>*****'.$order_query;
	$order_result = mySQL_query($order_query);
	$order_num = mysql_num_rows($order_result);
	if ($order_num>0) {
	$a12 = mysql_result($order_result, 0, "order_no"); 
	$a13 = mysql_result($order_result, 0, "cust_id"); 
	$a14 = mysql_result($order_result, 0, "reserved");
	
	
	$k=0;
	while ($k<$order_num) {
		
		$a12 = mysql_result($order_result, $k, "order_no"); 
		$a13 = mysql_result($order_result, $k, "cust_id"); 
		$a14 = mysql_result($order_result, $k, "reserved");
		$a16 = mysql_result($order_result, $k, "order_reference");
		
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$delivery_query = "SELECT delivery_date FROM $table_name WHERE order_reference = '".$a16."' ";
		$delivery_result = mySQL_query($delivery_query);
		$a15 = mysql_result($delivery_result, 0, "delivery_date");
		if ($a15=='2000-01-01') {
			  $a15 = 't.b.a.';
		}
		else {
			$a15 = date("d-m-Y", strtotime($a15));
		}
		
		
		  
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT comp_name FROM $table_name2 WHERE cust_id ='".$a13."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$cust_name = mysql_result($customer_result, 0, "comp_name"); 
	
		$reservedTT_line = $reservedTT_line . '<tr><td width=75>'.$a12.'</td><td width=75;>'.$a15.'<td><tdwidth=175>'.$cust_name.'</td><td  width=50; align=right>'.$a14.'<td><tr>';
				
		$k++;
	}
	$reservedTT = '<table class=sortable-onload-1r rowstyle-alt no-arrow; ><tr><td class=sortable-text>Order no</td><td class=sortable-date-dmy;>Delivery date</td><td class=sortable-text>Customer</td><td class=sortable-numeric>Reserved</td></tr>'.$reservedTT_line.'</table>';
	}
	else {
	$reservedTT = 'No reservations.';
	}
		   
$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 = mySQL_query($history_query);
$history_num = mysql_num_rows($history_result); 
		
$historyTT_body	='';  
$k=0;
while ($k<$history_num) {
          $h1 = mysql_result($history_result, $k, "date");
		  $h2 = mysql_result($history_result, $k, "entry");
		  $h2a='';
		  if ($h2=="5") {$h2a='stock count';}
		  if ($h2=="10") {$h2a='item added to store';}
		  if ($h2=="11") {$h2a='stock received';}
		  if ($h2=="13") {$h2a='order delivered';}
		  if ($h2=="18") {$h2a='return from order';}
		  if ($h2=="20") {$h2a='used in production';}
		  if ($h2=="21") {$h2a='from production';}
		  if ($h2=="25") {$h2a='adjustment depalleting';}
		  
		  $h3 = mysql_result($history_result, $k, "quantity");
		  if ($h2==5) {if ($h3>0) {$h3='+'.$h3;} }
		  if (($h2==11) OR ($h2==18) OR ($h2==21)) {$h3='+'.$h3;}
		  if ( ($h2==13) ) {$h3='-'.$h3;}
		  $h4 = mysql_result($history_result, $k, "total");

$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="right"><?php echo number_format($a5 , 0, '.', ','); ?></td>
          <td align="right"><?php echo $a5 ?></td>
          <td align="right" ><a href="reserved_order_list.php?store_item_id=<?php echo $a1 ?>" onMouseover="ddrivetip('<?php echo $reservedTT ?>', 375)"; onMouseout="hideddrivetip()"> <?php echo number_format($a11, 0, '.', ',') ?>  </a></td>
          <td align="right" ><?php echo $a11 ?></td>
          <td align="right">
		  
		  <?php
         
			if($a5-$a11<0){
				echo "<font color=\"red\">".number_format($a5-$a11 , 0, '.', ',')."</font>";
				}
			else {
			 if($a5-$a11<$a6){

				echo "<font color=\"orange\">".number_format($a5-$a11 , 0, '.', ',')."</font>";
				}
		       else{
			     echo number_format($a5-$a11 , 0, '.', ',');
		     }	
		   }
			
			?>
		  
		  
		  <?php //echo number_format(($a5-$a11), 0, '.', ',') ?></td>
          <td align="right"><?php echo ($a5-$a11) ?> </td>
          <td align="right"><?php echo number_format($a6, 0, '.', ',') ?></td>
          <td align="right"><?php echo $a6 ?></td>
        </tr>
        <?php 
		$i++;
    	}
    	?>
      </tbody>
    </table>
          </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_table2').dataTable( {
					"sScrollY": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 1, "asc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
                             	{"iDataSort": 3},
								{"bVisible": false},
								{"iDataSort": 6},
								{"bVisible": false},
								{"iDataSort": 8},
								{"bVisible": false},
								{"iDataSort": 10},
								{"bVisible": false}
								 ]
				} );
			} );
</script>			         

<label class="label_std"  style="font-size:16px;">Accessories</label>
  <table width="662" id="scroll_table2" class="data_display">
      <thead>
        <tr>
          <th width="181">Product Name</th>
          <th width="23">&nbsp;</th>
          <th width="74">Stock qty</th>
          <th width="26">&nbsp;</th>
          <th width="77">Reserved</th>
          <th width="77">&nbsp;</th>
          <th width="91">Available</th>
          <th width="91">&nbsp;</th>
          <th width="74">Min stock</th>
          <th width="74">&nbsp;</th>
        </tr>
      </thead>
      <tbody>
        <?php 
    $i=0;
    while($i < $accessories_num){
	       $a1 = mysql_result($accessories_result, $i, "store_item_id");
	       $a2 = mysql_result($accessories_result, $i, "item_name");
	       $a3 = mysql_result($accessories_result, $i, "location");
		   $a4 = mysql_result($accessories_result, $i, "subCategory");
	       $a5 = mysql_result($accessories_result, $i, "quantity");
	       $a6 = mysql_result($accessories_result, $i, "minimumStock");
	       $a7 = mysql_result($accessories_result, $i, "price");
		   $a8 = mysql_result($accessories_result, $i, "unit");
		   $a9 = mysql_result($accessories_result, $i, "supplier_id");
		   
		  	$table_name = $_SESSION['db_prefix']."_order_lines";
			$reserved_query = "SELECT SUM(quantity-delivered+returned-cancelled) AS reserved FROM $table_name WHERE store_item_id = '".$a1."' GROUP BY store_item_id  ";
			$reserved_result = mySQL_query($reserved_query);
			$reserved_num = mysql_num_rows($reserved_result);
			if ($reserved_num>0) {
			$a11 = mysql_result($reserved_result, 0, "reserved");
			}
			else {
				$a11=0;
			}
			
			$reservedTT_line='';
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$order_query = "SELECT order_no, order_reference, cust_id, (quantity-delivered+returned-cancelled) AS reserved FROM $table_name WHERE store_item_id = '".$a1."' AND (quantity-delivered+returned-cancelled) > 0 ";
	$order_result = mySQL_query($order_query);
	$order_num = mysql_num_rows($order_result);
	if ($order_num>0) {
	$a12 = mysql_result($order_result, 0, "order_no"); 
	$a13 = mysql_result($order_result, 0, "cust_id"); 
	$a14 = mysql_result($order_result, 0, "reserved");
	
	$k=0;
	while ($k<$order_num) {
		$a12 = mysql_result($order_result, $k, "order_no"); 
		$a13 = mysql_result($order_result, $k, "cust_id"); 
		$a14 = mysql_result($order_result, $k, "reserved");
		$a16 = mysql_result($order_result, $k, "order_reference");
		   
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT comp_name FROM $table_name2 WHERE cust_id ='".$a13."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$cust_name = mysql_result($customer_result, 0, "comp_name"); 
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$delivery_query = "SELECT delivery_date FROM $table_name WHERE order_reference = '".$a16."' ";
		$delivery_result = mySQL_query($delivery_query);
		$a15 = mysql_result($delivery_result, 0, "delivery_date");
		if ($a15=='2000-01-01') {
			  $a15 = 't.b.a.';
		}
		else {
			$a15 = date("d-m-Y", strtotime($a15));
		}
	
		$reservedTT_line = $reservedTT_line . '<tr><td width=75>'.$a12.'</td><td width=75>'.$a15.'<td><tdwidth=175>'.$cust_name.'</td><td  width=50; align=right>'.$a14.'<td><tr>';
				
		$k++;
	}
	$reservedTT = '<table class=sortable-onload-1r rowstyle-alt no-arrow; ><tr><td class=sortable-text>Order no</td><td class=sortable-date-dmy;>Delivery date</td><td class=sortable-text>Customer</td><td class=sortable-numeric>Reserved</td></tr>'.$reservedTT_line.'</table>';
	}
	else {
	$reservedTT = 'No reservations.';
	}
		   
$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 = mySQL_query($history_query);
$history_num = mysql_num_rows($history_result); 
		
$historyTT_body	='';  
$k=0;
while ($k<$history_num) {
          $h1 = mysql_result($history_result, $k, "date");
		  $h2 = mysql_result($history_result, $k, "entry");
		  $h2a='';
		  if ($h2=="5") {$h2a='stock count';}
		  if ($h2=="10") {$h2a='item added to store';}
		  if ($h2=="11") {$h2a='stock received';}
		  if ($h2=="13") {$h2a='order delivered';}
		  if ($h2=="18") {$h2a='return from order';}
		  if ($h2=="20") {$h2a='used in production';}
		  if ($h2=="21") {$h2a='from production';}
		  if ($h2=="25") {$h2a='adjustment depalleting';}
		  
		  $h3 = mysql_result($history_result, $k, "quantity");
		  if ($h2==5) {if ($h3>0) {$h3='+'.$h3;} }
		  if (($h2==11) OR ($h2==18) OR ($h2==21)) {$h3='+'.$h3;}
		  if ( ($h2==13) ) {$h3='-'.$h3;}
		  $h4 = mysql_result($history_result, $k, "total");

$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="right"><?php echo number_format($a5 , 0, '.', ','); ?></td>
          <td align="right"><?php echo $a5 ?></td>
          <td align="right" ><a href="reserved_order_list.php?store_item_id=<?php echo $a1 ?>" onMouseover="ddrivetip('<?php echo $reservedTT ?>', 375)"; onMouseout="hideddrivetip()"><?php echo number_format($a11, 0, '.', ',') ?></a></td>
          <td align="right" ><?php echo $a11 ?></td>
          <td align="right"><?php
         
			if($a5-$a11<0){
				echo "<font color=\"red\">".number_format($a5-$a11 , 0, '.', ',')."</font>";
				}
			else {
			 if($a5-$a11<$a6){

				echo "<font color=\"orange\">".number_format($a5-$a11 , 0, '.', ',')."</font>";
				}
		       else{
			     echo number_format($a5-$a11 , 0, '.', ',');
		     }	
		   }
			
			?>
          <?php //echo number_format(($a5-$a11), 0, '.', ',') ?></td>
          <td align="right"><?php echo ($a5-$a11) ?></td>
          <td align="right"><?php echo number_format($a6, 0, '.', ',') ?></td>
          <td align="right"><?php echo $a6 ?></td>
        </tr>
        <?php 
		$i++;
    	}
    	?>
      </tbody>
    </table>
          </div>
</div>



<?php include ('../navigation/side_bar.php'); ?>

</body>

<?php } else {
	
$_SESSION['denied_return'] = $_SESSION['location'];	
include ("../navigation/store_sidebar.php");
include ('../navigation/side_bar.php');
?>
	<body onload="accessdenied(); return false">
	</body>
<?php }
?>


</html>