<!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");
	
				  
		$subCat = mysql_real_escape_string($_GET['subCatSelect']);
								
		$subCatText = $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 ($subCat=="Mitek") {
		$items_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Mitek' AND class <> 'Plate'  ORDER BY item_name ASC";
		$plates_query=  "SELECT * FROM $table_name2 WHERE subCategory = 'Mitek' AND class = 'Plate' ORDER BY item_name ASC";
		$plates_result = execsql($plates_query);
    	$plates_num = mysql_num_rows($plates_result);
		
		}
		
		if ($subCat=="Sundries") {
		$items_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Sundries'  ORDER BY item_name ASC";
		}
		
		if ($subCat=="Old") {
		$items_query = "SELECT * FROM $table_name2 WHERE category='Old' ORDER BY item_name ASC";	
		}
		
		$items_result = execsql($items_query);
    	$items_num = mysql_num_rows($items_result);
		
		if ($items_num==0) {
		  $message="No records to show";
		  }
		else {
		  $message="End of list";
		  }
			
?>
<!--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">

<!--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": "250px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 1, "asc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
                             	{"iDataSort": 3},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
								 ]
				} );
			} );
		</script>
        
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table_2').dataTable( {
					"sScrollY": "<?php if ($subCat=='Mitek') { echo '250px'; } else { echo '400px';} ?>",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 1, "asc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
                             	{"iDataSort": 3},
								{"bVisible": false},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
								 ]
				} );
			} );
		</script>

</head>

<body>

<div class="main_frame">
 
  <div class="page_head_frame">
    <label class="label_page_head">Store Items List <?php echo $subCatText ?></label>
  </div>
 <div class="page_middle_frame" style="height:<?php if ($subCat=='Mitek'){ echo '750px;';} else { echo '500px;'; }?>">
  <?php if ($subCat=='Mitek') { ?>
  <div class="page_inside_frame">
  Plates
    <br>
    <?php 
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stored_area_query = "SELECT SUM(area * quantity) AS summed_area FROM $table_name WHERE class='Plate' GROUP BY class";
	//echo '<br>'.$stored_area_query;
	$stored_area_result = mySQL_query($stored_area_query);
	$stored_area = mysql_result($stored_area_result, 0, "summed_area");
	
	$table_name = $_SESSION['db_prefix']."_captured_jobs";
	$estimated_area_query = "SELECT SUM(plate_area) AS summed_plate_area FROM $table_name WHERE compiled='yes' AND cutting_bill IS NULL";
	$estimated_area_result = mySQL_query($estimated_area_query);
	$estimated_area = mysql_result($estimated_area_result, 0, "summed_plate_area");
	
	$table_name2 = $_SESSION['db_prefix']."_captured_cash_sale";
	$estimated_area2_query = "SELECT SUM(plate_area) AS summed_plate_area FROM $table_name2 WHERE captured='yes' ";
	$estimated_area2_result = mySQL_query($estimated_area2_query);
	$estimated_area2 = mysql_result($estimated_area2_result, 0, "summed_plate_area");
	
	$total_estimated_area = $estimated_area + $estimated_area2;
	
	$tip_estimated_text = '';
	$tip_estimated2_text = '';
	//if ($cap_job_type=='Project') { $table_name = $_SESSION['db_prefix']."_captured_jobs"; } 
	//if ($cap_job_type=='Cash sale') { $table_name = $_SESSION['db_prefix']."_captured_cash_sale"; }
	
	$table_name2 = $_SESSION['db_prefix']."_captured_jobs";
	$tip_estimated_query = "SELECT job_no, cust_id, project_no, project_place, plate_area  FROM $table_name2 WHERE compiled='yes' AND cutting_bill IS NULL";
	$tip_estimated_result = mySQL_query($tip_estimated_query);
	$tip_estimated_num = mysql_num_rows($tip_estimated_result);
	
	$i=0;
	while ($i<$tip_estimated_num) {
	$tip_estimated_job_no = mysql_result($tip_estimated_result, $i, "job_no");
	$tip_estimated_cust_id = mysql_result($tip_estimated_result, $i, "cust_id");
	$tip_estimated_project_no = mysql_result($tip_estimated_result, $i, "project_no");
	$tip_estimated_project_place = mysql_result($tip_estimated_result, $i, "project_place");
	$tip_estimated_plate_area = mysql_result($tip_estimated_result, $i, "plate_area");
	
	$table_name20 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, comp_name, name_2, title FROM $table_name20 WHERE cust_id ='".$tip_estimated_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");
 			}
	
	$tip_estimated_text =  '<tr><td><label class=tip_text_label> '.$tip_estimated_job_no.'</label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$name.'</label></td><td><label class=tip_text_label> '.$tip_estimated_project_no.', '.$tip_estimated_project_place.'</label></td><td align=right><label class=tip_text_label> '.$tip_estimated_plate_area.'</label></td></tr>'.$tip_estimated_text;
	$i++;
	}
	
	$table_name2 = $_SESSION['db_prefix']."_captured_cash_sale";
	$tip_estimated2_query = "SELECT job_no, cust_id, project_no, project_place, plate_area  FROM $table_name2 WHERE captured='yes' ";
	$tip_estimated2_result = mySQL_query($tip_estimated2_query);
	$tip_estimated2_num = mysql_num_rows($tip_estimated2_result);
	
	$j=0;
	while ($j<$tip_estimated2_num) {
	$tip_estimated2_job_no = mysql_result($tip_estimated2_result, $j, "job_no");
	$tip_estimated2_cust_id = mysql_result($tip_estimated2_result, $j, "cust_id");
	$tip_estimated2_project_no = mysql_result($tip_estimated2_result, $j, "project_no");
	$tip_estimated2_project_place = mysql_result($tip_estimated2_result, $j, "project_place");
	$tip_estimated2_plate_area = mysql_result($tip_estimated2_result, $j, "plate_area");
	
	$table_name20 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, comp_name, name_2, title FROM $table_name20 WHERE cust_id ='".$tip_estimated2_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");
 			}
	
	$tip_estimated2_text =  '<tr><td><label class=tip_text_label> '.$tip_estimated2_job_no.'</label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$name.'</label></td><td><label class=tip_text_label> '.$tip_estimated2_project_no.', '.$tip_estimated2_project_place.'</label></td><td align=right><label class=tip_text_label> '.$tip_estimated2_plate_area.'</label></td></tr>'.$tip_estimated2_text;
	$j++;
	}
	
	$tip_estimated_head='Estimated:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
	$tip_estimated_foot='</table><br>';
	$estimatedTT = $tip_estimated_head.$tip_estimated_text.$tip_estimated2_text.$tip_estimated_foot;	
	
	
	$table_name3 = $_SESSION['db_prefix']."_cutting_bill_lines";
	$table_name4 = $_SESSION['db_prefix']."_store_items";
	$cutting_bill_area_query = "SELECT SUM(($table_name3.quantity-$table_name3.not_delivered) * $table_name4.area) AS summed_cutting_bill_area 
