<?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 Payment";
$page="71_employee_salary_payment.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=2;
$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'];
//$remarks = $_POST['remarks'];
$tr_from='Payment';
 	
//$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);


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

 echo $sql_py = 'select s.*, e.emp_name, e.employee_type from '.$table.' e, employee_monthly_salary s 
	  where e.emp_code=s.emp_code and s.mon="'.$_POST['mon'].'" and s.year="'.$_POST['year'].'" '.$employee_type_con.' ';
$query_py = mysqli_query($conn,$sql_py);
while ($data_py = mysqli_fetch_object($query_py)) {

//if($_POST['gross_salary_'.$data->emp_code]>0)
//
//{

$salary_exp_ledger= $_POST['salary_exp_ledger_'.$data_py->emp_code];
$salary_pay_ledger= $_POST['salary_pay_ledger_'.$data_py->emp_code];
$tds_ledger= $_POST['tds_ledger_'.$data_py->emp_code];
$salary_advance= $_POST['salary_advance_'.$data_py->emp_code];
$other_benefits= $_POST['other_benefits_'.$data_py->emp_code];
$other_deduction= $_POST['other_deduction_'.$data_py->emp_code];



  $ins_sqljv = 'INSERT INTO employee_monthly_salary_payment (jv_no, jv_date, master_ledger_id, order_no, emp_code, department, designation, tr_no, tr_date, mon, year, td, hd, wd, lv, ab, att, lt, ot, day_salary, hourly_salary, gross_salary, basic_salary, house_rent, conveyance, medical, others, benefits, ot_wages, total_earnings, pf_amt, tds_amt, deduction, advance_deduction, ab_deduction, lt_deduction, total_deduction, net_salary, status, entry_at, entry_by) VALUES

("'.$jv_no.'", "'.$jv_date.'", "'.$master_ledger_id.'", "'.$data_py->id.'", "'.$data_py->emp_code.'", "'.$data->department.'", "'.$data->designation.'", "'.$data->tr_no.'", "'.$data->tr_date.'", "'.$data->mon.'", "'.$data->year.'", "'.$data->td.'", "'.$data->hd.'", "'.$data->wd.'", "'.$data->lv.'", "'.$data->ab.'", "'.$data->att.'", "'.$data->lt.'", "'.$data->ot.'", "'.$data->day_salary.'", "'.$data->hourly_salary.'", "'.$data->gross_salary.'", "'.$data->basic_salary.'", "'.$data->house_rent.'", "'.$data->conveyance.'", "'.$data->medical.'", "'.$data->others.'", "'.$data->benefits.'", "'.$data->ot_wages.'", "'.$data->total_earnings.'", "'.$data->pf_amt.'", "'.$data->tds_amt.'", "'.$data->deduction.'", "'.$data->advance_deduction.'", "'.$data->ab_deduction.'", "'.$data->lt_deduction.'", "'.$data->total_deduction.'", "'.$data->net_salary.'", "'.$data->status.'", "'.$entry_at.'", "'.$entry_by.'")';
	
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);
//}





?>

<!--<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="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:90%">
		  <option></option>
			
			<?php foreign_relation($conn,'month','month_id','month_name',$_POST['mon'],'1 order by month_id');?>
        	</select>		  </td>
          <td width="28%" align="left"><label for="inputEmail">Cash/Bank:</label>
            </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="15%" align="right">Employee Type:</td>
          <td width="15%" align="right">
		  <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>
        <td width="7%" align="right">Year:&nbsp;&nbsp;</td>
        <td width="17%" align="left"><select id="year" name="year"  class="form-select"  required  style="width:90%" >
          <option></option>
          <?php foreign_relation($conn,'year','year','year',$_POST['year'],'1 order by year');?>
        </select></td>
        <td align="left"><select id="master_ledger_id" name="master_ledger_id" required class="form-select" style="width:100%" >
	  <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;">gross salary</span></th>
                    <th width="9%" scope="col"><span style="text-align:right"> benefits</span></th>
                    <th width="9%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Advance</span></th>
                    <th width="8%" scope="col"><span jsslot=""><span data-dobid="hdw">absence</span></span></th>
                    <th width="5%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Late</span></th>
                    <th width="5%" scope="col">TDS</th>
                    <th width="6%" scope="col">Deduction</th>
                    <th width="10%" scope="col">Salary Pay </th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
		
if($_POST['job_status']!=''){
$job_status_con=" and e.job_status='".$_POST['job_status']."' ";
}

if($_POST['employee_type']!=''){
$employee_type_con=" and e.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, salary_advance, other_benefits, other_deduction 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;
		 $salary_advance[$info->id]=$info->salary_advance;
		 $other_benefits[$info->id]=$info->other_benefits;
		 $other_deduction[$info->id]=$info->other_deduction;
		}
		
		 $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 s.*, e.emp_name, e.employee_type from '.$table.' e, employee_monthly_salary s 
	  where e.emp_code=s.emp_code and s.mon="'.$_POST['mon'].'" and s.year="'.$_POST['year'].'" '.$employee_type_con.' ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {?>
					
	<?php //if($gross_salary[$data->emp_code]==0 && $_POST['mon']>0 && $_POST['year']>0){?>		
      <tr>
      <td><a href="view_file/salary_payslip_print_view.php?tr_no=<?php echo $data->tr_no;?>" target="_blank"><?php echo $data->emp_code;?></a></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="text"
	   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="text"
	   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="text"
	   value="<?php echo $tds[$data->employee_type];?>" style="width:90%; height:30px;" readonly="" />	
	   
	    <input class="form-control" name="salary_advance_<?php echo $data->emp_code?>" id="salary_advance_<?php echo $data->emp_code?>" type="text"
	   value="<?php echo $salary_advance[$data->employee_type];?>" style="width:90%; height:30px;" readonly="" />
	    <input class="form-control" name="other_benefits_<?php echo $data->emp_code?>" id="other_benefits_<?php echo $data->emp_code?>" type="text"
	   value="<?php echo $other_benefits[$data->employee_type];?>" style="width:90%; height:30px;" readonly="" />
	    <input class="form-control" name="other_deduction_<?php echo $data->emp_code?>" id="other_deduction_<?php echo $data->emp_code?>" type="text"
	   value="<?php echo $other_deduction[$data->employee_type];?>" 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="" />
	  <?php echo number_format($data->gross_salary,2); $tot_gross_salary +=$data->gross_salary;?></td>
      <td><?php echo number_format($tot_benefits=$data->benefits+$data->ot_wages,2); $net_tot_benefits+=$tot_benefits;?></td>
      <td><?php echo number_format($data->advance_deduction,2); $net_total_advance_deduction+=$data->advance_deduction;?></td>
      <td><?php echo number_format($data->ab_deduction,2); $net_total_ab_deduction+=$data->ab_deduction;?></td>
      <td><?php echo number_format($data->lt_deduction,2); $net_total_lt_deduction+=$data->lt_deduction;?></td>
      <td><?php echo number_format($data->tds_amt,2); $net_total_tds_amt+=$data->tds_amt;?></td>
      <td><?php echo number_format($data->deduction,2); $net_total_deduction+=$data->deduction;?></td>
      <td><?php echo number_format($data->net_salary,2); $net_total_net_salary+=$data->net_salary;?></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>