<?php 
require_once("config/configDB.php"); 
require "userPermission.php";
require "financial_year_calculation.php";
require "accEmpFetchData.php";
//echo $_SESSION['user_id'];
$edit_id  =  $_GET['eid'];

$fromDate 	= '';
if($_GET['SalaryfromDate']){
	$invoicedate_temp   = new DateTime(stripslashes($_GET['SalaryfromDate']));
	$fromDate 		= $invoicedate_temp->format('Y-m-d');
}
$toDate 	= '';
if($_GET['SalarytoDate']) {
	$invoicedate_temp1   = new DateTime(stripslashes($_GET['SalarytoDate']));
	$toDate 		= $invoicedate_temp1->format('Y-m-d');
}


$sql ="SELECT lm.ename as ename,lad.shifttype,lad.extraworkinghrs,lad.shiftworkamt,lad.extrahoursamt,lad.enameid,lad.payedsalary,lad.invoicedate,lad.hdr_id,lad.advancesalary
FROM employee_attendance_dtl as lad 
LEFT JOIN employee_master as lm ON lm.id=lad.enameid 
WHERE  lad.enameid='$edit_id' and lad.isdelete=1 ";

if($fromDate !=''  && $toDate !='') {
	$sql.=" and lad.invoicedate BETWEEN '".$fromDate."' AND '".$toDate."'";
} 

$Details_Result  = mysqli_query($conn,$sql); 

$sql1 ="SELECT lm.ename as ename,lad.enameid,lad.hdr_id,sum(lad.advancesalary) as advancesalary
FROM employee_attendance_dtl as lad 
LEFT JOIN employee_master as lm ON lm.id=lad.enameid 
WHERE  lad.enameid='$edit_id' and lad.isdelete=1 ";

//echo $sql1;

// if($fromDate !=''  && $toDate !='') {
// 	$sql1.=" and lad.invoicedate BETWEEN '".$fromDate."' AND '".$toDate."'";
// } 
$sql1.="  GROUP BY lad.enameid ";

$Query_Adv  = mysqli_query($conn,$sql1); 
$ResultAdv     = mysqli_fetch_array($Query_Adv,MYSQLI_ASSOC); 


$sql11 = "SELECT lm.ename as ename,sum(lad.shifttype) as shifttype,sum(lad.extraworkinghrs) as extraworkinghrs,  lad.enameid,sum(lad.advancesalary) as advancesalary 
FROM employee_attendance_dtl as lad 
LEFT JOIN employee_master as lm ON lm.id=lad.enameid where lad.isdelete=1  and lad.enameid='$edit_id'  ";

if($fromDate !=''  && $toDate !='' ) {
    $sql11.=" and lad.invoicedate BETWEEN '".$fromDate."' AND '".$toDate."'";

} 

$sql11.="  GROUP BY lad.enameid ";	
$query_gettotalshift      = mysqli_query($conn, $sql11);
$data_step11   = mysqli_fetch_array($query_gettotalshift,MYSQLI_ASSOC); 

$query_master_data     = mysqli_query($conn,"SELECT ename,id,extrahoursamt ,shiftworkamt FROM employee_master WHERE id=".$edit_id." ");
$data_step12    = mysqli_fetch_array($query_master_data,MYSQLI_ASSOC);

$shiftworkamt   = $data_step12['shiftworkamt'];
$extrahoursamt  = $data_step12['extrahoursamt'];

$sql_advsalary = "SELECT lm.ename as ename,sum(lad.advancesalary) as advancesalary 
FROM employee_attendance_dtl as lad 
LEFT JOIN employee_master as lm ON lm.id=lad.enameid where lad.enameid='$edit_id'  ";

$sql_advsalary.="  GROUP BY lad.enameid ";	
$query_advsalary    = mysqli_query($conn, $sql_advsalary);
$data_advsalary     = mysqli_fetch_array($query_advsalary,MYSQLI_ASSOC); 
$advancesalary      = $data_advsalary['advancesalary'];

$sql_advcollect = "SELECT lm.ename as ename,sum(lad.advance_collection_amount) as collectadvanceamt 
FROM employee_advance_collection as lad 
LEFT JOIN employee_master as lm ON lm.id=lad.employee_id where lad.employee_id='$edit_id' and lad.status='' ";

//echo $sql_advcollect;

