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

// End Common Information


if(isset($_POST['confirm_data']))

{
//$_POST['entry_by'] = $_SESSION['user']['id'];
$status='CHECKED';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');
$group_for = $_SESSION['user']['group'];
$employee_code=$_POST['employee_code'];
$master_ledger_id=$_POST['master_ledger_id'];
$jv_date=$_POST['tr_date'];
//$month_name=find_a_field($conn,'month','month_name','month_id="'.$mon.'"');
$jv_narration='Advance Closing';

$jv_type=1;
$jv_special=1;

$jv_title=find_a_field($conn,'journal_type','jv_title','id="'.$jv_type.'"');

		$YR = date('Y',strtotime($jv_date));
 		$year = date('y',strtotime($jv_date));
  		$month = date('m',strtotime($jv_date));
 		$inv_cy_id = find_a_field($conn,'journal_master','max(jv_id)','year="'.$YR.'" and jv_type="'.$jv_type.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
   		$jv_no=''.$year.''.$month.''.$jv_type.''.$cy_id;
		$view_jv_no=$jv_title.''.$year.''.$month.''.$jv_type.''.$cy_id;
		
$master_ledger_id=$_POST['master_ledger_id'];

$tr_from='Receipt';
 	
$config_sql="SELECT * FROM config_ledger  WHERE group_for='".$group_for."'";
$config_data = find_all_field_sql($conn,$config_sql);

  $ins_sql = 'INSERT INTO  journal_master (jv_no, year, jv_id, view_jv_no, jv_type, jv_date, group_for, master_ledger_id, tr_from, remarks, status, entry_at, entry_by, ref_no, jv_special) VALUES

("'.$jv_no.'", "'.$YR.'", "'.$cy_id.'", "'.$view_jv_no.'", "'.$jv_type.'", "'.$jv_date.'", "'.$group_for.'", "'.$master_ledger_id.'", "'.$tr_from.'", "'.$jv_narration.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$ref_no.'", "'.$jv_special.'")';
	
mysqli_query($conn,$ins_sql);


if($employee_code!=''){
$employee_con=" and e.emp_code='".$employee_code."' ";
}

 $sql = 'select a.*, e.emp_name from '.$table.' a, employee_info e 
where  a.emp_code=e.emp_code '.$employee_con.' order by a.emp_code, a.year, a.mon';
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

//if($_POST['gross_salary_'.$data->emp_code]>0)
if(in_array($data->id, $_POST['point']))
{
$emp_code= $_POST['emp_code_'.$data->id];
$mon= $_POST['mon_'.$data->id];
$month= $_POST['month_'.$data->id];
$year= $_POST['year_'.$data->id];
$monthly_amt= $_POST['monthly_amt_'.$data->id];

$cr_narration=$month.', '.$year;

$tot_net_adjustment +=$monthly_amt;

 $ins_sqljv = 'INSERT INTO employee_advance_closing (emp_code, master_ledger_id, tr_no, tr_date, order_no, installment_no, mon, year, monthly_amt, entry_at, entry_by) VALUES

("'.$emp_code.'", "'.$master_ledger_id.'", "'.$jv_no.'", "'.$jv_date.'", "'.$data->id.'", "'.$data->installment_no.'", "'.$mon.'", "'.$year.'", "'.$monthly_amt.'", "'.$entry_at.'", "'.$entry_by.'")';
	
mysqli_query($conn,$ins_sqljv);

$ins_id=mysqli_insert_id($conn);

add_to_sec_journal($conn, $jv_no, $jv_date, $config_data->salary_advance, $data->emp_code, $cr_narration, $dr_amt, $monthly_amt, $tr_from, $jv_no, $master_ledger_id, $cc_code, $ins_id, $group_for, $status, $entry_by, $entry_at);

}	
}

if($tot_net_adjustment>0){
add_to_sec_journal($conn, $jv_no, $jv_date, $master_ledger_id, $employee_code, $jv_narration,  $tot_net_adjustment, $cr_amt,  $tr_from, $jv_no, $config_data->salary_advance, $cc_code, $ins_id, $group_for, $status, $entry_by, $entry_at);
}


$jv_approval = find_a_field($conn,'journal_type','jv_approval','journal_type="'.$tr_from.'"');

if($jv_approval=="Yes"){

sec_journal_journal($conn,$jv_no,$jv_no,$tr_from);

$time_now = date('Y-m-d H:i:s');

$up_sj='update secondary_journal set status="CHECKED", checked_at="'.$time_now.'", checked_by="'.$_SESSION['user']['id'].'" 
where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_sj);

$up_jv='update journal set status="CHECKED", checked_by="'.$_SESSION['user']['id'].'", checked_at="'.$time_now.'"
 where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_jv);

$up_jm='update journal_master set status="CHECKED", checked_by="'.$_SESSION['user']['id'].'", checked_at="'.$time_now.'"
 where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_jm);

}elseif($jv_approval=="No") {
$up_jm='update journal_master set status="UNCHECKED" where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_jm);

$up_sj='update secondary_journal set status="UNCHECKED" where jv_no="'.$jv_no.'" and tr_from="'.$tr_from.'"';
mysqli_query($conn,$up_sj);
}





