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

<head>

<?php
include('../include/session.php');
$loans_id = $_GET['loans_id'];

$table_name = $_SESSION['db_prefix']."_loans";
$loans_query = "SELECT * FROM $table_name WHERE loans_id = '".$loans_id."' ";
$loans_result = mySQL_query($loans_query);
$loans_num = mysql_num_rows($loans_result);
$emp_id = mysql_result($loans_result, 0, "emp_id");
$issue_date = mysql_result($loans_result, 0, "issue_date");
$issued_by = mysql_result($loans_result, 0, "issued_by");
$reason = mysql_result($loans_result, 0, "reason");
$notes = mysql_result($loans_result, 0, "notes");
$amount = mysql_result($loans_result, 0, "amount");
	
$table_name2 = $_SESSION['db_prefix']."_employee";
$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$emp_id."' ";
$employee_result = mySQL_query($employee_query);
$employee_num = mysql_num_rows($employee_result);	
$a0 = mysql_result($employee_result, 0, "emp_first_name");
$a1 = mysql_result($employee_result, 0, "emp_last_name");

$table_name = $_SESSION['db_prefix']."_loans_lines";
$line_query = "SELECT * FROM $table_name WHERE loans_id = '".$loans_id."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);

?>

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

if ($permission_num==1) {
	include ("../navigation/hr_sidebar.php");
?>
<div class="main_frame">
  <div class="page_head_frame">
    <label class="label_page_head">Loan statement</label><label class="label_page_head_2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $a0.' '.$a1 ?></label>
      </div>
        <br />

<br />
<div class="page_middle_frame">
<div class="page_inside_frame">
<table width="639"  id="table_display">
  <tr>
    <td width="83">Loan no:</td>
    <td width="218"><?php echo $loans_id ?></td>
    <td width="101">Amount:</td>
    <td width="217"><?php echo $amount ?>&nbsp;</td>
  </tr>
  <tr>
    <td>Issued to:</td>
    <td><a href="employee_details.php?emp_id=<?php echo $emp_id?> " onMouseover="ddrivetip('Click for details', 150)"; onMouseout="hideddrivetip()"> <?php echo $a0.' '.$a1 ?></a></td>
    <td>Issued by:</td>
    <td><?php echo $issued_by ?>&nbsp;</td>
  </tr>
  <tr>
    <td>Issued for:</td>
    <td><?php echo $reason ?>&nbsp;</td>
    <td>Issue date:</td>
    <td><?php echo date("d-m-Y", strtotime($issue_date)) ?>&nbsp;</td>
  </tr>
  <tr>
    <td>Notes:</td>
    <td colspan="3"><?php echo $notes ?>&nbsp;</td>
    </tr>
</table>

<div align="left"></div>
</div>

<div class="page_inside_frame">
<div align="left"></div>
</div>

<div class="page_inside_frame">
<table width="640" id="table_display">
 
 <thead>
  <tr>
    <td>Date</td>
    <td>Reference</td>
    <td>Reference no</td>
    <td>Credit</td>
    <td>Debit</td>
    <td>&nbsp;</td>
  </tr>
  </thead>
  <?php 
  $i=0;
  $a4_tot=0;
  $a5_tot=0;
  while ($i<$line_num) {
	  $a1 = mysql_result($line_result, $i, "reference_date");
	  $a2 = mysql_result($line_result, $i, "reference");
	  $a3 = mysql_result($line_result, $i, "reference_no");
	  $a4 = mysql_result($line_result, $i, "amount_credit");
	  $a5 = mysql_result($line_result, $i, "amount_debit");
	  $a4_tot=$a4_tot+$a4;
	  $a5_tot=$a5_tot+$a5;
	  
	  ?>
  <tr>
    <td><?php echo date("d-m-Y", strtotime($a1)) ?></td>
    <td><?php  echo $a2 ?></td>
    <td><?php  echo $a3 ?></td>
    <td align="right"><?php  if ($a4!=0) { echo number_format($a4, 2, '.', ','); } ?></td>
    <td align="right"><?php  if ($a5!=0) {echo number_format($a5, 2, '.', ','); } ?></td>
    <td>&nbsp;</td>
  </tr>
  <?php
  $i++;
  }
  ?>
<tfoot>  
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td align="right">Totals:</td>
    <td align="right"><?php  echo number_format($a4_tot, 2, '.', ',');  ?></td>
    <td align="right"><?php  echo number_format($a5_tot, 2, '.', ','); ?></td>
    <td>&nbsp;</td>
  </tr>
 </tfoot>
</table>
<?php $balance=$a4_tot-$a5_tot;
if ($balance==0) { ?>
<img src="../images/check_mark_green.png" width="32" height="32">&nbsp;There is no balance.
<?php } else { ?>
<img src="../images/exclamation_mark_green_small.png" width="20" height="32">There is a balance of P <?php echo number_format($balance, 2, '.', ','); ?>.
<?php } ?> 

</div>
  
</div>
</div>

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