FROM $table_name3, $table_name4 
WHERE $table_name3.type= 'plate' AND $table_name3.store_item_id = $table_name4.store_item_id 
GROUP BY $table_name3.type ";
	$cutting_bill_area_result = mySQL_query($cutting_bill_area_query);
	$cutting_bill_area_num = mysql_num_rows($cutting_bill_area_result);
	if ($cutting_bill_area_num!=0) {
	$cutting_bill_area = mysql_result($cutting_bill_area_result, 0, "summed_cutting_bill_area");
	}
	else {
		$cutting_bill_area=0;
	}
	
	$table_name5 = $_SESSION['db_prefix']."_cutting_supplies_lines";
	$table_name4 = $_SESSION['db_prefix']."_store_items";
	$cutting_supplies_area_query = "SELECT $table_name5.store_item_id, SUM($table_name5.quantity * $table_name4.area) AS summed_cutting_supplies_area 
FROM $table_name5, $table_name4 
WHERE $table_name5.type= 'plate' AND $table_name5.store_item_id = $table_name4.store_item_id 
GROUP BY $table_name5.type ";
	$cutting_supplies_area_result = mySQL_query($cutting_supplies_area_query);
$cutting_supplies_area_num = mysql_num_rows($cutting_supplies_area_result);
	if ($cutting_supplies_area_num!=0) {
	       $cutting_supplies_area = mysql_result($cutting_supplies_area_result, 0, "summed_cutting_supplies_area");
	}
	else {
		$cutting_supplies_area = 0;
	}
	
	$reserved = $cutting_bill_area-$cutting_supplies_area;
	$available = $stored_area-$reserved;
	
	$tip_reserved_text = '';
	$table_name8 = $_SESSION['db_prefix']."_cutting_bill_lines";
	$table_name9 = $_SESSION['db_prefix']."_captured_jobs";
	$table_name4 = $_SESSION['db_prefix']."_store_items";
	$tip_reserved_query = "SELECT 
