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

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

include('../include/period_select_date.php');	 
$supply_group = $_GET['supply_group'];
$selector = $_GET['selector'];
 
$table_name = $_SESSION['db_prefix']."_internal_supply";
if ($supply_group=='All') {	 	
$display_query = "SELECT * FROM $table_name WHERE supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
$selector_display='';
}

if ($supply_group=='employee') {	
if ($selector=='') { 	
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
else {
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND issued_to = '".$selector."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
$table_name2 = $_SESSION['db_prefix']."_employee";
$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$selector."' ";
$employee_result = mySQL_query($employee_query);
$employee_num = mysql_num_rows($employee_result);
if ($employee_num>0) {	
$selector_display = mysql_result($employee_result, 0, "emp_first_name").' '.mysql_result($employee_result, 0, "emp_last_name");
}
else {
$selector_display='';
}
}


if ($supply_group=='department') {	
if ($selector=='') { 	
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
else {
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND issued_to = '".$selector."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
$selector_display = $selector;	
}

if ($supply_group=='company') {	
if ($selector=='') { 	
$display_query = "SELECT * FROM $table_name WHERE supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
else {
$display_query = "SELECT * FROM $table_name WHERE company = '".$selector."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
$selector_display = $selector;	
}

if ($supply_group=='vehicle') {	
if ($selector=='') { 	
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
else {
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND issued_to = '".$selector."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
$table_name2 = $_SESSION['db_prefix']."_vehicle";
$vehicle_query = "SELECT registration_no, make, model FROM $table_name2 WHERE vehicle_id ='".$selector."' ";
$vehicle_result = mySQL_query($vehicle_query);
$vehicle_num = mysql_num_rows($vehicle_result);
if ($vehicle_num>0) {	
$selector_display = mysql_result($vehicle_result, 0, "registration_no").' '.mysql_result($vehicle_result, 0, "make").' '.mysql_result($vehicle_result, 0, "model");
}
else {
$selector_display='';
}
}

if ($supply_group=='machinery') {	
if ($selector=='') { 	
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
else {
$display_query = "SELECT * FROM $table_name WHERE supply_group = '".$supply_group."' AND issued_to = '".$selector."' AND supply_date >= '".$start."' AND supply_date <= '".$end."'  ORDER BY internal_supply_id DESC";
}
$table_name2 = $_SESSION['db_prefix']."_machinery";
$machine_query = "SELECT machine_id, machine_name, make, model FROM $table_name2 WHERE machine_id ='".$selector."' ";
$machine_result = mySQL_query($machine_query);
$machine_num = mysql_num_rows($machine_result);
if ($machine_num>0) {	
$selector_display = mysql_result($machine_result, 0, "machine_name").' '.mysql_result($machine_result, 0, "make").' '.mysql_result($machine_result, 0, "model");
}
else {
$selector_display='';
}
}
$display_result = execsql($display_query);
$display_num = mysql_num_rows($display_result); 

$table_name = $_SESSION['db_prefix']."_employee";
$employeeQuery = "SELECT emp_id, emp_first_name, emp_last_name FROM $table_name WHERE end_employ_date > '".$today."' ORDER BY emp_first_name ASC";
	$employeeResult = execsql($employeeQuery);
	$employeeNum = mysql_num_rows($employeeResult);
	$w = 0;
	$arrId2 = array ();
	$arrName2 = array ();
	while ($w < $employeeNum) {
		$arrId2[$w] = mysql_result($employeeResult, $w, "emp_id");
		$arrName2[$w] = mysql_result($employeeResult, $w, "emp_first_name").' '.mysql_result($employeeResult, $w, "emp_last_name");
		$w++;
	}
	
$table_name = $_SESSION['db_prefix']."_list_department";
$departmentQuery = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$departmentResult = execsql($departmentQuery);
	$departmentNum = mysql_num_rows($departmentResult);
	$w = 0;
	$arrId6 = array ();
	$arrName6 = array ();
	while ($w < $departmentNum) {
		$arrId6[$w] = mysql_result($departmentResult, $w, "item_name");
		$arrName6[$w] = mysql_result($departmentResult, $w, "item_name");
		$w++;
	}	
	

$table_name = $_SESSION['db_prefix']."_list_company";
$companyQuery = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$companyResult = execsql($companyQuery);
	$companyNum = mysql_num_rows($companyResult);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $companyNum) {
		$arrId4[$w] = mysql_result($companyResult, $w, "item_name");
		$arrName4[$w] = mysql_result($companyResult, $w, "item_name");
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_vehicle";
$vehicleQuery = "SELECT vehicle_id, registration_no, make, model FROM $table_name ORDER BY registration_no ASC";
	$vehicleResult = execsql($vehicleQuery);
	$vehicleNum = mysql_num_rows($vehicleResult);
	$w = 0;
	$arrId3 = array ();
	$arrName3 = array ();
	while ($w < $vehicleNum) {
		$arrId3[$w] = mysql_result($vehicleResult, $w, "vehicle_id");
		$arrName3[$w] = mysql_result($vehicleResult, $w, "registration_no").' '.mysql_result($vehicleResult, $w, "make").' '.mysql_result($vehicleResult, $w, "model");
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_machinery";
$machineQuery = "SELECT machine_id, machine_name FROM $table_name ORDER BY make, model ASC";
	$machineResult = execsql($machineQuery);
	$machineNum = mysql_num_rows($machineResult);
	$w = 0;
	$arrId5 = array ();
	$arrName5 = array ();
	while ($w < $machineNum) {
		$arrId5[$w] = mysql_result($machineResult, $w, "machine_id");
		$arrName5[$w] = mysql_result($machineResult, $w, "machine_name");
		$w++;
	}
?> 

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

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 


</head>


<body>
<?php
include ("../navigation/access.php");
if ($permission_num==1) {
	?>

<div class="main_frame">
<div class="page_head_frame">
  <label class="label_page_head">Internal supplies to <?php echo $supply_group ?></label>&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $selector_display ?></label></div>

<div class="page_middle_frame">

<?php
$page='all_internal_supply_list.php';
$reference_name='supply_group';
$reference=$supply_group;
$reference_name_2='selector';
$reference_2=$selector;
include('../include/period_select_block.php');
?>
<div class="page_inside_frame">
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 1, "desc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
                              	{"bSortable": true},
								{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}					
								 ]
				} );
			} );
		</script> 
<?php if ($supply_group=='employee') { ?>
<form action="" method="get">
Filter on employee:&nbsp;&nbsp;
<select name="supply_to_select" id="supply_to_select" class="select160" onchange="window.location.href= this.form.supply_to_select.options[this.form.supply_to_select.selectedIndex].value">
  <option value="" selected="selected"></option>
  <option value="all_internal_supply_list.php?selector=&supply_group=employee&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>">All</option>
  <?php 
    	for ($w = 0; $w < count($arrId2); $w++) {
		echo "<option value=all_internal_supply_list.php?selector=".urlencode($arrId2[$w])."&supply_group=employee&date=3&start_date=".$start."&end_date=".$end.">" . $arrName2[$w] . "</option>";	
	    }
     ?>
</select>
</form>
<?php } ?>
<?php if ($supply_group=='department') { ?>
<form action="" method="get">
Filter on department:&nbsp;&nbsp;
<select name="supply_to_select" id="supply_to_select" class="select160" onchange="window.location.href= this.form.supply_to_select.options[this.form.supply_to_select.selectedIndex].value">
  <option value="" selected="selected"></option>
  <option value="all_internal_supply_list.php?selector=&supply_group=department&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>">All</option>
  <?php 
    	for ($w = 0; $w < count($arrId6); $w++) {
		echo "<option value=all_internal_supply_list.php?selector=".urlencode($arrId6[$w])."&supply_group=department&date=3&start_date=".$start."&end_date=".$end.">" . $arrName6[$w] . "</option>";	
	    }
     ?>
</select>
</form>
<?php } ?>          
<?php if ($supply_group=='company') { ?>
<form action="" method="get">
Filter on company:&nbsp;&nbsp;
<select name="supply_to_select" id="supply_to_select" class="select160" onchange="window.location.href= this.form.supply_to_select.options[this.form.supply_to_select.selectedIndex].value">
  <option value="" selected="selected"></option>
  <option value="all_internal_supply_list.php?selector=&supply_group=company&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>">All</option>
  <?php 
    	for ($w = 0; $w < count($arrId4); $w++) {
		echo "<option value=all_internal_supply_list.php?selector=".urlencode($arrId4[$w])."&supply_group=company&date=3&start_date=".$start."&end_date=".$end.">" . $arrName4[$w] . "</option>";	
	    }
     ?>
</select>
</form>
<?php } ?>
<?php if ($supply_group=='vehicle') { ?>
<form action="" method="get">
Filter on vehicle:&nbsp;&nbsp;
<select name="supply_to_select" id="supply_to_select" class="select160" onchange="window.location.href= this.form.supply_to_select.options[this.form.supply_to_select.selectedIndex].value">
  <option value="" selected="selected"></option>
  <option value="all_internal_supply_list.php?selector=&supply_group=vehicle&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>">All</option>
  <?php 
    	for ($w = 0; $w < count($arrId3); $w++) {
		echo "<option value=all_internal_supply_list.php?selector=".urlencode($arrId3[$w])."&supply_group=vehicle&date=3&start_date=".$start."&end_date=".$end.">" . $arrName3[$w] . "</option>";	
	    }
     ?>
</select>
</form>
<?php } ?>  
<?php if ($supply_group=='machinery') { ?>
<form action="" method="get">
Filter on machinery:&nbsp;&nbsp;
<select name="supply_to_select" id="supply_to_select" class="select160" onchange="window.location.href= this.form.supply_to_select.options[this.form.supply_to_select.selectedIndex].value">
  <option value="" selected="selected"></option>
  <option value="all_internal_supply_list.php?selector=&supply_group=machinery&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>">All</option>
  <?php 
    	for ($w = 0; $w < count($arrId5); $w++) {
		echo "<option value=all_internal_supply_list.php?selector=".urlencode($arrId5[$w])."&supply_group=machinery&date=3&start_date=".$start."&end_date=".$end.">" . $arrName5[$w] . "</option>";	
	    }
     ?>
</select>
</form>
<?php } ?>           	
<table width="661" id="scroll_table" class="data_display" >
	<thead>
		<tr>
		  <th width="41" align="center">Date</th>
		  <th width="26" align="center">&nbsp;</th>
		  <th width="54" align="center">Supply no</th>
		  <th width="85" align="center">Store item</th>
          <th width="41" align="center" >Qty</th>
          <th width="51" align="center" >Unit Price</th>
          <th width="56" align="center" >Supply price</th>
		  <th width="61" align="center" >Supply group</th>
		  <th width="59" align="center" >Supply to</th>
		  <th width="69" align="center" >Received by</th>
		  <th width="70" align="center" >Company </th>
		  </tr>
	</thead>
	<tbody>
  <?php
    $supply_link = '';
    $b7_tot = 0;
    $i=0;
	while($i < $display_num){
		 //$b0 = mysql_result($display_result, $i, "dep_supply_no");
		 $b1 = mysql_result($display_result, $i, "supply_date");
		 $b2 = mysql_result($display_result, $i, "supply_no");
		 $b3 = mysql_result($display_result, $i, "store_item_id");
		 $b4 = mysql_result($display_result, $i, "store_item_name");
		 $b5 = mysql_result($display_result, $i, "quantity");
		 $b6 = mysql_result($display_result, $i, "price");
		 $b7 = $b5 * $b6;
		 $b7_tot = $b7_tot + $b7;
		 $b8 = mysql_result($display_result, $i, "supply_group");
		 $b9 = mysql_result($display_result, $i, "issued_to");
		    if ($b8=='employee') {
		 		$table_name2 = $_SESSION['db_prefix']."_employee";
				$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$b9."' ";
				$employee_result = mySQL_query($employee_query);
				$employee_num = mysql_num_rows($employee_result);	
				$b9 = mysql_result($employee_result, 0, "emp_first_name").' '.mysql_result($employee_result, 0, "emp_last_name");
			}
			if ($b8=='vehicle') {
		 		$table_name2 = $_SESSION['db_prefix']."_vehicle";
				$vehicle_query = "SELECT registration_no, make, model FROM $table_name2 WHERE vehicle_id ='".$b9."' ";
				$vehicle_result = mySQL_query($vehicle_query);
				$vehicle_num = mysql_num_rows($vehicle_result);	
				$b9 = mysql_result($vehicle_result, 0, "registration_no");
			}
			if ($b8=='machinery') {
		 		$table_name2 = $_SESSION['db_prefix']."_machinery";
				$machine_query = "SELECT machine_name, make, model FROM $table_name2 WHERE machine_id ='".$b9."' ";
				$machine_result = mySQL_query($machine_query);
				$machine_num = mysql_num_rows($machine_result);	
				$b9 = mysql_result($machine_result, 0, "machine_name");
			}
		 $b10 = mysql_result($display_result, $i, "company");
		 $b11 = mysql_result($display_result, $i, "received_by");
		 $b12 = mysql_result($display_result, $i, "clock_reading");
		 

		 if ($b8=='employee') {$supply_link = 'employee_supply.php?emp_supply_no='.$b2; }
		 if ($b8=='department') {$supply_link = 'department_supply.php?dep_supply_no='.$b2; }
		 if ($b8=='company') {$supply_link = 'company_supply.php?dep_supply_no='.$b2; }
		 if ($b8=='machinery') {$supply_link = 'machinery_supply.php?mach_supply_no='.$b2; }
		 if ($b8=='vehicle') {$supply_link = 'vehicle_supply.php?veh_supply_no='.$b2; }
		 
		?>
		<tr >
		  <td ><?php echo date("d-m-Y", strtotime($b1 ))?></td>
		  <td ><?php echo $b1 ?>&nbsp;</td>
		  <td align="left"><a href="<?php echo $supply_link ?>" onMouseOver="ddrivetip('Click for supply note', 150)"; onMouseOut="hideddrivetip()"><?php echo $b2 ?></a></a></td>
		  <td align="left"><?php echo $b4 ?></td>
		    <td align="left"><?php echo $b5?></td>
			<td align="right"><?php echo number_format($b6, 2, '.', ',')?></td>
			<td align="right"><?php echo number_format($b7, 2, '.', ',')?></td>
			<td align="left"><?php echo $b8 ?></td>
			<td align="left"><?php echo $b9 ?><?php if ($supply_group=='vehicle') { echo '<br>at<br> '.$b12.' km'; } ?><?php if ($supply_group=='machinery') { echo '<br>at<br> '.$b12; } ?></td>
			<td align="left"><?php echo $b11 ?></td>
			<td align="left"><?php echo $b10 ?></td>
	      </tr>
		     
		<?php
		$i++; 
	}
	?>
    </tbody>
    <tfoot>
    <tr >
		  <td colspan="6" align="right" >Totals:&nbsp;&nbsp;</td>
		  <td align="right"><?php echo number_format($b7_tot, 2, '.', ',')?></td>
		  <td colspan="4" align="right">&nbsp;</td>
		  </tr>
        </tfoot>
	
</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'); ?>