<?php session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
$title = _('Telephonic Order Memo');
include('includes/SQL_CommonFunctions.inc');
include('CRM/functions.php');
include('includes/leave_fun.php');
$s=new db_query();
 $sql_app_count = DB_fetch_array(DB_query("SELECT no_of_assignee FROM conf_purch_approval WHERE doc_type='AA'",$db));
 $total_app = $sql_app_count['no_of_assignee'];
if($_REQUEST['save']!='')
{

for($i = 0; $i < count($_REQUEST['emp_id']); $i++)
  { 
    
        if(!empty($_REQUEST['emp_id']))
        {   
          $data1['emp_id'] = $_REQUEST['emp_id'];
          $data1['month']    =$_REQUEST['month'];
          $data1['year']=$_REQUEST['year'];  
          $data1['s_no']     = $_REQUEST['tab']+1;
          $data1['approved_by']  = $_SESSION['UserID'];
          $data1['approved_date']  = date('Y-m-d');
          
           
          $s->RecordInsert('hr_attendance_approval',$data1);
          echo "<pre/>";
          print_r($data1); 
         } 
          $sql_max_app = DB_fetch_array(DB_query("SELECT MAX(s_no) FROM hr_attendance_approval WHERE emp_id='".$_REQUEST['emp_id']."'",$db));
         // print_r($sql_max_app[0]);
          if($total_app == $sql_max_app[0])
    {
       $data['branch']   = $_REQUEST['branch'];
    $data['month']    =$_REQUEST['month'];
    $data['year']=$_REQUEST['year'];  
    $data['emp_type']=$_REQUEST['emp_type'];   
    $data['created_by']=$_SESSION['UserID'];  
    $data['created_date']=date('Y-m-d');
    $data['CompanyId']=$_SESSION['ACGCompanyId'];
    
   // $s->RecordInsert('hr_monthly_attendence_justify',$data); 
    
    $last_id=DB_Last_Insert_ID($db,'hr_monthly_attendence_justify','id');

    for($k=1;$k<=count($_REQUEST['emp_id']);$k++)
    {                     
        $dataEmpSal['hr_monthly_attendence_justify_id']=$last_id;
        $dataEmpSal['emp_id']=$_REQUEST['emp_id'][$k];      
        $dataEmpSal['working_days']=$_REQUEST['working_days'][$k];
        $dataEmpSal['salary_days']=$_REQUEST['salary_days'][$k];
        $dataEmpSal['deducted_days']=$_REQUEST['deducted_days'][$k];     
       // $s->RecordInsert('hr_monthly_attendence_justify_detail',$dataEmpSal);
    }
    }

   
  }
  die();
   // $s->pageLocation("index.php?route=HRModule/Master/monthly_attandance_justification".$rurl);
}


?>
<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">

