<?php session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
$title = _('Telephonic Order Memo');
include('includes/SQL_CommonFunctions.inc');
include('CRM/functions.php');
$s=new db_query();

if($_REQUEST['save']!='')
{
  
$_REQUEST['hr_incentive_master']['approved']=$_REQUEST['approved'];   
$_REQUEST['hr_incentive_master']['modified_by']=$_SESSION['UserID'];  
$_REQUEST['hr_incentive_master']['modified_date']=date('Y-m-d');
$_REQUEST['hr_incentive_master']['CompanyId']=$_SESSION['ACGCompanyId'];
$condd="id='".$_REQUEST['incentive_id']."'";
$s->RecordEdit('hr_incentive_master',$_REQUEST['hr_incentive_master'],$condd); 

$condition="hr_incetive_master_id='".$_REQUEST['incentive_id']."'";
$s->DeleteRecord(hr_incentive_master_detail,$condition);
 for($k=0;$k<count($_REQUEST['rate']);$k++)
      {
        if($_REQUEST['rate'][$k]!='')
        {
          foreach($_REQUEST['apr_id'][$_REQUEST['per_id'][$k]] as $key=>$value)  
           {

          $dataEmpSal['hr_incetive_master_id']=$_REQUEST['incentive_id'];
          $dataEmpSal['hr_appraisal_master_details_id']=$_REQUEST['per_id'][$k];
       // $dataEmpSal['over100']=$_REQUEST['over_100'][$k];
      //  $dataEmpSal['over110']=$_REQUEST['over_110'][$k];
       // $dataEmpSal['over120']=$_REQUEST['over_120'][$k];
        $dataEmpSal['apprisal_policy_master_detail_id']=$value;
        $dataEmpSal['result']=$_REQUEST['result'][$_REQUEST['per_id'][$k]][$key];      
        $s->RecordInsert('hr_incentive_master_detail',$dataEmpSal);
       // $s->RecordInsert('incentive_master_detail',$dataEmpSal);
       /* $dataEmpSal['incetive_master_id']=$_REQUEST['incentive_id'];
        $dataEmpSal['pref_app_mast_detail_id']=$_REQUEST['per_id'][$k];
        $dataEmpSal['over100']=$_REQUEST['over_100'][$k];
        $dataEmpSal['over110']=$_REQUEST['over_110'][$k];
        $dataEmpSal['over120']=$_REQUEST['over_120'][$k];
        $dataEmpSal['over140']=$_REQUEST['over_140'][$k];
        $s->RecordInsert('incentive_master_detail',$dataEmpSal);*/
        }
      }
      }

    $s->pageLocation("index.php?route=HRModule/Master/incentive_master".$rurl);
}
  $condition="id='".$_REQUEST['id']."'";
  $allready=$s->GetRecord('hr_incentive_master',$condition);
  $Select=DB_query($allready,$db);
  $RData=DB_fetch_array($Select);
 // print_r($RData);
  

?>
<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;
}
</style>
<div class="body_contain">
<div class="tab_div">
<h1>Incentive Master</h1>
<form method="post" action="">
   <table cellpadding="3" cellspacing="0" width="100%" border="0">
        <tr class="row1">
            <td width="13%"><strong>Branch</strong></td>
            <td width="35%"><input type="hidden" name="incentive_id" id="incentive_id" value="<?php echo $RData['id']; ?>">            
              <select name="branch" id="branch" disabled >
                <option value="0">All</option>
                <?php $locsql= DB_query("select * from locations where mst_companyid='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')",$db);
                while($locres=DB_fetch_array($locsql)) {  ?>
                <option value="<?php echo $locres['loccode']; ?>"<?php if($RData['branch']==$locres['loccode']) echo "selected";?>><?php echo $locres['locationname']; ?></option> 
                <?php  } ?>
                </select></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;" onchange="get_rating_data(this.value);" disabled>
              <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 if($RData['eff_fin_year']==$yearRow['id']){ echo "selected";} ?>><?php echo ConvertSQLDate($yearRow['year_start'])."-TO-".ConvertSQLDate($yearRow['year_end']); ?></option>
                <?php } ?>
              </select>           
            </td>
                              
        </tr>
        <tr class="row1">
            <td><strong>Staff Type</strong></td>
            <td><select name="Staff_type" id="Staff_type" disabled>
                    <option value="k" <?php if($RData['key_staff']=='k') echo "selected"; ?>>Key staff</option>
                    <option value="s" <?php if($RData['key_staff']=='s') echo "selected"; ?>>Support staff</option>
                    <option value="os" <?php if($RData['key_staff']=='os') echo "selected"; ?>>Other staff</option>
                    <option value="oss" <?php if($RData['key_staff']=='oss') echo "selected"; ?>>Other Support staff</option>
                </select></td>
                <!--<td class="tabledata1"><strong>Prepared By</strong></td>-->
            <td>              
              <input type="hidden" readonly name="prepared" id="prepared" value="<?php 
                if($RData['prepared']!='')
                {
                  echo $RData['prepared'];
                }else{
              echo  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'realname');
              }   ?>"></td>
            <td></td>
        </tr>
        <tr class="row1">
            <td><strong>Approved By</strong></td>
            <td><input type="text" name="approved" id="approved" value="<?php echo $RData['approved']; ?>"></td>
            <td colspan="2"></td>
        </tr>        
		</table>
