<!-- <link rel="stylesheet" type="text/css" href="css/table_frize_1.css"> -->
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name");?></h1>
<?php 
//unset($_SESSION['employee_code']);
//error_reporting(0);
include('./includes/fun-php.php');
include('./includes/fun-HRClass.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$target_rurl="index.php?route=HRModule/SalaryProcess_new&modid=".$modid."&frmid=".$frmid."";
$rurl_view="index.php?route=View_master_payslip&action=View&modid=".$modid."&frmid=".$frmid."";

$apprisal_rs = DB_fetch_array(DB_query("SELECT apprisal_type FROM apprisal_policy_master",$db));

 $arr=array(1=>"January","February","March","April","May","June","July","August","September","October","November","December");
//if($_REQUEST['paymonth']==date("n") && isset($_REQUEST['Search'])){?>
  <script>//alert("You not process current month salary.");</script>
  <script>
function chk_employee()
{
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);
    document.getElementById("employee_code").innerHTML=xmlhttp.responseText;
  }
  }
  var site=document.getElementById("site").value;
  xmlhttp.open("GET","check_data_hr.php?section=chk_employee_site&site="+site,true);
  xmlhttp.send();
}
</script>
<?php
/*if($_REQUEST['UnProcess'])
{
  for($i=0;$i<count($_REQUEST['frmnamerole']);$i++)
  {
       DB_query("DELETE FROM payment_revised_details WHERE id ='".$_REQUEST['adIdde'][$i]."'",$db);
     $main_rec=DB_fetch_array(DB_query("SELECT id FROM payment_revised_details where payment_revised_id='".$_REQUEST['adId'][$i]."'",$db));
     if($main_rec['id']!=''){
     }else{
             DB_query("DELETE FROM payment_revised WHERE id ='".$_REQUEST['adId'][$i]."'",$db);
     }
  }
}*/

