<?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="Salary Provision";
$page="71_employee_salary_provision.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_info';	
$unique='emp_code';

// 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'];
$mon=$_POST['mon'];
$year=$_POST['year'];
$jv_date=$_POST['tr_date'];
$month_name=find_a_field($conn,'month','month_name','month_id="'.$mon.'"');
$jv_narration='For the month of '.$month_name.', '.$year;

$jv_type=3;
$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=0;
//$remarks = $_POST['remarks'];
$tr_from='Journal';
 	
//$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.'", "'.$remarks.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$ref_no.'", "'.$jv_special.'")';
	
mysqli_query($conn,$ins_sql);


$sql = 'select * from '.$table.' where gross_salary>0 ';
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

if($_POST['gross_salary_'.$data->emp_code]>0)
{
$basic_salary= $_POST['basic_salary_'.$data->emp_code];
$house_rent= $_POST['house_rent_'.$data->emp_code];
$conveyance= $_POST['conveyance_'.$data->emp_code];
$medical= $_POST['medical_'.$data->emp_code];
$others= $_POST['others_'.$data->emp_code];
$gross_salary= $_POST['gross_salary_'.$data->emp_code];
$tds_amt= $_POST['tds_amt_'.$data->emp_code];

$salary_exp_ledger= $_POST['salary_exp_ledger_'.$data->emp_code];
$salary_pay_ledger= $_POST['salary_pay_ledger_'.$data->emp_code];
$tds_ledger= $_POST['tds_ledger_'.$data->emp_code];


$ins_sqljv = 'INSERT INTO employee_salary_provision (emp_code, department, designation, tr_no, tr_date, mon, year, gross_salary, basic_salary, house_rent, conveyance, medical, others, status, entry_at, entry_by, tds_amt) VALUES

("'.$data->emp_code.'", "'.$data->department.'", "'.$data->designation.'", "'.$jv_no.'", "'.$jv_date.'", "'.$mon.'", "'.$YR.'", "'.$gross_salary.'", "'.$basic_salary.'", "'.$house_rent.'", "'.$conveyance.'", "'.$medical.'", "'.$others.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$tds_amt.'")';
	
mysqli_query($conn,$ins_sqljv);

$ins_id=mysqli_insert_id($conn);

add_to_sec_journal($conn, $jv_no, $jv_date, $salary_exp_ledger, $data->emp_code, $jv_narration, $gross_salary, $cr_amt,  $tr_from, $jv_no, '0', $cc_code, $ins_id, $group_for, $status, $entry_by, $entry_at);

add_to_sec_journal($conn, $jv_no, $jv_date, $salary_pay_ledger, $data->emp_code, $jv_narration, $dr_amt, $gross_salary, $tr_from, $jv_no, '0', $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);
}


//$dis_amt = find_a_field($conn,'journal_receive_payment','sum(discount_amt)','jv_no="'.$jv_no.'"');

//$dis_data=mysqli_fetch_object(mysqli_query($conn,'select sum(discount_amt) as dis_amt from journal_receive_payment where jv_no="'.$jv_no.'"'));

//if($dis_amt>0) {
//auto_insert_invoice_discount_secoundary_journal($conn,$jv_no);
//}





?>

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

<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="right"> <input type="text" class="form-control" id="tr_date" name="tr_date" value="<?php echo $_REQUEST['tr_date']?>" required ></td>
          <td align="right">Month:&nbsp;&nbsp;</td>
          <td align="left">
		  <select id="mon" name="mon"  class="form-select"  required style="width:100%">
		  <option></option>
			
			<?php foreign_relation($conn,'month','month_id','month_name',$_POST['mon'],'1 order by month_id');?>
        	</select>		  </td>
          <td width="17%" align="right">Job Status:&nbsp;&nbsp;</td>
          <td width="16%">
		  <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="18%" 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="10%" align="right">&nbsp;</td>
          <td width="15%" align="right">&nbsp;</td>
        <td width="11%" align="right">Year:&nbsp;&nbsp;</td>
        <td width="13%" align="left"><select id="year" name="year"  class="form-select"  required  style="width:100%" >
          <option></option>
          <?php foreign_relation($conn,'year','year','year',$_POST['year'],'1 order by year');?>
        </select></td>
        <td align="right">Employee Type:&nbsp;&nbsp;</td>
        <td><select id="employee_type" name="employee_type"  class="form-select"  required  style="width:100%" >
          <option></option>
          <?php foreign_relation($conn,'employee_type','id','employee_type',$_POST['employee_type'],'1 order by id');?>
        </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="20%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">EMP Name </span></th>
                    <th width="12%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Designation</span></th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Basic</span></th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">House Rent </span></th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">conveyance</span></th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">medical</span></th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">others</span></th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">gross salary</span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
		
