<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php 
include('../include/session.php');
					  
$status = $_GET['status'];		
								
		
		$table_name2 = $_SESSION['db_prefix']."_store_items"; 
		
				 
if ($status=='min') {		
		$items_query = "SELECT * FROM $table_name2 WHERE quantity < minimumStock ORDER BY item_name ASC";
		$items_result = execsql($items_query);
    	$items_num = mysql_num_rows($items_result);
		$list_heading='Store items with stock quantity below minimum stock required.';
}

if ($status=='non') {		
		$items_query = "SELECT * FROM $table_name2 WHERE (quantity <= 0) AND (minimumStock <> 0)  ORDER BY item_name ASC";
		$items_result = execsql($items_query);
    	$items_num = mysql_num_rows($items_result);
		$list_heading='Store items with no stock available (minimum stock not 0 (zero). )';
}
		
		$tip_reserved_text='';
		$reservedTT='';
		    	
?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<link href="../css/main.css" rel="stylesheet" type="text/css">

</head>

<body>
<?php
include ("../navigation/header.php");
$page_name = "Stores-main";
include ("../navigation/access.php");
if ($permission_num==1) {
	 include ("../navigation/store_sidebar.php");
	 ?>
<div class="main_frame">
 
  <div class="page_head_frame">
    <label class="label_page_head">Store Status List <?php echo $subCatText ?></label>
  </div>
 

<div class="page_inside_frame">
<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script>
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script> 
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table_1').dataTable( {
					"sScrollY": "410px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "asc" ]],
					"aoColumns": [
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
								 ]
				} );
			} );
		</script>
  <label class="label_std"><?php echo $list_heading ?></label>      
  <table width="650" id="scroll_table_1" class="data_display">
      <thead>
        <tr>
          <th>Product Name</th>
          <th style="border-left:hidden;">&nbsp;</th>
          <th width="101">Group</th>
          <th width="56">Stock<br>qty</th>
          <th width="41">Unit</th>
          <th width="56">Reser<br>
            ved</th>
          <th width="56">Avai<br>
            lable</th>
          <th width="56">Min<br>stock</th>
          <th width="66">Max<br>stock</th>
        </tr>
      </thead>
      <tbody>
        <?php 
    $i=0;
	
    while($i < $items_num){
	       $b1 = mysql_result($items_result, $i, "store_item_id");
	       $b2 = mysql_result($items_result, $i, "item_name");
	       $b3 = mysql_result($items_result, $i, "location");
		   $b4 = mysql_result($items_result, $i, "subCategory");
	       $b5 = mysql_result($items_result, $i, "quantity");
	       $b6 = mysql_result($items_result, $i, "minimumStock");
	       $b7 = mysql_result($items_result, $i, "maximumStock");
		   $b8 = mysql_result($items_result, $i, "unit");
		   $b9 = mysql_result($items_result, $i, "supplier_id");
		   $b10 = mysql_result($items_result, $i, "price_2");
		   
		   $reserved_bodyTT='';
		   	$table_name = $_SESSION['db_prefix']."_order_job_lines";
			$reserved1_query = "SELECT SUM(quantity-delivered) AS summed_quantity FROM $table_name WHERE store_item_id = '".$b1."' AND misc IS NULL GROUP BY store_item_id";
			$reserved1_result = mySQL_query($reserved1_query);
			$reserved1_num = mysql_num_rows($reserved1_result);
			if ($reserved1_num!=0) {
			$b12_1 = mysql_result($reserved1_result, 0, "summed_quantity");
			}
			else {
				$b12_1=0;
			}
			
			$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines";
			$reserved2_query = "SELECT SUM(quantity-delivered) AS summed_quantity FROM $table_name WHERE store_item_id = '".$b1."' AND misc IS NULL GROUP BY store_item_id";
			$reserved2_result = mySQL_query($reserved2_query);
			$reserved2_num = mysql_num_rows($reserved2_result);
			if ($reserved2_num!=0) {
			$b12_2 = mysql_result($reserved2_result, 0, "summed_quantity");
			}
			else {
				$b12_2=0;
			}
			
			$b12 = $b12_1 + $b12_2;
			
			$table_name = $_SESSION['db_prefix']."_order_job_lines";
			$job_reserved_query = "SELECT job_card_no, quantity, delivered FROM $table_name WHERE store_item_id = '".$b1."' AND misc IS NULL ";
			$job_reserved_result = mySQL_query($job_reserved_query);
			$job_reserved_num = mysql_num_rows($job_reserved_result);
			$m=0;
			while ($m<$job_reserved_num) {
						$job_card_no = mysql_result($job_reserved_result, $m, "job_card_no");
						$reserved = mysql_result($job_reserved_result, $m, "quantity")-mysql_result($job_reserved_result, $m, "delivered");
						
						$table_name = $_SESSION['db_prefix']."_compiled_jobs";
						$project_query = "SELECT cust_id, project FROM $table_name WHERE job_card_no = '".$job_card_no."' ";
						$project_result = mySQL_query($project_query);
						$cust_id = mysql_result($project_result, 0, "cust_id");
						$project = mysql_result($project_result, 0, "project");
						
						$table_name2 = $_SESSION['db_prefix']."_customer";
						$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
						$customer_result = mySQL_query($customer_query);
						$customer_num = mysql_num_rows($customer_result);	
						$a1 = mysql_result($customer_result, 0, "account_type");
						if ($a1=="corporate") { 
								$name = mysql_result($customer_result, 0, "comp_name"); 
						} 
						else {
								$name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 						}
						
						
						if ($reserved>0) {
						$reserved_bodyTT = $reserved_bodyTT."<tr><td align=left>".$job_card_no."</td><td>".$project."</td><td>".$name."</td><td align=center>".$reserved."</td></tr>"; 
						}
						$m++;
			}
			
			$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines";
			$job_reserved_query = "SELECT job_card_no, quantity, delivered FROM $table_name WHERE store_item_id = '".$b1."' AND misc IS NULL ";
			$job_reserved_result = mySQL_query($job_reserved_query);
			$job_reserved_num = mysql_num_rows($job_reserved_result);
			$m=0;
			while ($m<$job_reserved_num) {
						$job_card_no = mysql_result($job_reserved_result, $m, "job_card_no");
						$reserved = mysql_result($job_reserved_result, $m, "quantity")-mysql_result($job_reserved_result, $m, "delivered");
						
						$table_name = $_SESSION['db_prefix']."_compiled_jobs";
						$project_query = "SELECT cust_id, project FROM $table_name WHERE job_card_no = '".$job_card_no."' ";
						$project_result = mySQL_query($project_query);
						$cust_id = mysql_result($project_result, 0, "cust_id");
						$project = mysql_result($project_result, 0, "project");
						
						$table_name2 = $_SESSION['db_prefix']."_customer";
						$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
						$customer_result = mySQL_query($customer_query);
						$customer_num = mysql_num_rows($customer_result);	
						$a1 = mysql_result($customer_result, 0, "account_type");
						if ($a1=="corporate") { 
								$name = mysql_result($customer_result, 0, "comp_name"); 
						} 
						else {
								$name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 						}
						
						
						if ($reserved>0) {
						$reserved_bodyTT = $reserved_bodyTT."<tr><td align=left>".$job_card_no."(M)</td><td>".$project."</td><td>".$name."</td><td align=center>".$reserved."</td></tr>"; 
						}
						$m++;
			}
				   
		     
			$itemTT = "<table id=table_tool_tip width=250 ><tr><td  width=75>Item name:</td><td width=175>".$b2."</td></tr><tr><td>Group</td><td>".$b4."</td></tr><tr><td>Location:</td><td>".$b3."</td></tr><tr><td>Min. stock</td><td>".$b6."</td></tr><tr><td>Max. stock</td><td>".$b7."</td></tr></table><br><b>Click for details</b>";
			
			$reservedTT ="Reserved<br><table id=table_tool_tip border=1 ><tr><td  width=75>Job card</td><td width=175>Project</td><td  width=250>Customer</td><td  width=25 >Reserved</td></tr>".$reserved_bodyTT."</table><br><b>Click for details</b>";
		      
    ?>
        <tr class="alt">
          <td width="156" align="left"><a href="store_item_display.php?store_item_id=<?php echo $b1 ?> " onMouseover="ddrivetip('<?php echo $itemTT ?>', 260)"; onMouseout="hideddrivetip()"><?php echo $b2 ?></a></td>
          <td width="22" align="right" style="border-left:hidden;"><a href="store_item_history.php?store_item_id=<?php echo $b1 ?>&date=0" onMouseOver="ddrivetip('<?php           
$table_name3 = $_SESSION['db_prefix']."_item_history";
$history_query = "SELECT entry, quantity, total, date FROM $table_name3 WHERE store_item_id = '".$b1."' ORDER BY history_id DESC LIMIT 10";
$history_result = mySQL_query($history_query);
$history_num = mysql_num_rows($history_result); 
?><b>Recent history:</b><br><br><table id=table_tool_tip width=300 border=1 cellspacing=0 cellpadding=0><tr><td width=75>date</td><td width=125>action</td><td width=50>qty</td><td width=50>stock qty</td></tr><?php
$k=0;
while ($k<$history_num) {
          $h1 = mysql_result($history_result, $k, "date");
		  $h2 = mysql_result($history_result, $k, "entry");
		  if ($h2=="5") {$h2a='stock count';}
		  if ($h2=="10") {$h2a='new';}
		  if ($h2=="20") {$h2a='received';}
		  if ($h2=="30") {$h2a='to department';}
		  if ($h2=="31") {$h2a='to employee';}
		  if ($h2=="32") {$h2a='supplied to job';}
		  if ($h2=="33") {$h2a='additional supply';}
		  if ($h2=="34") {$h2a='return from job';}
//		  if ($h2=="43") {$h2a='cash sale';}
		  $h3 = mysql_result($history_result, $k, "quantity");
		  if ($h2==5) {if ($h3>0) {$h3='+'.$h3;} }
		  if (($h2==20) OR ($h2==34)) {$h3='+'.$h3;}
		  if (($h2==30) OR ($h2==31) OR ($h2==40) OR ($h2==41) OR ($h2==42) OR ($h2==43)) {$h3='-'.$h3;}
		  $h4 = mysql_result($history_result, $k, "total");?>
<tr><td><?php echo date("d-m-Y", strtotime($h1)) ?></td><td><?php echo $h2a ?></td><td align=right><?php echo $h3 ?></td><td align=right><?php echo $h4 ?></td><?php 
$k++;
}
?></tr></table><br><b>Click for full history</b>', 300)"; onMouseOut="hideddrivetip()"><img src="../images/history_icon.png" width="16" height="17"></a></td>
          <td align="left"><?php echo $b4 ?>&nbsp;</td>
          <td align="center"><?php
         
			if($b5<0){
				if ($b8!='no') {echo "<font color=red>".number_format($b5, 2, '.', ',')."</font>";} else { echo "<font color=red>".number_format($b5)."</font>" ; }
				}
			else {
			 if($b5<$b6){
				if ($b8!='no') {echo "<font color=orange>".number_format($b5, 2, '.', ',')."</font>";} else { echo "<font color=orange>".number_format($b5)."</font>" ; }
				}
		       else{
			    if ($b8!='no') {echo number_format($b5, 2, '.', ',');} else { echo number_format($b5); }
		     }	
		   }
			
			?></td>
          <td align="center"><?php echo $b8 ?></td>
          <td align="center"><a href="" onMouseOver="ddrivetip('<?php echo $reservedTT?>  ', 400)"; onMouseOut="hideddrivetip()"><?php if ($b8!='no') {echo number_format($b12, 2, '.', ',');} else { echo number_format($b12); } ?></a></td>
          <td align="center"><?php if ($b8!='no') {echo number_format($b5-$b12, 2, '.', ',');} else { echo number_format($b5-$b12); } ?></td>
          <td align="center"><?php if ($b8!='no') {echo number_format($b6, 2, '.', ',');} else { echo number_format($b6); } ?></td>
          <td align="center"><?php if ($b8!='no') {echo number_format($b7, 2, '.', ',');} else { echo number_format($b7); } ?></td>
        </tr>
        <?php 
		$i++;
    	}
    	?>
      </tbody>
    </table>
  </div>  
  </div>
</div>
</body>
<?php } else { 
    $_SESSION['denied_return'] = "store/store_main.php";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
