<h2>Loan Management</h2>
<?php  include('includes/fun-php.php');
include('includes/Class_Branch.php');
?>
<?php 
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];

//$rurl="&modid=".$modid."&frmid=".$frmid."";
//$target="index.php?route=loan_management".$rurl;
if($_REQUEST['Search']   )
		{
 		$_SESSION['empcode']=$_REQUEST['employee_code'];
	 		$employee_code=$_SESSION['empcode'];
		$_SESSION['emptype']=$_REQUEST['emptype'];
		$_SESSION['emp_salprocess_site']=$_REQUEST['site'];
     }	 
	 if($_REQUEST['emp_id'])
	 {     
	 
		 $emp_id =$s->getField('empmaster','id',$_REQUEST['emp_id'],'id');
	  	 $employment_type =$s->getField('empmaster','id',$emp_id,'employment_type');
		 $emp_site =$s->GetFieldValue('empmaster',"id='".$emp_id."'",'emp_site');
		$_SESSION['empcode']= $emp_id;
		$_SESSION['emptype']=$employment_type;
		$_SESSION['emp_salprocess_site']=$emp_site;

	 }
if($_REQUEST['Save'])
{
 	$data['loan_date'] = FormatDateForSQL($_REQUEST['loan_date']);
	$data['emp_id'] = $_REQUEST['emp_id'];
	$data['emp_code'] = $_REQUEST['emp_code'];
	$data['emp_name'] = $_REQUEST['emp_name'];
	$data['loan_type'] = $_REQUEST['loan_type'];
	$data['loan_amount'] = $_REQUEST['loan_amount'];
	$data['loan_balance'] = $_REQUEST['loan_balance'];
	
	$data['deduction_mode'] = $_REQUEST['deduction_mode'];
	$data['percentage_val'] = $_REQUEST['percentage_val'];
	$data['no_of_installment'] = $_REQUEST['no_of_installment'];
	
	$data['loan_deduction_month'] = $_REQUEST['loan_deduction_month'];
	$data['installment_amount'] = $_REQUEST['installment_amount'];
	  
 	if($_REQUEST['action']!='Edit')
	$insert=$s->RecordInsert('loan_management', $data);
	else {
		$cond="id='".$_REQUEST['id']."'";
		$insert=$s->RecordEdit('loan_management', $data, $cond);
	}
	if($insert)
	{
		  echo "<center>Record Updated Successfully</center>";
		  $s->pageLocation( $target.'&action=view');
	}	
}

?>
<script language="javascript">
function chkinst_amount()
{
	var loan_amount = parseInt(document.getElementById("loan_amount").value);
	var installment_amount=	 parseInt(document.getElementById("installment_amount").value);		
	 
				var ansD = document.getElementById("no_of_installment"); 
				t=  		loan_amount / installment_amount; 						
     			ansD.value =t.toFixed(1);
			    
	}

function cal_percentage_deduction(str)
{	 
	document.getElementById("hid_percentage").style.display="block";
}	
function cal_percentage_amount(str)
{	 
 
	var loan_amount = parseInt(document.getElementById("loan_amount").value);
	 	 document.getElementById("installment_amount").value=loan_amount * str *.01;
	 document.getElementById("no_of_installment").value=loan_amount / document.getElementById("installment_amount").value;
	 
}	
</script>




