<?php $act =4; ?>
<!DOCTYPE html>
<!-- =========================================================
* ekseba Admin Template - Pro | v1.0.0
==============================================================

* Product Page: https://ekseba.com
* Created by: ekseba
* License: ekseba.com
* Copyright ThemeSelection (https://ekseba.com)

=========================================================
 -->
<!-- beautify ignore:start -->
<html lang="en"  class="light-style layout-menu-fixed"  dir="ltr"  data-theme="theme-default"  data-assets-path="assets/"  data-template="vertical-menu-template-free">
  <head>
    
    <?php include './html/head.php'; ?>
      <style>
#customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%; font-size: 12px;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 5px 2px 5px 2px; font-size: 12px;
  text-align: center;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #04AA6D;
  color: white; font-size: 12px;
}
      </style>
  </head>

  <body>
    <!-- Layout wrapper -->
    <div class="layout-wrapper layout-content-navbar">
      <div class="layout-container">
        <!-- Menu -->
        <?php 
        include './menue.php';
        ?>
        <!-- / Menu -->

        <!-- Layout container -->
        <div class="layout-page">
          <!-- Navbar -->

          <?php include './navbar.php'; ?>

          <!-- / Navbar -->

          <!-- Content wrapper -->
          <div class="content-wrapper">
            <!-- Content -->

            <!-- Content wrapper -->
          <div class="content-wrapper">
            <!-- Content -->

            <div class="container-xxl flex-grow-1 container-p-y">
              <h4 class="fw-bold py-3 mb-4"><span class="text-muted fw-light">Forms/</span> Plots Information</h4>

              <!-- Basic Layout & Basic with Icons -->
              <div class="row">
                <!-- Basic Layout -->
                <div class="col-xl">
                  <div class="card mb-4">
                    <div class="card-header d-flex justify-content-between align-items-center">
                      <h5 class="mb-0">Payment Information</h5>
                      
                      <span class="float:right;" ><a class="btn btn-primary btn-block btn-sm col-lg-2 float-right"  style="width: 250px; height: 50px; font-size: 25px;" href="javascript:void(0)" id="new_student">
					<i class="fa fa-plus"></i> Add New Student 
				</a></span>
                  
                    </div>
                    <div class="card-body">
                     

                <div class="col-xxl">
                    
                    
                    <ul class="nav nav-pills flex-column flex-md-row mb-3">
                    <li class="nav-item">
                      <a class="nav-link active" href="javascript:void(0);"><i class="bx bx-user me-1"></i> Salary (General) </a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="salary-staff.php"><i class="bx bx-user me-1"></i> Salary (Staff) </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="salary-operator.php"><i class="bx bx-user me-1"></i> Salary (Operator) </a>
                    </li>
                    
                  </ul>
                    
                  <div class="card mb-4">
                     <table style="width:100%;" id="customers" >                                
                        <tr style="font-weight: bold; background-color: #D3D3D3; text-align: center;">
                        <td style="text-align: center;padding: 5px; width: 20px;">SL.No.</td>   
                        <td style="width: 10px;">ID</td>
                        <td style="width:200px; text-align: center; padding-left: 5px; padding-right: 5px;">
                        Employee&nbsp;Name
                        </td>
                        <td style="width:100px; text-align: center; padding-left: 5px; padding-right: 5px;">
                        Designation
                        </td>
                        <td style="width:30px; text-align: center; padding-left: 5px; padding-right: 5px;">
                        Total Day
                        </td>

                <?php

                include './login/connect.php';
                $timezone = "Asia/Calcutta";
                $Month=03;$Year=2021;
                $payofmonth = $Month.'-'.$Year;
                
                function calculateOvertimeHours($start, $end) {
                    $startTimestamp = strtotime($start);
                    $endTimestamp = strtotime($end);
                    $overtimeSeconds = $endTimestamp - $startTimestamp;
                    $overtimeHours = $overtimeSeconds / 3600; // 3600 seconds in an hour
                    return $overtimeHours;
                }
                
                date_default_timezone_set($timezone);
                $timestamp = mktime(0,0,0,$Month,1,$Year);
                $maxday = date("t",$timestamp);

                //echo "<td></td>";
                for ($k=1; $k<= $maxday; $k++) {  
                    $kv = sprintf('%02d', $k);
                    echo "<td hidden style=''>$kv</td>";  
                }

                /* Default Date Indexing ends here. */
                ?>
                 
                <td>TOT.P</td>
                <td>TOT.A</td>
                <td>Monthly Salary</td>
                <td>Net Salary</td>
                <td>Over Hour</td>
                <td>OT Rate</td>
                <td>OT Taka</td>
                <td>Attan. Bonus</td>
                <td>Gross Salary</td>
                
                <td>Pay</td>
                
            </tr>
            <?php
                include './login/connect.php';
            //Getting all employee details according roster assign table.
            $sqlRA = "SELECT * from employee_details where DivisionDbKey = 'ok' and Status='ACTIVE' ORDER BY EmployeeId ASC";
            $resultRA = mysqli_query($conn, $sqlRA);
            $i = 1;
            while ($rowRA = mysqli_fetch_array($resultRA, MYSQLI_ASSOC)) {
                $EmployeeDbKey = $rowRA['DivisionDbKey'];
                $EmployeeId = $rowRA['EmployeeId'];
                $EmployeeName = $rowRA['EmployeeName']; 
                $Designation = $rowRA['designation'];
                $Monthly_Salary = $rowRA['Monthly_Salary'];
                echo "<tr>";
                echo "<td style='text-align:center;'>$i</td>";
                echo "<td style='width:60px;'>$EmployeeId</td>";
                echo "<td>$EmployeeName</td>";
                echo "<td>$Designation</td>";
                echo "<td>$maxday</td>";
                
                $PCount = 0;$ACount = 0;$HLFCount = 0;$WOCount=0;$HLDCount=0;
                
$query =  "SELECT start_time, end_time FROM employee_attendance where EmployeeId= '$EmployeeId' and Status='ACTIVE' ORDER BY EmployeeId ASC";
$result = $conn->query($query);

// Calculate the monthly total overtime
$monthlyTotalOvertime = 0;

while ($row = $result->fetch_assoc()) {
    $start = $row['start_time'];
    $end = $row['end_time'];

    $overtimeHours = calculateOvertimeHours($start, $end);
    $monthlyTotalOvertime += $overtimeHours;
}
                
                for ($kk=1; $kk<= $maxday; $kk++) {  
                    $kkv = sprintf('%02d', $kk);
                    $CurrentOPDate = $kkv."/0".$Month."/".$Year; 

                    $sqlRAD = "SELECT * from employee_attendance where EmployeeId= '$EmployeeId' and Date='$CurrentOPDate' and Status='ACTIVE' ORDER BY EmployeeId ASC";
                    $resultRAD = mysqli_query($conn, $sqlRAD);
                    
                    
                    $rowcountRAD = mysqli_num_rows($resultRAD);
                    if ($rowcountRAD != 0) {
                        
                        while ($rowRAD = mysqli_fetch_array($resultRAD, MYSQLI_ASSOC)) {
                            
                            $Attendance = $rowRAD['Attendance'];  
                            
                            $colorcode="";
                            if($Attendance == "P" || $Attendance == "p"){
                               $PCount = $PCount + 1;
                               $colorcode = "background-color:#00C0EF; color:#FFF;"; 
                            }                                                                                    
                            elseif ($Attendance == "WO" || $Attendance == "wo" || $Attendance == "Wo") {
                                 $WOCount = $WOCount + 1;
                                $colorcode = "background-color:#2980B9; color:#FFF;";
                            }
                            elseif ($Attendance =="HLD" || $Attendance =="hld" || $Attendance == 'Hld') {
                                $HLDCount = $HLDCount + 1;
                                $colorcode = "background-color:#FFD455;color:#000;";
                            }
                            elseif ($Attendance == "HLF" || $Attendance == "hlf" || $Attendance == "Hlf") {
                                $HLFCount = $HLFCount + 1;
                                $colorcode = "background-color:#931336; color:#FFF;";
                            } 
                            elseif ($Attendance == "A" || $Attendance == "a") {
                                $ACount = $ACount + 1;
                                $colorcode = "background-color:#F00; color:#FFF;";
                            } 
                            else {
                                   
                            }
                            echo "<td hidden style='text-align:center;font-weight:bold;$colorcode' title=''>$Attendance</td>";
                        }
                    }
                    else{
                        $ACount = $ACount + 1;
                        $colorcode = "background-color:#F00; color:#FFF;";
                        echo "<td hidden style='text-align:center;font-weight:bold;$colorcode'>0</td>";
                    }
                }
                ?>                                                              
                <td style="text-align: center;font-weight: bold;color:green;"><?php echo $PCount; ?></td>
                <td style="text-align: center;font-weight: bold;color:red;"><?php echo $ACount; ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php echo $Monthly_Salary; ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php echo $Net_Salary = intval(($Monthly_Salary/$maxday)*$PCount); ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php 
                  $totalworking = $PCount*8;
                 if($monthlyTotalOvertime>$totalworking){
                     $overtim = $monthlyTotalOvertime-$totalworking;
                     echo intval($overtim);
                 } else {
                     echo '0';    
                 };
                ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php echo "14"; ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php echo $ot = intval($overtim*14); ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php echo "0"; ?></td>
                <td style="text-align: center;font-weight: bold;color:#F00;"><?php  
                
                if($monthlyTotalOvertime>$totalworking){
                     $overtim = $monthlyTotalOvertime-$totalworking;
                    echo $payamount = intval($Net_Salary+$ot);
                 } else {
                     echo $payamount = intval($Net_Salary);    
                 };
                
                ?></td>
                
                    
<?php 

$sql = "SELECT * FROM salary_pay where EmployeeID='$EmployeeId'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
      $com = $row['complete'];
    if($com==1){
        ?>
                <td style='background-color: green; color: #FFF;'>Paid</td>
        <?php
    } else {
        ?>
         <input type="button" name="view" value="Pay" id="<?php echo $EmployeeId.'.'.$payamount.'.'.$EmployeeName.'.'.$payofmonth.'.salary'; ?>" class="btn btn-info btn-xs view_data" />           
        <?php
    }
      
  }
} else {
  ?><td style='background-color: red;'>
         <input type="button" name="view" value="Pay" id="<?php echo $EmployeeId.'.'.$payamount.'.'.$EmployeeName.'.'.$payofmonth.'.salary'; ?>" class="btn btn-info btn-xs view_data" />            
</td> 
 <?php
}