<br>
      <div id="rating_data">      
		  <table width="100%" border="1" align="center" cellpadding="3" cellspacing="0" style="border:#808080 1px solid;">
    <tr>    
      <th width="10%" style="text-align:center;"><strong>Rating</strong></th>
      <th style="text-align:center;"><strong>Description</strong></th>
       <?php  $SQL1="select * from apprisal_policy_master_detail order by id ASC";
      $result1=DB_query($SQL1,$db,$ErrMsg);
      $j=0;
      while($resrow1=DB_fetch_array($result1)) {
        ?>
      <th width="10%" style="text-align:center;"><strong>over <?php echo number_format($resrow1['revenue_ach']);?>%</strong></th>
      <?php $j++;} ?>    
    </tr>
   <?php $incentive= DB_query("select * from hr_incentive_master_detail where hr_incetive_master_id='".$_REQUEST['id']."' order by id ",$db);
      $i=0;
          while($RDatas=DB_fetch_array($incentive)){
            $rating[]=$RDatas['hr_appraisal_master_details_id'];
            $result[$RDatas['hr_appraisal_master_details_id']][]=$RDatas['result'];
          }
          $rating=array_unique($rating);
          foreach($rating as $key=>$value)
          {
          ?>
          <tr>
            <td>
       <input type="hidden" name="per_id[<?php echo $i; ?>]" value="<?php echo $value; ?>">
      <input type="text"  readonly name="rate[<?php echo $i; ?>]" value="<?php echo $s->GetFieldValue('hr_appraisal_master_details',"id='".$value."'",'rating'); ?>"></td>
      <td><input type="text"  readonly name="rate[<?php echo $i; ?>]" value="<?php echo $s->GetFieldValue('hr_appraisal_master_details',"id='".$value."'",'rating_desc'); ?>"></td>


<?php $SQL2="select * from apprisal_policy_master_detail order by id ASC";
$result2=DB_query($SQL2,$db,$ErrMsg);
$j2=0;
while($resrow2=DB_fetch_array($result2)) { ?>
     <!--<td><input type="text" name="over_100[<?php echo $i; ?>]" value="<?php echo $RDatas['over100']; ?>"></td>
     <td><input type="text" name="over_110[<?php echo $i; ?>]" value="<?php echo $RDatas['over110']; ?>" ></td>
    <td><input type="text" name="over_120[<?php echo $i; ?>]" value="<?php echo $RDatas['over120']; ?>"></td>
  <td><input type="text" name="over_140[<?php echo $i; ?>]" value="<?php echo $RDatas['over140']; ?>"></td>-->
  <td><input type="hidden" name="apr_id[<?php echo $value; ?>][]" value="<?php echo $resrow2['id']?>">
   <input type="text" name="result[<?php echo $value;?>][]" value="<?php echo $result[$value][$j2]; ?>"></td>
   <?php  $j2++; } ?>
    </tr>
  <?php $i++;} ?>
      
</table>	
</div>
	
		  <p align="center">
        <button name="save" value="save" type="submit">Save</button> 
        &nbsp;<a href="index.php?route=TaskMgt/perf_appraisal_index<?php echo $rurl?>">
        <button name="cancel" value="" type="button" >Cancel</button></a></p>
</form>

</div>
</div>  


<script>
   
function generate_row()
{  
  var num= document.getElementById("app_num").value;
  if(num==0)
      {
          alert("Can not be zero");
          document.getElementById("app_num").value="";
          document.getElementById("app_num").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("all_data").innerHTML=xmlhttp.responseText;
    }
  }

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

 
function get_rating_data(str)
{  
 var branch=document.getElementById("branch").value;
  if(str=='' )
      {
          alert("Please Select Date ");
          
          document.getElementById("financial_year_id").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("rating_data").innerHTML=xmlhttp.responseText;
    }
  }

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


</script>
<SCRIPT language=Javascript>

       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
  function checkfrmsal(str)
  {
      
      var id=str.id;
      if(str.value==""){
          
            document.getElementById(id).value=0;
      }
      else
          {
              var val=parseFloat(str.value);
       document.getElementById(id).value=val.toFixed(2);       
          }
  }
  
  function checktosal(str)
  {
      var val=parseFloat(str.value);
      var id=str.id;
     var from=document.getElementById("fromsal").value;
     if(from!=""){
      if(str.value=='')
       document.getElementById(id).value=0;
      else
          {
              if(parseFloat(document.getElementById("fromsal").value)>=val)
                {
                  alert("From salary must be less than To Salary");
                    document.getElementById(id).value=0;    
                }  
                else
                {
                     document.getElementById(id).value=val.toFixed(2);
                }
          }
     }
     else
         {
            alert("Please enter From Salary First ");
            document.getElementById(id).value=0;
            document.getElementById("fromsal").focus();
         }
  }
    </SCRIPT>