<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

// Common Information

$page_title="Generate Payroll";
$page="71_employee_03.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_info';	
$unique='emp_code';

// End Common Information



//   if (isset($_POST['run_data'])) { 
//        
//         header("location:".$page."");
//    }



?>




<!DOCTYPE html>
<html lang="en">

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <script>



function getXMLHTTP() { //fuction to return the xml http object



		var xmlhttp=false;	



		try{



			xmlhttp=new XMLHttpRequest();



		}



		catch(e)	{		



			try{			



				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch(e){



				try{



				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");



				}



				catch(e1){



					xmlhttp=false;



				}



			}



		}



		 	



		return xmlhttp;



    }



	function update_value(id)
	{

var emp_code=id; // Rent
var mon=(document.getElementById('mon').value); 
var year=(document.getElementById('year').value)*1; 
var td=(document.getElementById('td_'+id).value)*1;
var hd=(document.getElementById('hd_'+id).value)*1;
var wd=(document.getElementById('wd_'+id).value)*1;
var lv=(document.getElementById('lv_'+id).value)*1; 
var lwp=(document.getElementById('lwp_'+id).value)*1; 
var ab=(document.getElementById('ab_'+id).value)*1; 
var att=(document.getElementById('att_'+id).value)*1; 
var lt=(document.getElementById('lt_'+id).value)*1; 
var ot=(document.getElementById('ot_'+id).value)*1; 
var advance=(document.getElementById('advance_'+id).value)*1;
var deduction=(document.getElementById('deduction_'+id).value)*1;
var benefits=(document.getElementById('benefits_'+id).value)*1; 

var flag=(document.getElementById('flag_'+id).value); 

var strURL="payroll_process_ajax.php?emp_code="+emp_code+"&mon="+mon+"&year="+year+"&td="+td+"&hd="+hd+"&wd="+wd+"&lv="+lv+"&lwp="+lwp+"&ab="+ab+"&att="+att+"&lt="+lt+"&ot="+ot
+"&advance="+advance+"&deduction="+deduction+"&benefits="+benefits+"&flag="+flag;



		var req = getXMLHTTP();



		if (req) {



			req.onreadystatechange = function() {

				if (req.readyState == 4) {

					// only if "OK"

					if (req.status == 200) {						

						document.getElementById('divi_'+id).style.display='inline';

						document.getElementById('divi_'+id).innerHTML=req.responseText;						

					} else {

						alert("There was a problem while using XMLHTTP:\n" + req.statusText);

					}

				}				

			}

			req.open("GET", strURL, true);

			req.send(null);

		}	



}


function cal_all(id)

	{
var emp_code=id; // Rent
var td=Math.round((document.getElementById('td_'+id).value)*1);
var hd=(document.getElementById('hd_'+id).value)*1;
//var wd=(document.getElementById('wd_'+id).value)=(td-hd);
var wd=Math.round((document.getElementById('wd_'+id).value)*1);
var lv=(document.getElementById('lv_'+id).value)*1; 
var lwp=(document.getElementById('lwp_'+id).value)*1; 
var ab=Math.round((document.getElementById('ab_'+id).value)*1);

//var lt=parseInt((document.getElementById('lt_'+id).value)=(ltp+erp));

var att=(document.getElementById('att_'+id).value)=Math.round((wd-(lv+lwp+ab)));

//document.getElementById('pay_'+id).value=pay;
//document.getElementById('pre_'+id).value=pre;
	}



</script>

<body>

  <!-- ======= Header ======= -->
  <?php 
  require_once('inc_files/inc_header.php');
  ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">HR Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>
            <form class="row g-3" action="" method="post" name="codz" id="codz">
   		       
			
	<table width="80%" border="0" align="center">
      
      
        <tr height="50">
          <td align="right">Month:&nbsp;&nbsp;</td>
          <td align="left">
		  <select id="mon" name="mon"  class="form-select"  required  >
		  <option></option>
			
			<?php foreign_relation($conn,'month','month_id','month_name',$_POST['mon'],'1 order by month_id');?>
        	</select>		  </td>
          <td align="right">Department:&nbsp;&nbsp;</td>
          <td width="18%">
		  <select id="department" name="department"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'department','id','department',$_POST['department'],'1 order by id');?>
        	</select>		  </td>
          <td width="11%" align="right">Job Status:&nbsp;&nbsp;</td>
          <td width="18%">
		  <select id="job_status" name="job_status"  class="form-select" required>
		 
			<?php foreign_relation($conn,'job_status','id','job_status',$_POST['job_status'],'1 order by id');?>
        	</select></td>
          <td width="16%" rowspan="6" align="center" ><strong>
            &nbsp;&nbsp;&nbsp;
            <button type="submit" name="run_data" id="run_data" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
          </strong></td>
        </tr>
        <tr height="50">
        <td width="11%" align="right">Year:&nbsp;&nbsp;</td>
        <td width="12%" align="left">
		<select id="year" name="year"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'year','year','year',$_POST['year'],'1 order by year');?>
        	</select>		</td>
        <td width="14%" align="right">Designation:&nbsp;&nbsp;</td>
        <td>
		<select id="designation" name="designation"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'designation','id','designation',$_POST['designation'],'1 order by id');?>
        	</select>
			</td>
        <td align="right">EMP NAME:&nbsp;&nbsp;</td>
        <td>
		 <?php
			 auto_complete_from_db_sql($conn,'SELECT concat(emp_code," :: ",emp_name), concat(emp_code," :: ",emp_name) FROM employee_info
			WHERE 1 order by emp_code','employee_name');
		?>
                  <input type="text" class="form-control" id="employee_name" name="employee_name" value="<?php echo $_REQUEST['employee_name']?>" >	
				  
				<textarea name="all_h_date"  style="display:none;" ><?php 
				    $s_date = ''.$_POST['year'].'-'.$_POST['mon'].'-01';
					$e_date = date("Y-m-t", strtotime($s_date));
		 $o=0;
		  $hd_sql = "select h_date from hrm_holiday where h_date BETWEEN '".$s_date."' AND '".$e_date."'  group by h_date";
			$hd_query=mysqli_query($conn,$hd_sql);
			while($hd_data= mysqli_fetch_object($hd_query)){
			$o++;
			if ($o>1) echo ', ';
			echo '"'.$hd_data->h_date.'"';}
				   ?></textarea>   
				   
				   	
		</td>
        </tr>
    </table>
               
  </form>
  <br>
	
              <!-- Table with stripped rows -->
              <table width="100%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="8%" scope="col">EMP Code</th>
                    <th width="20%" scope="col">EMP Name</th>
                    <th width="5%" scope="col"><div align="center">TD</div></th>
                    <th width="5%" scope="col"><div align="center">HD</div></th>
                    <th width="5%" scope="col"><div align="center">WD</div></th>
                    <th width="5%" scope="col"><div align="center">LV</div></th>
                    <th width="5%" scope="col"><div align="center">LWP</div></th>
                    <th width="5%" scope="col"><div align="center">AB</div></th>
                    <th width="5%" scope="col"><div align="center">Att</div></th>
                    <th width="5%" scope="col"><div align="center">LT</div></th>
                    <th width="5%" scope="col"><div align="center">OT</div></th>
                    <th width="8%" scope="col">Advance</th>
                    <th width="8%" scope="col">Deduction</th>
                    <th width="8%" scope="col">Benefits</th>
                    <th width="8%" scope="col">Action</th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
					
 $employee_name = $_REQUEST['employee_name'];
  $employee=explode(' :: ',$employee_name);
   $employee_code=$employee[0];
  
if($employee_code!=''){
$employee_con=" and emp_code='".$employee_code."' ";
}

if($_POST['department']!=''){
$department_con=" and department='".$_POST['department']."' ";
}

if($_POST['designation']!=''){
$designation_con=" and designation='".$_POST['designation']."' ";
}

if($_POST['job_status']!=''){
$job_status_con=" and job_status='".$_POST['job_status']."' ";
}

		//if($_POST['all_h_date']!='') {
//		 $h_date_con .= ' and lv_date in ('.$_POST['all_h_date'].') ';
//		}

					
			



$start_date = ''.$_POST['year'].'-'.$_POST['mon'].'-01';
$end_date = date("Y-m-t", strtotime($start_date));

$fdate = strtotime($start_date);
$tdate = strtotime($end_date);
$datediff = $tdate - $fdate;

$day_count=$datediff / (60 * 60 * 24)+1;


					

		
	
		 $sql = "select count(id) as holiday from hrm_holiday where h_date BETWEEN '".$start_date."' AND '".$end_date."' ";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $hd_day=$info->holiday;
		}	
		
		 $sql = "select emp_code, count(id) as att_day, sum(grace_no) as grace_no from employee_daily_attendance where att_date BETWEEN '".$start_date."' AND '".$end_date."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $att_day[$info->emp_code]=$info->att_day;
		 $grace_no[$info->emp_code]=$info->grace_no;
		}			
					
	$sql = "select emp_code, count(id) as lv_day from leave_details where leave_type!=4 and lv_date BETWEEN '".$start_date."' AND '".$end_date."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $lv_day[$info->emp_code]=$info->lv_day;
		}
		
	$sql = "select emp_code, count(id) as lwp_day from leave_details where leave_type=4 and lv_date BETWEEN '".$start_date."' AND '".$end_date."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $lwp_day[$info->emp_code]=$info->lwp_day;
		}
		
	  $sql = "select b.emp_code, count(b.id) as hlv_day from hrm_holiday a, leave_details b 
	 where  a.h_date=b.lv_date and b.lv_date BETWEEN '".$start_date."' AND '".$end_date."' GROUP by b.emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $hlv_day[$info->emp_code]=$info->hlv_day;
		}
		
		 $sql = "select emp_code,sum(monthly_amt) as advance_amt from employee_advance where mon='".$_POST['mon']."' and year='".$_POST['year']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $advance_amt[$info->emp_code]=$info->advance_amt;
		}
		
		 $sql = "select emp_code,sum(monthly_amt) as monthly_cls_amt from employee_advance_closing
		  where mon='".$_POST['mon']."' and year='".$_POST['year']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $monthly_cls_amt[$info->emp_code]=$info->monthly_cls_amt;
		}
		
		
		 $sql = "select emp_code, emp_code, td, hd, wd, lv, lwp, ab, att, lt, ot, benefits, deduction  from employee_monthly_salary where mon='".$_POST['mon']."' and year='".$_POST['year']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $emp_code[$info->emp_code]=$info->emp_code;
		 $td[$info->emp_code]=$info->td;
		 $hd[$info->emp_code]=$info->hd;
		 $wd[$info->emp_code]=$info->wd;
		 $lv[$info->emp_code]=$info->lv;
		 $lwp[$info->emp_code]=$info->lwp;
		 $ab[$info->emp_code]=$info->ab;
		 $att[$info->emp_code]=$info->att;
		 $lt[$info->emp_code]=$info->lt;
		 $ot[$info->emp_code]=$info->ot;
		 $benefits[$info->emp_code]=$info->benefits;
		 $deduction[$info->emp_code]=$info->deduction;
		}
		
			
					    $sql = 'select * from '.$table.' where gross_salary>0 '.$employee_con.$designation_con.$department_con.$job_status_con.' ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
	<?php if(isset($_POST['run_data'])){?>		
      <tr>
      <td><?php echo $data->emp_code;?></td>
      <td><?php echo $data->emp_name;?></td>
      <td><input class="form-control" name="td_<?php echo $data->emp_code?>" id="td_<?php echo $data->emp_code?>" type="text"  value="<?php echo round($day_count);?>" style="width:100%; height:30px;" readonly="" onKeyUp="cal_all(<?php echo $data->emp_code?>)" /></td>
      <td><input class="form-control" name="hd_<?php echo $data->emp_code?>" id="hd_<?php echo $data->emp_code?>" type="text"  
	  value="<?php if($emp_code[$data->emp_code]>0){ echo $hd[$data->emp_code]; } else { echo $holiday=$hd_day-$hlv_day[$data->emp_code];} ?>" style="width:100%; height:30px;" 
 onKeyUp="cal_all(<?php echo $data->emp_code?>)" readonly="" />	  </td>
      <td><input class="form-control" name="wd_<?php echo $data->emp_code?>" id="wd_<?php echo $data->emp_code?>" type="text"  
	  value="<?php if($emp_code[$data->emp_code]>0){ echo round($wd[$data->emp_code]); } else { echo round($work_day=$day_count-$holiday);} ?>" style="width:100%; height:30px;" readonly="" onKeyUp="cal_all(<?php echo $data->emp_code?>)" /></td>
      <td><input class="form-control" name="lv_<?php echo $data->emp_code?>" id="lv_<?php echo $data->emp_code?>" type="text"  value="<?php echo $lv_day[$data->emp_code];?>" style="width:100%; height:30px;" onKeyUp="cal_all(<?php echo $data->emp_code?>)" readonly="" /></td>
      <td><input class="form-control" name="lwp_<?php echo $data->emp_code?>" id="lwp_<?php echo $data->emp_code?>" type="text" value="<?php echo $lwp_day[$data->emp_code];?>" style="width:100%; height:30px;" onKeyUp="cal_all(<?php echo $data->emp_code?>)" readonly="" /></td>
      <td><input class="form-control" name="ab_<?php echo  $data->emp_code?>" id="ab_<?php echo $data->emp_code?>" type="text"  
	  value="<?php if($emp_code[$data->emp_code]>0){ echo round($ab[$data->emp_code]); } else { echo round($ab_day=$work_day-($att_day[$data->emp_code]+$lv_day[$data->emp_code]+$lwp_day[$data->emp_code]));} ?>" style="width:100%; height:30px;" onKeyUp="cal_all(<?php echo $data->emp_code?>)" /></td>
      <td><input class="form-control" name="att_<?php echo $data->emp_code?>" id="att_<?php echo $data->emp_code?>" type="text"  readonly=""
	  value="<?php if($emp_code[$data->emp_code]>0){ echo round($att[$data->emp_code]); } else { echo round($att_day[$data->emp_code]);} ?>" style="width:100%; height:30px;" /></td>
      <td><input class="form-control" name="lt_<?php echo $data->emp_code?>" id="lt_<?php echo $data->emp_code?>" type="text"  
	  value="<?php  echo round($grace_no[$data->emp_code]);?>" style="width:100%; height:30px;" />
	  <?php /*?><?php if($emp_code[$data->emp_code]>0){ echo $lt[$data->emp_code]; } else { echo $grace_no[$data->emp_code];} ?><?php */?>	  </td>
      <td><input class="form-control" name="ot_<?php echo $data->emp_code?>" id="ot_<?php echo $data->emp_code?>" type="text"  value="<?php echo $ot[$data->emp_code];?>" style="width:100%; height:30px;" /></td>
      <td><input class="form-control" name="advance_<?php echo $data->emp_code?>" id="advance_<?php echo $data->emp_code?>" type="text"  value="<?php echo $advance_amt[$data->emp_code]-$monthly_cls_amt[$data->emp_code];?>"  readonly="" style="width:100%; height:30px;" /></td>
      <td><input class="form-control" name="deduction_<?php echo $data->emp_code?>" id="deduction_<?php echo $data->emp_code?>" type="text"  value="<?php echo $deduction[$data->emp_code];?>" style="width:100%; height:30px;" /></td>
      <td><input class="form-control" name="benefits_<?php echo $data->emp_code?>" id="benefits_<?php echo $data->emp_code?>" type="text"  value="<?php echo $benefits[$data->emp_code];?>" style="width:100%; height:30px;" /></td>
      <td>
	  <span id="divi_<?php echo $data->emp_code?>">

            <?php
			  if($emp_code[$data->emp_code]>0)
			  {?>

			  <input name="flag_<?php echo $data->emp_code?>" type="hidden" id="flag_<?php echo $data->emp_code?>" value="1" />

			  <input class="form-control" type="button" name="Button" value="Edit"  onclick="cal_all(<?php echo $data->emp_code?>), update_value(<?php echo $data->emp_code?>)" style="width:100%; height:30px; background-color:#FF3366; font-weight:700; font-size:12px;"/>
			  
			  <?php
			  }
			  else
			  {
			  ?>

			  <input name="flag_<?php echo $data->emp_code?>" type="hidden" id="flag_<?php echo $data->emp_code?>" value="0" />

			  <input class="form-control" type="button" name="Button" value="Save"  onclick="cal_all(<?php echo $data->emp_code?>), update_value(<?php echo $data->emp_code?>)" style="width:100%; height:30px;background-color:#66CC66;   font-weight:700; font-size:12px;"/><?php }?>
          </span>	  </td>
      </tr>
				  <?php }}?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <!-- ======= Footer ======= -->

</body>

</html>