?>



                    
                              
                <td hidden="" style="text-align: center;font-weight: bold;color:#F00;"><?php echo $WOCount; ?></td>
                <td hidden="" style="text-align: center;font-weight: bold;color:#F00;"><?php echo $PCount*20; ?></td> 
                <!--<td style="text-align: center;font-weight: bold;color:#F00;"><?php //echo $HLDCount; ?></td> -->                                           
                <?php
                echo "</tr>";                                                                       
                $i = $i + 1;
            }        
            ?>
        </table>
                  </div>
                </div>
                <!-- Basic with Icons -->
  
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <!-- / Content -->
          </div>
            <!-- Footer -->
           <?php require './footer.php'; ?>
<style>
	
	td{
		vertical-align: middle !important;
	}
	td p{
		margin: unset
	}
	img{
		max-width:100px;
		max-height:150px;
	}
</style>
<script>
	$(document).ready(function(){
		$('table').dataTable()
	})
	$('#new_student').click(function(){
		uni_modal("New Student ","manage_student.php","mid-large")
		
	})
	$('.edit_student').click(function(){
		uni_modal("Manage Student  Details","manage_student.php?id="+$(this).attr('data-id'),"mid-large")
		
	})
	$('.delete_student').click(function(){
		_conf("Are you sure to delete this Student ?","delete_student",[$(this).attr('data-id')])
	})
	function delete_student($id){
		start_load()
		$.ajax({
			url:'ajax.php?action=delete_student',
			method:'POST',
			data:{id:$id},
			success:function(resp){
				if(resp==1){
					alert_toast("Data successfully deleted",'success')
					setTimeout(function(){
						location.reload()
					},1500)

				}
			}
		})
	}
</script>

 