$table_name8.job_no, 
$table_name9.job_no, 
$table_name9.cust_id,
$table_name9.project_no,
$table_name9.project_place,
$table_name8.quantity,
$table_name9.supplied,
SUM($table_name8.quantity * $table_name4.area) AS reserved
FROM 
$table_name8, $table_name9, $table_name4
WHERE 
$table_name8.job_no = $table_name9.job_no  
AND
$table_name8.store_item_id = $table_name4.store_item_id
AND  
$table_name8.type = 'plate' 
AND 
$table_name9.supplied IS NULL
GROUP BY $table_name8.job_no";
//echo '<br>'.$tip_reserved_result;
	$tip_reserved_result = mySQL_query($tip_reserved_query);
	$tip_reserved_num = mysql_num_rows($tip_reserved_result);
	
	$i=0;
	while ($i<$tip_reserved_num) {
	$tip_reserved_job_no = mysql_result($tip_reserved_result, $i, "job_no");
	$tip_reserved_cust_id = mysql_result($tip_reserved_result, $i, "cust_id");
	$tip_reserved_project_no = mysql_result($tip_reserved_result, $i, "project_no");
	$tip_reserved_project_place = mysql_result($tip_reserved_result, $i, "project_place");
	$tip_reserved_reserved = mysql_result($tip_reserved_result, $i, "reserved");
	
	
	
	$table_name20 = $_SESSION['db_prefix']."_customer";
	$customer_query = "SELECT account_type, comp_name, name_2, title FROM $table_name20 WHERE cust_id ='".$tip_reserved_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");
 			}
	
	$tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$tip_reserved_job_no.'</label></td><td><label class=tip_text_label> '.$name.'</label></td><td><label class=tip_text_label> '.$tip_reserved_project_no.', '.$tip_reserved_project_place.'</label></td><td align=right><label class=tip_text_label> '.$tip_reserved_reserved.'</label></td></tr>'.$tip_reserved_text;
	$i++;
	}
	
	$tip_reserved_head='Reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
	$tip_reserved_foot='</table><br>';
	$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_foot;	
	