?>

<script language="javascript">
window.location.href = "<?php echo $page;?>";
</script>

<?php } ?>

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

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <?php
  do_calander('#tr_date');
  ?>
 
 
 <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 lv=(document.getElementById('lv_'+id).value)*1; 
var ab=(document.getElementById('ab_'+id).value)*1; 
var pay=(document.getElementById('pay_'+id).value)*1; 
var ot=(document.getElementById('ot_'+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+"&lv="+lv+"&ab="+ab+"&pay="+pay+"&ot="+ot+"&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=(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>

<script>

 

    function selectedAll(){

        $('input[type=checkbox]').prop('checked', true);
        
    }
	
	function RemoveAll(){

        $('input[type=checkbox]').prop('checked', false);
        
    }
    
    function select_all(perm){
       document.getElementById("point"+perm).checked = true;
      
    }
    
    function remove_all(perm){
       document.getElementById("point"+perm).checked = false;
       
    }
    
</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">Date:</td>
          <td align="left"> <input type="text" class="form-control" id="tr_date" name="tr_date" value="<?php echo $_REQUEST['tr_date']?>" required style="width:90%" ></td>
          <td width="32%" align="left"><label for="inputEmail">Cash/Bank:</label>            </td>
          <td width="21%" 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="14%" align="right">EMP NAME:</td>
          <td width="33%" align="left">
		   <?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']?>" style="width:90%" required >		  </td>
        <td align="left"><select id="master_ledger_id" name="master_ledger_id" required class="form-select" style="width:90%" >
	  <option></option>
	<?php foreign_relation($conn,'accounts_ledger','ledger_id','concat(ledger_id," :: ",ledger_name)',$_POST['master_ledger_id'],'ledger_type!="General"');?>
      </select>		</td>
        </tr>
    </table>
    
	
              <!-- 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="19%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">EMP Name </span></th>
                    <th width="14%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">TR No </span></th>
                    <th width="9%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Month</span></th>
                    <th width="9%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Year</span></th>
                    <th width="8%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Installment</span></th>
                    <th width="5%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Adjustment</span></th>
                    <th width="3%" scope="col">
					<span onClick="selectedAll()" class="btn" style="color:#242020;background-color:#80c4c7;border-color:#408d99; width:50px; height:30px; font-size:12px; ">ALL</span>					</th>
                    <th width="4%" scope="col">
					<span onClick="RemoveAll()" class="btn" style="color: #ffffff;background-color: #ec4848;border-color: #994040; width:50px; height:30px; font-size:12px;">DEL</span>					</th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
		
if($_POST['job_status']!=''){
$job_status_con=" and e.job_status='".$_POST['job_status']."' ";
}




 	$employee_name = $_REQUEST['employee_name'];
  	$employee=explode(' :: ',$employee_name);
    $employee_code=$employee[0];
  
if($employee_code!=''){
$employee_con=" and e.emp_code='".$employee_code."' ";
}

	
		 $sql = "select month_id, month_name from month where 1 group by month_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $month_name[$info->month_id]=$info->month_name;
		}		
		
		$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.' 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'])){?>					
	<?php if($adj_cls_amt=($advance_deduction[$data->emp_code][$data->year][$data->mon]+$monthly_cls_amt[$data->emp_code][$data->year][$data->mon])==0 ){?>		
      <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,'month_id="'.$data->mon.'" order by month_id');?>
      </select>
	   <input class="form-control" name="month_<?php echo $data->id?>" id="month_<?php echo $data->id?>" type="hidden"  value="<?php echo  $month_name[$data->mon];?>" style="width:90%; height:30px;" readonly=""/>
	  </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,'year="'.$data->year.'" 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];?>" style="width:90%; height:30px;" readonly="" />
	  <?php echo $monthly_cls_amt[$data->emp_code][$data->year][$data->mon];?>
	  </td>
      <td colspan="2"><center> <input type="checkbox" name="point[]" id="point" value="<?=$data->id?>" style="width:25px; text-align:center" /> </center></td>
      </tr>
				  <?php } } } ?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->
			<?php //if($tot_gross_salary>0) {?>  
			  <br>
			  
	 <input class="form-control" name="employee_code" id="employee_code" type="hidden"  value="<?php echo $employee_code;?>" style="width:90%; height:30px;" readonly=""/>	  
          
<div class="text-left" align="right">
				  <button type="reset" class="btn btn-primary btn-sm" onClick="parent.location='<?php echo $page?>'" >&nbsp;&nbsp;Clear&nbsp;&nbsp;</button>&nbsp;&nbsp;&nbsp;&nbsp
				  <button type="submit" class="btn btn-success btn-sm" name="confirm_data" id="confirm_data" >&nbsp;&nbsp;Save &amp; Close&nbsp;&nbsp;</button>
                  
              </div>
			  <?php //}?>
       </form>

            </div>
          </div>

        </div>

        
      </div>
    </section>

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

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

</body>

</html>