<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
include ('../inc/repfunctions.php');

function check_exsal($emid,$smonth,$syear,$key,$cl=''){
global $con;
$sql="SELECT COALESCE ((SELECT ".$key." FROM tbl_salarysheet WHERE empid='$emid' AND year='$syear' AND month='$smonth'),0) AS rdata";	
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$row = mysqli_fetch_array($result);
if($cl!=''){
if($row['rdata']>0){
return $row['rdata'];    
}else{
return $cl;    
}    
}else{    
return $row['rdata'];
}
}

function get_leaveday($empid,$dstart,$smonth,$key=''){
global $con;
$lday=0;
$id='';    
$sql="SELECT * FROM tbl_leaverequest WHERE empid='$empid' AND (CAST(lfrom AS DATE) BETWEEN '$dstart' AND '$smonth' OR CAST(lto AS DATE) BETWEEN '$dstart' AND '$smonth') AND status='1'";	
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
if($result->num_rows > 0){
while ($row=mysqli_fetch_array($result)){
if($row['lto']<=$smonth){
if($row['lfrom']>=$dstart){    
$lday+=$row['ldays'];
}else{
$date1=date_create($dstart);
$date2=date_create($row['lto']);
$diff=date_diff($date1,$date2);
$lday+= intval(($diff->format("%R%a"))+1);     
}
}else{
$date1=date_create($row['lfrom']);
$date2=date_create($smonth);
$diff=date_diff($date1,$date2);
$lday+= intval(($diff->format("%R%a"))+1);    
}    
}   
}else{
$lday=0;   
}
return $lday;     
}

if(isset($_POST['delsal'])){
$id=$_POST['delsal'];
$sql="DELETE FROM tbl_salarysheet WHERE id='$id'";
$results = mysqli_query($con, $sql);
$efid=mysqli_affected_rows($con);
if($efid>0){
echo 'Emplyee Salary Delete Sucess!!!';    
}else{
echo 'Emplyee Salary Delete Fail!!!';     
}    
exit;    
}



if(isset($_POST['salary'])){   
if(isset($_POST['branch']) ){
$year = $_POST['year'];
$month = $_POST['month'];    
$wbrid = $_POST['branch'];
$output = '';
    
$queirys = "SELECT tbl_salarysheet.id,tbl_employe.id AS eid,tbl_employe.name,tbl_employe.salary,tbl_employe.job,tbl_designation.name AS design,tbl_salarysheet.absamo,tbl_salarysheet.lateamo,tbl_salarysheet.levamo,tbl_salarysheet.pdayamo,tbl_salarysheet.pamo,tbl_salarysheet.phror,tbl_salarysheet.tohr,tbl_salarysheet.toam,tbl_salarysheet.tsalary,tbl_salarysheet.pstatus,tbl_salarysheet.paid FROM tbl_employe LEFT JOIN tbl_designation ON tbl_designation.id=tbl_employe.desid LEFT JOIN tbl_salarysheet ON tbl_salarysheet.empid=tbl_employe.id WHERE tbl_salarysheet.month='".$month."' AND tbl_salarysheet.year='".$year."' AND tbl_employe.wbrid='".$wbrid."' ORDER BY tbl_employe.id ASC";

$results = mysqli_query($con, $queirys);

while ($rowcu=mysqli_fetch_array($results)){    

$output .='<tr>';   
$output .= '<td class="center">'.count_id().'</td>';
$output .= '<td>'.$rowcu['name'].'<input type="hidden" name="data['.$rowcu['id'].'][id]" value="'.$rowcu['id'].'" class="form-control" autocomplete="off">'.'</td>';
$output .= '<td>'.$rowcu['design'].'</td>';
$output .= '<td>'.$rowcu['salary'].'</td>';    
$output .= '<td>'.$rowcu['absamo'].'</td>'; 
$output .= '<td>'.$rowcu['lateamo'].'</td>'; 
$output .= '<td>'.$rowcu['levamo'].'</td>'; 
$output .= '<td>'.$rowcu['pdayamo'].'</td>'; 
$output .= '<td>'.$rowcu['pamo'].'</td>'; 
$output .= '<td>'.$rowcu['tsalary'].'</td>';
$output .= '<td>'.$rowcu['phror'].'</td>';
$output .= '<td>'.$rowcu['tohr'].'</td>';
$output .= '<td>'.$rowcu['toam'].'</td>';			
$output .='</tr>';  
}
}
echo $output;   
exit; 
}