<link rel="stylesheet" type="text/css" href="css/table_frize_1.css">
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<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){  
    $update=DB_query("update hr_incentive_cal set other_cal_reserve='".$_REQUEST['reserveamt']."'
       where month='".$_REQUEST['month_other']."' and year='".$_REQUEST['year_other']."' ",$db);
	$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" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="12%" align="right">Site</td>
    <td width="20%"><select name="site" id="site" >
<!--			<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">Grade</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 width="12%" align="right">Month</td>
    <td width="20%"><select name="paymonth" id="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 width="12%" align="right">Year</td>
    <td><select name="payyear" id="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="6" style="text-align:center;"><input type="submit" name="Search" class="search" value="Search" onclick="return chksalprocess()"/> <input type="submit" name="Reset" class="search" value="Reset"/></td>
    </tr>
</table>
</form>	
<br>
<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="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
    <th width="5%" rowspan="2">S. No.</th>
    <th width="12%" rowspan="2">Emp Code</th>
    <th rowspan="2">Emp Name</th>
    <?php $sql_otherd=DB_query("select * from other_deduction_master where type='D'",$db);
    $dcol=DB_num_rows($sql_otherd);
    if($dcol>0){
    ?>
    <th colspan="<?php echo $dcol;?>" style="text-align:center">Deduction</th>
    <?php 
	}
    $sql_othera=DB_query("select * from other_deduction_master where type='A'",$db);
    $acol=DB_num_rows($sql_othera);
    if($acol>0){
    ?>
    <th colspan="<?php echo $dcol;?>" style="text-align:center">Addition</th>
    <?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)){ ?>
   <th width="10%"><?php echo $row_other['name']; ?></th><?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))
	{
	
   ?>
    <th width="10%"><?php echo $row_other['name']; ?></th>

<?php } ?>   
  </tr>
    <?php 

      $incentive_sql=DB_fetch_array(DB_query("SELECT B.total_amt as amt,B.other_deduction_master_id as id FROM `hr_incentive_cal_dtls` B INNER JOIN `hr_incentive_cal` A 
    	           ON A.id=B.hr_incentive_cal_id WHERE A.month='".$_REQUEST['paymonth']."' 
    	           AND A.year='".$_REQUEST['payyear']."' and B.amt='0.0000' ORDER BY B.id DESC LIMIT 1",$db));

        $incentive_amt=$incentive_sql['amt'];
        $incentiveid=$incentive_sql['id'];
       $distributedamt[$incentiveid]=$incentive_amt;

       $sql_incen=DB_query("SELECT B.total_amt as amt,B.id as id FROM `hr_incentive_cal_dtls` B INNER JOIN `hr_incentive_cal` A 
    	           ON A.id=B.hr_incentive_cal_id WHERE A.month='".$_REQUEST['paymonth']."' AND A.year='".$_REQUEST['payyear']."'
    	           and B.amt!='0.0000'",$db);
     while($row_inc=DB_fetch_array($sql_incen))
     {
     	$incdtls[$row_inc['id']]=$row_inc;
     	 $distributedamt[$row_inc['id']]=$row_inc['amt'];
     }

//echo "<pre>";
//print_r($incdtls);


    $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']."' and daily_attendance.pres_abs!='A' GROUP BY daily_attendance.employee ",$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_grade3=DB_fetch_array($sql_grade))
	   {	
          $row_grade2[]=$row_grade3;
          $i=0;
          $GetRecord1 =DB_query("Select * from empmaster where $filter2 and  
          	grade='".$row_grade3['id']."'",$db);
          while($rowemp=DB_fetch_array($GetRecord1))
          {
          	if(!empty($dailatt) && $dailatt[$rowemp['id']]['NumberOfOrders']>=25) {
	         	$i++;
	         $totalgrade_value[$row_grade3['id']]=$dailatt[$rowemp['id']]['NumberOfOrders']*$row_grade3['grade_amount'];
	         $gradeemp[$row_grade3['id']]=$i;  }  
	         }

	   }



       /************  calculating Ratio *************************/

          $ration='';
         if(!empty($totalgrade_value)){ 
          $minval=min($totalgrade_value);
         foreach($totalgrade_value as $key=>$value)   {
            $ration[$key]=$value/$minval; }
        }
        if(!empty($ration))
        $rationsum=array_sum($ration);
        else
        $rationsum=1;	

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

        $numrow=0;
        $i=1;
	   foreach($row_grade2 as $key=>$row_grade)
	   {
	   	if(isset($gradeemp[$row_grade['id']])) {
	   ?>
		<tr bgcolor="#E5FFE9">
	    <td colspan="<?php echo $count1; ?>"><?php echo $row_grade['grade_name']; ?>---<?php echo  $row_grade['grade_amount'];   ?></td>
	    <td colspan="3"><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=$numrow+DB_num_rows($ResultData);
	   if($numrow==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(!empty($dailatt) && $dailatt[$RowData['id']]['NumberOfOrders']>=25) {		
        $incentive_amt_gradewise[$row_grade['id']][$incentiveid]=(($incentive_amt*$ration[$row_grade['id']])/$rationsum)/$gradeemp[$row_grade['id']];
     // print_r($incentive_amt_gradewise);
   ?>
<tr>
    <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))
			{

				
				/*if(isset($incentive_amt_gradewise[$row_grade['id']][$row_other1['id']])){
				$readonly="readonly"; $inc_amt=round($incentive_amt_gradewise[$row_grade['id']][$row_other1['id']],2); }
			    else{  $readonly='';	$inc_amt='';}*/


			    if(isset($incentive_amt_gradewise[$row_grade['id']][$row_other1['id']])){
				$readonly="readonly";  $inc_amt=round($incentive_amt_gradewise[$row_grade['id']][$row_other1['id']],2);
                   $incamt_dis[$incentiveid][]=$inc_amt;
				 }
			    else{  $readonly='readonly';

			    $Y= cal_days_in_month(CAL_GREGORIAN, $_REQUEST['paymonth'], $_REQUEST['payyear']);
			    $X[$row_other1['id']]=$s->GetFieldValue("emp_allowance_dtls","allowance_id='".$row_other1['id']."' and emp_id='".$RowData['id']."'","allowance_amt");
                $Z=$dailatt[$RowData['id']]['NumberOfOrders'];

			    $inc_amt=round(($X[$row_other1['id']]/$Y)*$Z);
			    $incamt_dis[$row_other1['id']][]=$inc_amt;

			    }


	?>
	<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']; ?>][]" <?php echo $readonly; ?> value="<?php echo  $inc_amt; ?>"  />
	</td>
	<?php } ?>
   
  </tr>
	<?php $i++;
}  }
	}   }
   ?> 

   <tr bgcolor="#E5FFE9">
	<td colspan="<?php echo $count1; ?>">Total Opening</td>
     <?php 	
	$sql_other=DB_query("select * from other_deduction_master where type='A' ",$db);	
	while($row_other=DB_fetch_array($sql_other))
	{
	
   ?>
    <td><b><?php echo $distributedamt[$row_other['id']]; ?></b></td>

<?php } ?>   
	    </tr>
	 <tr bgcolor="#E5FFE9">
	<td colspan="<?php echo $count1; ?>">Total Distributed Amt</td>
     <?php 	
	$sql_other=DB_query("select * from other_deduction_master where type='A' ",$db);
	
	while($row_other=DB_fetch_array($sql_other))
	{
	
   ?>
    <td><b><?php if(!empty($incamt_dis[$row_other['id']])) echo $disamt[$row_other['id']]= array_sum($incamt_dis[$row_other['id']]); else 
               echo $disamt[$row_other['id']]= 0;  ?></b></td>

<?php } ?>   
	    </tr>
	     <tr bgcolor="#E5FFE9">
	<td colspan="<?php echo $count1; ?>">Total Reserve Amt</td>
     <?php 	$amt=0;
	$sql_other=DB_query("select * from other_deduction_master where type='A' ",$db);
	
	while($row_other=DB_fetch_array($sql_other))
	{
	   if(!empty($disamt[$row_other['id']])) $disamt1=$disamt[$row_other['id']]; else $disamt1=0;
	   if(!empty($distributedamt[$row_other['id']])) $disamt2=$distributedamt[$row_other['id']]; else $disamt2=0;
	   $reserve_amt=round($disamt2-$disamt1,2);
   ?>
    <td><b><?php  echo $reserve_amt; $amt=$amt+$reserve_amt; ?></b></td>

<?php } ?>  
<input type="hidden" name="reserveamt" value="<?php echo $amt; ?>"> 
	    </tr>
 </table>

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

</div>
</div>