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

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

$arr=array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");

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);
	}
	
	
	/**************  InserTing Records in load child tables *****************/
	
	
	
	
	
	
	
	/**********************************************************************/ 
	
	
	
	
	
	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)
{	
	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>
<?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');
	  }
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h3><strong>Loan Management</strong></h3>
<fieldset class="fieldset_normal"><legend> <strong>Employee Loan Detail</strong></legend>
   
      <form action="" method="post">
      	
      <table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="left" valign="middle"><strong>Date</strong></td>
    <td width="20%" align="left" valign="top"><input name="loan_date" type="text" style="width:80%;" class='datepicker' value="<?php  echo $loan_date?>"></td>
    <td width="13%" align="left" valign="middle"><strong>Emp Code</strong></td>
    <td width="20%" align="left" valign="top"><select name="emp_code" style="width:80%;" 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 width="13%" align="left" valign="middle"><strong>Loan Type</strong></td>
    <td align="left" valign="top">
     <?php  $arr_loantype=array('personal','official');
	 ?>
     <select name="loan_type" style="width:80%;">
     <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 class="row1">
    <td align="left" valign="middle"><strong>Loan Amount</strong></td>
    <td align="left" valign="top"><input name="loan_amount" id='loan_amount' style="width:80%;"  type="text" value="<?php  echo $loan_amount;?>"></td>
    <td align="left" valign="middle"><strong>Deduction Mode</strong></td>
    <td align="left" valign="top">
   
   <input name="deduction_mode" type="radio" value="Ins" <?php echo   ($deduction_mode=='Ins'?'checked':'')?> onclick="cal_percentage_deduction1(this.value)" >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'){
   $style='style="display:block"';
    }else {
	 $style='style="display:none"';}
	 
	?>
   	<div id="hid_percentage" <?php echo $style; ?> style="display:none" >
   		
   	<input type="text" name="percentage_val" onblur="cal_percentage_amount(this.value)" value="<?php echo $percentage_val; ?>" />
   </div></td>
    <td align="left" valign="middle"><strong>Installment Amount</strong></td>
    <td align="left" valign="top"><input name="installment_amount" style="width:80%;" id='installment_amount' type="text" value="<?php  echo $installment_amount;?>" onBlur="chkinst_amount();"></td>
  </tr>
  <tr class="row1">
    <td align="left" valign="middle"><strong>No of installment</strong></td>
    <td align="left" valign="top"><input name="no_of_installment"  id='no_of_installment' style="width:80%;" type="text" value="<?php  echo $no_of_installment;?>"></td>
    <td align="left" valign="middle"><strong>Loan Deduction Month</strong></td>
    <td align="left" valign="top"> 
    <select name="loan_deduction_month"  style="width:80%;">
 <?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>
    <td align="left" valign="top">&nbsp;</td>
    <td align="left" valign="top">&nbsp;</td>
  </tr>
  <tr class="row1">
    <td colspan="6" align="center" style="text-align:center;"><input type="submit" name="Save"  id="Save" value="Save" class="search"></td>
    </tr>
</table>


      </form>
</fieldset>
<fieldset class="fieldset_normal">
      <?php } else if($_REQUEST['action']!='Addnew'&& $_REQUEST['action']!=='Edit' ) 
      {?>
<div><a href="<?php echo $target;?>&action=Addnew"><input type="button" class="search" value="Apply Loan"  /></a></div>

      <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>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><a href="<?php echo $target.'&id='.$RowData['id'].'&emp_id='.$RowData['emp_id'].'&action=Edit' ; ?>">Edit</a></td>	
		<td><a href="#"   onclick='popup("LoanPaymentDetails.php?id=<?php  echo $RowData['id'].$rurl?>",600,600)' >details</a></td>		
	 </tr>
	<?php }?>
</table>
</fieldset>
</div>
</div>

	  <?php } ?>