<?php

include 'load.php';



?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard

">
<meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard

">
<meta name="author" content="Payroll | Pos & Invetory System | Dashboard

">
<meta name="robots" content="noindex, nofollow">

<title>List Salary | Payroll | Pos & Invetory System | Dashboard

</title>


<?php
require_once 'headercss.php';
?>
</head>
<body>
<div id="global-loader">
<div class="whirly-loader"> </div>
</div>

<div class="main-wrapper">
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>

<div class="page-wrapper">
<div class="content">
<div class="page-header">
<div class="page-title">
<h4>List Salary</h4>
<h6>Create Salary</h6>
</div>
</div>

<div class="card">
<div class="card-body">
<form method="POST" enctype="multipart/form-data">
									<?php

									if(isset($errMSG)){

									?>
									
									<div class="alert alert-danger border-0 bg-danger alert-dismissible fade show py-2">
									<div class="d-flex align-items-center">
										<div class="font-35 text-white"><i class="bx bxs-messcredit-square-x"></i>
										</div>
										<div class="ms-3">
											<h6 class="mb-0 text-white">Error Alerts</h6>
											<div class="text-white"> <?php echo $errMSG; ?>!</div>
										</div>
									</div>
									<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
									</div>
									
									<?php

									}
	
									else if(isset($successMSG)){

									?>
									
									<div class="alert alert-success border-0 bg-success alert-dismissible fade show py-2">
									<div class="d-flex align-items-center">
										<div class="font-35 text-white"><i class="bx bxs-check-circle"></i>
										</div>
										<div class="ms-3">
											<h6 class="mb-0 text-white">Success Alerts</h6>
											<div class="text-white"><?php echo $successMSG; ?>!</div>
										</div>
									</div>
									<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
									</div>
									
									<?php

									}

									?>	

<div class="row">
<div class="col-lg-4 col-sm-6 col-12">
<div class="form-group">
<label>Months</label>
<select class="form-select" name="month" required>
  <option value="">Select Month</option>
 <?php 
    $monthlist = mysqli_query($conn, "SELECT * FROM working_days");
    if ($monthlist->num_rows > 0) {
        while($row = $monthlist->fetch_assoc()) { ?>
            <option value="<?php echo $row['working_days_id']; ?>">Month - <?php echo $row['year_and_month']; ?> | Days - <?php echo $row['days']; ?></option>
       <?php }
    }
 
 
 ?>
</select>

</div>
</div>

<div class="col-lg-1 col-sm-12 col-12" style="margin-top: 30px;">

<button type="submit" name="search" class="btn btn-submit me-2">Search</button>
</div>
</div>
</form>


<div class="table-responsive">
<table class="table" id="betable">
<thead>
<tr>
<th>#</th>
<th>Action</th>
<th>User</th>
<th>Month</th>
<th>Total Working Hours</th>
<th>OT Hours</th>
<th>OT</th>
<th>Payments</th>
<th>Deductions</th>
<th>Commiision</th>
<th>Salary</th>
</tr>
</thead>
<tbody> 


<?php 
if(isset($_POST['search']))