?>
    <table width="645" id="table_list">
      <tr>
        <td width="137">Stock (m<sup>2</sup>)</td>
        <td width="191">Estimated (m<sup>2</sup>)</td>
        <td width="161">Reserved (m<sup>2</sup>)</td>
        <td width="158">Available (m<sup>2</sup>)</td>
      </tr>
      <tr>
        <td><?php echo number_format($stored_area, 6, '.', ',') ?>&nbsp;</td>
        <td><a href="" onMouseover="ddrivetip('<?php echo $estimatedTT?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($total_estimated_area, 6, '.', ',') ?></a></td>
        <td><a href="" onMouseover="ddrivetip('<?php echo $reservedTT?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($reserved, 6, '.', ',') ?></a></td>
        <td><?php echo number_format($available, 6, '.', ',') ?></td>
      </tr>
    </table>
    <table width="662" id="scroll_table_1" class="data_display">
      <thead>
        <tr>
          <th width="181">Product Name</th>
          <th width="26">&nbsp;</th>
          <th width="71">Stock qty </span></th>
          <th width="26">&nbsp;</th>
          <th width="161">Reserved</th>
          <th width="84">Available</th>
          <th width="81">Min stock</th>
        </tr>
      </thead>
      <tbody>
        <?php 
    $i=0;
    while($i < $plates_num){
	       $b1 = mysql_result($plates_result, $i, "store_item_id");
	       $b2 = mysql_result($plates_result, $i, "item_name");
	       $b3 = mysql_result($plates_result, $i, "location");
		   $b4 = mysql_result($plates_result, $i, "subCategory");
	       $b5 = mysql_result($plates_result, $i, "quantity");
	       $b6 = mysql_result($plates_result, $i, "minimumStock");
	       $b7 = mysql_result($plates_result, $i, "price");
		   $b8 = mysql_result($plates_result, $i, "unit");
		   $b9 = mysql_result($plates_result, $i, "supplier_id");
		   $b10 = mysql_result($plates_result, $i, "price_2");
		   //$b11 = mysql_result($items_result, $i, "prereserved");
		   $b12 = '0';
		   $b13 = mysql_result($plates_result, $i, "class");
		   
		   
		   
		   	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
			$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
			$reserved_query = "SELECT SUM(quantity-not_delivered) AS sum_reserved FROM $table_name WHERE store_item_id = '".$b1."' ";
			$reserved_result = mySQL_query($reserved_query);
			$reserved_num = mysql_num_rows($reserved_result);
			$b12a=mysql_result($reserved_result, 0, "sum_reserved");
			
			$reserved2_query = "SELECT SUM(quantity) AS sum_reserved2 FROM $table_name2 WHERE store_item_id = '".$b1."' ";
			$reserved2_result = mySQL_query($reserved2_query);
			$reserved2_num = mysql_num_rows($reserved2_result);
			$b12b=mysql_result($reserved2_result, 0, "sum_reserved2");
			
			if ($reserved_num!=0) {
			$b12=$b12a-$b12b;
			$b23='';
			}
			
			$table_name = $_SESSION['db_prefix']."_captured_mitek";
			$cash_sale_query = "SELECT SUM(auth_quantity) AS summed_auth_quantity FROM $table_name WHERE cap_item_id ='".$b1."'  GROUP BY cap_item_id ";
			$cash_sale_result = mySQL_query($cash_sale_query);
			$cash_sale_num = mysql_num_rows($cash_sale_result);
			if ($cash_sale_num!=0) {
			      $cash_sale_quantity = mysql_result($cash_sale_result, 0, "summed_auth_quantity");
			}
			else {
				$cash_sale_quantity = 0;
			}
			
			$b12 = $b12 + $cash_sale_quantity; 
			
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
			$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
			$job_query = "SELECT job_no FROM $table_name  WHERE $table_name.store_item_id = '".$b1."' ";
			
			$job_result = mySQL_query($job_query); 
			$job_num = mysql_num_rows($job_result);
			$tip_reserved2_text = '';
			$j=0;
			while ($j<$job_num) {
			$job_no = mysql_result($job_result, $j, "job_no");
			if ($job_no!='') {  
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
			$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
			$job_reserved_query = "SELECT SUM($table_name.quantity-$table_name.not_delivered) AS res_1 FROM $table_name WHERE $table_name.job_no = '".$job_no."' AND $table_name.store_item_id = '".$b1."' ";
			$job_reserved_result = mySQL_query($job_reserved_query);
			$job_reserved_num = mysql_num_rows($job_reserved_result);
			$job_reserved = mysql_result($job_reserved_result, 0, "res_1"); 
			
			$job_reserved2_query = "SELECT SUM($table_name2.quantity) AS res_2 FROM $table_name2 WHERE $table_name2.job_no = '".$job_no."' AND $table_name2.store_item_id = '".$b1."' ";
			$job_reserved2_result = mySQL_query($job_reserved2_query);
			$job_reserved2_num = mysql_num_rows($job_reserved2_result);
			if ($job_reserved2_num!=0) {
			       $job_reserved2 = mysql_result($job_reserved2_result, 0, "res_2"); 
			}
			else {
				$job_reserved2 = 0;
			}
			
			
			$table_name1 = $_SESSION['db_prefix']."_captured_mitek";
			$table_name2 = $_SESSION['db_prefix']."_store_items";
			$cash_reserved_query = "SELECT $table_name1.cap_job, $table_name1.auth_quantity FROM $table_name1, $table_name2  WHERE $table_name1.cap_item_id = '".$b1."' AND $table_name1.cap_item_id=$table_name2.store_item_id AND $table_name2.class = 'Plate'";
			$cash_reserved_result = mySQL_query($cash_reserved_query);
			$cash_reserved_num = mysql_num_rows($cash_reserved_result);
			if ($cash_reserved_num!=0) {
			$cap_job = mysql_result($cash_reserved_result, 0, "cap_job");
			$auth_quantity = mysql_result($cash_reserved_result, 0, "auth_quantity");
			
						 
			$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
			$details_query = "SELECT cust_id, project_no, project_place  FROM $table_name WHERE job_no='".$cap_job."' ";
			$details_result = mySQL_query($details_query);
			$cust_id = mysql_result($details_result, 0, "cust_id");
			$project_no = mysql_result($details_result, 0, "project_no");
			$project_place = mysql_result($details_result, 0, "project_place"); 
			
			$table_name = $_SESSION['db_prefix']."_customer";
			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
			$cust_name_result = mySQL_query($cust_name_query);
			$cust_name_num = mysql_num_rows($cust_name_result);
			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
			
			$tip_reserved2_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Cash sale</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$auth_quantity.'</label></td></tr>'.$tip_reserved2_text;
			}
			
					
			
			$job_reservation = $job_reserved - $job_reserved2;  
			$tip_reserved_text = '';
			if ($job_reservation!=0) { 
						 
			//$table_name = $_SESSION['db_prefix']."_captured_jobs";
//			$details_query = "SELECT cust_id, project_no, project_place  FROM $table_name WHERE job_no='".$job_no."' ";
//			
//			echo '<br>*****'.$details_query;
//			$details_result = mySQL_query($details_query);
//			$cust_id = mysql_result($details_result, 0, "cust_id");
//			$project_no = mysql_result($details_result, 0, "project_no");
//			$project_place = mysql_result($details_result, 0, "project_place"); 
//			
//			$table_name = $_SESSION['db_prefix']."_customer";
//			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
//			$cust_name_result = mySQL_query($cust_name_query);
//			$cust_name_num = mysql_num_rows($cust_name_result);
//			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
//			  
//			$tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label>Project</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$job_reservation.'</label></td></tr>'.$tip_reserved_text;
			}
			}
			  $j++;
			}
		
		$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved2_text.$tip_reserved_foot;			
		
	
		      
    ?>
        <tr class="alt">
          <td align="left"><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=250 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><?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=="39") {$h2a='cash delivery';}
		  if ($h2=="40") {$h2a='job delivery';}
		  if ($h2=="41") {$h2a='issued to job';}
		  if ($h2=="42") {$h2a='additional delivery to 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) {$h3='+'.$h3;}
		  if (($h2==30) OR ($h2==31) OR ($h2==39) 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>', 260)"; onMouseout="hideddrivetip()"><?php echo $b2 ?></a></td>
          <td align="left"><?php echo $b2 ?></td>
          <td align="right"><?php
         
			if($b5<0){
				echo "<font color=\"red\">".number_format($b5 , 0, '.', ',')."</font>";
				}
			else {
			 if($b5<$b6){
				echo "<font color=\"orange\">".number_format($b5 , 0, '.', ',')."</font>";
				}
		       else{
			     echo number_format($b5 , 0, '.', ',');
		     }	
		   }
			
			?></td>
          <td align="right"><?php echo $b5 ?></td>
          <td align="right"><a href="" onMouseover="ddrivetip('<?php echo $reservedTT?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($b12 , 0, '.', ',') ?></a></td>
          <td align="right"><?php echo ($b5-$b12) ?></td>
          <td align="right"><?php echo $b6 ?></td>
        </tr>
        <?php 
		$i++;
    	}
    	?>
      </tbody>
    </table>
    
   
    
  </div>
  <div style="float:left; height:60px;"></div>
  <?php } ?>
 

