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

$rurl="&modid=".$modid."&frmid=".$frmid."";
$target="index.php?route=HRModule/loan_management".$rurl;

if($_REQUEST['Save'])
{
 	$data['loan_date'] = FormatDateForSQL($_REQUEST['loan_date']);
	$data['emp_id'] = $_REQUEST['emp_code'];
	$data['loan_type'] = $_REQUEST['loan_type'];
	$data['loan_amount'] = $_REQUEST['loan_amount'];
	$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");   		 						
     			ansD.value =parseFloat((loan_amount / installment_amount),2);			    
	}

function cal_percentage_deduction(str)
{	
	 if(str=='Ins')
	 {
	 	document.getElementById("percentage_val").style.display='none';
	 }
	 else
	 {
	   document.getElementById("percentage_val").style.display='block';	
	 }

	document.getElementById("hid_percentage").style.display="block";

}	
function cal_percentage_deduction1(str)
{	
	document.getElementById("hid_percentage").style.display="none";
}	
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>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?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'];
		 $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','emp_code',$_REQUEST['empcode'],'id');
	  	// $emp_name =$s->getField('empmaster','id',$emp_id,'emp_name');
		// $emp_code =$s->getField('empmaster','id',$emp_id,'emp_code');
	  }
?>

<h1><strong>Employee Loan Details</strong></h1>
      <form action="" method="post">      	
      <table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
    <td align="right">Date</td>
    <td><input name="loan_date" type="text" class='datepicker' value="<?php  echo $loan_date?>"  /></td>
    <td align="right">Emp Code</td>
    <td><select name="emp_code" id="emp_code" >
	     <option value="0">Select</option>
		  <?php
		  $sql_emp1=DB_query("SELECT * FROM empmaster where CompanyId='".$_SESSION['ACGCompanyId']."'  order by emp_code ASC ",$db);
		  while($sql_emp_row1=DB_fetch_array($sql_emp1))
		  {	
		  $ss1='';
		  if($emp_id==$sql_emp_row1['id'])
		  $ss1="selected";	  
		   ?>
		   <option value="<?php echo $sql_emp_row1['id']; ?>" <?php echo $ss1; ?>>
		   <?php echo $sql_emp_row1['emp_name']."&nbsp;&nbsp;"."[".$sql_emp_row1['emp_code']."]" ?></option>
	<?php } ?>
	</select>
	</td>
	<td colspan="2"></td>
  </tr> 
  <tr class="row1">
    <td align="right">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>
    <td align="right">Loan Amount</td>
    <td><input name="loan_amount" id='loan_amount'  type="text" value="<?php  echo $loan_amount;?>"  /></td>
    <td colspan="2"></td>
  </tr>
  <tr class="row1">
    <td align="right">Deduction Mode</td>
    <td colspan="3"><div style="width:17%; float:left;"><input name="deduction_mode" type="radio" value="Ins" <?php echo ($deduction_mode=='Ins'?'checked':'')?> onclick="cal_percentage_deduction(this.value)" style="verticle-align:middle; margin-top:2px;" /> Installment</div>
    <div style="width:17%; float:left;"><input name="deduction_mode" type="radio" value="Per" <?php echo ($deduction_mode=='Per'?'checked':'')?> onclick="cal_percentage_deduction(this.value)" style="verticle-align:middle; margin-top:2px;" /> Percentage</div>
   <?php  //echo $percentage_val; 
   if ($deduction_mode=='Per'){
   $style="display: block";
    }else {
	 $style="display: none";}
	?>
   	<div id="hid_percentage" <?php echo $style; ?>>   		
   	<input type="text" name="percentage_val" id="percentage_val" onblur="cal_percentage_amount(this.value)" value="<?php echo $percentage_val; ?>" style="width:28%; float:left;" />   	
   </div>
   </td>
   <td colspan="2"></td>
  </tr>  
  <tr class="row1">
    <td width="13%" align="right">Installment Amount</td>
    <td width="20%"><input name="installment_amount"  id='installment_amount' type="text" value="<?php  echo $installment_amount;?>" onBlur="chkinst_amount();"  /></td> 
    <td width="13%" align="right">No. of installment</td>
   	<td width="20%"><input name="no_of_installment"  id='no_of_installment' type="text" value="<?php  echo $no_of_installment;?>"  /></td>
    <td width="13%" align="right">Loan Deduction Month</td>
    <td><select name="loan_deduction_month"  >
 <?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 class="row1">
    <td colspan="6" style="text-align:center;"><input type="submit" name="Save"  id="Save" value="Save" class="search"></td>
  </tr>
      </table>

      </form>

      <?php } else if($_REQUEST['action']!='Addnew'&& $_REQUEST['action']!=='Edit' ) 
      {?>
      <h1><?php echo $s->getField('modules_form','id',$_REQUEST['frmid'],'frm_name'); ?></h1>
<p align="left"><a href="<?php echo $target;?>&action=Addnew"><input type="button" value="Apply Loan" class="search"></a></p>

      <table cellpadding="3" cellspacing="0" border="0" width="100%" class="thborder">
	<tr>
	    <th width="12%">Emp. Code</th>
		<th>Emp. Name</th>
		<th width="9%">Month</th>
		<th width="7%" class="text-right">Installment</th>
        <th width="10%" class="text-right">Installment Amount</th>
		<th width="10%" class="text-right">Amount</th>
		<th width="7%" class="text-center">Status</th>
		<th width="6%" class="text-center">View</th>
		<th width="6%" class="text-center">Details</th>		
	</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))
	   {
	   		$checkStatus=$s->GetFieldValue("emp_loan_details","loanid='".$RowData['id']."' and emp_id='".$RowData['emp_id']."'","id");
	   		//DB_query("Select * from emp_loan_details where loanid='".$id."' and emp_id='".$RowData['emp_id']."'",$db);
		
	  ?> 
		<tr>
		<td><?php  echo $s->getField('empmaster','id',$RowData['emp_id'],'emp_code')?></td>
		<td><?php  //echo $RowData['emp_id'];
		echo $s->getField('empmaster','id',$RowData['emp_id'],'emp_name');	?></td>
		<td><?php  echo GetMonth($RowData['loan_deduction_month'])?></td>
		<td class="text-right"><?php  echo $RowData['no_of_installment']?></td>
        <td class="text-right"><?php  echo $RowData['installment_amount']?></td>        
		<td class="text-right"><?php  echo $RowData['loan_amount']?></td>
		<td class="text-center"><?php   if($checkStatus!='') { echo "Approved"; } else { echo "Pending"; } ?></td>		
		<td style="text-align:center;"><?php   if($checkStatus!='') { ?><img src="images/icon-edit.png" height="12" width="12" title="No permission to Edit" style="opacity:0.6;" /><?php } else {?>
		<a href="<?php echo $target.'&id='.$RowData['id'].'&emp_id='.$RowData['emp_id'].'&action=Edit' ; ?>"><img src="images/icon-edit.png" height="13" width="13" title="Edit Document" /></a><?php } ?></td>	
		<td style="text-align:center;"><a href="#" onclick='popup("LoanPaymentDetails.php?id=<?php  echo $RowData['id'].$rurl?>",600,400)' class="search">Details</a></td>		
	 </tr>
	<?php }?>
</table>

	  <?php } ?>
	</div>
</div>