<link rel="stylesheet" type="text/css" href="css/table_frize_1.css">
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain" style="width:100%;">
<div class="tab_div" style="width:100%;">
<h1><?php echo $s->GetFieldValue('modules_form','id="'.$_REQUEST['frmid'].'"','description');?> </h1>
<?php
	if(isset($_POST['Save']))
{   //echo "<pre>";
 //print_r($_REQUEST);
	//exit;
	for($j=0;$j<count($_REQUEST['empid']);$j++)
	{
		$cntr=0;
		foreach($_REQUEST['amount'][$_REQUEST['empid'][$j]] as $key=>$val)
		{
			if($val!='')
		    $cntr=$cntr+1;
		}
		//echo $cntr."<br>";
		//echo $_REQUEST['amount'][$_REQUEST['empid'][$j]]."<br>";
		$check=DB_query("select * from monthly_other_deducation where empid='".$_REQUEST['empid'][$j]."' and month='".$_REQUEST['month_other']."' and  year='".$_REQUEST['year_other']."' and site='".$_REQUEST['sitedata']."'",$db);
		$numrow=DB_num_rows($check);
		if($numrow<=0)
		{	
		$flag=1;
		if($cntr!='0'){	
        	$data['site']=$_REQUEST['sitedata'];
			$data['month']=$_REQUEST['month_other'];
			$data['year']=$_REQUEST['year_other'];
			$data['companyid']=$_SESSION['ACGCompanyId'];
			$data['created_by']=$_SESSION['UserID'];
			$data['empid']=$_REQUEST['empid'][$j];
			$data['created_date']=date('Y-m-d');			
			$s->RecordInsert('monthly_other_deducation',$data);
			//$last_id[]=DB_Last_Insert_ID($db,'monthly_other_deducation','id');
				
			$last_id=DB_Last_Insert_ID($db,'monthly_other_deducation','id');
           
            for($i=0;$i<count($_REQUEST['amount'][$data['empid']]);$i++)
			{
			    if($_REQUEST['amount'][$data['empid']][$i]!='' )	
			    {		
					$dataArr['parentid']=$last_id;
					$dataArr['other_ded']=$_REQUEST['other_ded'][$i];
					$dataArr['other_ded_type']=$_REQUEST['other_ded_type'][$i];						
					$dataArr['amount']=$_REQUEST['amount'][$data['empid']][$i];
					$s->RecordInsert('monthly_other_deducation_details',$dataArr);
				}		
			}
		}
	}
		else
		{
		$flag=0;
		
		}
		
	}
	
	if($flag==1)
	$s->pageLocation("index.php?route=HRModule/view_monthely_other_ded_index".$rurl);	
	else
	echo "<script> alert('Entry Already Exists');</script>";
}
	


 


?>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">

<tr class="row1">
<td style="padding:0px;">
	<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td align="left" valign="top" width="3%"><strong>Site</strong></td>
    <td align="left" valign="top"><select name="site" id="site" style="width:70px;">
<!--			<option value="000">---ALL---</option>-->
			<?php $site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
			while($site_row=DB_fetch_array($site_sql)){?>
			<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_SESSION['emp_salprocess_site']==$site_row['loccode']?'selected':'');?>><?php echo $site_row['locationname'];?></option>
			<?php } ?>
		</select></td>
    <td align="left" valign="top"><strong>Grade</strong></td>
    <td align="left" valign="top"><select name="empgrade" id="empgrade"  style="width:100px;"> 
			<option value="0" >---</option>
			<?php 
			$empgrade=DB_query("select * from emp_grade",$db);
			while($row1=DB_fetch_array($empgrade))
			{
			$grade='';
			if($row1['id']==$_REQUEST['empgrade'])
			$grade="selected";	
			?>
			<option value="<?php echo $row1['id']; ?>" <?php echo $grade; ?> ><?php echo $row1['grade_name']; ?></option>
			<?php } ?>
			
		</select></td>
   
    <td align="left" valign="top"><strong>Month :</strong></td>
    <td align="left" valign="top"><select name="paymonth" id="paymonth"  style="width:70px;">
 		<?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="left" valign="top"><strong>Year :</strong></td>
    <td align="left" valign="top"><select name="payyear" id="payyear"  style="width:70px;">
 		<?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="10" align="center" valign="top" style="text-align:center;"><input type="submit" name="Search" class="search" value="Search" onclick="return chksalprocess()"/>
      &nbsp;
      <input type="submit" name="Reset" class="search" value="Reset"/>	</td>
    </tr>
