<?php
ob_start();
$PageSecurity = 11;
include('includes/session.inc');
include('includes/function_lib.php');
$sqldateofjoi=DB_query("select doj from empmaster where id='".$_REQUEST['EMPID']."'",$db);
$DoJA=DB_fetch_array($sqldateofjoi);
function leave_allowed($empid,$type,$year,$eff_date,$leaveday)
{
	global $db;
        $tot=0;
        $eff_date=date('Y',strtotime($eff_date));
        $pre_year=$year-1;
       // $calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and approve_status='Y' and year(approved_date_from)='".$pre_year."' group by leave_type";	   
       $calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and approve_status='Y' and approved_date_from>='".$_SESSION['financialYrStart']."' and approved_date_to<='".$_SESSION['financialYrEnd']."'  group by leave_type";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
        /*for($p=$eff_date;$p<=$year;$p++){  echo "hi";
        $leavedays=$leavedays+$leaveday; 
        }
        if($eff_date<$year){
        $tot= $leavedays-$RowData['total'];
        }
        if($eff_date==$year){
         $tot= $leaveday;  
        }*/
       // return($tot);
        return $RowData['total'];
}
function leave_calculation($empid,$type,$year)
{
	global $db;
	 //$calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and year(approved_date_from)='".$year."' and approve_status='Y' group by leave_type";	   
	$calLeaveRecord = "Select sum(total_approval_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' and approved_date_from>='".$_SESSION['financialYrStart']."' and approved_date_to<='".$_SESSION['financialYrEnd']."' and approve_status='Y' group by leave_type";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
	return($RowData['total']);
}
function leave_calculationForApplied($empid,$type)
{
	global $db;
	$calLeaveRecord = "Select sum(total_days) as total from emp_leave_apply where empid='".$empid."' and leave_type='".$type."' group by leave_type  ";	   
	$ResultData = DB_query($calLeaveRecord,$db);
	$RowData = DB_fetch_array($ResultData);
	return($RowData['total']);
}
?> 
<form id="editdatafor" method="post" onsubmit="return validateFormEdit()" >
<input type="hidden" id="dojAE" value="<?php echo ConvertSQLDate($DoJA['doj']);?>"  />
<input type="hidden" id="leave_typeEdit" value="<?php echo $_REQUEST['LT'];?>"  />
<input type="hidden" id="empidEdit" value="<?php echo $_REQUEST['EMPID'];?>"  />
<table width="100%" border="0" class="thborder" align="center" cellspacing="0" cellpadding="3">
<tr>
	<th width="10%">Leave Type</th>
	<th width="10%">From Date</th>
	<th width="10%">To Date</th>
	<th width="8%">Applied Leave Days</th>
	<th width="8%">Approved Days</th>
	<th width="8%">Aval Leave</th>
	<th>Reason</th>
	<th width="10%">Approval Status</th>
	<th width="10%">Update</th>
</tr>
<?php  
$Approvedleave= leave_calculation($_REQUEST['EMPID'],$_REQUEST['LT'],'');

$TakenLeave=leave_calculationForApplied($_REQUEST['EMPID'],$_REQUEST['LT']);

$GetEmpLeave = "Select * from emp_leave where  emp_id='".$_REQUEST['EMPID']."' and leave_type='".$_REQUEST['LT']."'";	   
$ResultEmpLeave = DB_query($GetEmpLeave,$db);
$RowEmpLeave=DB_fetch_array($ResultEmpLeave);
$i=0;
 /* $leave_sql="Select * from emp_leave_apply where leave_type='".$_REQUEST['LT']."' and empid ='".$_REQUEST['EMPID']."'
 and year(period_from)='".$_SESSION['leave_year']."'"; */
  $leave_sql="Select * from emp_leave_apply where leave_type='".$_REQUEST['LT']."' and empid ='".$_REQUEST['EMPID']."'
 and period_from>='".$_SESSION['financialYrStart']."' and period_to<='".$_SESSION['financialYrEnd']."'";		
$leave_res=DB_query($leave_sql,$db);
$leaveType=$s->getField('leavemaster','id',$RowEmpLeave['leave_type'],'leavetype');
$eff_date=$s->getField('empmaster','id',$_REQUEST['EMPID'],'leave_eff_date');
       
        if($leaveType=='c')
        {
         //$allowed_leave=leave_allowed($_REQUEST['EMPID'],$_REQUEST['LT'],$_SESSION['leave_year'],$eff_date,$RowEmpLeave['leave_days']);
        	$allowed_leave=leave_allowed($_REQUEST['EMPID'],$_REQUEST['LT'],'',$eff_date,$RowEmpLeave['leave_days']);
        }
        if($leaveType=='e')
        {
         $allowed_leave=$RowEmpLeave['leave_days'];
        }
        $finalUnEarnLeave=$allowed_leave-$Approvedleave;
        //echo $allowed_leave."##".$Approvedleave;