<div class="page_inside_frame">
<?php if ($subCat=='Mitek') {echo 'Mitek products';}?>
  <table width="662" id="scroll_table_2" class="data_display">
      <thead>
        <tr>
          <th width="181">Product Name</th>
          <th width="23">&nbsp;</th>
          <th width="74">Stock qty </span></th>
          <th width="26">&nbsp;</th>
          <th width="80">Esitimated</th>
          <th width="77">Reserved</th>
          <th width="91">Available</th>
          <th width="74">Min 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, "price");
		   $b8 = mysql_result($items_result, $i, "unit");
		   $b9 = mysql_result($items_result, $i, "supplier_id");
		   $b10 = mysql_result($items_result, $i, "price_2");
		   //$b11 = mysql_result($items_result, $i, "prereserved");
		   $b12 = '0';
		   $b13 = mysql_result($items_result, $i, "class");
		   
		   if ($subCat=='Mitek') {
			$tip_reserved_text='';
			$reservedTT='';
			$job_no='';  
		   if ($b13=='Plate') {
		   	$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
			$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
			$reserved_query = "SELECT SUM(quantity-not_delivered) AS sum_reserved FROM $table_name WHERE store_item_id = '".$b1."' ";
			$reserved_result = mySQL_query($reserved_query);
			$reserved_num = mysql_num_rows($reserved_result);
			$b12a=mysql_result($reserved_result, 0, "sum_reserved");
			
			$reserved2_query = "SELECT SUM(quantity) AS sum_reserved2 FROM $table_name2 WHERE store_item_id = '".$b1."' ";
			$reserved2_result = mySQL_query($reserved2_query);
			$reserved2_num = mysql_num_rows($reserved2_result);
			$b12b=mysql_result($reserved2_result, 0, "sum_reserved2");
			
			if ($reserved_num!=0) {
			$b12=$b12a-$b12b;
			$b23='';
			}
			
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
			$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
			$job_query = "SELECT job_no FROM $table_name  WHERE $table_name.store_item_id = '".$b1."' ";
			
			$job_result = mySQL_query($job_query); 
			$job_num = mysql_num_rows($job_result);
			
			$j=0;
			while ($j<$job_num) {
			  $job_no = mysql_result($job_result, $j, "job_no");
			  
			if ($job_no!='') {  
			$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
			$table_name2 = $_SESSION['db_prefix']."_cutting_supplies_lines";
			$job_reserved_query = "SELECT SUM($table_name.quantity-$table_name.not_delivered) AS res_1 FROM $table_name WHERE $table_name.job_no = '".$job_no."' AND $table_name.store_item_id = '".$b1."' ";
			$job_reserved_result = mySQL_query($job_reserved_query);
			$job_reserved_num = mysql_num_rows($job_reserved_result);
			$job_reserved = mysql_result($job_reserved_result, 0, "res_1"); 
			
			$job_reserved2_query = "SELECT SUM($table_name2.quantity) AS res_2 FROM $table_name2 WHERE $table_name2.job_no = '".$job_no."' AND $table_name2.store_item_id = '".$b1."' ";
			$job_reserved2_result = mySQL_query($job_reserved2_query);
			$job_reserved2_num = mysql_num_rows($job_reserved2_result);
			if ($job_reserved2_num!=0) {
			       $job_reserved2 = mysql_result($job_reserved2_result, 0, "res_2"); 
			}
			else {
				$job_reserved2 = 0;
			}
			
			$job_reservation = $job_reserved - $job_reserved2;  
			
			if ($job_reservation!=0) {  
			$table_name = $_SESSION['db_prefix']."_captured_jobs";
			$details_query = "SELECT cust_id, project_no, project_place  FROM $table_name WHERE job_no='".$job_no."' ";
			$details_result = mySQL_query($details_query);
			$cust_id = mysql_result($details_result, 0, "cust_id");
			$project_no = mysql_result($details_result, 0, "project_no");
			$project_place = mysql_result($details_result, 0, "project_place"); 
			
			$table_name = $_SESSION['db_prefix']."_customer";
			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
			$cust_name_result = mySQL_query($cust_name_query);
			$cust_name_num = mysql_num_rows($cust_name_result);
			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
			  
			$tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$job_reservation.'</label></td></tr>'.$tip_reserved_text;
			}
			}
			  $j++;
			}
		
		$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_foot;			
			}
		   else {
					   
		    $table_name = $_SESSION['db_prefix']."_captured_mitek";
			$esitimate_query = "SELECT SUM(cap_quantity) AS sum_captured FROM $table_name WHERE cap_item_id = '".$b1."' AND cap_job_type <> 'Cash sale' AND authorised IS NULL GROUP BY cap_quantity ";
			$esitimate_result = mySQL_query($esitimate_query);
			$esitimate_num = mysql_num_rows($esitimate_result);
			
			if ($esitimate_num!=0) {
			  $b23 = mysql_result($esitimate_result, 0, "sum_captured");
			}
			else {
			   $b23 = 0;
			}
			
			
			$table_name = $_SESSION['db_prefix']."_captured_mitek";
			$table_name2 = $_SESSION['db_prefix']."_captured_jobs";
			$job_query = "SELECT cap_job, (cap_quantity) AS cap_quantity FROM $table_name  WHERE cap_item_id = '".$b1."' AND cap_job_type <> 'Cash sale' AND   authorised IS NULL ";
			$job_result = mySQL_query($job_query); 
			$job_num = mysql_num_rows($job_result);
			
			$tip_estimated_text='';
			$k=0;
			while ($k<$job_num) {
			  $job_no = mysql_result($job_result, $k, "cap_job");
			  
			  $b23a = mysql_result($job_result, $k, "cap_quantity");
			
			
			
			if ($job_no!='') { 
			
			if ($b23a!=0) { 
			
			$table_name = $_SESSION['db_prefix']."_captured_mitek";
			$job_type_query = "SELECT cap_job_type  FROM $table_name WHERE  cap_job='".$job_no."' ";
			
			$job_type_result = mySQL_query($job_type_query);
			$cap_job_type = mysql_result($job_type_result, 0, "cap_job_type");
						
			if ($cap_job_type=='Project') { $table_name2 = $_SESSION['db_prefix']."_captured_jobs"; } 
			if ($cap_job_type=='Cash sale') { $table_name2 = $_SESSION['db_prefix']."_captured_cash_sale"; } 
			
			$details_query = "SELECT cust_id, project_no, project_place FROM $table_name2 WHERE job_no='".$job_no."' ";
			
									
			$details_result = mySQL_query($details_query);
			$cust_id = mysql_result($details_result, 0, "cust_id");
			$project_no = mysql_result($details_result, 0, "project_no");
			$project_place = mysql_result($details_result, 0, "project_place"); 
			
			$table_name = $_SESSION['db_prefix']."_customer";
			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
			$cust_name_result = mySQL_query($cust_name_query);
			$cust_name_num = mysql_num_rows($cust_name_result);
			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
			  
			$tip_estimated_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label> '.$cap_job_type.'</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$b23a.'</label></td></tr>'.$tip_estimated_text;
			}
			}
			$k++;
			}
			$tip_estimated_head='Estimated:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Estimated</label></td></tr>';
		$tip_estimated_foot='</table><br>';
		$estimatedTT = $tip_estimated_head.$tip_estimated_text.$tip_estimated_foot;	
			
			
			$table_name = $_SESSION['db_prefix']."_captured_mitek";
			$reserved_query = "SELECT SUM(auth_quantity-de_authorised) AS auth_quantity FROM $table_name WHERE cap_item_id = '".$b1."' AND (auth_quantity-delivered-not_delivered-de_authorised)>0 GROUP BY cap_item_id ";
						
			$reserved_result = mySQL_query($reserved_query);
			$reserved_num = mysql_num_rows($reserved_result);
			
			if ($reserved_num!=0) {
			  $b12 = mysql_result($reserved_result, 0, "auth_quantity");
			}
			else {
			   $b12 = 0;
			}
			
				
			$table_name = $_SESSION['db_prefix']."_captured_mitek";
			$job_query = "SELECT cap_job, (auth_quantity-de_authorised) AS auth_quantity, cap_job_type FROM $table_name  WHERE cap_item_id = '".$b1."' AND (auth_quantity-delivered-not_delivered-de_authorised)>0 ";
			$job_result = mySQL_query($job_query); 
			$job_num = mysql_num_rows($job_result);
			
			
			$j=0;
			while ($j<$job_num) {
			  $job_no = mysql_result($job_result, $j, "cap_job");
			  $b12a = mysql_result($job_result, $j, "auth_quantity");
			  $cap_job_type = mysql_result($job_result, $j, "cap_job_type");
			  			  
			if ($job_no!='') { 
			
			if ($b12!=0) { 
			if ($cap_job_type=='Cash sale') {
				   $table_name = $_SESSION['db_prefix']."_captured_cash_sale";
			}
			else {
					$table_name = $_SESSION['db_prefix']."_captured_jobs";
			}
			$details_query = "SELECT cust_id, project_no, project_place  FROM $table_name WHERE job_no='".$job_no."' ";
			
			$details_result = mySQL_query($details_query);
			$cust_id = mysql_result($details_result, 0, "cust_id");
			$project_no = mysql_result($details_result, 0, "project_no");
			$project_place = mysql_result($details_result, 0, "project_place"); 
			
			$table_name = $_SESSION['db_prefix']."_customer";
			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
			$cust_name_result = mySQL_query($cust_name_query);
			$cust_name_num = mysql_num_rows($cust_name_result);
			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
			  
			$tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label> '.$cap_job_type.'</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$b12a.'</label></td></tr>'.$tip_reserved_text;
			}
			}
			$j++;
			}
			$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_foot;	
		   }
		   }
		   
		   if ($subCat=='Sundries') {
			$tip_reserved_text='';
			$reservedTT='';
			
			$table_name = $_SESSION['db_prefix']."_captured_sundries";
			$esitimate_query = "SELECT SUM(cap_quantity) AS sum_captured FROM $table_name WHERE cap_item_id = '".$b1."' AND cap_job_type <> 'Cash sale' AND  authorised IS NULL ";
			$esitimate_result = mySQL_query($esitimate_query);
			$esitimate_num = mysql_num_rows($esitimate_result);
			
			if ($esitimate_num!=0) {
			  $b23 = mysql_result($esitimate_result, 0, "sum_captured");
			}
			else {
			   $b23 = 0;
			}
			
			$table_name = $_SESSION['db_prefix']."_captured_sundries";
			$job_query = "SELECT cap_job, (cap_quantity) AS cap_quantity FROM $table_name  WHERE cap_item_id = '".$b1."' AND cap_job_type <> 'Cash sale' AND  authorised IS NULL ";
			$job_result = mySQL_query($job_query); 
			$job_num = mysql_num_rows($job_result);
			
			$tip_estimated_text='';
			$k=0;
			while ($k<$job_num) {
			  $job_no = mysql_result($job_result, $k, "cap_job");
			  $b23a = mysql_result($job_result, $k, "cap_quantity");
			  
			if ($job_no!='') { 
			
			if ($b23!=0) {
			$table_name = $_SESSION['db_prefix']."_captured_sundries";
			$job_type_query = "SELECT cap_job_type  FROM $table_name WHERE  cap_job='".$job_no."' ";
			$job_type_result = mySQL_query($job_type_query);
			$cap_job_type = mysql_result($job_type_result, 0, "cap_job_type");
				
			if ($cap_job_type=='Project') { $table_name2 = $_SESSION['db_prefix']."_captured_jobs"; } 
			if ($cap_job_type=='Cash sale') { $table_name2 = $_SESSION['db_prefix']."_captured_cash_sale"; } 	
			$details_query = "SELECT cust_id, project_no, project_place  FROM $table_name2 WHERE job_no='".$job_no."' ";
			
			
			$details_result = mySQL_query($details_query);
			$cust_id = mysql_result($details_result, 0, "cust_id");
			$project_no = mysql_result($details_result, 0, "project_no");
			$project_place = mysql_result($details_result, 0, "project_place"); 
			
			$table_name = $_SESSION['db_prefix']."_customer";
			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
			$cust_name_result = mySQL_query($cust_name_query);
			$cust_name_num = mysql_num_rows($cust_name_result);
			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
			  
			$tip_estimated_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label> '.$cap_job_type.'</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$b23a.'</label></td></tr>'.$tip_estimated_text;
			}
			}
			$k++;
			}
			$tip_estimated_head='Estimated:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Estimated</label></td></tr>';
		$tip_estimated_foot='</table><br>';
		$estimatedTT = $tip_estimated_head.$tip_estimated_text.$tip_estimated_foot;	
			
			$table_name = $_SESSION['db_prefix']."_captured_sundries";
			$reserved_query = "SELECT SUM(auth_quantity-delivered-not_delivered-de_authorised) AS sum_reserved FROM $table_name WHERE cap_item_id = '".$b1."' GROUP BY cap_item_id ";
			
			$reserved_result = mySQL_query($reserved_query);
			$reserved_num = mysql_num_rows($reserved_result);
			
			if ($reserved_num!=0) {
			  $b12 = mysql_result($reserved_result, 0, "sum_reserved");
			}
		   
		  
		  $table_name = $_SESSION['db_prefix']."_captured_sundries";
			$job_query = "SELECT cap_job,  (auth_quantity-de_authorised) AS auth_quantity, cap_job_type FROM $table_name  WHERE cap_item_id = '".$b1."' AND (auth_quantity-delivered-de_authorised)>0 ";
			
			$job_result = mySQL_query($job_query); 
			$job_num = mysql_num_rows($job_result);
			$j=0;
			while ($j<$job_num) {
			  $job_no = mysql_result($job_result, $j, "cap_job");
			  $b12a =  mysql_result($job_result, $j, "auth_quantity");
			  $cap_job_type = mysql_result($job_result, $j, "cap_job_type");
			
			if ($job_no!='0') { 
			
			if ($b12!=0) {
				
			if ($cap_job_type=='Cash sale') {
				   $table_name = $_SESSION['db_prefix']."_captured_cash_sale";
			}
			else {
					$table_name = $_SESSION['db_prefix']."_captured_jobs";
			}	  
			
			$details_query = "SELECT cust_id, project_no, project_place  FROM $table_name WHERE job_no='".$job_no."' ";
			
			$details_result = mySQL_query($details_query);
			$cust_id = mysql_result($details_result, 0, "cust_id");
			$project_no = mysql_result($details_result, 0, "project_no");
			$project_place = mysql_result($details_result, 0, "project_place"); 
			
			$table_name = $_SESSION['db_prefix']."_customer";
			$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id='".$cust_id."' ";
			$cust_name_result = mySQL_query($cust_name_query);
			$cust_name_num = mysql_num_rows($cust_name_result);
			$comp_name = mysql_result($cust_name_result, 0, "comp_name"); 
			  
			$tip_reserved_text =  '<tr><td><label class=tip_text_label> '.$job_no.'</label></td><td><label class=tip_text_label> '.$cap_job_type.'</label></td><td><label class=tip_text_label> '.$comp_name.'</label></td><td><label class=tip_text_label> '.$project_no.', '.$project_place.'</label></td><td align=right><label class=tip_text_label> '.$b12a.'</label></td></tr>'.$tip_reserved_text;
			}
			}
			$j++;
			}
			$tip_reserved_head='Actual reserved:<br><table border=1 cellpadding=5 style=border-collapse:collapse; ><tr><td><label class=tip_text_label_bold>Job no</label></td><td><label class=tip_text_label_bold>Type</label></td><td><label class=tip_text_label_bold>Customer</label></td><td><label class=tip_text_label_bold>Location</label></td><td><label class=tip_text_label_bold>Actual</label></td></tr>';
		$tip_reserved_foot='</table><br>';
		$reservedTT = $tip_reserved_head.$tip_reserved_text.$tip_reserved_foot;	
		   } 
		   
		   
		   //$table_name2 = $_SESSION['db_prefix']."_suppliers";