{		

		$month = $_POST['month'];
		$wdaysinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM working_days WHERE working_days_id='$month'"));
    	$workingmonth = $wdaysinfo['year_and_month'];
        $days = $wdaysinfo['days'];

        $date = new DateTime($workingmonth . '-01'); 
        $endDate = new DateTime($workingmonth . '-01'); 
        $endDate->modify('last day of this month'); 
        
        $interval = new DateInterval('P1D'); 
        $period = new DatePeriod($date, $interval, $endDate->modify('+1 day')); 
        
       // foreach ($period as $day) {
       //     echo $day->format('Y-m-d') . "\n"; 
        //}
        $startOfMonth = $workingmonth . '-01 00:00:00';
        $endOfMonth = date("Y-m-t 23:59:59", strtotime($startOfMonth));

       
        $users = mysqli_query($conn, "SELECT * FROM mobileusers WHERE basicsalary > '0'");
        $c = 1;
        if ($users->num_rows > 0) {
            while($row = $users->fetch_assoc()) {
                $userid = $row['user_id'];
                $basicsalary = $row['basicsalary'];
                $dailyworkinhours = $row['dailyworkinhours'];
                $othourrate = $row['othourrate'];
                $wh = $days * $dailyworkinhours;
                $hoursarlary = $basicsalary / $wh;
                $normalworkinghours = 0;
                $totalHours = 0;
                $totalWorkingSeconds = 0;
                $othours = 0;
                foreach ($period as $day) {
                         $dbdate = $day->format('Y-m-d'); 
                         $dailyhours = 0;
                         if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$userid' AND DATE(date_time)='$dbdate'"))==2){
                           $inoutinfo1 =  mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$userid' AND in_or_out='0' AND DATE(date_time)='$dbdate'"));
                           $inoutinfo2 =  mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$userid' AND in_or_out='1' AND DATE(date_time)='$dbdate'"));
                           $inTime = new DateTime($inoutinfo1['date_time']);
                           $outTime = new DateTime($inoutinfo2['date_time']);
                           $interval = $inTime->diff($outTime);

                          
                           $seconds = ($interval->h * 3600) + ($interval->i * 60) + $interval->s;
                           $dailyhours = floor($seconds / 3600);
                           if($dailyhours > $dailyworkinhours){
                                $othours += $dailyhours -  $dailyworkinhours;
                           }
                                $totalWorkingSeconds += $seconds;
                                $totalHours = floor($totalWorkingSeconds / 3600);
                           
                           
                         }
                }
               
               ?>
               
<tr>
    <td><?php echo $c; ?></td>
    <td><a href="salary-slip.php?user=<?php echo $userid; ?>&month=<?php echo $month; ?>" class="btn btn-block btn-outline-dark active btn-sm text-white" title="salary slip" onClick="JavaScript:return confirm('Are your sure view salary slip?');"><i class="fa fa-print" data-bs-toggle="tooltip" title="" data-bs-original-title="Recipt" aria-label="fa fa-print"></i></a></td>
    <td><?php  echo  $row['fullname']; ?></td>             
   
    <td><?php echo $workingmonth; ?></td>
    <td><?php echo $totalHours; ?></td>
    <td><?php echo $othours; ?></td>
    <td><?php echo $othours*$row['othourrate']; ?></td>
    <td>
        <?php $payments = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount FROM payments_and_deductions WHERE payments_or_deductions='1' AND user_Id='$userid' AND added_date >= '$startOfMonth' AND added_date <= '$endOfMonth'")); 
            echo $payments['amount'];
        ?>
    </td>
    <td>
    <?php $deductions = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount FROM payments_and_deductions WHERE payments_or_deductions='2' AND user_Id='$userid' AND added_date >= '$startOfMonth' AND added_date <= '$endOfMonth'"));
        
         
         echo $deductions['amount'];
    ?>
    </td>
    <td>
    <?php $commiision = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(admin_commiision) as admin_commiision FROM mobileorders WHERE admin_id='$userid' AND create_at >= '$startOfMonth' AND create_at <= '$endOfMonth'"));
        
         
         echo $commiision['admin_commiision'];
    ?>
    </td>
    <td>
        <?php 
           $normalworkinghours = $totalHours - $othours;
           $sal =  $hoursarlary * $normalworkinghours ;
           $sal = $sal + $payments['amount'];
           $sal = $sal - $deductions['amount'];
           $sal = $sal + $commiision['admin_commiision'];

           echo $sal;
        ?>

    </td>
</tr>








<?php
$c++;
            }
        }
}



?>

</tbody>
</table>
</div>




</div>
</div>

</div>
</div>
</div>

<?php
require_once 'footerjs.php';
?>
</body>
</html>