<form method="post" action="" onsubmit="return validation() ;">
   <table cellpadding="3" cellspacing="0" width="100%" border="0">
        <tr class="row1">
            <td width="13%"><strong>Branch</strong></td>
            <td width="35%"><select name="branch" id="branch" onchange="getEmployee(this.value);">
                 <option value="0">Select</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($locres['loccode']==$_REQUEST['branch']){ echo "selected" ;} ?>><?php echo $locres['locationname']; ?></option> 
                 <?php  } ?>
                </select></td>
            <td width="13%"><strong>Month<?php $currentmonth= date('F'); ?></strong></td>
            <td>
              <select id="month" name="month" style="width:48%; float:left; margin-right:3px;">
              <?php
              $arr=array(1=>"Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
              foreach ($arr as $key => $value) {
              $p='';
              if($_REQUEST['month']!=''){
              if( $key==$_REQUEST['month'])
              $p='selected';
              }
              else{
              if($value==$currentmonth){
              $p='selected';  
              }
              }
              ?>
              <option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
 <?php } ?>
 </select><?php  $currentyear= date('Y'); ?>
            <select id="year" name="year" style="width:48%; float:left;"> 
            <?php 
            for($i=date('Y');$i>=2010;$i--)
            {
            $p='';
            if($_REQUEST['year']!=''){
            if( $i==$_REQUEST['year'])
            $p='selected';
            }
            else{
            if($i==$currentyear){
            $p='selected';  
            }
            }
           ?><option value="<?php echo $i;?>" <?php echo $p;?>><?php echo $i;?></option>
           <?php 
           }
           ?>
            </td>                              
        </tr>
        <tr class="row1">
            <td><strong>Employee Type</strong></td>
            <td><select name="emp_type" id="emp_type" onchange="getEmployee(this.value);">
                    <option value="All" <?php if($_REQUEST['emp_type']=='All') echo "selected";?>>All</option>
                    <option value="Permanent" <?php if($_REQUEST['emp_type']=='Permanent') echo "selected";?>>Permanent</option>  
                    <option value="Temporary" <?php if($_REQUEST['emp_type']=='Temporary') echo "selected";?>>Temporary</option>                          
                </select>               
            </td>
            <td><strong>Employee</strong></td>
            <td>
               <select name="emp" id="emp">
                  
                  <option value="All" <?php if($_REQUEST['emp']=='All'){ echo 'selected'; } ?>>All</option>  
                  <?php 
                    if($_REQUEST['branch']!='' && $_REQUEST['branch']!='0'){
                    $app_detail=DB_query("SELECT * FROM empmaster WHERE emp_site IN('".$_REQUEST['branch']."') order by emp_name",$db);
                    }
                    else{
                    $app_detail=DB_query("SELECT * FROM empmaster order by emp_name",$db); 
                    }
                   while($app_rateRow=DB_fetch_array($app_detail))
                   {?>
                    <option value="<?php echo $app_rateRow['id']; ?>" <?php if($_REQUEST['emp']==$app_rateRow['id']){ echo 'selected'; } ?>> <?php echo $app_rateRow['emp_name']; ?></option>
                     
                  <?php  } ?>                                
                </select>
            </td>
        <tr class="row1">
            <td colspan="4" style="text-align:center;">
                <button name="search" value="Search" type="submit">Search</button> 
            </td>
        </tr>
		</table>
<br>
<?php 
if(isset($_REQUEST['search']))
{
    $filter = array();
    $filter2 = array();

    if($_REQUEST['branch']!='0') 
    {
        $filter[]="emp_site='".$_REQUEST['branch']."'";
    }
    

    if($_REQUEST['emp_type']!='All') 
    {
        $filter[]="employment_type='".$_REQUEST['emp_type']."'";
    }
    if($_REQUEST['emp']!='All') 
    {
        $filter[]="id='".$_REQUEST['emp']."'";

    }

    if(count($filter)>0)
    {
        $filter1 = implode(" AND ",$filter);

        $sql = "SELECT * FROM empmaster WHERE 1=1 AND  $filter1";
    }
    else
    {
          $sql = "SELECT * FROM empmaster";
    }
<<<<<<< .mine
    echo  $sql;
=======
   // echo  $sql;
>>>>>>> .r547
    if($_REQUEST['month']!='') 
    {
        if($_REQUEST['month']<10)
        {
            $attendance_month = "0".$_REQUEST['month'];
        }
        else
        {
            $attendance_month =$_REQUEST['month'];
        }
        $filter2[]="attendance_month='".$attendance_month."'";
    }
    if($_REQUEST['year']!='') 
    {
        $filter2[]="attendance_year='".$_REQUEST['year']."'";
    }
    $filter3 = implode(" AND ", $filter2);
    
 ?>
    <div id="rating_data">    
          <table width="100%" border="1" align="center" cellpadding="3" cellspacing="0" style="border:#808080 1px solid;">
          <?php 
            
              $empresult=DB_query($sql,$db);
              if(DB_num_rows($empresult)>0)
              { 
                  $i=1;
                  ?>
                  <tr>    
                        <th width="10%"><strong>Code</strong></th>
                        <th><strong>Name</strong></th>
                        <th width="10%"><strong>Working Days</strong></th>
                        <th width="10%"><strong>Salaried Days</strong></th>
                        <th width="10%"><strong>Non Working Days</strong></th>     
                         <th width="10%"><strong>Preview</strong></th>     
                  </tr>
                  <?php 
                  while($empres=DB_fetch_array($empresult))
                  {

      $tab = $_REQUEST['tab']+1;
    if($_REQUEST['tab']=='0')
    {
      $sql_first_app=DB_query("SELECT id from hr_attendance_approval where emp_id='".$_REQUEST['emp_id']."' and s_no='".$tab."'",$db);
          
          $numrow2=DB_num_rows($sql_first_app);
            
          if($numrow2=='0')
          {
					  
                      $sql_1 = DB_fetch_array(DB_query("SELECT COUNT(*) AS total_attendance_day FROM daily_attendance WHERE employee='".$empres['id']."' AND $filter3",$db));

                      if($sql_1['total_attendance_day']>0)
                      { 
						 
						  // calcualte total and alllowed leave  and balance leave  as per rule
							leave_taken_log( $empres['id']  , $_REQUEST['year'] , $_REQUEST['month'] );   
						  //  pass emp_id , year , and month  
						   
                          $month= $_REQUEST['month'];
                          $year=$_REQUEST['year'];
                          $day=cal_days_in_month(CAL_GREGORIAN,$month,$year);

                          /*----------- get Salary Days---------------*/

                          $sql_salary_days = DB_fetch_array(DB_query("SELECT COUNT(*) AS total_salary_day FROM daily_attendance WHERE employee='".$empres['id']."' AND $filter3 AND pres_abs!='A'",$db));

                          $deducted_days = $day - $sql_salary_days['total_salary_day'];
                    ?>
                      <tr>   
                          <td><input type="hidden" name="emp_id[<?php echo $i; ?>]" id="emp_id_<?php echo $i;?>"  readonly value="<?php echo $empres['id'];?>">
                            <input type="text" name="code[<?php echo $i; ?>]" id="code_<?php echo $i;?>"  readonly value="<?php echo $empres['emp_code'];?>"></td>
                          <td><input type="text" name="name[<?php echo $i; ?>]" id="name_<?php echo $i; ?>" readonly value="<?php echo $empres['emp_name'];?>"></td>
                          <td><input type="text" name="working_days[<?php echo $i; ?>]" id="working_days_<?php echo $i; ?>" value="<?php echo $day; ?>" readonly ></td>
                          <td><input type="text" name="salary_days[<?php echo $i; ?>]"  id="salary_days_<?php echo $i;?>" value="<?php echo $sql_salary_days['total_salary_day']; ?>"  readonly='' ></td>
                          <td><input type="text" name="deducted_days[<?php echo $i; ?>]"  id="deducted_days_<?php echo $i;?>" value="<?php echo $deducted_days ?>" readonly ></td>

                          <td><a href="index.php?route=HRModule/SalaryProcess_preview<?php echo $rurl?>&emp_id=<?php echo $empres['id'];?>&month=<?php echo $_REQUEST['month'] ?>&year=<?php echo $_REQUEST['year'] ?>&salary_days=<?php echo $sql_salary_days['total_salary_day']; ?>" target='_blank'>Preview</a></td>

                      </tr>  
                      <?php
                      $i++;
                      }
    }
    else
    {
      $tab = $_REQUEST['tab']+1;
      $tab1 = $tab+1;
      $sql_prev_app=DB_query("SELECT id from hr_attendance_approval where emp_id='".$empres['id']."'  and s_no='".$_REQUEST['tab']."'",$db);

      $sql_curr_app=DB_query("SELECT id from hr_attendance_approval where emp_id='".$empres['id']."' and s_no='".$tab."'",$db);

      $sql_next_app=DB_query("SELECT id from hr_attendance_approval where emp_id='".$empres['id']."'  and s_no='".$tab1."'",$db);
          
          $numrow1=DB_num_rows($sql_prev_app);
          $numrow2=DB_num_rows($sql_curr_app);
          $numrow3=DB_num_rows($sql_next_app);
              
            //echo $numrow1."----".$numrow2."----".$numrow3."<br>";
          if($numrow1!='0' && $numrow2=='0' && $numrow3=='0')
          {
            ?><tr>   
                          <td><input type="hidden" name="emp_id[<?php echo $i; ?>]" id="emp_id_<?php echo $i;?>"  readonly value="<?php echo $empres['id'];?>">
                            <input type="text" name="code[<?php echo $i; ?>]" id="code_<?php echo $i;?>"  readonly value="<?php echo $empres['emp_code'];?>"></td>
                          <td><input type="text" name="name[<?php echo $i; ?>]" id="name_<?php echo $i; ?>" readonly value="<?php echo $empres['emp_name'];?>"></td>
                          <td><input type="text" name="working_days[<?php echo $i; ?>]" id="working_days_<?php echo $i; ?>" value="<?php echo $day; ?>" readonly ></td>
                          <td><input type="text" name="salary_days[<?php echo $i; ?>]"  id="salary_days_<?php echo $i;?>" value="<?php echo $sql_salary_days['total_salary_day']; ?>"  readonly='' ></td>
                          <td><input type="text" name="deducted_days[<?php echo $i; ?>]"  id="deducted_days_<?php echo $i;?>" value="<?php echo $deducted_days ?>" readonly ></td>

                          <td><a href="index.php?route=HRModule/SalaryProcess_preview<?php echo $rurl?>&emp_id=<?php echo $empres['id'];?>&month=<?php echo $_REQUEST['month'] ?>&year=<?php echo $_REQUEST['year'] ?>&salary_days=<?php echo $sql_salary_days['total_salary_day']; ?>" target='_blank'>Preview</a></td>

                      </tr>  
            
                      <?php 
                      $i++;
                      }
    }
                      
                  } }  ?>
          </table>  
          <p align="center"><button name="save" value="save" type="submit">Save</button> 
         <a href="index.php?route=HRModule/Master/monthly_attandance_justification<?php echo $rurl?>"><button name="cancel" value="" type="button" >Cancel</button></a></p>
      </form>
    </div>
<?php   }
else {?> 
        <p align="center">Data not found </p>
        
<?php } 
           } ?>
</div>
</div>

<script>
   function checkvalue(value,id)
 {
    var work_day=parseFloat(document.getElementById("working_days_"+id).value);
    var sal_day=parseFloat(document.getElementById("salary_days_"+id).value);
    if(sal_day<=work_day){
      deducat_day=(work_day-sal_day);
      document.getElementById("deducted_days_"+id).value=deducat_day;
      
    }
    else{
      alert("Please enter currect salary days");
      document.getElementById("deducted_days_"+id).value='';
      document.getElementById("salary_days_"+id).value='';
    }


 }
 

 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 getEmployee(branch){
var dataString='&branch='+branch;
if(branch==''){
//alert('empty');
}
else{
$.ajax({
type:'POST',
url:'ajax_getEmployee.php',
data:dataString,
cache:false,
success:function(result){
$('#emp').html(result); 
}
});
}
}
 
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();
}
}



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

          return true;
       }
 
 
</script>
<script>
function getEmployee(){
var emptype=$('#emp_type').val();
var branch=$('#branch').val();
var dataString='&branch='+branch + '&emptype=' + emptype;
if(branch==''){
//alert('empty');
}
else{
$.ajax({
type:'POST',
url:'ajax_getEmpTypeWise.php',
data:dataString,
cache:false,
success:function(result){
$('#emp').html(result); 
}
});
}
} 
</script>