//           $suppliers_query = "SELECT account_name   FROM $table_name2 WHERE supplier_id = '".$b9."' ";
//           $suppliers_result = mySQL_query($suppliers_query);
//		   $suppliers_num = mysql_num_rows($suppliers_result);
//           if ($suppliers_num!=0) {
//		   $b9a = mysql_result($suppliers_result, 0, "account_name");
//		   }
//		   else {
//			   $b9a="";
	


		   
           		   
		   //$itemTT =
//		   "<img src=../images/view.png  width=20 height=16><table id=table_tool_tip width=250 ><tr><td  width=75>Item name:</td><td width=175>".$b2."</td></tr><tr><td>Supplier:</td><td>".$b9a."</td></tr></table><br><b></b>";
		   
		      
    ?>
        <tr class="alt">
          <td align="left"><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=250 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><?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=="39") {$h2a='cash delivery';}
		  if ($h2=="40") {$h2a='job delivery';}
		  if ($h2=="41") {$h2a='issued to job';}
		  if ($h2=="42") {$h2a='Additional delivery to 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) {$h3='+'.$h3;}
		  if (($h2==30) OR ($h2==31) OR ($h2==39) 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>', 260)"; onMouseout="hideddrivetip()"><?php echo $b2 ?></a></td>
          <td align="left"><?php echo $b2 ?></td>
          <td align="right"><?php
         
			if($b5<0){
				echo "<font color=\"red\">".number_format($b5 , 0, '.', ',')."</font>";
				}
			else {
			 if($b5<$b6){
				echo "<font color=\"orange\">".number_format($b5 , 0, '.', ',')."</font>";
				}
		       else{
			     echo number_format($b5 , 0, '.', ',');
		     }	
		   }
			
			?></td>
          <td align="right"><?php echo $b5 ?></td>
          <td align="right"><?php if ($b13=='Plate') { echo 'n/a'; } else { ?><a href="" onMouseover="ddrivetip('<?php echo $estimatedTT?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($b23 , 0, '.', ',') ?></a><?php } ?></td>
          <td align="right"><a href="" onMouseover="ddrivetip('<?php echo $reservedTT?>  ', 450)"; onMouseout="hideddrivetip()"><?php echo number_format($b12 , 0, '.', ',') ?></a></td>
          <td align="right"><?php echo ($b5-$b12) ?></td>
          <td align="right"><?php echo $b6 ?></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'); ?>
