<?php 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='".$_REQUEST['frmid']."'",$db));
    $prefix1=$prefix['prefix_code'];
    
    $financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
      $sql="Select appraisal_no from hr_year_appraisal order by appraisal_no 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);
         if($prefix1!='')
            {
            $ExplBill=explode("/",$myrow['appraisal_no']);
            $maxval = $ExplBill[2];
            }
            else
            {
            $ExplBill=explode("/",$myrow['appraisal_no']);
            $maxval = $ExplBill[1];
            }           
    //$ExplBill=explode("/",$myrow['Reqno']);
    //$maxval = $ExplBill[3];
    //echo $req_part1;
    $req_final=$maxval+1;
    }else
    {
    $req_final="1";
    }
            if($prefix!='')
             $string=$prefix1."/".$financial."/";
             else
             $string=$financial."/";

  // $string=$prefix."/".$_REQUEST['sitecode']."/".$financial."/";
        $final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT); 
//echo $final_string;
  // die;   

 
    DB_query("insert into hr_year_appraisal (branch,appraisal_no,eff_fin_year,prepared,approved,created_by,created_date,CompanyId,apprisal_policy_master_detail_id) value('".$_REQUEST['branch']."','".$final_string."','".$_REQUEST['financial_year_id']."','".$_REQUEST['prepared']."','".$_REQUEST['approved']."','".$_SESSION['UserID']."','".date('Y-m-d')."','".$_SESSION['ACGCompanyId']."','".$_REQUEST['achivment']."')",$db);  
 $last_id=  DB_Last_Insert_ID($db, perf_year_appraisal, id);;
    for($i=0;$i<count($_REQUEST['empid']);$i++)
    {
        $empid=$_REQUEST['empid'][$i];
        if(isset($_REQUEST['appemp'][$empid]))
        {
          //  echo $empid;

        DB_query( "insert into hr_year_appraisal_detail (hr_year_appraisal_id,empid,hr_appraisal_master_details_id,growth,revise_salary) value('".$last_id."','".$_REQUEST['empid'][$i]."','".$_REQUEST['appemp'][$empid]."','".$_REQUEST['growth'][$i]."','".$_REQUEST['revise_sal'][$i]."') ",$db);  
        }
    }
    //die;
    //$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><?php echo $page_title; ?></h1>
<form method="post" action="" onsubmit="return validation() ;">
    <table width="100%" border="0" cellspacing="0" cellpadding="3">
        <tr class="row1">
            <td width="13%"><strong>Branch</strong></td>
            <td width="20%">
                <select name="branch" id="branch" style="width:98%;" onchange="removefinyear();">
                    <option value="0">--Select--</option>
                    	<?php $Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."') order by locationname";
                    	$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%"><strong>Appraisal No.</strong></td>
            <td width="20%"><input type="text" style="width:98%;" readonly="true" value="Auto Generate" name="app_no" id="app_no" ></td>
            <td width="13%"><strong>Effective Financial Year</strong></td>
            <td>
                <select name="financial_year_id" id="financial_year_id" style="width:98%; background:#f8f8f8; border:#878787 1px solid; height:24px;" onchange="get_rating_data(this.value);">
                  <option value=""> Select financial year</option>
                <?php $fyear="select * from company_financial_years";
                  $fyearRes=DB_query($fyear,$db); 
                  while($yearRow=DB_fetch_array($fyearRes)){?>?>
                  <option value="<?php echo $yearRow['id'];?>"><?php echo ConvertSQLDate($yearRow['year_start'])."-TO-".ConvertSQLDate($yearRow['year_end']); ?></option>
                <?php } ?>
                </select>           
            </td>
        </tr>
        <tr class="row1">
          <td><strong>Approved By</strong></td>
            <td><input type="text" style="width:98%;" name="approved" id="approved" value="">
                <!--<td><strong>Prepared By</strong></td>-->
            <input type="hidden" name="prepared" id="prepared" value="<?php echo  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'realname');   ?>"></td>
            <td><strong>Yearly Achivement</strong></td>
            <td>     
               <select name="achivment" id="achivment" style="width:98%;">
            <?php   $SQL1="select * from apprisal_policy_master_detail order by id ASC";
$result1=DB_query($SQL1,$db,$ErrMsg);
$i=0;
while($resrow1=DB_fetch_array($result1)) { ?>
                
                  <option value="<?php echo $resrow1['id'];?>">Over <?php echo number_format($resrow1['revenue_ach']);?>%</option>
                  <?php  } ?>
                 </select>
             <!-- <input type="text" name="prepared" id="prepared" style="width:80%;" value="<?php echo  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'realname');   ?>">--></td>
             <td colspan="2"></td>
        </tr>
  </table>
   <br>
          <div id ="employee_detail">

          </div>
        <p align="center"></p>
    
</form>
</div>
</div>
<script>
   function removefinyear(){
 //var num= document.getElementById("branch").value;
 document.getElementById("financial_year_id").value='';
 document.getElementById("financial_year_id").focus();
    //financial_year_id
}


 function validation()
 {

 var branch=document.getElementById("branch").value;
  var financial_year_id=document.getElementById("financial_year_id").value;
 

   if(branch=='')
      {
          alert("Please select Branch");
        
          document.getElementById("branch").focus();
          return false;
      }
     if(financial_year_id=='')
      {
          alert("Please select Effective financial Year");
        
          document.getElementById("financial_year_id").focus();
          return false;
      }

 }  



function get_rating_data(str)
{  
  //  alert(str);
 var branch=document.getElementById("branch").value;

  if(str=='' )
      {
          alert("Please Select Date ");          
          document.getElementById("financial_year_id").focus();
      }
     if(branch=='0'){
          alert("Please Select branch ");          
          document.getElementById("branch").focus();
     }
      else{
  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)
    {
    document.getElementById("employee_detail").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("POST","ajax_autorating_with_emp.php?num="+str+"&branch="+branch,true);
xmlhttp.send();
}
}


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>
