<!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']."_product_store";
$product_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Sales product' ORDER BY item_name DESC";
$product_result = $con->query($product_query);
$product_num = mysqli_num_rows($product_result);

$table_name2 = $_SESSION['db_prefix']."_product_store";
$production_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Production' ORDER BY item_name DESC";
$production_result = $con->query($production_query);
$production_num = mysqli_num_rows($production_result);

$table_name2 = $_SESSION['db_prefix']."_product_store";
$recycle_query = "SELECT * FROM $table_name2 WHERE subCategory = 'Production' ORDER BY item_name DESC";
$recycle_result = $con->query($recycle_query);
$recycle_num = mysqli_num_rows($recycle_result);

$table_name = $_SESSION['db_prefix']."_production";
$all_stock_query = "SELECT SUM(produced_no-recycled_no-packed_no) AS all_stock FROM $table_name WHERE (produced_no-packed_no) <> 0";
$all_stock_result = $con->query($all_stock_query);
$row = mysqli_fetch_assoc($all_stock_result);
$all_stock = $row['all_stock'];
		

?>
</head>
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<body>

<div class="main_frame">
 
<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_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>			         

<label class="label_std" style="font-size:16px;">Blocks in store</label>
  <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 
	
    while($row = $product_result->fetch_assoc()) {
		   $a1 = $row["product_item_id"];
	       $a2 = $row["item_name"];
	       $a3 = $row["location"];
		   $a4 = $row["subCategory"];
	       $a5a = $row["quantity"];
	       $a6 = $row["minimumStock"];
	       $a7 = $row["price"];
		   $a8 = $row["unit"];
		   $a9 = $row["supplier_id"];
		   
	
			$table_name2 = $_SESSION['db_prefix']."_product_store_lines";
			$balance_query = "SELECT product_store_line_id, product_item_id, production_no, production_date, packing_date, batch_code, sr.quantity_in, sr.quantity_out, 
       							(SELECT SUM(sr2.quantity_in) - SUM(sr2.quantity_out) 
        						FROM $table_name2 sr2
        						WHERE sr2.product_store_line_id >= sr.product_store_line_id
       							AND product_item_id = '".$a1."'
								) AS stock_balance
								FROM  $table_name2 sr
								WHERE product_item_id = '".$a1."'
								";
								
			$balance_result = $con->query($balance_query);
			
			$stockTT_line = '';	
			$date_head = '';
			$a5=0;			
			while($row = $balance_result->fetch_assoc()) {
				$production_no = $row["production_no"];
				$display_date = $row["production_date"];
				$date_head = 'Production<br>date';
				if ($display_date=='') {
					  $display_date = $row["packing_date"];
					  $date_head = 'Packing<br>date';
				}
				$batch_code = $row["batch_code"];
				
				$line_stock_balance = $row["quantity_in"] - $row["quantity_out"];
				
				$a5 = $a5 + $line_stock_balance;
				
				if ($line_stock_balance>0) {		
				$stockTT_line = $stockTT_line.'<tr><td>'.$production_no.'</td><td>'.date("d-m-Y", strtotime($display_date)).'</td><td>'.$batch_code.'</td><td>'.$line_stock_balance.'</td></tr>';
				}
				
			}
			$stockTT='<table><tr><td>Production<br>no</td><td>'.$date_head.'</td><td>Batch code</td><td>Balance</td></tr>'.$stockTT_line.'</table><br>Total stock: '.$a5a.'.';
	
		   
		   
		   
		   
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$reserved_query = "SELECT SUM(quantity-delivered+returned-cancelled) AS reserved FROM $table_name WHERE product_item_id = '".$a1."' AND (quantity-delivered-cancelled) > 0  GROUP BY product_item_id ";
	$reserved_result = $con->query($reserved_query);
	$reserved_num = mysqli_num_rows($reserved_result);
	if ($reserved_num>0) {
		while($row = $reserved_result->fetch_assoc()) {
			$a11 = $row["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 product_item_id = '".$a1."' AND (quantity-delivered-cancelled) > 0 ";
	$order_result = $con->query($order_query);
	$order_num = mysqli_num_rows($order_result);
	if ($order_num>0) {
	while($row = $order_result->fetch_assoc()) {
      $a12 = $row["order_no"];
	  $a13 = $row["cust_id"]; 
	  $a14 = $row["reserved"];
		
		$a12 = $row["order_no"];
	 	$a13 = $row["cust_id"]; 
	    $a14 = $row["reserved"];
		$a16 = $row["order_reference"];
					
		$table_name = $_SESSION['db_prefix']."_orders";
		$delivery_query = "SELECT delivery_date FROM $table_name WHERE order_reference = '".$a16."' ";
		$delivery_result = $con->query($delivery_query);
		while($row = $delivery_result->fetch_assoc()) {
			$a15 = $row["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 = $con->query($customer_query);
		$customer_num = mysqli_num_rows($customer_result);
		while($row = $customer_result->fetch_assoc()) {
			$cust_name = $row["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>';
		
	$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']."_product_history";
$history_query = "SELECT entry, quantity, total, date FROM $table_name3 WHERE product_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	='';  
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 edited';}
		  if ($h2=="13") {$h2a='order delivered';}
		  if ($h2=="14") {$h2a='cash delivery';}
		  if ($h2=="18") {$h2a='return from order';}
		  if ($h2=="20") {$h2a='used in production';}
		  if ($h2=="21") {$h2a='produced';}
		  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=="29") {$h2a='products recycled';}
		  
		  $h3 = $row["quantity"];
		  if (($h2==5) OR ($h2==12)) {if ($h3>0) {$h3='+'.$h3;} }
		  if (($h2==5) OR ($h2==12)) {if ($h3<0) {$h3='-'.$h3;} }
		  if (($h2==11) OR ($h2==18) OR ($h2==21)) {$h3='+'.$h3;}
		  if ( ($h2==13) || ($h2==14) ) {$h3='-'.$h3;}
		  
		  $h4 = $row["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>'; 
}

$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="product_item_history.php?product_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">
          <a href="" onMouseover="ddrivetip('<?php echo $stockTT ?>', 350)"; onMouseout="hideddrivetip()">
		  <?php
           	if($a5a<0){
				echo "<font color=\"red\">".number_format($a5a , 2, '.', ',')."</font>";
				}
			else {
			 if($a5a<$a6){

				echo "<font color=\"orange\">".number_format($a5a , 2, '.', ',')."</font>";
				}
		       else{
			     echo number_format($a5a , 2, '.', ',');
		     }	
		   }
			?>
            </a>
          </td>
          <td align="right"><?php echo $a5a ?></td>
          <td align="right" ><a href="reserved_order_list.php?product_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 
		}
    	
    	?>
      </tbody>
    </table>
          </div>


<div class="page_inside_frame"> 
<br>
<br>
<br>
<label class="label_std" style="font-size:16px;">Blocks in curing</label>  
<!--<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},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
								]
				} );
			} );
</script>			         


  <table width="658" id="scroll_table2" class="data_display">
      <thead>
        <tr>
          <th width="212">Product Name</th>
          <th width="23">&nbsp;</th>
          <th width="96">Stock qty</th>
          <th width="211">&nbsp;</th>
          <th width="92">Min stock</th>
        </tr>
      </thead>
      <tbody>
        <?php 
	
    while($row = $production_result->fetch_assoc()) {
		   $a1 = $row["product_item_id"];
	       $a2 = $row["item_name"];
	       $a3 = $row["location"];
		   $a4 = $row["subCategory"];
	       $a5a = $row["quantity"];
	       $a6 = $row["minimumStock"];
	       $a7 = $row["price"];
		   $a8 = $row["unit"];
		   $a9 = $row["supplier_id"];
		   	   
	
			$table_name2 = $_SESSION['db_prefix']."_product_store_lines";
			$balance_query = "SELECT product_store_line_id, product_item_id, production_no, production_date, packing_date, batch_code, sr.quantity_in, sr.quantity_out, 
       							(SELECT SUM(sr2.quantity_in) - SUM(sr2.quantity_out) 
        						FROM $table_name2 sr2
        						WHERE sr2.product_store_line_id >= sr.product_store_line_id
       							AND product_item_id = '".$a1."'
								) AS stock_balance
								FROM  $table_name2 sr
								WHERE product_item_id = '".$a1."'
								AND quantity_in <> quantity_out 
								AND quantity_in > quantity_out
								";
						
			$balance_result = $con->query($balance_query);
			$balance_num = mysqli_num_rows($balance_result);
			
			$stockTT_line = '';	
			$date_head = '';
			$a5=0;
			
			if ($balance_num>0) {			
			while($row = $balance_result->fetch_assoc()) {
				$production_no = $row["production_no"];
				$display_date = $row["production_date"];
				$date_head = 'Production<br>date';
				if ($display_date=='') {
					  $display_date = $row["packing_date"];
					  $date_head = 'Packing<br>date';
				}
				$batch_code = $row["batch_code"];
				$line_stock_balance = $row["quantity_in"] - $row["quantity_out"];
					
				$a5 = $a5 + $line_stock_balance;
												
				if ($line_stock_balance>0) {
				$stockTT_line = $stockTT_line.'<tr><td>'.$production_no.'</td><td>'.date("d-m-Y", strtotime($display_date)).'</td><td>'.$batch_code.'</td><td>'.$line_stock_balance.'</td></tr>';
				}
				
			}
			}
			$stockTT='<table><tr><td>Production<br>no</td><td>'.$date_head.'</td><td>Batch code</td><td>Balance</td></tr>'.$stockTT_line.'</table><br>Total stock: '.$a5a.'  ('.$a5.').';
	
		   
$table_name3 = $_SESSION['db_prefix']."_product_history";
$history_query = "SELECT entry, quantity, total, date FROM $table_name3 WHERE product_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	='';  
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 edited';}
		  if ($h2=="13") {$h2a='order delivered';}
		  if ($h2=="14") {$h2a='cash delivery';}
		  if ($h2=="18") {$h2a='return from order';}
		  if ($h2=="20") {$h2a='used in production';}
		  if ($h2=="21") {$h2a='produced';}
		  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=="29") {$h2a='products recycled';}
		  
		  $h3 = $row["quantity"];
		  if (($h2==5)  OR ($h2==12)) {if ($h3>0) {$h3='+'.$h3;} }
		  //if (($h2==5)  OR ($h2==12)) {if ($h3<0) {$h3='-'.$h3;} }
		  if (($h2==11) OR ($h2==18) OR ($h2==21)) {$h3='+'.$h3;}
		  if ( ($h2==13)|| ($h2==14) ) {$h3='-'.$h3;}
		  
		  $h4 = $row["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>'; 
}

$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="product_item_history.php?product_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">
            <a href="" onMouseover="ddrivetip('<?php echo $stockTT ?>', 350)"; onMouseout="hideddrivetip()">
            <?php
           	if($a5a<0){
				echo "<font color=\"red\">".number_format($a5a , 2, '.', ',')."</font>";
				}
			else {
			 if($a5a<$a6){

				echo "<font color=\"orange\">".number_format($a5a , 2, '.', ',')."</font>";
				}
		       else{
			     echo number_format($a5a , 2, '.', ',');
		     }	
		   }
			?>
            </a>
          </td>
          <td align="right">&nbsp;</td>
          <td align="right"><?php echo number_format($a6, 0, '.', ',') ?></td>
        </tr>
        <?php 
		}
    	
    	?>
      </tbody>
    </table>
          </div>


<div class="page_inside_frame"> 
<br>
<br>
<br>
<label class="label_std" style="font-size:16px;">Blocks to recycle</label> <br>
<br>
 
<!--<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script> -->    
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table3').dataTable( {
					"sScrollY": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "asc" ]],
					"aoColumns": [
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
								 ]
				} );
			} );
</script>			         


  <table width="653" id="scroll_table3" class="data_display">
      <thead>
        <tr>
          <th width="224" >Product Name</th>
          <th width="91" >Recycle qty</th>
          <th width="322" >&nbsp;</th>
        </tr>
      </thead>
      <tbody>
        <?php 
	
    while($row = $recycle_result->fetch_assoc()) {
		   $a1 = $row["product_item_id"];
	       $a2 = $row["item_name"];
	       $a3 = $row["recycle_qty"];
		   
    ?>
        <tr class="alt">
          <td align="left"><a href="recycle_item_history.php?product_item_id=<?php echo $a1 ?>&date=0" onMouseover="ddrivetip('Click for recycle history', 125)"; onMouseout="hideddrivetip()"><?php echo $a2 ?></a></td>
          <td align="right"><?php echo $a3 ?></td>
          <td align="right">&nbsp;</td>
        </tr>
        <?php 
		}
    	
    	?>
      </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>