<?php 

include('../dbconnect.php');
ob_start();
session_start();

if( !isset($_SESSION['user']) ) {
 header("Location: ../index.php");
 exit;
}
$empid = $_SESSION['user'];

if(isset($_POST['fileot'])){
  $dates= $_POST['dates'];
  $dates = date('Y-m-d', strtotime($dates));
  $datefiled = date('Y-m-d');
  $sched = $_POST['sched'];
  $att = $_POST['att'];
  $type = $_POST['type'];
  $duration = $_POST['dur'];


 $filesql = "INSERT INTO `ot`( `date`, `datefiled`, `schedule`, `attendance`, `type`,`hrs`,`empid`,`status`) 
  VALUES ('$dates','$datefiled','$sched','$att','$type','$duration','$empid','0')";

  if(mysqli_query($conn, $filesql)){

    $alert='<div class="alert alert-success" role="alert"> OT successfully submitted </div>';
    header('location:fileot.php?alert='.$alert);

  }else{

    echo mysqli_error($conn);
  }

  
}


function timedeci($time){

  $hms = explode(":", $time);
  $decimal = $hms[0] + ($hms[1]/60) + ($hms[2]/3600);



 $numhrs = substr($decimal, strrpos($decimal, '.') ,2);

$dur = substr($numhrs, strrpos($numhrs, '.')+1);

if($dur >= 5){

  $decimal = substr($decimal, 0, strrpos($decimal, '.')).'.5';
}else{
  
   $decimal = number_format($decimal);


}




  



  return ($decimal);


}

