<?php session_name('CAKEPHP'); session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');

include('includes/SQL_CommonFunctions.inc');
include('CRM/functions.php');
$s=new db_query();

if(isset($_REQUEST['save']))
{
   $prefix=DB_fetch_array(DB_query("select * from prefix_tbl where prefix_name='Yearly Appraisal Creation' ",$db));
   
    $str=substr($_REQUEST['branch'],0,3);
   $cnt=DB_fetch_array(DB_query("select id from perf_year_appraisal order by id desc",$db));
   
   if($cnt['id']=='')
           $val=1;
   else
       $val=$cnt['id'];
    
    $finalstr=$prefix['prefix_code']."/".$str."/".$val;      
           
 
    DB_query("insert into perf_year_appraisal (branch,appraisal_no,date,created_by,created_date,companyid) value('".$_REQUEST['branch']."','".$finalstr."','".ConvertSQLDate($_REQUEST['app_date'])."','".$_SESSION['UserID']."','".date('Y-m-d')."','".$_SESSION['ACGCompanyId']."') ",$db);  
 $last_id=  DB_Last_Insert_ID($db, perf_year_appraisal, id);;
    for($i=0;$i<count($_REQUEST['empid']);$i++)
    {
        if(($_REQUEST['growth'][$i]!=0) && ($_REQUEST['growth'][$i]!=''))
        {
         DB_query( "insert into perf_year_appraisal_detail (masterid,empid,app_detail_id,growth,revise_salary) value('".$last_id."','".$_REQUEST['empid'][$i]."','".$_REQUEST['appraisalid'][$i]."','".$_REQUEST['growth'][$i]."','".$_REQUEST['revise_sal'][$i]."') ",$db);  
        }
    }
    $s->pageLocation("index.php?Application=performance&modid=".$_REQUEST['modid']);  
}
?>
<script src="ui/jquery.ui.timepicker.js"></script>
<link rel="stylesheet" href="css/new_mxerp.css">
<style>
#showdata {
	display: block;
	background: url(Default.png) no-repeat;
	top: 0px;
	left: 0px;
	width: 300px;
	min-height: 60px;
	z-index:0;
}
#back {
	display: none;
	top: 0px;
	left: 0px;
	width: 300px;
	min-height: 60px;
	z-index:0;
}
.tab_div{font-size:12px;}
</style>
<div class="body_contain">
<div class="tab_div">
<h1>Yearly appraisal creation</h1>
<form method="post" action="">
    <table width="100%">
        <tr class="row1">
            <td width="13%" class="tabledata1" style="text-align:center;"><strong>Branch</strong></td>
            <td width="20%" class="tabledata1"><select name="branch"  style="width:80%;">
                    <option value="">Select</option>
	<?php $Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	$SiteRes=DB_query($Sitesql,$db);
        
	while($SiteRow=DB_fetch_array($SiteRes)){?>
            <option value="<?php  echo $SiteRow['loccode'];?>" <?php  echo $_REQUEST['site']==$SiteRow['loccode']?'selected':''?> >
            <?php  echo $SiteRow['locationname'];?>
            </option>
<?php }?>
          </select></td>
            <td width="13%" class="tabledata1" style="text-align:center;"><strong>Appraisal No.</strong></td>
            <td width="20%" class="tabledata1"><input type="text" style="width:80%;" readonly="true" value="Auto Generate" name="app_no" id="app_no" ></td>
            <td width="13%" class="tabledata1" style="text-align:center;"><strong>Date</strong></td>
            <td class="tabledata1"><input type="text" name="app_date" class="datepicker" id="app_date" style="width:80%;"></td>
        </tr>
		<tr class="row1">
            <td colspan="6">&nbsp;</td>
        </tr>
        <tr>
            <th>Emplyee Code</th>
            <th>Emplyee Name</th>
            <th>Net Salary</th>
           <th>Appraisal(%)</th>
            <th>Growth(%)</th>
            <th>Revise Salary</th>
        </tr>
        <?php 
        $dept=DB_query("select * from deptmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
        $i=1;
        while($deptres=DB_fetch_assoc($dept))
        { 
            $empsql=DB_query("select * from empmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and department='".$deptres['id']."' ",$db); 
            if(DB_num_rows($empsql)>0)
            { ?>
                <tr><td><strong><?php echo $deptres['deptname'] ?></strong></td></tr>
        <?php while($empres=DB_fetch_assoc($empsql))
                { $app_rate =0; ?>
                <tr>
                    <td><?php echo $empres['emp_code']; ?> <input type="hidden" value="<?php echo  $empres['id']; ?>" name="empid[<?php echo $i; ?>]"></td>
                    <td><?php echo  $empres['emp_name']; ?></td>
                    <td><?php echo $empres['net_sal']; ?></td>
     <?php
     if($empres['net_sal']!=''){
     $appp_master=DB_fetch_assoc(DB_query("SELECT * FROM perf_appraisal_master WHERE salary_from<=".$empres['net_sal']." AND salary_to>=".$empres['net_sal']." AND valid_from<='".date('Y-m-d')."' AND valid_to>='".date('Y-m-d')."' and companyid='".$_SESSION['ACGCompanyId']."' ",$db)); 
     }
     $grth=DB_fetch_assoc(DB_query("SELECT SUM(total_com)as tot,COUNT(*)as cnt FROM perf_task_assign a,perf_task_assign_details b WHERE a.user_id='".$empres['mst_www_userid']."' and a.id=b.task_id ",$db)) ;
     $avg_grth=$grth['tot']/$grth['cnt'];
    if($appp_master['id']!=''){  if($avg_grth!=''){ $app_rate=DB_fetch_assoc(DB_query("select * from perf_appraisal_master_details where masterid='".$appp_master['id']."' and growth_from<=".$avg_grth." and growth_to>=".$avg_grth." ",$db)); }
    
    $app_rate=$app_rate['appraisal'];}else{$app_rate=0;}
    
    if($empres['net_sal']>0){
    $last_sal=$empres['net_sal']+(($app_rate*$empres['net_sal'])/100);
    }
     ?>
        <td><input type="hidden" readonly name="appraisalid[<?php echo $i; ?>]"value="<?php echo $app_rate['id']; ?>"><?php echo $app_rate; ?></td>
        <td><input type="hidden" readonly name="growth[<?php echo $i; ?>]"value="<?php echo round($avg_grth,2); ?>"><?php echo round($avg_grth,2); ?></td>
        <td><input type="hidden" readonly name="revise_sal[<?php echo $i; ?>]"value="<?php echo $last_sal; ?>"><?php echo $last_sal; ?></td>        
        </tr>
        <?php $i++; } }  } ?>
        <tr>
            <td colspan="6" style="text-align: center"><button name="save" value="save" type="submit">Save</button></td>
        </tr>
    </table> 
    
</form>
   </div>
<script>
   
function ajax_getemployee_detail(str)
{  
  var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        //alert(xmlhttp.responseText);
       var data=xmlhttp.responseText.split("__");
       document.getElementById("emp_code").value=data[0];
       document.getElementById("emp_sal").value=data[1];
       document.getElementById("growth").value=data[2];
       document.getElementById("appraisal_rate").value=data[3];
       document.getElementById("revised_sal").value=data[4];
    }
  }
xmlhttp.open("POST","ajax_getemployee_detail.php?emp="+str.value,true);
xmlhttp.send();
}

</script>
