<?php 

require_once("config/configDB.php"); 
require "userPermission.php";


$from=$_POST['fromDate'];
$to=$_POST['toDate'];
$ecode=$_POST['ecode'];

//echo $from;
//echo $to;
//echo $dname;

$sql_ename=mysqli_query($conn,"select * from employee_master where ecode='$ecode'");
while($row_emp=mysqli_fetch_array($sql_ename))
{
$ename=$row_emp['ename'];
$eid=$row_emp['id'];
}
//echo "select sum(advance_amount) as advance from employee_advance where employee_id='$eid' and status=''";
$sql_empadvance=mysqli_query($conn,"select sum(advance_amount) as advance from employee_advance where employee_id='$eid' and status='' and advance_date<='$to'");
while($row_empadvance=mysqli_fetch_array($sql_empadvance))
{
$advance=number_format($row_empadvance['advance'],2,'.','');

}

$sql_empadvance_collection=mysqli_query($conn,"select sum(advance_collection_amount) as collection from employee_advance_collection where employee_id='$eid' and status='' and advance_date<='$to'");
while($row_empadvance_collection=mysqli_fetch_array($sql_empadvance_collection))
{
$collection=number_format($row_empadvance_collection['collection'],2,'.','');

}

$balance=$advance-$collection;


$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='$eid' and lad.invoicedate BETWEEN '".$from."' AND '".$to."' 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=".$eid." ");
$data_step12    = mysqli_fetch_array($query_master_data,MYSQLI_ASSOC);

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



$sql_pending_salary=mysqli_query($conn,"select sum(salary_amount) as salaryamt,sum(paidamount)as paidamt from employee_payment where ecode='$ecode' and given_date<='$to' and status=''");
while($row_pending_salary=mysqli_fetch_array($sql_pending_salary))
{

$pending_salary=$row_pending_salary['salaryamt']-$row_pending_salary['paidamt'];

}


$sql_test=mysqli_query($conn,"select count(*) as testcode from employee_payment where fromdate='".$from."' AND todate='".$to."' and ecode='$ecode' and status=''");

//echo "select count(*) as testcode from employee_payment where fromdate='".$from."' AND todate='".$to."' and ecode='$ecode' and status=''";

$row_test    = mysqli_fetch_array($sql_test,MYSQLI_ASSOC);
$invoice_count_test     = $row_test['testcode'];

if($invoice_count_test==0)
{
$result="";

}

else
{
$result="Already Generated";

}


$sql_InvoiceQuery   = mysqli_query($conn,"SELECT count(id) as invoice FROM employee_payment where isdelete='1' and status=''  ");

$rowInvoiceCount    = mysqli_fetch_array($sql_InvoiceQuery,MYSQLI_ASSOC);
$invoice_count      = $rowInvoiceCount['invoice']+1;

?>

<!DOCTYPE html>
<html>
<head>
    <title>Employee Payment</title>
    <?php require 'header.php' ?>



</head>
<body>
    <?php require 'menu.php' ?>
    <div class="container-fluid text-center">
    <div class="row col-sm-12">
      <strong><h3>Employee Payment</h3></strong>
      <h4 style="color:#FF0000;"><?php echo $result;?></h4>
      </div>
    </div>
    <br><br>