if($_POST['job_status']!=''){
$job_status_con=" and job_status='".$_POST['job_status']."' ";
}

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


	
		 $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 id, job_status from job_status where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $job_status[$info->id]=$info->job_status;
		}
		
		$sql = "select id, salary_provision, salary_expense, tds from employee_type where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $salary_provision[$info->id]=$info->salary_provision;
		 $salary_expense[$info->id]=$info->salary_expense;
		 $tds[$info->id]=$info->tds;
		}
		
		 $sql = "select emp_code, gross_salary  from employee_salary_provision where mon='".$_POST['mon']."' and year='".$_POST['year']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $gross_salary[$info->emp_code]=$info->gross_salary;
		}
		
			
					    $sql = 'select * from '.$table.' where gross_salary>0 '.$employee_con.$designation_con.$department_con.$job_status_con.$employee_type_con.' ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {?>
	<?php /*?><?php if(isset($_POST['run_data'])){}?>	<?php */?>					
	<?php if($gross_salary[$data->emp_code]==0 && $_POST['mon']>0 && $_POST['year']>0){?>		
      <tr>
      <td><?php echo $data->emp_code;?></td>
      <td><?php echo $data->emp_name;?>
	  <input class="form-control" name="salary_exp_ledger_<?php echo $data->emp_code?>" id="salary_exp_ledger_<?php echo $data->emp_code?>" type="hidden"
	   value="<?php echo $salary_expense[$data->employee_type];?>" style="width:90%; height:30px;" readonly="" />
	   <input class="form-control" name="salary_pay_ledger_<?php echo $data->emp_code?>" id="salary_pay_ledger_<?php echo $data->emp_code?>" type="hidden"
	   value="<?php echo $salary_provision[$data->employee_type];?>" style="width:90%; height:30px;" readonly="" />
	   <input class="form-control" name="tds_ledger_<?php echo $data->emp_code?>" id="tds_ledger_<?php echo $data->emp_code?>" type="hidden"
	   value="<?php echo $tds[$data->employee_type];?>" style="width:90%; height:30px;" readonly="" />
	  </td>
      <td><?php echo $designation[$data->designation]?></td>
<td><input class="form-control" name="basic_salary_<?php echo $data->emp_code?>" id="basic_salary_<?php echo $data->emp_code?>" type="text"  value="<?php echo $data->basic_salary;?>" style="width:90%; height:30px;" 
readonly="" onKeyUp="cal_all(<?php echo $data->emp_code?>)" /></td>
      <td><input class="form-control" name="house_rent_<?php echo $data->emp_code?>" id="house_rent_<?php echo $data->emp_code?>" type="text"  value="<?php echo $data->house_rent;?>" style="width:90%; height:30px;"
	  onkeyup="cal_all(<?php echo $data->emp_code?>)" readonly=""/></td>
      <td><input class="form-control" name="conveyance_<?php echo  $data->emp_code?>" id="conveyance_<?php echo $data->emp_code?>" type="text"  value="<?php echo $data->conveyance;?>" style="width:90%; height:30px;"
	  onkeyup="cal_all(<?php echo $data->emp_code?>)" readonly=""/></td>
      <td><input class="form-control" name="medical_<?php echo $data->emp_code?>" id="medical_<?php echo $data->emp_code?>" type="text" value="<?php echo $data->medical;?>" 
	  style="width:90%; height:30px;" readonly="" /></td>
      <td><input class="form-control" name="others_<?php echo $data->emp_code?>" id="others_<?php echo $data->emp_code?>" type="text"  value="<?php echo $data->others;?>" style="width:90%; height:30px;" readonly="" /></td>
      <td><input class="form-control" name="gross_salary_<?php echo $data->emp_code?>" id="gross_salary_<?php echo $data->emp_code?>" type="text" value="<?php echo $data->gross_salary; $tot_gross_salary +=$data->gross_salary;?>" style="width:90%; height:30px;" readonly="" />
	  <input class="form-control" name="tds_amt_<?php echo $data->emp_code?>" id="tds_amt_<?php echo $data->emp_code?>" type="hidden" value="<?php echo $data->tds_amt; ?>" style="width:90%; height:30px;" readonly=""  /></td>
      </tr>
				  <?php }}?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->
			<?php if($tot_gross_salary>0) {?>  
			  <br>	  
          
<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>