<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--style sheets--> 
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<!--jscript-->

<?php
include('../include/session.php');

$emp_id = $_GET['emp_id'];

if ($emp_id == '') {	
$table_name2 = $_SESSION['db_prefix']."_loans";
$loans_query = "SELECT * FROM $table_name2 WHERE (amount-returned_amount) > 0  ";
$loans_result = mySQL_query($loans_query);
$loans_num = mysql_num_rows($loans_result);
}
else {
$table_name2 = $_SESSION['db_prefix']."_loans";
$loans_query = "SELECT * FROM $table_name2 WHERE (amount-returned_amount) > 0 AND emp_id = '".$emp_id."' ";
$loans_result = mySQL_query($loans_query);
$loans_num = mysql_num_rows($loans_result);
}

?>
<script type="text/javascript" src="../jscript/tablesort.js"></script>
<script language="javascript" src="../jscript/calendar.js"></script>
</head>
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<body>
<?php
include ("../navigation/header.php");
$page_name = "HR-loans";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/hr_sidebar.php");
    include ('../include/tooltip.php');
	?>

<div class="main_frame">

  <div class="page_head_frame">
      <table width="809" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="385"><label class="label_page_head">Outstanding loans</label></td>
            <td width="253">&nbsp;</td>
            <td width="171" align="right">&nbsp;</td>
          </tr>
        </table>
      </div>
<div class="page_middle_frame">
  <div class="page_inside_frame">
  <!--jscript-->
  <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": [[ 0, "desc" ]],
					"aoColumns": [
					     		{"iDataSort": 7},
								{"iDataSort": 8},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>
<table width="680" id="scroll_table" class="data_display"  >
  <thead>
  <tr>
    <th>No</th>
    <th>Date</th>
    <th>Employee</th>
    <th>Reason</th>
    <th>Amount</th>
    <th>Balance</th>
    <th>Issued by</th>
    <th>&nbsp;</th>
    <th>&nbsp;</th>
    </tr>
    </thead>
<?php 

$i=0;
$a3_total=0;
$a6_total=0;
while ($i<$loans_num) {

	$a =  mysql_result($loans_result, $i, "loans_id");
	$a0 = mysql_result($loans_result, $i, "issue_date");
		
	$a1 = mysql_result($loans_result, $i, "emp_id");
	   	$table_name = $_SESSION['db_prefix']."_employee";
    	$employee_query = "SELECT emp_first_name, emp_last_name, department  FROM $table_name WHERE emp_id = $a1 ";
		$employee_result = mySQL_query($employee_query);
        $a1a = mysql_result($employee_result, 0, "emp_first_name")." ".mysql_result($employee_result, 0, "emp_last_name");
		
	$a2 = mysql_result($loans_result, $i, "reason");
	$a3 = mysql_result($loans_result, $i, "amount");
	$a6 = $a3 - mysql_result($loans_result, $i, "returned_amount");

	$table_name = $_SESSION['db_prefix']."_loans_lines";
	$balance_query = "SELECT amount_credit, SUM(amount_debit) AS amount_debit FROM $table_name WHERE loans_id = '".$a."' GROUP BY loans_id   ";
	$balance_result = mySQL_query($balance_query);
	$balance_num = mysql_num_rows($balance_result);
	$loaned = mysql_result($balance_result, 0, "amount_credit");
	$repaid = mysql_result($balance_result, 0, "amount_debit");
	$balance = $loaned - $repaid;
	
	$a4 = mysql_result($loans_result, $i, "issued_by");
	$a3_total = $a3_total + $a3;
	$a6_total = $a6_total + $a6;
	$employeeTT = "<label class=tip_text_label>".$a1."</label><br><label class=tip_click_label>Click for details</label>";  
	
	?>

  <tr>
    <td width="35" align="left"><a href="loans_voucher.php?loans_id=<?php echo $a?>" onmouseover="ddrivetip('Click for detals', 150)"; onmouseout="hideddrivetip()"><?php echo $a ?></a></td>
    <td width="56" align="left"><?php echo date("d-m-Y", strtotime($a0 )) ?></td>
    <td width="97" align="left"><a href="employee_details.php?emp_id=<?php echo $a1?>" onmouseover="ddrivetip('<?php echo $employeeTT ?>  ', 150)"; onmouseout="hideddrivetip()"><?php echo $a1a ?></a></td>
    <td width="150" align="left"><?php echo $a2 ?></td>
    <td width="71" align="right"><?php //echo number_format($a3, 2, '.', ',') ?><?php echo number_format($loaned, 2, '.', ',') ?></td>
    <td width="71" align="right"><?php //echo number_format($a6, 2, '.', ',') ?><?php echo number_format($balance, 2, '.', ',') ?></td>
    <td width="90" align="left"><?php echo $a4 ?></td>
    <td width="47" align="center"><?php echo $a ?></td>
    <td width="23" align="left"><?php echo $a0 ?>&nbsp;</td>
    </tr>
  
  <?php
  $i++;
}
?>
<tfoot>
<tr>
    <td colspan="4" align="right">Totals:</td>
    <td align="right"><?php echo number_format($a3_total, 2, '.', ',') ?>&nbsp;</td>
    <td align="right"><?php echo number_format($a6_total, 2, '.', ',') ?></td>
    <td colspan="3" align="left">&nbsp;</td>
    </tr>
  </tfoot>
</table>
</div>
<a href="loans_all_list.php?date="><input name="all_button" type="button" value="All loans" class="imagebutton"></a>
</div>
</div>

<?php } else {
$_SESSION['denied_return'] = "hr_module/hr_main.php?date=";	
include ("../navigation/hr_sidebar.php");?>
<body onload="accessdenied(); return false">
</body>
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>


</body>
</html>