<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Over Time Report</h1>

<?php error_reporting(0);
include('./includes/fun-php.php');
include('Class-EmpShift.php');
include('./includes/fun-HRClass.php');
unset($_SESSION['query']);
unset($_SESSION['query1']);

 ?>	 
<form action="" method="post">
  <table border="0" cellpadding="3" cellspacing="0"  width="100%" style="border-collapse:collapse;">
	<tr class="row1">
		<td width="13%" align="right">Employee</td>
		<td width="35%"><?php  $query="Select id,emp_code ,  emp_name from empmaster where 1=1 order by emp_name ASC";	
		$sql_emp=DB_query($query,$db);
	 	if(!$sql_emp){die('Error.'.DB_error());	}
	 	?>
		<select name="empcode" >
		<option value="0">--All--</option>
		<?php 	
		while($rowEmp=DB_fetch_array($sql_emp))
		{
		 $p='';
		 if($_REQUEST['empcode']==$rowEmp['id'])
		 $p='selected';
		?>
		<option value="<?php echo $rowEmp['id'] ?>" <?php echo $p; ?>><?php echo $rowEmp['emp_name'].'('. $rowEmp['emp_code'].')' ?></option>
		 <?php 
		}
		?></select></td>
		<td width="13%" align="right">Branch</td>
		<td><select name="site" id="site" >
		<option value="000">---All---</option>
		<?php 

		if($_REQUEST['site']!='000'){
		$site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);}
		else
		{$site_sql=DB_query("Select loccode, locationname from locations",$db);}
		while($site_row=DB_fetch_array($site_sql)){

		?>
		<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'');?>><?php echo $site_row['locationname'];?></option>
		<?php } ?>
		</select></td>
	</tr>
	<tr class="row1">
		<td align="right">Month</td>
	 	<td><select name="paymonth" >
		 <?php
		 $arr=array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
		 foreach ($arr as $key => $value) {
		 	$p='';
		 	if( $key==$_REQUEST['paymonth'])
			$p='selected';
		   ?><option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
		 <?php } ?></select></td>   
		<td align="right">Year</td>
	   	<td><select name="payyear" >
		 <?php
		 
		 for($i=date('Y');$i>2000;$i--)
		 {
		 	$p='';
		 	if( $i==$_REQUEST['payyear'])
			$p='selected';
		 ?><option value="<?php echo $i;?>" <?php echo $p;?>><?php echo $i;?></option>
		 <?php 
		 }
		 ?></select></td> 
	</tr>
	<tr class="row1">      
		<td colspan="4" align="center"><input type="submit" name="Search" value="Search" class="search" /></td>
	</tr>
</table>	
</form>	