if(DB_num_rows($leave_res)>0){
   while($leave_row=DB_fetch_array($leave_res)){ 
   if($leave_row['approve_status']=='Y')
   {
   		$disabled='disabled';
   }
   else
   {
   		$disabled='';
   }

?>
<tr>
	<td><input type="hidden" name="leaveappid[<?php echo $leave_row['id']; ?>]" id="leaveappid_<?php echo $i; ?>" value="<?php echo $leave_row['id']; ?>" />
	<?php echo  $s->GetFieldValue('leavemaster',"id='".$leave_row['leave_type']."'",'leavename');?></td>
	<td><div id="fromdateval_<?php echo $i; ?>"><?php echo  ConvertSQLDate($leave_row['period_from']);?></div>
	<input type="hidden" name="fromdate[<?php echo $leave_row['id']; ?>]" id="fromdate_<?php echo $i; ?>" onchange="return LeaveJoinEdit(this.value,this.id);" value="<?php echo  ConvertSQLDate($leave_row['period_from']);?>"  />
	<input type="hidden" id="staticfrmdate_<?php echo $i; ?>" value="<?php echo  ConvertSQLDate($leave_row['period_from']);?>" />
	</td>
	<td><div id="todateval_<?php echo $i; ?>"><?php echo  ConvertSQLDate($leave_row['period_to']);?></div>
	<input type="hidden" name="todate[<?php echo $leave_row['id']; ?>]" id="todate_<?php echo $i; ?>" onchange="return LeaveTypeValueEdit(<?php echo $i; ?>);" value="<?php echo  ConvertSQLDate($leave_row['period_to']);?>"   />
	<input type="hidden" id="statictodate_<?php echo $i; ?>" value="<?php echo  ConvertSQLDate($leave_row['period_to']);?>" />
	</td>
	<td><div id="reqleaveEdit_<?php echo $i; ?>"><?php echo $leave_row['total_days']; ?></div>
	<input type="hidden" name="reqleave[<?php echo $leave_row['id']; ?>]" id="reqleave_<?php echo $i; ?>" value="<?php echo $leave_row['total_days']; ?>"  readonly="true">
	<input type="hidden" name="reqleave1" id="reqleave1_<?php echo $i; ?>" value="<?php echo $leave_row['total_days']; ?>" readonly="true">
</td>
	<td><?php if($leave_row['approve_status']!='Y'){echo '0';}else{echo $leave_row['total_approval_days'];} ?></td>
	<td><div id="empleaveEdit_<?php echo $i; ?>"><?php echo $finalUnEarnLeave; ?></div>
	<input type="hidden" name="NoOfLeaveDays[<?php echo $leave_row['id']; ?>]" id="NoOfLeaveDays_<?php echo $i; ?>" value="<?php echo $finalUnEarnLeave; ?>"  readonly="true"></td>
	<td><div id="empleaveEditReason_<?php echo $i; ?>"><?php echo  $leave_row['leave_reason'];?></div>
	<input type="hidden" name="leave_reason[<?php echo $leave_row['id']; ?>]" id="leave_reason_<?php echo $i; ?>" value="<?php echo $leave_row['leave_reason']; ?>" />
	</td>
	<td><?php if($leave_row['approve_status']=='N'){echo 'Pending';} else if($leave_row['approve_status']=='C') {  echo "Cancelled"; }else{echo 'Approved';}?>
	<input type="hidden" id="approvalStatus_<?php echo $i; ?>" value="<?php echo $leave_row['approve_status']; ?>" />
	</td>
	<td>
	<input type="checkbox" name="checkboxEdit[]" id="checkboxEdit_<?php echo $i; ?>" value="<?php echo $leave_row['id']; ?>" onclick="changestatus(<?php echo $i; ?>)" <?php echo $disabled; ?> /></td>
</tr>
<?php $i++;}}else{ ?>
<tr>
	<td colspan="9" align="center" style="color:#FF0000;">No Record Found</td>
</tr>
<?php }?>
<input type="hidden" id="cout" value="<?php echo $i; ?>" />
</table>
</form>