<?php
include('includes/Class_Branch.php');//include('includes/function_lib.php');
$s=new db_query();
?>

<?php 
$title = _('Revised Employee salary');

function archive_record($archived_tablename,$row)
{
 global $db;
  	  $sql="Insert into ".$archived_tablename ." set ";
 		$i=0;
 		foreach($row as $key => $val)
 			{
 			if($i==0)
			$sql.=" ".$key." = '".$val."'";
			else
			$sql.=", ".$key." = '".$val."'";			
			$i++;
			}
	 $r=DB_query($sql,$db);
return true;
}

if(isset($_REQUEST['Save']))
{		 
		$financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
		$prefix="REV";
		/************  Inserting Records in Revise tables ****************************/
		for($i=0;$i<count($_REQUEST['empid']);$i++)
		{
			$empid=$_REQUEST['empid'][$i];
		 if($_REQUEST['basic_salary'][$empid]!='0')
		 {
			
			   $sql="Select revision_no from empmaster_revised_sal where  INSTR(`revision_no`, '$financial') > 0 
			         order by id desc limit 0,1";
		
			$result=DB_query($sql,$db);
			$myrow=DB_fetch_array($result);
			//echo DB_num_rows($result);
			if(DB_num_rows($result)>0)
			{
			//$Reqno=$myrow['Reqno'];
			//$req_part1=substr($Reqno,-4);
			$ExplBill=explode("/",$myrow['revision_no']);
			$maxval = $ExplBill[2];
			//echo $req_part1;
			$req_final=$maxval+1;
			//echo "".$req_final;
			}
			else
			{
			$req_final="1";
			}
			
			//echo $maxval;
			
			$string=$prefix."/".$financial."/";	     
	        $final_string1=$string.str_pad($req_final,4,"0",STR_PAD_LEFT);	
				
		    $data_arr['revision_no']      =$final_string1;
			$data_arr['empid']            =$empid;
			$data_arr['basic_salary']     =$_REQUEST['basic_salary'][$empid];
			$data_arr['allow_tot']        =$_REQUEST['allow_tot'][$empid];
			$data_arr['deduct_tot']       =$_REQUEST['deduct_tot'][$empid];
			$data_arr['gross_sal']        =$_REQUEST['gross_sal'][$empid];
			$data_arr['applicable_month'] =$_REQUEST['applicable_month'];
			$data_arr['increment_type']   =$_REQUEST['increment_type'];
			$data_arr['applicable_year']  =$_REQUEST['applicable_year'];
			$data_arr['net_sal']          =$_REQUEST['net_sal'][$empid];
			$data_arr['Companyid']        =$_SESSION['ACGCompanyId'];
			$data_arr['created_by']       =$_SESSION['UserID'];
			$data_arr['created_date']     =date('Y-m-d');
			$data_arr['log_type']          ='revise';			
			$sal_result1=$s->RecordInsert('empmaster_revised_sal', $data_arr);
		    $last_id=DB_Last_Insert_ID($db,'empmaster_revised_sal','id');
	/********************************************************************************/	
	foreach($_REQUEST['allid'][$empid] as $key=>$value)
	 {	
		$dataEmpsal['emp_id']               =$empid; 
		$dataEmpsal['allowance_deduction']  =$value;  
		$dataEmpsal['percentage']           =$_REQUEST['aperc'][$empid][$value];  
		$dataEmpsal['percentage_value ']    =$_REQUEST['allow'][$empid][$value]; 
		$dataEmpsal['all_ded_prefix']       ='A';
		$dataEmpsal['mst_revid']            =$last_id;
		$s->RecordInsert('emp_salary_revise',$dataEmpsal);
	
	}
	//exit;	
	
		$dataspecial['emp_id']                =$empid; 
		$dataspecial['allowance_deduction']   ='100';  
		$dataspecial['percentage']            ='0.00';  
		$dataspecial['percentage_value']      =$_REQUEST['special_allow'][$empid][$value];
		$dataspecial['all_ded_prefix']        ='A';
		$dataspecial['mst_revid']             =$last_id;
		$s->RecordInsert('emp_salary_revise',$dataspecial);	
	
//************************DEDUCTION PART*************AS*************START HERE************12-06-2013*****************	
	
	foreach($_REQUEST['dedid'][$empid] as $key1=>$va1)
	{		
			$dataded['emp_id']               =$empid;
			$dataded['allowance_deduction']  =$va1;  
			$dataded['percentage']           =$_REQUEST['dperc'][$empid][$va1];  
			$dataded['percentage_value']     =$_REQUEST['deduction'][$empid][$va1];
			$dataded['all_ded_prefix']       ='D';
			$dataded['mst_revid']            =$last_id;
			//echo "<pre>";
			//print_r($dataded);
			$s->RecordInsert('emp_salary_revise',$dataded);		
	}		
}		
}		
	/********************************************************************************************************************/
		
}
?>
 <link rel="stylesheet" href="css/new_mxerp.css">