<?php 
  if($_REQUEST['Search'] )
  {
	   $paystatus=true;  
	   $years=$_REQUEST['payyear'];
	   $month=$_REQUEST['paymonth'];
	   $empcode=$_REQUEST['empcode'];
	   $site=$_REQUEST['site'];
	   
	   //$filter='';
	   //if(isset($site) && $site!='000')
	   //$filter[]="emp_site='".$site."'";
	   
if(isset($site) && $site!='000')
{
	if($empcode!=0)
	{
		$query="select * from empmaster where  id='".$empcode."' and emp_site='".$site."' ";
	}else
	{	
		$query="select * from empmaster where  emp_site='".$site."' and !(employee_type  IN(SELECT id FROM shift_master WHERE shift_for='Staff') AND gross_sal>=10000 )";

	}
}
else
{
	if($empcode!=0)
	{
		$query="select * from empmaster where  id='".$empcode."'";
	}else
	{
		$query="select * from empmaster where !(employee_type  IN(SELECT id FROM shift_master WHERE shift_for='Staff') AND gross_sal>=10000 )";

	}
}
 
 $_SESSION['query1']=$query;
  //echo   $query;
	 $ResultEmpData = DB_query($query,$db);
   	 while($RowEmpData = DB_fetch_array($ResultEmpData))
	 {

  $Total_Otamount=	array();
  $array_of_time=array();
      $GetRecord="SELECT A.emp_weekly_off, A.emp_code,A.emp_site ,A.employee_type AS
	 emp_shift ,A.emp_name,A.basic_salary,A.basic_perc,A.gross_sal
   ,B.pres_abs, B.shift_start,B.shiftstart_AMPM ,  B.shift_end,B.shiftend_AMPM,B.time_in ,B.timein_AMPM,B.time_out,
   B.timeout_AMPM ,B.work_hrs ,B.ot_hrs ,B.attendance_date,B.leave_code FROM empmaster 
   A, daily_attendance B WHERE A.id=B.employee AND MONTH(B.attendance_date)='".$month."'
   AND YEAR(B.attendance_date)='".$years."' and A.id='".$RowEmpData['id']."' order by B.attendance_date ";
  $_SESSION['query']=$GetRecord;
//echo $GetRecord;

  /*	
  $GetRecord="SELECT A.emp_code,A.emp_site ,A.employee_type AS
 emp_shift ,A.emp_name,A.basic_salary,A.basic_perc,A.gross_sal
  ,B.pres_abs, B.shift_start,B.shiftstart_AMPM ,  B.shift_end,B.shiftend_AMPM,B.time_in ,B.timein_AMPM,B.time_out,
  B.timeout_AMPM ,B.work_hrs ,B.ot_hrs ,B.attendance_date FROM empmaster 
  A, daily_attendance B WHERE A.id=B.employee AND MONTH(attendance_date)='".$month."'
   AND YEAR(attendance_date)='".$years."' AND A.emp_site='".$site."'  ";
 	*/   
	 
?> <br />

 <table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">  
   <tr>
    <th colspan="2"><b>Emp Name : <?php  echo  $RowEmpData['emp_name']?></b></th>    
    <th><b>EmpCode : <?php  echo $RowEmpData['emp_code'] ?></b></th>
    <th><b>Emp Status : <?php  echo  $s->getField('shift_master','id',$s->GetFieldValue('empmaster',"id='".$RowEmpData['id']."'" ,'employee_type'),'shift_for');?></b></th>
    <th colspan="3">&nbsp;</th>
   </tr>
  <tr>
    <th width="22%">Date</th>
    <th width="8%">Attendance</th>
    <th width="14%">Time In</th>
	<th width="14%">Time Out</th>
    <th width="14%">OT Hours /Days </th>    
	<th width="14%">Wages Rate</th>
   <!-- <th width="12%">Rate HR/DYs </th>-->
    <!--<th width="12%">IN/HR Days </th>-->
    <th width="14%">OT Amount </th>
   </tr>
 
 <?php 
	$total_ot='';$cr=0;
     $ResultData = DB_query($GetRecord,$db);
   	 while($RowData = DB_fetch_array($ResultData))
	 {		
		$netgross_sal=$RowData['gross_sal'];
		$CalOverTime=$OBJ_EMPSHIFT->ConvertTimeINTO_Mins($RowData['ot_hrs']) ;
		$DefineEmptype= $s->getField('shift_master','id',$RowData['emp_shift'],'shift_for');
		$dd=explode( '-',$RowData['attendance_date']);
		$workingdays= cal_days_in_month(CAL_GREGORIAN, $dd[1], $dd[0]);	
			
	 //echo $DefineEmptype;
	 if($DefineEmptype=='Staff')
	 {
	    	//echo $CalOverTime.'---'.$RowData['gross_sal'].'<br>';
			 if($CalOverTime>=390 && $RowData['gross_sal']>10000){
			 ?>
			 <tr  bgcolor="#FFFFFF"><td colspan="8">----------</td></tr>
			 <?php 
			 }else{     //if($CalOverTime>=390)
 	 	if($CalOverTime>=390)
		{
			$cr++;
		}		
		?>
		<tr>
			<td><?php  $flag=0;
			 $week_off_day=GetDay($RowData['emp_weekly_off']);
			 echo ConvertSQLDate($RowData['attendance_date'])." (".DayOfDate($RowData['attendance_date']).")";
			 if($week_off_day==DayOfDate($RowData['attendance_date']))
			 {echo "<b> Weekly Off</b>"; $flag=1;}
			 ?></td>
			<td><div align="center"><?php echo $RowData['pres_abs'];?></div></td>
			<td><?php if($RowData['pres_abs']=='P')echo $RowData['time_in']." ".$RowData['timein_AMPM'];?></td>
			<td><?php if($RowData['pres_abs']=='P')echo $RowData['time_out']." ".$RowData['timeout_AMPM'];?></td>
			<td><?php     
			if($RowData['pres_abs']=='P')
			if($flag==1)
			{if($RowData['work_hrs']!=''){echo $RowData['work_hrs']; $CalOverTime=$OBJ_EMPSHIFT->ConvertTimeINTO_Mins($RowData['work_hrs']) ;} else echo "&nbsp;"; $cr++;}
			elseif(isset($RowData['ot_hrs']))echo $RowData['ot_hrs']; else echo "&nbsp;";?></td>			 
	    	 <td><?php  if($RowData['pres_abs']=='P' && $CalOverTime>=390)echo $RowData['gross_sal']?></td>
			<?php 
			$one_day_sal= round($RowData['gross_sal']/$workingdays,2); 
			?>
			<!--<td></td>-->
			<!--<td><?php  //echo  $one_day_sal;?></td>-->
			<td><?php  
			if($CalOverTime>=390)
			{
			echo  $one_day_sal;
			$Total_Otamount[]=$one_day_sal;}
			?></td>
		  </tr>
		<?php } //echo $cr;
		   ?>
		 <?php  
	 }
	 else //if($RowData['pres_abs']!='WL')
	 {
	 ?>
	 <tr>
			<td><?php  $flag=0;
			 $week_off_day=GetDay($RowData['emp_weekly_off']);
			 echo ConvertSQLDate($RowData['attendance_date'])." (".DayOfDate($RowData['attendance_date']).")";
			 if($week_off_day==DayOfDate($RowData['attendance_date']))
			 {echo "<b> Weekly Off</b>"; $flag=1;}
			 ?></td>
			<td><div align="center"><?php 
			 if($RowData['pres_abs']!='L'){echo $RowData['pres_abs'];} 
			 else
			 {echo $s->GetFieldValue('leavemaster',"leavecode='".$RowData['leave_code']."'",'leavename');}
			 ?>			 
			 </div></td>
			<td><?php if($RowData['pres_abs']=='P')echo $RowData['time_in']." ".$RowData['timein_AMPM'];?></td>
			<td><?php if($RowData['pres_abs']=='P')echo $RowData['time_out']." ".$RowData['timeout_AMPM'];?></td>
			<td><?php 
			//echo $flag;   
			if($RowData['pres_abs']=='P'){
			if($flag==1)
			{if($RowData['work_hrs']!=''){echo $RowData['work_hrs']; $CalOverTime=$OBJ_EMPSHIFT->ConvertTimeINTO_Mins($RowData['work_hrs']) ;} else echo "&nbsp;";}
			else{
			if($RowData['ot_hrs']!='')echo $RowData['ot_hrs']; else echo "&nbsp;";   } }/*$RowData['emp_code']."(".$RowData['emp_shift']."(".$DefineEmptype.*/
			
			$array_of_time[]=$CalOverTime;
			?></td>
			 
	    	 <td><?php 
			 if($RowData['pres_abs']=='P'){ 
			 //if($flag==0){
			 if($RowData['gross_sal']>=5000)
			  echo $g='5000';
			 else			 
			 echo $g=$RowData['gross_sal'];}//}?></td>
			<?php 
			if($RowData['gross_sal']>=5000)
 			{ $one_day_sal=round(5000/$workingdays,2); }
			else
			{ $one_day_sal=round($RowData['gross_sal']/$workingdays); }
						
						$hrsal=round($one_day_sal/8,2);
					  ?>
			<!--<td></td>-->
			<td><?php   
			//echo $CalOverTime."<br>";
			if($RowData['pres_abs']=='P')
			//if($flag==0)
			echo $OBAMT=round($CalOverTime*$hrsal/60,2); 
			$Total_Otamount[]=$OBAMT;?></td>
			<!--<td><?php   //echo round($OBAMT,2);?></td>-->
		  </tr>			
	 <?php 
	 }
 } ?>
 <?php ?> 
  <tr>
			   <td>&nbsp;</td>
			   <td>&nbsp;</td>
			   <td>&nbsp;</td>
			   <td><?php if($DefineEmptype=='Staff')echo "Total OT Days "; else{?>Total OT Hours :<?php }?> </td>
			   <td><span class="style1">
		       <?php 
			   if($DefineEmptype=='Staff')
			   {echo $cr;}else{
			   //print_r($array_of_time);
			   if($array_of_time!=''){
			   $sum_of_minutes=array_sum($array_of_time);
			   $q=($sum_of_minutes/60);
			   $q_brk=explode('.',$q);
			   $r=$sum_of_minutes%60;
			   echo $q_brk[0].":".$r;}
			   else $sum_of_minutes=''; }
			   ?>
			   </span></td>
			   <td>Total OT Amount :</td>			  
			   <td><?php  
			   
			   if($DefineEmptype=='Staff')
			   {//echo $one_day_sal;
			   echo round($one_day_sal*$cr);
			   }
			   else{
			   $min_sal=$hrsal/60;
			   $min1=$q_brk[0]*60;
			   $total_min=$min1+$r;
			   
			   echo round($total_min*$min_sal);}
			
			   //echo round(array_sum($Total_Otamount),2);?> </td>
   </tr>
 </table>
<?php 
} ?>
<?php
 if(DB_num_rows($ResultEmpData)>0){  ?><p align="center"><a href="overtimeReport_export.php" class="search">Export</a></p><?php } ?>
<?php } ?>
</div>
</div>