$sql_advcollect.="  GROUP BY lad.employee_id ";	
$query_advcollect    = mysqli_query($conn, $sql_advcollect);
$data_advcollect     = mysqli_fetch_array($query_advcollect,MYSQLI_ASSOC); 
$collectadvanceamt      = $data_advcollect['collectadvanceamt'];

?>
<!DOCTYPE html>
<html>
<head>
    <?php 
      //require 'meta.php'; 
      require 'header.php'; 
    ?>
</head>
<body>
  <?php require 'menu.php' ?>
  <div class="container-fluid">
    <h3 class="text-center"><strong> Employee Pay Slip Details</strong></h3>
    <form class="form-horizontal"  method="post" id="accLabourForm">
      <div class="col-sm-12 ">
      
      <div class="col-sm-2">
      <label>Emp&nbsp;Name</label>
      <h5><strong><?php echo $ResultAdv['ename']; ?></strong>  </h5>
      </div>
      <!-- <div class="col-sm-2">
      <label>Total Advance Amount</label>
      <h5><strong><?php echo $advancesalary-$collectadvanceamt; ?></strong>  </h5>
      </div> -->
      <div class="col-sm-1">
        <label>From&nbsp;Date</label>
        <h5><strong><?php echo $fromDate; ?></strong>  </h5>
      </div>
      <div class="col-sm-1">
        <label>To&nbsp;Date</label>
        <h5><strong><?php echo $toDate; ?></strong>  </h5>
      </div>
      <div class="col-sm-2">
      <label>Total&nbsp;Salary</label>
      <h5><strong><?php echo ($data_step12['shiftworkamt']*$data_step11['shifttype']) + ($data_step12['extrahoursamt']*$data_step11['extraworkinghrs']) ; ?></strong>  </h5>
      </div>
      
      <div class="col-sm-2">
      <!-- <label>Collect Advance Amount</label> -->
      <input type="hidden" id="fromDate" autocomplete="off" value="<?php echo $fromDate; ?>" >
      <input type="hidden" id="toDate" autocomplete="off" value="<?php echo $toDate; ?>">
      <input type="hidden" id="totaladvanceamt" autocomplete="off" value="<?php echo $advancesalary; ?>">
      <input type="hidden" id="totalshift" autocomplete="off" value="<?php echo $data_step11['shifttype']; ?>">
      <input type="hidden" id="totalextra" autocomplete="off" value="<?php echo $data_step11['extraworkinghrs']; ?>">
      <input type="hidden" id="shiftamt" autocomplete="off" value="<?php echo $data_step12['shiftworkamt']; ?>">
      <input type="hidden" id="extraamt" autocomplete="off" value="<?php echo $data_step12['extrahoursamt']; ?>">

      

      <input type="hidden" class="form-control input-lg" value="0" id="collectadvanceamt"  autocomplete="off">
      </div>
        
        
      </div>

      <div class="col-sm-12 ">
      <div class="col-sm-1">
      </div>
        <div class="col-sm-2 form-group">
          <!-- <a type="button" href='reportLabourPDF.php?startDate=<?php echo $fromDate; ?>&endDate=<?php echo $toDate; ?>&labourname=<?php echo $edit_id; ?>' class="btn btn-success btn-lg report-submit-pdf" target="_blank" >Generate PDF</a> -->
          <input type="hidden" value=<?php echo $edit_id; ?> name="edit_id">
        </div>
        <!--<div class="col-sm-1 form-group">
          <a id='laboursMaster_<?php echo $edit_id; ?>' onclick='ConfirmlaboursPaySalary(<?php echo $edit_id; ?>)'  class='btn btn-primary btn-lg report-submit-pdf'>Pay </a>
        </div>-->
        
        
      </div>
      
           
  