?>
<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Employee | Over Time</title>

    <!-- Bootstrap core CSS-->
    <link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">

    <!-- Page level plugin CSS-->
    <link href="../vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">

    <!-- Custom styles for this template-->
        <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">
    <link rel="icon" href="../images/logo.jpg" type="image/gif" sizes="16x16">

  </head>

  <body id="page-top" onload=display_ct();>

    <?php include('uppernav.php')?>



      <!-- Sidebar -->
      
      <?php 
       $tools = "active";
       $ot = "active";
      include('sidebar.php');
      ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">

            <li class="breadcrumb-item"> <a href="ot.php">Over Time </a></li>
            <li class="breadcrumb-item active">File Over Time</li>
          </ol>
          <div class="row p-3">
            <div class="col-lg-12">
                <h5>Over Time</h5>
            </div>
            <div class="col-lg-12">
              <?php echo $_GET['alert']; ?>
                <table class="table" id="otlist">
                    <thead class="thead-dark" >
                        <th>Date</th>
                        <th>Schedule</th>
                        <th>Attendance PST</th>
                        <th>Type</th>
                        <th>No. of Hrs.</th>
                        <th>Action</th>
                    </thead>
                    <tbody>
                        <?php 

                        
          



                        $otsql = "SELECT * FROM tbl_login WHERE empid='$empid'";
                        $otres = mysqli_query($conn, $otsql);
                        $year = date('Y');

                        if(mysqli_num_rows($otres) > 0){

                            while($otrow = mysqli_fetch_assoc($otres)){

                                $id = $otrow['id'];
      
                                  $attendance = $otrow['date'];
      
                                  $weekno = date('W', strtotime($attendance));
      
                                  $getmnlhrs = "SELECT * FROM schedule WHERE empid='$empid' AND week='$weekno' AND year='$year' AND starttime != 'OFF' AND starttime !='HOLIDAY OFF'";
                                  $getmnlhrsres = mysqli_query($conn, $getmnlhrs);
                                  $mnlrow = mysqli_fetch_assoc($getmnlhrsres);
                                  $mnlshced = $mnlrow['starttime'];
      
      
                                  $mnltopst = new DateTime($mnlshced, new DateTimeZone('Asia/Manila'));
                                  $mnltopst->setTimezone(new DateTimeZone('America/Los_Angeles'));
                                  $MNLpstsched = $mnltopst->format('H:i');
      
      
                                  $attendanceMNL = new DateTime($attendance.' '.$MNLpstsched, new DateTimeZone('America/Los_Angeles'));
                     
      
                                  $attendanceMNL->setTimezone(new DateTimeZone('Asia/Manila'));
                                   $attendanceMNL->format('Y-m-d H:i') ;
                                 $dateattMNL = $attendanceMNL->format('Y-m-d');
                                 $timeoutattMNL = $attendanceMNL->format('H:i');
      
      
                                  $schedsql = "SELECT * FROM schedule WHERE date ='$dateattMNL' AND empid='$empid'";
                                  $schedres = mysqli_query($conn, $schedsql);
                                  $schedrow = mysqli_fetch_assoc($schedres);
                                  $starttime = $schedrow['starttime'];
                                  $endtime = $schedrow['endtime'];
                                  $scheddate = $schedrow['date'];
                                  $schedcode = $schedrow['schedcode'];
      
                                  $schedstart = $scheddate.' '.$starttime;
      
                                  $schedend = $scheddate.' '.$endtime;
      
      
                                  ///check if OT is already filed 
               
      
                                    if($starttime == 'HOLIDAY OFF'){


                                      $type = 'RDOT';
                                      $RDOTdate= date('Y-m-d', strtotime($otrow['date']));

                                      $sqlRDOT = "SELECT * FROM ot WHERE date='$RDOTdate' AND empid='$empid' AND type='$type' AND status != '5' ";
                                      $RDOTres = mysqli_query($conn, $sqlRDOT);

                                      if(mysqli_num_rows($RDOTres) > 0){

                                         
                                            


                                      }else{

                                      echo '<tr><td>'.$otrow['date'].'</td>';
                                      echo '<td> HOLIDAY OFF </td>';
                                      echo '<td>'.$otrow['timein'].' - '.$otrow['timeout'].'</td>';
                                      echo '<td> RDOT </td>';
                                      $timein = new DateTime($otrow['timein']);
                                      $timeout = new DateTime($otrow['timeout']);
                                      $nohrs =  $timeout->diff($timein);
                                      $othrs = $nohrs->format('%H:%I');
                                      echo '<td>'.timedeci($othrs).'</td>';
                                  
                                      echo '<td><a href="#accept'.$id.'" data-toggle="modal"  class="btn btn-success btn-sm"> <i class="fa fa-check"> </i> File  </a> |
                                          
                                          
        
                                          
                                      <a href="viewutil.php?date='.$otrow['date'].'" class="btn btn-primary btn-sm"> <i class="far fa-eye"></i> View Utilization </a></td> </tr>';
                                      }


        
                                    }else if ($starttime == 'OFF'){
                                    

                                      $type = 'RDOT';
                                      $RDOTdate= date('Y-m-d', strtotime($otrow['date']));

                                      $sqlRDOT = "SELECT * FROM ot WHERE date='$RDOTdate' AND empid='$empid' AND type='$type' AND status != '5' ";
                                      $RDOTres = mysqli_query($conn, $sqlRDOT);

                                      if(mysqli_num_rows($RDOTres) > 0){

                                         
                                            


                                      }else{

                                      echo '<tr><td>'.$otrow['date'].'</td>';
                                      echo '<td>  OFF </td>';
                                      echo '<td>'.$otrow['timein'].' - '.$otrow['timeout'].'</td>';
                                      echo '<td> RDOT </td>';
                                      $timein = new DateTime($otrow['timein']);
                                      $timeout = new DateTime($otrow['timeout']);
                                      $nohrs =  $timeout->diff($timein);
                                      $othrs = $nohrs->format('%H:%I');
                                      echo '<td>'.timedeci($othrs).'</td>';
                                  
                                      echo '<td><a href="#accept'.$id.'" data-toggle="modal"  class="btn btn-success btn-sm"> <i class="fa fa-check"> </i> File  </a> |
                                          
                                          
        
                                          
                                      <a href="viewutil.php?date='.$otrow['date'].'" class="btn btn-primary btn-sm"> <i class="far fa-eye"></i> View Utilization  </a></td> </tr>';
                                      }



        
                                    }else{
        
        
                                      $schedstartPST = new DateTime($schedstart, new DateTimeZone('Asia/Manila'));
                    
        
                                      $schedstartPST->setTimezone(new DateTimeZone('America/Los_Angeles'));
          
                                     $startschedPST = $schedstartPST->format('h:i A');
      
                            
          
          
                                      $schedendPST = new DateTime($schedend, new DateTimeZone('Asia/Manila'));
                      
          
                                      $schedendPST->setTimezone(new DateTimeZone('America/Los_Angeles'));
                                      $endschedPST = $schedendPST->format('h:i A');
          
          
          
          
                                      //OT
                                      $schedentime = new DateTime($endschedPST);
                                      $attendancetimeout = new DateTime($otrow['timeout']);
        
                                      //echo $endschedPST.' '.$otrow['timeout'].$otrow['date'].'<br>';
      
      
                                      //PRE OT  
                                      $schedST = new DateTime($startschedPST);
                                      $attST = new DateTime($otrow['timein']);
                                      
      
      
                                     $startschedPST.' '.$otrow['timein'].'   '.$otrow['date'].'<br>';
      
                                      ///OTT
      
                                     $OT = $attendancetimeout->diff($schedentime);
                                     $othrs = $OT->format('%H:%I');
      
                                     $hrot = new DateTime($othrs);
                                     $thirty = new DateTime('01:00');
      
      
                                     //PRE OT
      
      
      
                                     $preOT = $attST->diff($schedST);
                                     $pothrs = $preOT->format('%H:%I');
      
                                     
      
                                    $hrspre = new DateTime($pothrs);
                                    $thirty = new DateTime('01:00');
      
        
        
                               
          
                                      if(($attendancetimeout > $schedentime) &&  ($hrot >= $thirty) ){
                                        $type = 'Post OT';
                                        $hrs = $othrs;


                                        $POTtype = 'Post OT';
                                        $POThrs = $othrs;
                                        $POTdate = date('Y-m-d', strtotime($otrow['date']));
                                    
        
        
      
        
        
                                        
                    



                                          $sqlPOT = "SELECT * FROM ot WHERE date='$POTdate' AND empid='$empid' AND type='$POTtype' AND status != '5' ";

                                          $POTres = mysqli_query($conn, $sqlPOT);
                                          if(mysqli_num_rows($POTres) > 0){

                                         
                                            


                                          }else{

                                           

                                                    
                                          echo '<tr><td>'.$otrow['date'].'</td>';
                                          echo '<td>'. $startschedPST.' - '. $endschedPST.'</td>';
                                      
                                          echo '<td>'. $otrow['timein'].' - '. $otrow['timeout'].'</td>';
                                          echo '<td>'.$type.'</td>';
          
                                          echo '<td>'.timedeci($othrs).'</td>';
                
                                          echo '<td>';

                                            echo '    <a href="#POT'.$id.'" data-toggle="modal"  class="btn btn-success btn-sm"> <i class="fa fa-check"> </i> File  </a> |';

                                            echo'


           
                                            <a href="viewutil.php?date='.$otrow['date'].'" class="btn btn-primary btn-sm"> <i class="far fa-eye"></i> View Utilization </a></td> </tr>';

                                          }




        
        
                                      
      
      
                                        } 
                                        
                                        
                                        if(($attST < $schedST) && ($hrspre >= $thirty)){  //ELSE IF PRE OT 
                                          $type = 'Pre OT';
                                          $hrs = $pothrs;
                                          $PREtype = 'Pre OT';
                                          $PREdate =date('Y-m-d', strtotime($otrow['date']));
                                         
                                        


                                            
                                          $sqlPRE = "SELECT * FROM ot WHERE date='$PREdate' AND empid='$empid' AND type='$PREtype' AND status != '5' ";

                                          $PREres = mysqli_query($conn, $sqlPRE);
                                          if(mysqli_num_rows($PREres) > 0){

                                          }else{                                                    
                                            echo '<tr><td>'.$otrow['date'].'</td>';
                                            echo '<td>'. $startschedPST.' - '. $endschedPST.'</td>';
                                        
                                            echo '<td>'. $otrow['timein'].' - '. $otrow['timeout'].'</td>';
                                            echo '<td>'.$type.'</td>';
            
                                            echo '<td>'.timedeci($hrs).'</td>';
                  
                                            echo '<td>';
  
                     

                                            echo '<a href="#PRE'.$id.'" data-toggle="modal"  class="btn btn-success btn-sm"> <i class="fa fa-check"> </i> File  </a> | ';
                                            echo'<a href="viewutil.php?date='.$otrow['date'].'" class="btn btn-primary btn-sm"> <i class="far fa-eye"></i> View Utilization </a></td> </tr>';

                                          }
          

                                          
      
                                      
                                      }
        
          
                                    
      
      
                                  }
      
      
      
                                
        
        
                                  
        
        
                                
                                
                                
                                 ?>
      
                                  
      
                                    
                                  <div id="accept<?php echo $id; ?>" class="modal fade" role="dialog">
                                      <form method="post" class="form-horizontal" role="form">
                                          <div class="modal-dialog modal-md modal-dialog-centered">
                                              <!-- Modal content-->
                                              <div class="modal-content">
                                                  <div class="modal-header">
                                                      <h4 class="modal-title">File Over Time </h4>
                                                      <button type="button" class="close" data-dismiss="modal">&times;</button>
      
                                                  </div>
                                                  <div class="modal-body">
                                                      <input type="hidden" name="att" value="<?php echo $id; ?>">
      
                                                      <input type="hidden" name="dates" value="<?php echo $attendance; ?>">
                              
                                                      <input type="hidden" id="sched" name="sched" value="<?php echo $schedcode?>" >
      
                                                      <input type="hidden" id="type" name="type" value="<?php echo $type?>" >
                                                      <input type="hidden" id="dur" name="dur" value="<?php echo timedeci($othrs) ?>" >
      
      
                                                      Are you sure you want to file OT?
      
      
                                                  </div>
      
                                                  <div class="modal-footer">
                                                      <button type="submit" class="btn btn-success" name="fileot"><i
                                                              class="fas fa-check"> </i> Yes</button>
                                                      <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                                              class="fas fa-close"> </i> Cancel</button>
                                                  </div>
                                              </div>
                                          </div>
                                          <input type="text" id="pagenoaccept" name="pagenoaccpet" >
                                      </form>
                                  </div>


                                  <div id="POT<?php echo $id; ?>" class="modal fade" role="dialog">
                                      <form method="post" class="form-horizontal" role="form">
                                          <div class="modal-dialog modal-md modal-dialog-centered">
                                              <!-- Modal content-->
                                              <div class="modal-content">
                                                  <div class="modal-header">
                                                      <h4 class="modal-title">File Over Time </h4>
                                                      <button type="button" class="close" data-dismiss="modal">&times;</button>
      
                                                  </div>
                                                  <div class="modal-body">
                                                      <input type="hidden" name="att" value="<?php echo $id; ?>">
      
                                                      <input type = "hidden" name="dates" value="<?php echo $attendance; ?>">
                              
                                                      <input type = "hidden" id="sched" name="sched" value="<?php echo $schedcode?>" >
      
                                                      <input type = "hidden" id="type" name="type" value="<?php echo $POTtype?>" >
                                                      <input type = "hidden" id="dur" name="dur" value="<?php echo  timedeci($POThrs) ?>" >
      
      
                                                      Are you sure you want to file OT?
      
      
                                                  </div>
      
                                                  <div class="modal-footer">
                                                      <button type="submit" class="btn btn-success" name="fileot"><i
                                                              class="fas fa-check"> </i> Yes</button>
                                                      <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                                              class="fas fa-close"> </i> Cancel</button>
                                                  </div>
                                              </div>
                                          </div>
                                          <input type="text" id="pagenoaccept" name="pagenoaccpet" >
                                      </form>
                                  </div>


                                  <div id="PRE<?php echo $id; ?>" class="modal fade" role="dialog">
                                      <form method="post" class="form-horizontal" role="form">
                                          <div class="modal-dialog modal-md modal-dialog-centered">
                                              <!-- Modal content-->
                                              <div class="modal-content">
                                                  <div class="modal-header">
                                                      <h4 class="modal-title">File Over Time </h4>
                                                      <button type="button" class="close" data-dismiss="modal">&times;</button>
      
                                                  </div>
                                                  <div class="modal-body">
                                                      <input type = "hidden" name="att" value="<?php echo $id; ?>">
      
                                                      <input type = "hidden" name="dates" value="<?php echo $attendance; ?>">
                              
                                                      <input type = "hidden" id="sched" name="sched" value="<?php echo $schedcode?>" >
      
                                                      <input type = "hidden" id="type" name="type" value="<?php echo $type?>" >
                                                      <input type = "hidden" id="dur" name="dur" value="<?php echo timedeci($hrs) ?>" >
      
      
                                                      Are you sure you want to file OT?
      
      
                                                  </div>
      
                                                  <div class="modal-footer">
                                                      <button type="submit" class="btn btn-success" name="fileot"><i
                                                              class="fas fa-check"> </i> Yes</button>
                                                      <button type="button" class="btn btn-danger" data-dismiss="modal"><i
                                                              class="fas fa-close"> </i> Cancel</button>
                                                  </div>
                                              </div>
                                          </div>
                                          <input type="text" id="pagenoaccept" name="pagenoaccpet" >
                                      </form>
                                  </div>
      
        
        
        
      
      
                                <?php
                             
      
      
                                }
      
      
      
                                
                              
                        
                        
                        }else{

                            echo '<tr><td colspan="6"> No data found </td></tr>';
                        }


 
                        mysqli_close($conn);

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


          

          <!-- Icon Cards-->
      
        <!-- /.container-fluid -->

        <!-- Sticky Footer -->


      </div>
      <!-- /.content-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
            <a class="btn btn-primary" href="login.html">Logout</a>
          </div>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Page level plugin JavaScript-->
    <script src="../vendor/chart.js/Chart.min.js"></script>
    <script src="../vendor/datatables/jquery.dataTables.js"></script>
    <script src="../vendor/datatables/dataTables.bootstrap4.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin.min.js"></script>

    <!-- Demo scripts for this page-->
    <script src="../js/demo/datatables-demo.js"></script>
    <script src="../js/demo/chart-area-demo.js"></script>

          <script language="javascript" type="text/javascript">
            $(document).ready(function(){
            $("form").on("submit", function(){
                $("#pageloader").fadeIn();
            });//submit

             $('#preloader').fadeOut();
            });//document ready

            $(document).ready(function() {
                $('#otlist').DataTable();
            } );



        </script>

</body>

</html>