if($_REQUEST['Save'])
{

    
  $flag = 0;
  $_REQUEST['Search']=1; 
           $prefix=$s->getField('companies','CompanyId',$_SESSION['ACGCompanyId'],'company_code');
         $prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$_REQUEST['frmid']."'",$db);
       $prefix_row=DB_fetch_array($prefix_sql);
       $prefix1=$prefix_row['prefix_code']; 
       $financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
       $sql="Select arrear_sheet_no from hr_arrear_sheet where INSTR(`arrear_sheet_no`, '$financial') > 0 order by id desc limit 0,1";
       $result=DB_query($sql,$db);
       $myrow=DB_fetch_array($result);
  
        if(DB_num_rows($result)>0)
        {
        
            if($prefix!='')
            {
            $ExplBill=explode("/",$myrow['arrear_sheet_no']);
            $maxval = $ExplBill[4];
              }
              else
              {
              $ExplBill=explode("/",$myrow['arrear_sheet_no']);
            $maxval = $ExplBill[3];
              }   
               $req_final=$maxval+1;
        
        }
        else
         { $req_final="1";  }

    if($prefix!='')
    $string=$prefix."/".$financial."/".$prefix1."/";
      else
  $string=$financial."/".$prefix1."/";  
  $final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT); 
  $data['arrear_sheet_no'] =$final_string;
  $data['arrear_sheet_date'] =FormatDateForSQL($_REQUEST['arrear_sheet_date']);
  $data['paymonth'] =$_REQUEST['paymonth'];
  $data['payyear'] = date('Y');
  $data['CompanyId'] =$_SESSION['ACGCompanyId'];
  $data['created_by'] = $_SESSION['UserID'];
  $data['created_date'] = date('Y-m-d');
  
  $s->RecordInsert('hr_arrear_sheet', $data);
  $last_id=DB_Last_Insert_ID($db,'hr_arrear_sheet','id');
  if($last_id!='')
  {
  for($i=0;$i<count($_REQUEST['frmnamerole']);$i++)
   {
 $emp_query = "SELECT A.id FROM hr_arrear_sheet AS A INNER JOIN hr_arrear_sheet_details AS B ON A.id=B.arrear_sheet_id WHERE B.emp_id='".$_REQUEST['frmnamerole'][$i]."' AND A.payyear='".date('Y')."'";
      $emp_result = DB_query($emp_query,$db) or die(DB_error());
      if(DB_num_rows($emp_result)>0){
      }
      else
      { 
      $dataArr['arrear_sheet_id'] =$last_id;
      $dataArr['emp_id'] =$_REQUEST['frmnamerole'][$i];
      $dataArr['fm_month'] =  $_REQUEST['hid_fm_month'][$i];
      $dataArr['to_month'] =  $_REQUEST['hid_to_month'][$i];
      $dataArr['apprisal_amt'] =  $_REQUEST['hid_arrear_amount'][$i];
      $dataArr['arrear_amount'] =  $_REQUEST['hid_tot_arrear_amount'][$i];
      $dataArr['paid_status'] =  'Y';
      $s->RecordInsert('hr_arrear_sheet_details', $dataArr);
      }
    } 
  }
  $target_rurl="index.php?route=HRModule/ArrearSheet_index&modid=".$modid."&frmid=".$frmid."";
  $s->PageLocation($target_rurl);
}
?>
<form action="" method="post" name="frm" id="frm">
  <table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="12%" align="right">Site</td>
    <td width="20%"><select name="site" id="site"  onclick="chk_employee();">
      <option value="000">---ALL---</option>
      <?php $site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
      while($site_row=DB_fetch_array($site_sql)){?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'');?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
    <td width="12%" align="right">Arrear Sheet No.</td>
    <td width="20%"><input type="text" name="arr_sheet_no" value="Auto Number" readonly="" ></td>
    <td width="12%" align="right">Date</td>
    <td><input type="text" name="arrear_sheet_date" value="<?php echo ($_REQUEST['bk_advice_date']!=''?$_REQUEST['bk_advice_date']:date('d/m/Y'))?>" readonly="" class="datepicker" >
    </td>
  </tr>
  <tr class="row1">
    <td align="right">From Month</td>
    <td><select name="fm_month" id="fm_month"  >
    <?php
        if($apprisal_rs['apprisal_type']=='y'){
          $arr1=array(1=>"January");
   }
   if($apprisal_rs['apprisal_type']=='f'){
         $arr1=array(4=>"April");
   }    if($arr1){
        foreach ($arr1 as $key => $value) {
        $p='';
        if( $key==$_REQUEST['fm_month'])
        $p='selected';?>
      <option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
    <?php } }?>
    </select></td>
    <td align="right">To Month</td>
    <td><select name="to_month" id="to_month"  >
    <!--<option value="">--Select--</option>-->
    <?php if($apprisal_rs['apprisal_type']=='y'){ $arr2=array(12=>"December");}
          if($apprisal_rs['apprisal_type']=='f'){ $arr2=array(3=>"March");}
        if($arr2){
        foreach ($arr2 as $key => $value) {
        $p='';
        if( $key==$_REQUEST['to_month'])
        $p='selected';?>
      <option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
    <?php } } ?>
    </select></td>
  <td align="right">Paymonth</td>
    <td>
    <select name="paymonth" id="paymonth"  >
    <?php 
   
        foreach ($arr as $key => $value) {
        $p='';
        if( $key==$_REQUEST['paymonth'])
        $p='selected';?>
      <option value="<?php echo $key;?>" <?php echo $p;?> ><?php echo $value;?></option>
    <?php } ?>
    </select></td>
  </tr>
  <tr class="row1">
    <td colspan="6" align="center" valign="top" style="text-align:center;"><input type="submit" name="Search" class="search" value="Search" onclick="return chksalprocess()"/></td>
    </tr>