<div class="container-fluid">
      
      
      <div class="col-sm-7" style="padding-top: 10px;margin-top: 10px;">          
        <table class="table table-responsive table-striped table-bordered dt-responsive nowrap" id="accLabourDetailsAdd" cellspacing="0">
          <thead>
            <tr>
              <th>Working Date</th>
              <th>Shift </th>
              <th>Extra Working Hours</th>
              <!-- <th>Advance Salary</th> 
              <th>Action</th>-->
            </tr>
          </thead>
          <tbody>
          <?php
            $trlength = 0;
            //lm.ename,lad.shifttype,lad.extraworkinghrs,lad.shiftworkamt,lad.extrahoursamt,lad.enameid
            while($drow = mysqli_fetch_array($Details_Result))
            {
              $trlength++;
              $DetailsTable ='<tr id="'.$trlength.'">';
                $DetailsTable .=  '<td class="product_name">'.$drow["invoicedate"].'<input class="form-control labourenameID" name="labourenameID[]"  value="'.$drow["enameid"].'" type="hidden"></td>';
            
                $DetailsTable .= '<td>'.$drow["shifttype"].'<input class="form-control shifttypeID" name="shifttypeID[]" type="hidden" value="'.$drow["shifttype"].'" ></td>';
            
                $DetailsTable .= '<td>'.$drow["extraworkinghrs"].'<input class="form-control extraworkinghrs" name="extraworkinghrs[]" type="hidden" value="'.$drow["extraworkinghrs"].'" ></td>';
                
                //$DetailsTable .= '<td>'.$drow["advancesalary"].'<input class="form-control advancesalary" name="advancesalary[]" type="hidden" value="'.$drow["advancesalary"].'" ></td>';
                if($drow["payedsalary"]==0) {
                  //$DetailsTable .= '<td><input type="button" class="btn btn-default removeTripDetails" id="removeTripDetails" value="Edit ShiftType"></td>';
                 // $DetailsTable .="<td><a href='accEmpEdit.php?id=".$drow['hdr_id']."' id='accLabour_".$row['hdr_id']."' class='btn btn-primary'>Edit</a></td>&nbsp";
                } else {
                  //$DetailsTable .= '<td><input type="button" class="btn btn-default removeTripDetails" id="removeTripDetails" value="Edit ShiftType"></td>';
                  //$DetailsTable .="<td><a href='accEmpEdit.php?id=".$drow['hdr_id']."' id='accLabour_".$row['hdr_id']."' class='btn btn-primary'>Edit</a></td>&nbsp";

                }
             
              $DetailsTable .= '</tr>';
     
              echo $DetailsTable;
                   
            }
          ?>
          </tbody>
        </table>
      </div>


    </div> 



    <div class="form-group">        
      <div class="col-sm-12 text-center">
       
      </div>
    </div>
    </form>   
  </div>
  <?php require 'footer.php' ?>
  
  <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
  
  <script type="text/javascript" src="accEmpEditScript.js"></script>
<script>
	$(document).ready(function(){
	  var date_input=$('input[name="invoicedate"]'); //our date input has the name "date"
		var container=$('form > .datepicker-invoicedate').length>0 ? $('form > .datepicker-invoicedate').parent() : "body";
		date_input.datepicker({
			  format: 'dd-mm-yyyy',
        container: container,
			  todayHighlight: true,
			  autoclose: true,
    });
    //$('input[name="invoicedate"]').datepicker( "setDate", new Date());   

      
    
  

  });

  function ConfirmlaboursPaySalary(eid) {

    
    if(confirm('Are you sure paid salary?'))    
    {           
        //console.log("Data has been Delected "+eid);
        var fromDate  = $('input#fromDate').val();
        var toDate    = $('input#toDate').val();
        var collectadvanceamt = $('input#collectadvanceamt').val();
        var totaladvanceamt   = $('input#totaladvanceamt').val();
        var totalshift    = $('input#totalshift').val();
        var totalextra    = $('input#totalextra').val();
        var shiftamt      = $('input#shiftamt').val();
        var extraamt      = $('input#extraamt').val();
        $.ajax({
            url: base_url+'accEmppaysalary.php?eid='+eid+'&SalaryfromDate='+fromDate+'&SalarytoDate='+toDate+'&collectadvamt='+collectadvanceamt+'&totaladvanceamt='+totaladvanceamt+'&totalshift='+totalshift+'&totalextra='+totalextra+'&shiftamt='+shiftamt+'&extraamt='+extraamt,     
            dataType: "json",   
            type: 'GET',    
            success: function(data) {
                console.log("Data has been Delected "); 
                //window.location.href = "accLabourDashboard.php";       
            }                 
        });
        window.location.href = "accEmpDashboard.php";     
        
        window.open(base_url+"reportEmpPDF.php?startDate="+fromDate+"&endDate="+toDate+"&ename="+eid+" ","_blank");
    }
}
  

</script>
</body>
</html>