<div class="container-fluid">
    <form class="form-horizontal" action="employeePaymentInsert.php" method="post" onSubmit="return checkForm(this);"  name="myForm">
    
    
    <div class="col-sm-12 row">
      <!--<div class=" col-sm-2"><label class="control-label">Sal.No</label></div>
        <div class=" col-sm-2 form-group">
          
         <input type="text" class="form-control"  name="salary_no"  value="<?php echo $invoice_count;?>" readonly>
        </div>-->
        
        <div class=" col-sm-2"> <label class="control-label">Sal.Date</label></div>
        <div class="col-sm-2 form-group">
          
          <input type="text" class="form-control given_date"  name="given_date">
        </div>
        
        <div class=" col-sm-2"> <label class="control-label">Emp.Name</label></div>
        <div class="col-sm-4 form-group">
         
         <input type="text" class="form-control"  name="employee_name"  value="<?php echo $ename;?>" readonly>
      <input type="hidden" class="form-control"  name="ecode"  value="<?php echo $ecode;?>" readonly>
        </div>
        </div>
    
    
    
     <div class="col-sm-12 row">
      <div class=" col-sm-2"><label class="control-label">From Date</label></div>
        <div class=" col-sm-2 form-group">
          
         <input type="text" class="form-control given_date"  name="fromDate" value="<?php echo $from;?>" readonly>
        </div>
        
        <div class=" col-sm-2"> <label class="control-label">To Date</label></div>
        <div class="col-sm-2 form-group">
         
           <input type="text" class="form-control given_date"  name="toDate" value="<?php echo $to;?>" readonly>
        </div>
        
        <div class=" col-sm-2"> <label class="control-label">Total&nbsp;Advance</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control "  name="advance" value="<?php echo $advance;?>" readonly>
        </div>
        </div>
    
    
    <div class="col-sm-12 row">
      <div class=" col-sm-2"><label class="control-label">Collected&nbsp;Amount</label></div>
        <div class=" col-sm-2 form-group">
          
        <input type="text" class="form-control given_date"  name="collected_amt" value="<?php echo $collection;?>" readonly>
        </div>
        
        <div class=" col-sm-2"> <label class="control-label"> Remaining&nbsp;Amount</label></div>
        <div class="col-sm-2 form-group">
         
          <input type="text" class="form-control" placeholder="" name="remaining_amt" value="<?php echo number_format($balance,2,'.','');?>" required readonly>
        </div>
        
        <div class=" col-sm-2"> <label class="control-label">Total&nbsp;Shift</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control "  name="total_shift" value="<?php echo $data_step11['shifttype'];?>" readonly>
        </div>
        </div>
        
         <div class="col-sm-12 row">
          <div class=" col-sm-2"> <label class="control-label">Total&nbsp;Extra.Hrs</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control "  name="total_ot" value="<?php echo $data_step11['extraworkinghrs'];?>" readonly>
        </div>
        
        <div class=" col-sm-2"> <label class="control-label">Salary Amount</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control"  name="salary_amount" value="<?php echo ($data_step12['shiftworkamt']*$data_step11['shifttype']) + ($data_step12['extrahoursamt']*$data_step11['extraworkinghrs']) ; ?>" required readonly>
        </div>
        
         <div class=" col-sm-2"> <label class="control-label">Pending&nbsp;Salary</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control"  name="pending_salary"  required value="<?php echo number_format($pending_salary,2,'.','');?>" readonly>
        </div>
         
         </div>
         
         
         
         <div class="col-sm-12 row">
          <div class=" col-sm-2"> <label class="control-label">Total&nbsp;Salary&nbsp;Amt</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control "  name="total_salary" value="<?php echo (($data_step12['shiftworkamt']*$data_step11['shifttype']) + ($data_step12['extrahoursamt']*$data_step11['extraworkinghrs']))+$pending_salary ; ?>" readonly >
        </div>
        
       
        
         <div class=" col-sm-2"> <label class="control-label">Paid Salary</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control"  name="paidamount"  required >
        </div>
        
          <div class=" col-sm-2"> <label class="control-label">Dedcted&nbsp;Adv.Amount</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control"  name="deducted_amt" id="deducted_amt"  required >
        </div>
         
         </div>
         
    
	<div class="col-sm-12 row">
    
     <div class=" col-sm-2"> <label class="control-label">Remaining Given Amount</label></div>
        <div class="col-sm-2 form-group">
         <input type="text" class="form-control"  name="cash_hand" id="cash_hand"  required >
        </div>
     
        <div class=" col-sm-2"> <label class="control-label">Given By</label></div>
       <div class="col-sm-2 form-group">
    <?php $sql1 = mysqli_query($conn,"select ename from employee_master ");?>
        <select  class="form-control input-lg  select-trip-drivername selectpicker"  name="givenby" id="givenby" required>
        <option value="">Select Employee</option>
        <?php while($row1=mysqli_fetch_array($sql1)){?>
        <option><?php echo $row1['ename'];}?></option>
		</select> 
        </div>
        
        <div class=" col-sm-2"> <label class="control-label">Payment&nbsp;Type</label></div>
         <div class="col-sm-2 form-group">
    
        <select  class="form-control"  name="paytype" id="paytype" required>
		<option value="">Select</option>
            <option >Cash</option>
            <option >Cheque</option>
            <option >Online</option>
		</select>
        </div>
        </div>
        <div class="col-sm-12 row">
        
        <div class=" col-sm-2"> <label class="control-label">Refno</label></div>
   <div class="col-sm-2 form-group"> <input type="text" class="form-control "  name="refno" >
       
        </div>
        
         <div class=" col-sm-2"> <label class="control-label">Remarks</label></div>
   <div class="col-sm-4 form-group"> <input type="text" class="form-control "  name="remarks" >
       
        </div>
    
    </div>
    
    

	<div class="form-group">        
      <div class="col-sm-12 text-center">
        <button type="submit" name="myButton" class="btn btn-default btn-lg">Save</button>
      </div>
    </div>
	
	
    
  </form>   
</div>


    <?php require 'footer.php' ?>
	
	<script>
$(document).ready(function(){
  
  var date_input=$('input[name="given_date"]'); //our date input has the name "date"
	var container=$('form >  .datepicker-given_date').length>0 ? $('form > .datepicker-given_date').parent() : "body";
	date_input.datepicker({ format: 'yyyy-mm-dd',container: container,todayHighlight: true,autoclose: true  });
    
  $('input[name="given_date"]').datepicker( "setDate", new Date()); 

});
</script>
<script>$(document).ready(function(){
    $("select").change(function(){
        $(this).find("option:selected").each(function(){
            var optionValue = $(this).attr("value");
            if(optionValue){
                $(".box").not("." + optionValue).hide();
                $("." + optionValue).show();
            } else{
                $(".box").hide();
            }
        });
    }).change();
});
</script>

<script>

  var checkForm = function(form) {

    //
    // validate form fields
    //

    form.myButton.disabled = true;
    return true;
  };

</script>

</body>
</html>