<?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="Employee Advance Schedule";
$page="71_employee_advance_schedule.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_advance';	
$unique='id';

// End Common Information

?>

<!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 order_no=id; // Rent
 
var emp_code=(document.getElementById('emp_code_'+id).value)*1;
var mon=(document.getElementById('mon_'+id).value)*1; 
var year=(document.getElementById('year_'+id).value)*1; 

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

var strURL="employee_advance_schedule_ajax.php?order_no="+order_no+"&mon="+mon+"&year="+year+"&emp_code="+emp_code+"&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=(document.getElementById('td_'+id).value)*1;
var lv=(document.getElementById('lv_'+id).value)*1; 
var ab=(document.getElementById('ab_'+id).value)*1;

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

var pay=(document.getElementById('pay_'+id).value)=(td-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" style="background: #ebf5fb ;">
      
      
        <tr height="50">
          <td align="right">Month:&nbsp;&nbsp;</td>
          <td align="left">
		  <select id="mon" name="mon"  class="form-select"    >
		  <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">&nbsp;</td>
          <td width="18%">&nbsp;</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"    >
			<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']?>" >	
		</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:#ebf5fb;  border-top: 1px solid #CCCCCC;">
                    <th width="8%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">EMP Code </span></th>
                    <th width="27%"  scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">EMP Name </span></th>
                    <th width="9%"  scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">TR No </span></th>
                    <th width="13%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Month</span></th>
                    <th width="12%"  scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Year</span></th>
                    <th width="12%"  scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;" >Installment  </span></th>
                    <th width="12%"  scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Adjustment</span></th>
                    <th width="7%"  scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Action</span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
					
 $employee_name = $_REQUEST['employee_name'];
  $employee=explode(' :: ',$employee_name);
   $employee_code=$employee[0];
  
if($employee_code!=''){
$employee_con=" and e.emp_code='".$employee_code."' ";
}

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

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

if($_POST['mon']!=''){
$mon_con=" and a.mon='".$_POST['mon']."' ";
}

if($_POST['year']!=''){
$year_con=" and a.year='".$_POST['year']."' ";
}

$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 id, designation from designation where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $designation[$info->id]=$info->designation;
		}			
					
		 
		
		 $sql = "select emp_code, year, mon, advance_deduction from employee_monthly_salary where 1 group by emp_code, year, mon";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $advance_deduction[$info->emp_code][$info->year][$info->mon]=$info->advance_deduction;
		}
		
		$sql = "select emp_code, year, mon, monthly_amt as monthly_cls_amt from employee_advance_closing where 1 group by emp_code, year, mon";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $monthly_cls_amt[$info->emp_code][$info->year][$info->mon]=$info->monthly_cls_amt;
		}
		
			
					     $sql = 'select a.*, e.emp_name from '.$table.' a, employee_info e 
						 where  a.emp_code=e.emp_code '.$employee_con.$designation_con.$department_con.$mon_con.$year_con.' order by a.emp_code, a.year, a.mon ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
	<?php if(isset($_POST['run_data'])){?>		
      <tr>
      <td><?php echo $data->emp_code;?>
	  <input class="form-control" name="emp_code_<?php echo $data->id?>" id="emp_code_<?php echo $data->id?>" type="hidden"  value="<?php echo $data->emp_code;?>" style="width:90%; height:30px;" readonly=""/></td>
      <td><?php echo $data->emp_name;?></td>
      <td><a href="view_file/Voucher_view.php?jv_no=<?php echo $data->tr_no;?>" target="_blank"><?php echo $data->tr_no;?></a></td>
      <td>
	  <select id="mon_<?php echo $data->id?>" name="mon_<?php echo $data->id?>"  class="form-select" style="width:90%; font-size:12px;"  >
		 
			
			<?php foreign_relation($conn,'month','month_id','month_name',$data->mon,'1 order by month_id');?>
        	</select>	  </td>
      <td><select id="year_<?php echo $data->id?>" name="year_<?php echo $data->id?>"  class="form-select" style="width:90%; font-size:12px;"   >
			
			<?php foreign_relation($conn,'year','year','year',$data->year,'1 order by year');?>
        	</select>	</td>
      <td><input class="form-control" name="monthly_amt_<?php echo $data->id?>" id="monthly_amt_<?php echo $data->id?>" type="text"  value="<?php echo $data->monthly_amt;?>" style="width:90%; height:30px;" readonly=""/></td>
      <td>
	  <input class="form-control" name="advance_deduction_<?php echo $data->id?>" id="advance_deduction_<?php echo $data->id?>" type="text"  
	  value="<?php echo $advance_deduction[$data->emp_code][$data->year][$data->mon]+$monthly_cls_amt[$data->emp_code][$data->year][$data->mon];?>" style="width:90%; height:30px;" readonly="" />
	  </td>
      <td>
	  <span id="divi_<?php echo $data->id?>">

            <?php if($adv_cls_amt=($advance_deduction[$data->emp_code][$data->year][$data->mon]+$monthly_cls_amt[$data->emp_code][$data->year][$data->mon])==0){?>

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

			  <input class="form-control" type="button" name="Button" value="Edit"  onclick="update_value(<?php echo $data->id?>)" style="width:80%; height:30px; background-color:#FF3366; 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>