</table></td>
</tr>
</form>	
<form action="" method="post" name="myform">
<?php if($_REQUEST['Search']) 
{
?>
<input type="hidden" name="sitedata" value="<?php echo $_REQUEST['site'];?>" />
<input type="hidden" name="month_other" value="<?php echo $_REQUEST['paymonth']; ?>" />
<input  type="hidden" name="year_other" value="<?php echo $_REQUEST['payyear']; ?>" />
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
  <td class="tabledata" rowspan="2">Sno.</td>
    <td class="tabledata" rowspan="2">Emp Code</td>
    <td class="tabledata" rowspan="2">Emp Name</td>
    <?php $sql_otherd=DB_query("select * from other_deduction_master where type='D'",$db);
    $dcol=DB_num_rows($sql_otherd);
    if($dcol>0){
    ?>
    <td class="tabledata" colspan="<?php echo $dcol;?>" style="text-align:center">Deduction</td>
    <?php 
	}
    $sql_othera=DB_query("select * from other_deduction_master where type='A'",$db);
    $acol=DB_num_rows($sql_othera);
    if($acol>0){
    ?>
    <td class="tabledata" colspan="<?php echo $dcol;?>" style="text-align:center">Addition</td>
    <?php } ?>
	</tr>
	<tr>
	<?php 
	$count1=0;
	$sql_other=DB_query("select * from other_deduction_master where type='D'",$db);
	$count1=$count1+DB_num_rows($sql_other);
	while($row_other=DB_fetch_array($sql_other)){ ?>
   <td class="tabledata"><?php echo $row_other['name']; ?></td><?php } ?>
   <?php 	
	$sql_other=DB_query("select * from other_deduction_master where type='A'",$db);
	$count1=$count1+DB_num_rows($sql_other);
	while($row_other=DB_fetch_array($sql_other))
	{
	
   ?>
<td class="tabledata"><?php echo $row_other['name']; ?></td>

<?php } ?>
   
  </tr>
    <?php 
    $dailatt='';
    //$attendance=DB_query("select ",$db);

    $daily_att=DB_query("SELECT daily_attendance.employee as emp, COUNT(daily_attendance.id) AS NumberOfOrders 
         	FROM daily_attendance  where month(attendance_date)='".$_REQUEST['paymonth']."' and year(attendance_date)='".$_REQUEST['payyear']."' GROUP BY daily_attendance.employee HAVING COUNT(daily_attendance.pres_abs ='P' 
         		AND daily_attendance.pres_abs ='L' AND daily_attendance.pres_abs ='HO' AND daily_attendance.pres_abs ='WO')",$db);
    while($row_att=DB_fetch_array($daily_att))
    {
    	$dailatt[$row_att['emp']]=$row_att;
    }
   

	$filter=array();	
	if($_REQUEST['site']!='')
	$filter[]="emp_site = '".$_REQUEST['site']."'";	
	if($filter!=''){
	$filter2=implode(' AND ',$filter);}
	else
	$filter2="1=1";

	   if($_REQUEST['empgrade']!='0')
	   $sql_grade=DB_query("select id,grade_name,grade_amount from emp_grade where id='".$_REQUEST['empgrade']."'",$db);
	   else
	   $sql_grade=DB_query("select id,grade_name,grade_amount from emp_grade",$db);		
	   while($row_grade=DB_fetch_array($sql_grade))
	   {	
	
	?>
	<tr bgcolor="#E5FFE9">
    <td colspan="<?php echo $count1; ?>"><?php echo $row_grade['grade_name']; ?></td>
    <td colspan="3"><?php echo  $row_grade['grade_amount'];   ?><input type="hidden" name="gradeamt[<?php echo $row_grade['id']; ?>]" id="gradeamt_<?php echo $j; ?>" value="<?php echo $row_grade['grade_amount']; ?>"></td>
    </tr>
	<?php	
	   $GetRecord = "Select * from empmaster where $filter2 and  grade='".$row_grade['id']."'";
	   $ResultData = DB_query($GetRecord,$db);
	   $i=1;
	   $numrow=DB_num_rows($ResultData);
	   if(DB_num_rows($ResultData)==0)
	   echo "<td colspan='5'>--No Record--</td>";
	   $j=0;
	   while($RowData = DB_fetch_array($ResultData))
	   {  


		  $chkSql= "Select id from monthly_other_deducation where site='".$_REQUEST['site']."' AND month='".$_REQUEST['paymonth']."'
		   AND year='".$_REQUEST['payyear']."' AND empid='".$RowData['id']."'";
		   $chk_data = DB_fetch_array(DB_query($chkSql,$db));
		   if(!$chk_data){

		 if($dailatt[$RowData['id']]['NumberOfOrders']>=25) {
          $totalgrade_value=$dailatt[$RowData['id']]['NumberOfOrders']*$row_grade['grade_amount'];

   ?>
<tr bgcolor="#c1c1c1">
     <td ><div align="center"><?php echo $i; ?></div></td>
    <td><?php echo $RowData['emp_code']; ?><input type="hidden" name="empid[]" value="<?php echo $RowData['id']; ?>" /></td>
	<!-- <td><?php //echo $RowData['AccountsType']; ?>	  </td>-->
	 <td><?php echo $RowData['emp_name'] ?>	  </td>
 	<?php  $sql_other1=DB_query("select * from other_deduction_master where type='D'",$db);
	while($row_other1=DB_fetch_array($sql_other1))
	{
	?>
	<td>
		<input type="hidden" name="other_ded[]" value="<?php echo $row_other1['id']; ?>" />
		<input type="hidden" name="other_ded_type[]" value="<?php echo $row_other1['type']; ?>" />
		<input type="text" name="amount[<?php echo $RowData['id']; ?>][]"  />	
	</td>
	<?php } ?>
	<?php  $sql_other1=DB_query("select * from other_deduction_master where type='A'",$db);
			while($row_other1=DB_fetch_array($sql_other1))
			{
	?>
	<td>
		<input type="hidden" name="other_ded[]" value="<?php echo $row_other1['id']; ?>" />
		<input type="hidden" name="other_ded_type[]" value="<?php echo $row_other1['type']; ?>" />
		<input type="text" name="amount[<?php echo $RowData['id']; ?>][]"  />
	</td>
	<?php } ?>
   
  </tr>
	<?php $i++;}  }
	} }
   ?> 
 </table>

<?php if($numrow!=0) { ?>
<p align="center" style="text-align:center"><input type="submit" name="Save" value="Save" class="search" /></p>
<?php } ?>
<?php } ?>
</form>

</div>
</div>