<?php
 if(($_REQUEST['action']!='Addnew' && $_REQUEST['action']!='Edit'))
 {
?>
<form action="" method="post">
	<table border="0" cellpadding="5" cellspacing="0"  width="80%">
	<tr>
		
<td width="10%">Site <?php echo $_SESSION['emp_salprocess_site'];?>
 <?php $ClassBranch->SiteRecord('site',$_SESSION['emp_salprocess_site'] ); ?>
 </td>
		<td  width="20%">Employee Type: 
<select name="emptype" id="emptype" onchange="ShowDiv(this.value,'hid-empdiv','GetEmployee.php?site='+document.getElementById('site').value)" style="width:107px;" > 
	<option value="0" >---</option>
		<option value="Permanent" <?php echo($_SESSION['emptype']=='Permanent'?'selected':'')?>>Permanent</option>
		<option value="Temporary" <?php echo($_SESSION['emptype']=='Temporary'?'selected':'')?>>Temporary</option>
	</select></td> 

		
		
		
		<td  width="20%"> Employee Code:
		<div id='hid-empdiv'>
		<?php  	include('UnderUsert.php');?>
 	 	 </div></td>
<!-- <input type="text" name="empcode" value="<?php //echo $_REQUEST['empcode']; ?>">-->  
 <td><br />	<input type="submit" name="Search"   value="Search"/> </td></tr></table>
	
	
</form>
<?php   }



 if(($_REQUEST['action']=='Addnew'|| $_REQUEST['action']=='Edit' )  )
   {
  		//echo $_REQUEST['empcode'];
	  
	   
	  if($_REQUEST['action']=='Edit'){
     $GetLoanRecord = "Select * from loan_management where id='".$_REQUEST['id']."'  ";
		$ResultLoanData = DB_query($GetLoanRecord,$db);	   
   	     $RowLoanData = DB_fetch_array($ResultLoanData);
		 $loan_date = ConvertSQLDate($RowLoanData['loan_date']);
		 $emp_id = $RowLoanData['emp_id'];
		 $emp_name =$s->getField('empmaster','id',$emp_id,'emp_name');
		 $emp_code =$s->getField('empmaster','id',$emp_id,'emp_code');
		 
		 $loan_type = $RowLoanData['loan_type'];
		 $loan_amount = $RowLoanData['loan_amount'];
		  $status = $RowLoanData['status'];
		 $deduction_mode = $RowLoanData['deduction_mode'];
		 $percentage_val=$RowLoanData['percentage_val'];
		 $no_of_installment = $RowLoanData['no_of_installment'];
	     $loan_deduction_month= $RowLoanData['loan_deduction_month'];
		  $installment_amount= $RowLoanData['installment_amount'];
		  
		  
		 
		 
      }
	  else{
	  	     $emp_id =$s->getField('empmaster','id',$_SESSION['empcode'],'id');
	  	 $emp_name =$s->getField('empmaster','id',$emp_id,'emp_name');
		 $emp_code =$s->getField('empmaster','id',$emp_id,'emp_code');
	  }
?>




<fieldset>

	  <legend> Employee Loan Detail</legend>
   
      <form action="" method="post">
      	<input type=hidden name="emp_id" value="<?php echo $emp_id; ?>">
      <table width="100%" border="0"  cellpadding="2" cellspacing="2">
  <tr>
    <td width="25%">Date</td>
    <td width="75%"><input name="loan_date" type="text" class='datepicker' value="<?php  echo $loan_date?>"></td>
  </tr>
  <tr>
    <td>Emp Code</td>
    <td><input name="emp_code" type="text" value="<?php   echo $emp_code?>"></td>
  </tr>
  <tr>
    <td>Emp Name</td>
    <td><input name="emp_name" type="text" value="<?php  echo $emp_name?>"></td>
  </tr>
  <tr>
    <td>Loan Type</td>
     <td>
     <?php  $arr_loantype=array('personal','official');
	 ?>
     <select name="loan_type">
     <option value="000">--Select Loan Type--</option>
     <?php 
	    for($i=0;$i<count($arr_loantype);$i++)
		{
			 $p='';
			  if($loan_type==$arr_loantype[$i])
			  $p='selected';
	 ?>
      <option value="<?php echo $arr_loantype[$i]?>" <?php echo $p; ?>><?php echo $arr_loantype[$i]?></option>
    <?php } ?>
     </select></td>
  </tr>
  <tr>
    <td>Loan Amount</td>
      <td><input name="loan_amount" id='loan_amount'  type="text" value="<?php  echo $loan_amount;?>"></td>
  </tr>
  <tr>
    <td>Deduction Mode</td>
   <td>
   
   <input name="deduction_mode" type="radio" value="Ins" <?php echo   ($deduction_mode=='Ins'?'checked':'')?> >Instalment
   <input name="deduction_mode" type="radio" value="Per" <?php echo   ($deduction_mode=='Per'?'checked':'')?> onclick="cal_percentage_deduction(this.value)" >Percentage
   <?php  //echo $percentage_val; 
   if ($deduction_mode=='Per'){?>
   <div id="hid_percentage" >
   	<?php }else {?>
   	<div id="hid_percentage" style="display: none">
   		<?php } ?>
   	<input type="text" name="percentage_val" onblur="cal_percentage_amount(this.value)" value="<?php echo $percentage_val; ?>" />
   	
   	
   </div>
   </td>
  </tr>
  
  <tr>
    <td>Installment Amount</td>
     <td><input name="installment_amount"  id='installment_amount' type="text" value="<?php  echo $installment_amount;?>" onBlur="chkinst_amount();"></td>
   
  </tr>
  
  <tr>
    <td>No of installment</td>
   <td><input name="no_of_installment"  id='no_of_installment' type="text" value="<?php  echo $no_of_installment;?>"></td>
  </tr>
  
  <tr>
    <td>Loan Deduction Month</td>
    <td> 
    <select name="loan_deduction_month"  style="width:120px;">
 <?php
 $arr=array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
 foreach ($arr as $key => $value) {
 	$p='';
 	if( $key==$loan_deduction_month)
	$p='selected';
   ?><option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
 <?php } ?></select></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
	<?php if( $status =='N'){?>
	<input type="submit" name="Save"  id="Save" value="Save">
	<?php } ?>
	</td>
  </tr>
      </table>

      </form>

</fieldset>
      <?php } else //if($_REQUEST['action']!='Addnew'&& $_REQUEST['action']!=='Edit' ) 
      {
	 	//	  unset( $_SESSION['empcode']);
		 //  unset( $_SESSION['emptype']);
	 	 //  unset( $_SESSION['emp_salprocess_site']);

	  ?>
 
      <table cellpadding="3" cellspacing="0" border="1" width="100%">
	<tr><td><strong>EmpCode</strong></td>
		<td><strong>EmpName</strong></td>
		<td><strong>Month</strong></td>
		<td><strong>Installment</strong></td>
        <td><strong>Installment Amount</strong></td>
		<td><strong>Amount</strong></td>
		<td><strong>Status</strong></td>
			<td><strong>Comments</strong></td>
		<!-- <td><strong>View</strong></td> -->	<td><strong>Details</strong></td>		
	</tr>
	<?php
 	    $GetProcess = "Select * from loan_management  ";		 	   
       $ResultProcess_SalData = DB_query($GetProcess,$db);
	  // echo DB_num_rows($ResultData);
   	   while($RowData = DB_fetch_array($ResultProcess_SalData))
	   {
	  ?> 
		<tr>
		<td><?php echo $s->getField('empmaster','id',$RowData['emp_id'],'emp_code'); ?>&nbsp;</td>
		<td><?php  //echo $RowData['emp_id'];
		echo $s->getField('empmaster','id',$RowData['emp_id'],'emp_name');
			?>&nbsp;</td>
		<td><?php  echo GetMonth($RowData['loan_deduction_month'])?>&nbsp;</td>
		<td><?php  echo $RowData['no_of_installment']?>&nbsp;</td>
        <td><?php  echo $RowData['installment_amount']?>&nbsp;</td>
        
		<td><?php  echo $RowData['loan_amount']?>&nbsp;</td>
		<td><?php  echo $RowData['status']?>&nbsp;</td>
		<td><?php  echo $RowData['loancomment']?>&nbsp;</td>
		<!-- <td>
		<?php if($RowData['status']=='N'){?>
		<a href="<?php echo $target.'&id='.$RowData['id'].'&emp_id='.$RowData['emp_id'].'&action=Edit' ; ?>">Edit</a>
		<?php } else{?>
		<a href="<?php echo $target.'&id='.$RowData['id'].'&emp_id='.$RowData['emp_id'].'&action=Edit' ; ?>">View</a>
		<?php } ?>
		</td>	-->
		<td>
		<?php 
		if($_SESSION['empcode']==$RowData['emp_id'] && $RowData['status']=='N' )
		{
		 echo "Under Process";
		}
		else if($RowData['status']=='N' ){?>
		<a href="#"   onclick='popup("LoanPaymentDetails.php?id=<?php  echo $RowData['id'].$rurl?>",600,600)' >
		<input name="Approved" type="button" value="Approved" /></a>
		<?php }else{ ?>
				<a href="#"   onclick='popup("LoanPaymentDetails.php?id=<?php  echo $RowData['id']."&ins=1".$rurl?>",600,600)' >
				View Installments</a>

		<?php } ?>
		
		
		</td>		
	 </tr>
	<?php }?>
</table>

	  <?php } ?>