<?php  

if($_REQUEST['save'])
{	
	for($i=0;$i<count($_REQUEST['EmpId']);$i++)
	{
		$getval2=DB_query("Select * from ot_management where emp_id='".$_REQUEST['EmpId'][$i]."' and month1='".$_REQUEST['month_name'][$i]."' and year1='".$_REQUEST['year'][$i]."'",$db);
	
		$data['emp_id']=$_REQUEST['EmpId'][$i];
		$data['month1']=$_REQUEST['month_name'][$i];
		$data['year1']=$_REQUEST['year'][$i];
		$data['ot_days']=$_REQUEST['ot_days'][$i];
		$data['ot_amr']=$_REQUEST['ot_amt'][$i];
		$data['status']='Pending';
		 
	if(DB_num_rows($getval2)>0){
		 $cond="emp_id='".$_REQUEST['EmpId'][$i]."' and month1='".$_REQUEST['month_name'][$i]."' and year1='".$_REQUEST['year'][$i]."'";
		 $s->RecordEdit('ot_management', $data, $cond);
		}
		else
		{
		 $s->RecordInsert('ot_management', $data);
		}
	}	
	 
	prnMsg( _('OT Amount Added!'),'success');
				echo "<script>location.href='index.php?route=HRModule/over_time_management&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if($_REQUEST['GO'])
{
	if($_REQUEST['site']!='0')
	{
		$filter[]="emp_site='".$_REQUEST['site']."'";
	}
	if($_REQUEST['employment_type']!='')
	{
		$filter[]="staff_type='".$_REQUEST['employment_type']."'";
	}
	if($_REQUEST['emp_name']!='0' )
	{
		$filter[]="id='".$_REQUEST['emp_name']."'";
	}
	$filter[]="otapplicable = 'Y' ";
	
	 
	if($filter!='')
	{
		$filter1=implode(" AND ",$filter);
		
		$SQLDATA=DB_query("SELECT * FROM empmaster WHERE $filter1",$db);
	}
}
?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Over time Feeding</h1>
<script type="text/javascript" language="javascript">
function GetEmpName(str)
{	

}

function cal_otAmt(i)
{
	var monthdays=0;
	var month=document.getElementById("month").value;
	if(month==1){var monthdays=31;}
	if(month==2){var monthdays=28;}
	if(month==3){var monthdays=31;}
	if(month==4){var monthdays=30;}
	if(month==5){var monthdays=31;}
	if(month==6){var monthdays=30;}
	if(month==7){var monthdays=31;}
	if(month==8){var monthdays=31;}
	if(month==9){var monthdays=30;}
	if(month==10){var monthdays=31;}
	if(month==11){var monthdays=30;}
	if(month==12){var monthdays=31;}
	var NetSal=parseFloat(document.getElementById("net_sal_"+i).value);
	var perdaysal=NetSal/monthdays;
	var perdaysal1=perdaysal.toFixed(2);
	var OtDays=document.getElementById("ot_days_"+i).value;
	if(OtDays==''){OtDays=0;}
	var OTamt1=perdaysal1*OtDays;
	document.getElementById("ot_amt_"+i).value=OTamt1.toFixed(2);
}
</script>
<form method="post" action="">
<table width="100%" cellpadding="3" cellspacing="0" border="0">	
<tr class="row1">
	<td width="13%"><strong>Branch</strong></td>
	<td width="20%">
		<select name="site" id="site" style="width:98%;" onchange="loadEmployee(this.value)">
			<option value="0">Select</option>
			 <?php $site_sql=DB_query("Select loccode, locationname from locations where loccode in('".$_SESSION['AssignedBranch']."')",$db);?>
			 <?php 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 endwhile;?>
		</select>
	</td>

	<td width="13%"><strong>Employment Type</strong></td>
	<td width="20%"><select name="employment_type" id="employment_type" style="width:98%;">
		<option value="">Select Employment Type</option>
		<option value="R"<?php echo($_REQUEST['employment_type']=='R'?'selected':'')?>>Regular Employee</option>
		<option value="P"<?php echo($_REQUEST['employment_type']=='P'?'selected':'')?>>Contract Employee</option>
		<option value="C"<?php echo($_REQUEST['employment_type']=='C'?'selected':'')?>>Probation Employee</option>
      </select></td>
	<td width="13%"><strong>Employee Name</strong></td>
	<td id="EMPNAME">
		<select name="emp_name" id="emp_name" style="width:98%;" >
		
		<?php $emp_sql=DB_query("Select id, emp_code,emp_name,employee_type from empmaster where    emp_site in('".$_SESSION['AssignedBranch']."') and otapplicable='Y' order by emp_name",$db);?>
	 	<option value="0">Select</option>
			<?php if(DB_num_rows($emp_sql)>0):?>
		
			<?php while ($data = DB_fetch_array($emp_sql)) :?>
		
				<option value="<?php echo $data['id'] ?>" <?php if($_REQUEST['emp_name']==$data['id']){ echo "Selected";} ?>>
					<?php echo $data['emp_name']; ?>
				</option>	
			<?php endwhile;	endif;?>
	  	</select>
    </td>
</tr>
<tr class="row1">
	<td><strong>Month</strong></td>
	<td>
		<select name="month" id="month" style="width:98%;">
		<?php $amonth=array(1=>'Jan','Feb','March','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');
	  		for($i=1;$i<=count($amonth) ;$i++):?>
	   		 <?php 
	   		 	$p=''; 
	   		 	if($_REQUEST['month']==$i)
	   				$p='selected';
	   			if($i<10)
	   			{
	   				$j="0".$i;
	   			}
	   			else
	   			{
	   				$j=$i;
	   			}
	    echo "<option value='".$j."' $p>$amonth[$i]</option>";
	  endfor;
	?></select></td>
	<td><strong>Year</strong></td>
	<td>
		<select name="years" id="years" style="width:98%;">
			<?php 
		  	for($i=date('Y');$i>=2000;$i--):?>
		  	
			<?php $p='';if($_REQUEST['years']==$i)$p='selected'; echo "<option value='".$i."' $p>$i</option>";
		  endfor;?>
		</select>
	</td>
	<td colspan="2"></td>
</tr>

<tr class="row1">
	<td style="text-align:center;" colspan="6"><input type="submit" name="GO" value="Search" class="search"></td>
</tr>
</table>
<br/>
<?php if($_REQUEST['GO']) {?>
	<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:#808080 1px solid; border-collapse:collapse;">	
	<tr>
		<th width="4%">S. No.</th>
		<th width="10%">Employee Code</th>
		<th>Employee Name</th>
		<th width="5%">Month</th>
		<th width="10%">Gross Salary</th>
		<th width="10%">Salary/Day</th>
		<th width="10%">OT days</th>
		<th width="10%">OT Amount</th>
	</tr>
	<?php 
		$i=0; while($GetAll=DB_fetch_array($SQLDATA)) {  ?>
		
		<?php 
		$getval=DB_query("Select * from ot_management where emp_id='".$GetAll['id']."' and month1='".$_REQUEST['month']."' and year1='".$_REQUEST['years']."'",$db);
		if(DB_num_rows($getval)>0):?>
		 
		<?php 	$fetdata=DB_fetch_array($getval);?>
		<tr bgcolor="#fff">
			<td><?php echo $i+1; ?>
				<input type="hidden" name="EmpId[]" style="width:98%;" id="EmpId_<?php echo $i; ?>" value="<?php  echo $fetdata['emp_id']; ?>">
			</td>
			<td><?php echo $GetAll['emp_code']; ?></td>
			<td><?php echo $GetAll['emp_name']; ?></td>
			<td>
				<?php 
					if($_REQUEST['month']==01){echo 'Jan';}
					if($_REQUEST['month']==02){echo 'Feb';}
					if($_REQUEST['month']==03){echo 'March';}
					if($_REQUEST['month']==04){echo 'Apr';}
					if($_REQUEST['month']==05){echo 'May';}
					if($_REQUEST['month']==06){echo 'June';}
					if($_REQUEST['month']==07){echo 'July';}
					if($_REQUEST['month']==08){echo 'Aug';}
					if($_REQUEST['month']==09){echo 'Sept';}
					if($_REQUEST['month']==10){echo 'Oct';}
					if($_REQUEST['month']==11){echo 'Nov';}
					if($_REQUEST['month']==12){echo 'Dec';} 
				?>
				<input type="hidden" name="month_name[]" id="month_name_<?php echo $i; ?>" value="<?php  echo $fetdata['month1']; ?>">
				<input type="hidden" name="year[]" id="year_<?php echo $i; ?>" value="<?php  echo $fetdata['year1']; ?>">
			</td>
			<td>
				<input type="text" readonly="" style="width:98%;" name="net_sal[]" id="net_sal_<?php echo $i; ?>" value="<?php  echo $GetAll['gross']; ?>">
			</td>
	 
			<td>
			<?php 
				$perdaysalary = 0;
			 	if($_REQUEST['month']==1){echo $perdaysalary= round( ($GetAll['gross']/31),2); }
				if($_REQUEST['month']==2){echo $perdaysalary= round($GetAll['gross']/28,2);}
				if($_REQUEST['month']==3){echo $perdaysalary=round($GetAll['gross']/31,2);}
				if($_REQUEST['month']==4){echo $perdaysalary=round($GetAll['gross']/30,2);}
				if($_REQUEST['month']==5){echo $perdaysalary=round($GetAll['gross']/31,2);}
				if($_REQUEST['month']==6){echo $perdaysalary=round($GetAll['gross']/30,2);}
				if($_REQUEST['month']==7){echo $perdaysalary=round($GetAll['gross']/31,2);}
				if($_REQUEST['month']==8){echo $perdaysalary= round($GetAll['gross']/31,2);}
				if($_REQUEST['month']==9){echo $perdaysalary=round($GetAll['gross']/30,2);}
				if($_REQUEST['month']==10){echo $perdaysalary=round($GetAll['gross']/31,2);}
				if($_REQUEST['month']==11){echo $perdaysalary=round($GetAll['gross']/30,2);}
				if($_REQUEST['month']==12){echo $perdaysalary=round($GetAll['gross']/31,2);} 
			?>
	</td>
	<td>
		<?php 
		 	 
			$ot_result = DB_query("SELECT sum( ot_hrs )
				FROM daily_attendance
				WHERE employee = '".$GetAll['id']."'
				AND MONTH( attendance_date ) = '".$_REQUEST['month']."'
				AND YEAR( attendance_date ) = '".$_REQUEST['years']."'
				AND pres_abs = 'P'",$db);
			 
			if(DB_num_rows($ot_result)>0){
				$ot_hrs1 = DB_fetch_array($ot_result);
				$ot_hrs = $ot_hrs1[0];
				$ot_days = round( ($ot_hrs/8),2);
			}
			else{
				
				$ot_days = 0;
			}

			$totalsal = round( ($perdaysalary*$ot_days) ,2);

		?>
		<input type="text" name="ot_days[]" id="ot_days_<?php echo $i; ?>" value="<?php echo $ot_days;?>" onKeyUp="cal_otAmt(<?php echo $i; ?>)" style="width:98%;">
	</td>
	<td>
		<input type="text" readonly="" style="width:98%;" name="ot_amt[]" id="ot_amt_<?php echo $i; ?>" value="<?php echo $totalsal;?>"  >
	</td>
</tr>
<?php else:?>
<tr bgcolor="#fff">
	<td><?php echo $i+1; ?>
	<input type="hidden" name="EmpId[]" id="EmpId_<?php echo $i; ?>" value="<?php  echo $GetAll['id']; ?>">
	</td>
	<td><?php  echo $GetAll['emp_code']; ?></td>
	<td><?php  echo $GetAll['emp_name']; ?></td>
	<td><?php 
	if($_REQUEST['month']==1){echo 'Jan';}
	if($_REQUEST['month']==2){echo 'Feb';}
	if($_REQUEST['month']==3){echo 'March';}
	if($_REQUEST['month']==4){echo 'Apr';}
	if($_REQUEST['month']==5){echo 'May';}
	if($_REQUEST['month']==6){echo 'June';}
	if($_REQUEST['month']==7){echo 'July';}
	if($_REQUEST['month']==8){echo 'Aug';}
	if($_REQUEST['month']==9){echo 'Sept';}
	if($_REQUEST['month']==10){echo 'Oct';}
	if($_REQUEST['month']==11){echo 'Nov';}
	if($_REQUEST['month']==12){echo 'Dec';} ?>
	<input type="hidden" name="month_name[]" id="month_name_<?php echo $i; ?>" value="<?php  echo $_REQUEST['month']; ?>">
	<input type="hidden" name="year[]" id="year_<?php echo $i; ?>" value="<?php  echo $_REQUEST['years']; ?>">
	</td>
	<td>
	<input type="text" readonly="" style="width:98%;" name="net_sal[]" id="net_sal_<?php echo $i; ?>" value="<?php  echo $GetAll['gross']; ?>"></td>
		<td>
		<?php 
			$perdaysalary = 0;
			 if($_REQUEST['month']==1){echo $perdaysalary= round( ($GetAll['gross']/31),2); }
			if($_REQUEST['month']==2){echo $perdaysalary= round($GetAll['gross']/28,2);}
			if($_REQUEST['month']==3){echo $perdaysalary=round($GetAll['gross']/31,2);}
			if($_REQUEST['month']==4){echo $perdaysalary=round($GetAll['gross']/30,2);}
			if($_REQUEST['month']==5){echo $perdaysalary=round($GetAll['gross']/31,2);}
			if($_REQUEST['month']==6){echo $perdaysalary=round($GetAll['gross']/30,2);}
			if($_REQUEST['month']==7){echo $perdaysalary=round($GetAll['gross']/31,2);}
			if($_REQUEST['month']==8){echo $perdaysalary= round($GetAll['gross']/31,2);}
			if($_REQUEST['month']==9){echo $perdaysalary=round($GetAll['gross']/30,2);}
			if($_REQUEST['month']==10){echo $perdaysalary=round($GetAll['gross']/31,2);}
			if($_REQUEST['month']==11){echo $perdaysalary=round($GetAll['gross']/30,2);}
			if($_REQUEST['month']==12){echo $perdaysalary=round($GetAll['gross']/31,2);} 
		?>
	
	</td>
	<td>
	<?php 
			//$ot_days =0; getTotalHrs($GetAll['id'],$_REQUEST['month'],$_REQUEST['years']);
	$ot_result = DB_query("SELECT sum( ot_hrs )
				FROM daily_attendance
				WHERE employee = '".$GetAll['id']."'
				AND MONTH( attendance_date ) = '".$_REQUEST['month']."'
				AND YEAR( attendance_date ) = '".$_REQUEST['years']."'
				AND pres_abs = 'P'",$db);
			if(DB_num_rows($ot_result)>0){
				$ot_hrs1 = DB_fetch_array($ot_result);
				$ot_hrs = $ot_hrs1[0];
				$ot_days = round( ($ot_hrs/8),2);
			}
			else{
				
				$ot_days = 0;
			}

			$totalsal = round( ($perdaysalary*$ot_days) ,2);

//echo "###".$ot_days;
	?>
	<input type="text" name="ot_days[]" id="ot_days_<?php echo $i; ?>"  readonly style="width:98%;" value="<?php echo $ot_days;?>" ></td>
	<td><input type="text" readonly="" name="ot_amt[]" id="ot_amt_<?php echo $i; ?>" value="<?php echo $totalsal;  ?>" style="width:98%;"></td>
</tr>


<?php endif;



$i++;} ?>
<tr bgcolor="#fff">
	<td style="text-align:center;" colspan="8"><input type="submit" name="save" value="Save" class="search"></td>
</tr>
</table>
<?php  }?>
</form>
</div>
</div>

<?php 
		function getOtHrs(){

	 		
		}
		function getTotalHrs($emp_id,$month,$years)
		{
			global $db;


            /************************************************************************************/

                    $days_in_month=cal_days_in_month(CAL_GREGORIAN ,$month , $years );
					$timestamp = mktime(0, 0, 0, $month, 1);
					$monthName = date('F', $timestamp );
					$holidays=DB_query("select id from holidays where MONTH(start_date)='".$month."' ",$db);
			               $count_hday=DB_num_rows($holidays);
					$wk_off=DB_query("select emp_weekly_off,employee_type from empmaster where id='".$emp_id."'",$db);
			                $wk_off_day=DB_fetch_array($wk_off);
                                        if($wk_off_day['emp_weekly_off']!=0)
					$wk_off_days=intval($days_in_month/$wk_off_day['emp_weekly_off']);
                                        else
                                        $wk_off_days=0;

					 $tot_work_days=$days_in_month-($count_hday+$wk_off_days);
					$off_hr=DB_query("select office_hours from daily_attendance where shift_category='".$wk_off_day['employee_type']."' order by id desc limit 0,1",$db);
			               $off_hrs=DB_fetch_array($off_hr);
					$hr=explode(':',$off_hrs['office_hours']);
					$cal_hr=$tot_work_days*$hr['0'];
					 $cal_min=$tot_work_days*$hr['1'];
					
					if($cal_min>=60){
					$d = floor ($cal_min/ 1440);
			        $h = floor (($cal_min - $d * 1440) / 60);
					"<br>".$m = $cal_min - ($d * 1440) - ($h * 60);
					$oh=$cal_hr+$h;
					if($m=='0'){
					$om='00';}
					else{
					$om=$m;
					}
					}else{
					$oh=$cal_hr;
					$om=$cal_min;
					}
					$tot_ohs=$oh.':'.$om;
                


            /*************************************************************************************/



	//echo "SELECT sum(ot_hrs) FROM daily_attendance WHERE employee='".$emp_id."' AND MONTH(attendance_date)='".$month."' AND YEAR(attendance_date)='".$years."' AND pres_abs='P'  ";

			$cow = DB_query("SELECT * FROM daily_attendance WHERE employee='".$emp_id."' AND MONTH(attendance_date)='".$month."' AND YEAR(attendance_date)='".$years."' AND pres_abs='P'",$db);
          	if(DB_num_rows($cow)>0)
          	{
          		while($cow_arr=DB_fetch_array($cow))
            {
				$work_hrs_brk=explode(':',$cow_arr['work_hrs']);
				$wh_arr[]=$work_hrs_brk['0'];
				$wm_arr[]=$work_hrs_brk['1'];
			}


			$tot_wh=array_sum($wh_arr);
			$tot_wm=array_sum($wm_arr);
			if($tot_wm>=60)
			{
				$dy = floor ($tot_wm/ 1440);
        		        $hr = floor (($tot_wm - $dy * 1440) / 60);
				"<br>".$mn = $tot_wm - ($dy * 1440) - ($hr * 60);
				$wh=$tot_wh+$hr;
				if($mn=='0')
				{
					$wm='00';
				}
				else
				{
					$wm=$mn;
				}

			}
			else
			{
				$wh=$tot_wh;
				$wm=$tot_wm;
			}
			 $tot_whs=$wh.':'.$wm;
			if($wh>$oh)
			{
				if($om>$wm)
				{
					$wm=$wm+60;
					$wh=$wh-1;
					$total_oth=$wh-$oh;
					$total_otm=$wm-$om;
					$total_ot=$total_oth.':'.($total_otm=='0'?'00':$total_otm);
				}
				else
				{
					$total_oth=$wh-$oh;
					$total_otm=$wm-$om;
					$total_ot=$total_oth.':'.($total_otm=='0'?'00':$total_otm);
				}
			}
			else
			{
				$total_ot=0;
			}
				if($total_ot!=0)
				{
					return round($total_ot/8,2);
				}	
          	}
          	else
          		return 0 ;
            
		}
?>

<script type="text/javascript">
	function loadEmployee(value){	
		$.post("GetEmployee_ot.php",{"LoadEmployee":"yes","site":value},function(data){
			document.getElementById('emp_name').innerHTML = data;
		});
	}
</script>