</table>
<?php if($_REQUEST['Search']){
  if($_REQUEST['site']!='000')
  $filter[]="B.emp_site = '".$_REQUEST['site']."'";
  if($filter!='')
  {
  $filter=implode(' and ',$filter);
  }
  else
  {
  $filter="1=1";
  }
?>
<br>
<input type="hidden" name="month" value="<?php echo $_REQUEST['paymonth']; ?>" />
<input type="hidden" name="year" value="<?php echo $_REQUEST['payyear']; ?>" />
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th width="5%"><input type="checkbox" name="assigned" id="assigned_site" value="0" onclick="FProfileMaster(document.frm.frmnamerole)">All</th>
    <th width="8%">Employee Code</th>
    <th>Employee Name</th>
    <th width="10%">Designation</th>
    <?php if($apprisal_rs['apprisal_type']=='y'){
    $arrM=array(1=>"January","February","March","April","May","June","July","August","September","October","November","December");
    }else{
    $arrM=array(4=>"April",5=>"May",6=>"June",7=>"July",8=>"August",9=>"September",10=>"October",11=>"November",12=>"December",1=>"January",2=>"February",3=>"March",);
   }
    if(!empty($arrM)){
    foreach ($arrM as $key => $value) { 
      ?>
    <th width="5%"><?php echo $value;?></th>
      <?php $p++;} }?>
    <th width="10%">Total</th>
    <th width="8%">Paid Status</th>
  </tr>
  <?php 
  $i=0;
  
  $sql=DB_query("SELECT A.gross_sal AS cur_gross,A.applicable_month ,B.emp_code,B.id,B.designation,B.emp_name,B.gross_sal AS prev_gross FROM empmaster_revised_sal AS  A 
    INNER JOIN empmaster AS B ON A.empid=B.id where $filter AND A.log_type='revise' AND A.applicable_year='".date('Y')."'",$db);
  if(DB_num_rows($sql)>0){
  while($row=DB_fetch_array($sql))
  { $flag=0;
   
  $record = DB_fetch_array(DB_query("SELECT A.id FROM hr_arrear_sheet AS A INNER JOIN hr_arrear_sheet_details AS B ON A.id=B.arrear_sheet_id WHERE B.emp_id='".$row['id']."' 
    AND A.payyear='".date('Y')."'",$db));
  if($record){
   $flag=1;
  }else{
    $flag=0;
  }
  $basic_tot='';
  if(!$flag){
  ?>  
  <tr>
    <td><input type="checkbox" name="frmnamerole[]" id="frmname_<?php echo $i; ?>"  value="<?php echo $row['id']; ?>"  /></td>
    <td><?php echo $row['emp_code'];?></td>
    <td><?php  echo $row['emp_name'];?></td>
    <td><?php echo $s->getField('desigmaster','id',$row['designation'],'designame');?></td>
    <?php $j=0;
    if(!empty($arrM)){
    foreach ($arrM as $key => $value) { 
      if($key>=$_REQUEST['fm_month'] && $key<$_REQUEST['paymonth']){
      $arrear_amount=$row['cur_gross']-$row['prev_gross'];
      }else{
      $arrear_amount=0;
      }
      ?>
      <td>
      <?php echo $arrear_amount;
      $basic_tot[]=$arrear_amount; 
      $basic[$key][]=$arrear_amount;?>
      </td>
      <?php $j++;} }?>
      <td><?php if(!empty($basic_tot)){ echo $arrear_amt=$arrear_amt_tot[]=array_sum($basic_tot);}else{ echo $arrear_amt=0;} ?>
        <input type="hidden" name="hid_tot_arrear_amount[]" value="<?php echo $arrear_amt;?>">
        <input type="hidden" name="hid_fm_month[]" value="<?php echo $f=$_REQUEST['fm_month'];?>">
        <input type="hidden" name="hid_to_month[]" value="<?php echo $t=($row['applicable_month']-1);?>">
        <input type="hidden" name="hid_arrear_amount[]" value="<?php echo round($arrear_amt/($t-$f+1),2);?>"></td>
      <td><?php $Locked=DB_fetch_array(DB_query("SELECT A.id,B.paid_status,B.id AS cid FROM hr_arrear_sheet AS A INNER JOIN hr_arrear_sheet_details AS B ON A.id=B.arrear_sheet_id WHERE A.payyear='".date('Y')."' and B.emp_id='".$row['id']."'",$db)); echo ($Locked['paid_status']==''?'N':'Y');?></td>
    </tr>
 <?php $i++; } }?>
 <?php if($i!=0){?>
 <tr bgcolor="#fff"><td colspan="4" align="right"><strong>Total :</strong></td>
 <?php if(!empty($arrM)){
    foreach ($arrM as $key => $value) { ?>
     <td><strong><?php echo array_sum($basic[$key])?></strong></td>
     <?php }}?>
     <td><strong><?php echo array_sum($arrear_amt_tot)?></strong></td>
     <td>&nbsp;</td>
     </tr>
  <tr>
  <td style="text-align:center;" colspan="<?php echo $p+7?>">
  <input type="submit" name="Save" class="search"  value="Process Arrear"  />
   <!--<input type="submit" name="UnProcess" class="search"  value="UnProcess Arrear"/>-->
   </td>
</tr>
<?php }else{ echo "<tr class='row1'><td style='text-align:center;'' class='tabledata1' colspan='18'> Sorry !! No Record Found.</td></tr>"; } ?>
 <?php }else{?>
 <tr><td style="text-align:center;" colspan="<?php echo $p+7?>">No Data Found !!</td></tr>
<?php } ?>
 
</table>

</form>


  
  <?php }   ?>
  </div></div>
<script language="javascript">
function FProfileMaster(str)
{
 //alert(str);
   myCheckboxArray = document.frm.elements["frmnamerole[]"];
  var t=myCheckboxArray.length;
  
  if(document.getElementById('assigned_site').checked==true)
  { 
  
  if(t==undefined)  
  document.getElementById('frmname_'+0).checked=true;
  for (var i = 0; i <=t; i++)
  {
  document.getElementById('frmname_'+i).checked=true; 
  } 
  }
  else
  {
  if(t==undefined)  
  document.getElementById('frmname_'+0).checked=false;
  for (var i = 1; i <= t; i++)
  {
  document.getElementById('frmname_'+i).checked=false;  
  }
  }
}
</script>
</div>
</div>