<form action="" method="post">
<div class="body_contain">
<div class="tab_div">

<h1>Revised Employee salary</h1>
<!-- Table start 1 -->
<table width="100%" border="0" cellspacing="0" cellpadding="3" align="left">
<tr class="row1">
    <td width="12%" align="right">Applicable From Month</td>
	<td width="30%"><select name="applicable_month" style="width:48%;">
          <?php
 $arr=array(01=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
 foreach ($arr as $key => $value) {
 	$p='';
 	if( $key==$_REQUEST['applicable_month'])
	$p='selected';
   ?>
          <option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
          <?php } ?>
        </select>
		<select name="applicable_year"  style="width:48%;">
          <?php
 for($i=date('Y');$i>2000;$i--)
 {
 	$p='';
 	if( $i==$_REQUEST['applicable_year'])
	$p='selected';
 ?>
          <option value="<?php echo $i;?>" <?php echo $p;?>><?php echo $i;?></option>
          <?php 
 }
  $all_sql=DB_query("Select * from allowmaster",$db); 	
	        $allow_num_rows=DB_num_rows($all_sql); 
	        while($all_row=DB_fetch_array($all_sql)){
	  	    $allow[]=$all_row; }
 ?>
        </select>
    </td>
    <td width="12%" align="right">Increment Type</td>
	<td width="16%"><select  name="increment_type">
			<option value="Annual">Annual</option>
			<option value="Promotional">Promotional</option>
			<option value="Other">Other</option>
		</select>
	</td>
	<td width="12%" align="right">Appraisal %</td>
	<td colspan="<?php echo $allow_num_rows-1; ?>"><input name="incrementper" type="text" id="incrementper" value="<?php echo $s->GetFieldValue("apprisal_policy_master","apprisal_type='Y'","apprisal_per"); ?>" readonly  tabindex="1" placeholder="Appraisal Percentage" size="8"  ></td>
</tr>
<tr class="row1">
	<td colspan="6" style="text-align:center;"><input type="submit" name="Search" value="Search" class="search"></td>
</tr>
</table>

<div id='hid_revised' style="width:100%; height:auto; float:left; margin-top:15px;">
	<?php if(isset($_REQUEST['Search'])) { ?>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
	<?php           
	  	// echo "select empid from empmaster_revised_sal where applicable_year='".$_REQUEST['applicable_year']."'";
	     $sql_row=DB_query("select empid from empmaster_revised_sal where applicable_year='".$_REQUEST['applicable_year']."'",$db); 
	     while($rowsql=DB_fetch_array($sql_row))
	     {
	     	$emp_id_dtls[]=$rowsql['empid'];
	     }
	    
	     if(!empty($emp_id_dtls) && count($emp_id_dtls)>1)
	     { $employeeid=implode(",",$emp_id_dtls);	}
	     else{ $employeeid='';}

           if($employeeid==''){
            $emp_dob_dtls=DB_query("SELECT id,doj,basic_perc ,basic_salary,allow_tot,employment_type FROM empmaster WHERE DAY(doj)<='25' and month(doj)='".$_REQUEST['applicable_month']."' and year(doj)<'".$_REQUEST['applicable_year']."'",$db); }
        else {
        $emp_dob_dtls=DB_query("SELECT id,doj,basic_perc ,basic_salary,allow_tot,employment_type FROM empmaster WHERE DAY(doj)<='25' and month(doj)='".$_REQUEST['applicable_month']."' and year(doj)<'".$_REQUEST['applicable_year']."' and id not in(".$employeeid.")",$db);      
        	 }
            while($row_emp=DB_fetch_array($emp_dob_dtls)){          
           	$empdob[$row_emp['id']]=$row_emp['doj'];
           	$empid[]=$row_emp['id']; 
            $basic_per[$row_emp['id']]=$row_emp['basic_perc'];
             $basic_sal[$row_emp['id']]=$row_emp['basic_salary']+($row_emp['basic_salary']*$_REQUEST['incrementper']/100);
             $allow_tot[$row_emp['id']]=$row_emp['allow_tot']+($row_emp['allow_tot']*$_REQUEST['incrementper']/100);
             $emptype[$row_emp['id']]=$row_emp['employment_type'];
           }
           $get_sql3=DB_query("Select * from emp_salary where  all_ded_prefix='A' 
           	        and allowance_deduction not in('100','101','102','103')  order by allowance_deduction ASC",$db);
	
	while($get_row1=DB_fetch_array($get_sql3))
	{
		$aperc[$get_row1[emp_id]][$get_row1['allowance_deduction']]=$get_row1['percentage'];  
		//$allow[$k]=$get_row1['percentage_value'];
		
	}

	$get_sql10=DB_query("Select * from emp_salary where  all_ded_prefix='A' and allowance_deduction=100",$db);
	while($get_row10=DB_fetch_array($get_sql10))
	{
	$spc_allow[$get_row10['emp_id']]=$get_row10['percentage'];  	
	}

	$get_sql4=DB_query("Select * from emp_salary where  all_ded_prefix='D' order by id ASC",$db);
	$K=1;
	while($get_row4=DB_fetch_array($get_sql4))
	{
		$dperc[$get_row4['emp_id']][$get_row4['allowance_deduction']]=$get_row4['percentage'];  
		
	}	
	//$special_allow=$get_row10['percentage_value'];
           	$i=1;        
	  ?>
		<tr>
			<th rowspan="2">S. No.</th>
			<th rowspan="2">Emp. Name</th>
			<th rowspan="2">Basic</th>
			<th colspan="<?php echo $allow_num_rows; ?>" style="text-align:center">Allowance Part</th>
			<th rowspan="2">S.A</th>		
		</tr>
		<tr>			
			<?php foreach($allow as $k1=>$val1) { ?>
			<th><?php echo $val1['allowcode']; ?></th>
			<?php } ?>				
		</tr>	
			<?php  
           if(!empty($empid)) {
			foreach($empid as $key=>$value){ 
            $allowamt='';
			?>
		<tr>
			<td><?php echo $i; ?><input name="empid[]" type="hidden" value="<?php echo $value; ?>"></td>	
			<td><?php echo $s->getField('empmaster','id',$value,'emp_name'); ?></td>
			<td><input type="hidden" name="basic_perc[<?php echo $value; ?>]" id="basic_perc" value="<?php echo $basic_per[$value];?>" size="8" tabindex="2"/><input type="text" name="basic_salary[<?php echo $value; ?>]" id="basic_salary" value="<?php echo trim($basic_sal[$value]);?>" size="8" tabindex="3" readonly  class="number" onfocus="calculate_basic()"  /></td>
			<?php foreach($allow as $k1=>$val1) { ?>
			<td><input name="aperc[<?php echo $value; ?>][<?php echo $val1['id']; ?>]" type="hidden" id="aperc_<?php echo $i;?>" value="<?php echo $aperc[$value][$val1['id']];?>" 
			 tabindex="<?php echo $tabindex+=1;?>" size="8"  />	
			 <?php $allowamt=$basic_sal[$value]*$aperc[$value][$val1['id']]/100; $allamt[$value][]=$allowamt; ?>
			 <input name="allow[<?php echo $value; ?>][<?php echo $val1['id']; ?>]" type="text" id="allow_<?php echo $i;?>" value="<?php echo $allowamt; ?>" size="8" class="number" readonly tabindex="<?php echo $tabindex+=1;?>"  />
             <input type="hidden" name="allow_code[<?php echo $value; ?>][<?php echo $val1['id']; ?>]" id="allow_code_<?php echo $i;?>" value="<?php echo $val1['code']; ?>"  />
		     <input type="hidden" name="allid[<?php echo $value; ?>][]" id="allid_<?php echo $i;?>" value="<?php echo $val1['id']; ?>"  />
			 </td>
			<?php } 
            $special_allow=trim($allow_tot[$value])-(array_sum($allamt[$value])+trim($basic_sal[$value]));
			?>
		    <td><input name="spc_allow[<?php echo $value; ?>]" type="hidden" id="spc_allow" value="<?php echo $spc_allow[$value]; ?>" size="8"  tabindex="<?php echo $tabindex+=1;?>"  /><input type="text" name="special_allow[<?php echo $value; ?>][]" id="special_allow" value="<?php echo $special_allow; ?>" size="8" readonly  class="number" tabindex="<?php echo $tabindex+=1;?>"  />
            <input name="allow_tot[<?php echo $value; ?>]" type="hidden" id="allow_tot" value="<?php echo $totalallow= trim($allow_tot[$value]);?>" size="8"  readonly="true" tabindex="<?php echo $tabindex+=1;?>"   />
            <?php
               $SqlESi=DB_query("select esiper from esi_master where $totalallow <salto and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			    $ESI=DB_fetch_array($SqlESi);
			    $ESIPer=$ESI['esiper'];
                 $basicsal=trim($basic_sal[$value]);  

			     $pfpercentage=DB_fetch_array(DB_query("select pf_per,range_to from hr_pf_contrbution_master 
			     	            where emp_type='".$emptype[$value]."' and  $basicsal between range_from and range_to ",$db));
			  	 $pf_percentage=$pfpercentage['pf_per'];			  	 
			  	 $pf_limit=$pfpercentage['range_to'];			  	 

	              $ded_sql=DB_query("Select * from deduction_master",$db); 	
				  while($ded_row=DB_fetch_array($ded_sql))
				  {
                 ?>
                 <input name="empdedtype[<?php echo $value; ?>]"  id="empdedtype_<?php echo $j;?>" type="hidden" value="<?php echo $ded_row['code'];?>" />
                 <input type="hidden" name="ded_code[<?php echo $value; ?>]" id="ded_code_<?php echo $j;?>" value="<?php echo $ded_row['code'];?>" />
	             <input type="hidden" name="dedid[<?php echo $value; ?>][]" id="dedid_<?php echo $j;?>" value="<?php echo $ded_row['id'];?>" />	 
                 <input name="dperc[<?php echo $value; ?>][<?php echo $ded_row['id'];?>]" type="hidden" id="dperc_<?php echo $j;?>" value="<?php  if($ded_row['code']=='PF') { echo $pf_per= $pf_percentage; } else { echo $pf_per= $dperc[$value][$ded_row['id']]; } ?>" size="5" class="number" tabindex="<?php echo $tabindex+=1;?>" placeholder="Percentage"/>
 <?php
     if($ded_row['code']=='PF') { if(trim($basic_sal[$value])>=$pf_limit) $deduction=$pf_limit*$pf_per/100;	 
     else { $deduction=trim($basic_sal[$value])*$pf_per/100; } }
     	else  if($ded_row['code']=='ESI') {  $deduction=($totalallow*$ESIPer)/100;  }
     	 else {  $deduction=trim($basic_sal[$value])*$pf_per/100; }
 ?>
       <input name="deduction[<?php echo $value; ?>][<?php echo $ded_row['id'];?>]" type="hidden" id="deduction_<?php echo $j;?>" value="<?php echo $totalded[$value][]= $deduction;?>" size="10" class="number" tabindex="<?php echo $tabindex+=1;?>" placeholder="Ded Amt"/>
             <?php } ?>
             <input name="deduct_tot[<?php echo $value; ?>]" type="hidden" id="deduct_tot" value="<?php echo $totaldedtoal= array_sum($totalded[$value]); ?>" size="15"  readonly="true" tabindex="<?php echo $tabindex+=1;?>"/>
             <input name="gross_sal[<?php echo $value; ?>]" type="hidden" id="gross_sal" value="<?php echo $totalallow;?>"  style="width:150px;" readonly="true"/>
		<input name="net_sal[<?php echo $value; ?>]" type="hidden" id="net_sal" value="<?php echo $totalallow-$totaldedtoal;?>"  style="width:150px;"  readonly="true" tabindex="<?php echo $tabindex+=1;?>"/>
		     </td>
            </tr>
			<?php $i++; }  ?>
<tr><td colspan="<?php echo $allow_num_rows+4; ?>" style="text-align:center"><input type="submit" name="Save" value="Save" class="search" /></td></tr>

		<?php } ?>

</table>
<?php } ?>
</div> 
</div></div>
</form>
