<!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 = $con->query($employee_query);
$employee_num = mysqli_num_rows($employee_result);
if ($employee_num>0) {	
$row = mysqli_fetch_assoc($employee_result);
$selector_display = $row["emp_first_name"].' '.$row["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;	
}

$display_result = $con->query($display_query);
$display_num = mysqli_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 = $con->query($employeeQuery);
	$w = 0;
	$arrId2 = array ();
	$arrName2 = array ();
	while($row = $employeeResult->fetch_assoc()) {
		$arrId2[$w] = $row["emp_id"];
		$arrName2[$w] = $row["emp_first_name"].' '.$row["emp_last_name"];
		$w++;
	}
	
$table_name = $_SESSION['db_prefix']."_list_department";
$departmentQuery = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$departmentResult = $con->query($departmentQuery);
	$w = 0;
	$arrId6 = array ();
	$arrName6 = array ();
	while($row = $departmentResult->fetch_assoc()) {
		$arrId6[$w] = $row["item_name"];
		$arrName6[$w] = $row["item_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 } ?>          

          	
<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($row = $display_result->fetch_assoc()) {
         $b1 = $row["supply_date"];
		 $b2 = $row["supply_no"];
		 $b3 = $row["store_item_id"];
		 $b4 = $row["store_item_name"];
		 $b5 = $row["quantity"];
		 $b6 = $row["price"];
		 $b7 = $b5 * $b6;
		 $b7_tot = $b7_tot + $b7;
		 $b8 = $row["supply_group"];
		 $b9 = $row["issued_to"];
		 $b10 = $row["company"];
		 $b11 = $row["received_by"];
		 $b12 = $row["clock_reading"];
		 
		 
		    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 = $con->query($employee_query);
				$row = mysqli_fetch_assoc($employee_result);
				$b9 = $row["emp_first_name"].' '.$row["emp_last_name"];
			}
			
		 
		 

		 if ($b8=='employee') {$supply_link = 'employee_supply.php?emp_supply_no='.$b2; }
		 if ($b8=='department') {$supply_link = 'department_supply.php?dep_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'); ?>