
<?php 
 include("auth.php");
$connection = mysqli_connect("localhost","root","","p2");

if ($connection->connect_error)
{
  die("Connection failed: " . $connection->connect_error);
} 

    $sql       = 'select * from records where emp_id';

    $results   = mysqli_query($connection, $sql);


    $row_count = mysqli_num_rows($results);
    $id = 0;
  
    while ($row = mysqli_fetch_array($results)) {
      /*  echo '<tr><td><a href="OBApprove.php?id='.$row['obid'].'">'.$row['emp_number'].'</a></td></tr>';
        echo '<td>'.$row['emp_id'].'</td>';
        echo '<td>'.$row['dept_from'].'</td>';
        echo '<td>'.$row['dept_to'].'</td>';
        echo '<td>'.$row['purpose'].'</td>';
        echo '<td>'.$row['dept_time'].'</td>';
        echo '<td>'.$row['return_time'].'</td>';
        echo '<td>'.$row['status'].'</td>';*/

        $emp_name = $row['lname']."".$row['fname'];
        $emp_id = $row['emp_id'];
        $joined_date = $row['jdate'];
        $department = $row['division'];
        // /$job_title_code = $row['job_title_code'];

    
}
          
               

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

    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Bootstrap, a sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.">
    <meta name="keywords" content="HTML, CSS, JS, JavaScript, framework, bootstrap, front-end, frontend, web development">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">

    <title></title>

    <script>
      <!--
        var ScrollMsg= "Employee Management System - "
        var CharacterPosition=0;
        function StartScrolling() {
        document.title=ScrollMsg.substring(CharacterPosition,ScrollMsg.length)+
        ScrollMsg.substring(0, CharacterPosition);
        CharacterPosition++;
        if(CharacterPosition > ScrollMsg.length) CharacterPosition=0;
        window.setTimeout("StartScrolling()",150); }
        StartScrolling();
      // -->
    </script>

    <link href="assets/must.png" rel="shortcut icon">
    <link href="assets/css/justified-nav.css" rel="stylesheet">


    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <!-- <link href="data:text/css;charset=utf-8," data-href="assets/css/bootstrap-theme.min.css" rel="stylesheet" id="bs-theme-stylesheet"> -->
    <!-- <link href="assets/css/docs.min.css" rel="stylesheet"> -->
    <link href="assets/css/search.css" rel="stylesheet">
    <!-- <link rel="stylesheet" href="assets/css/styles.css" /> -->
    <link rel="stylesheet" type="text/css" href="assets/css/dataTables.min.css">

  </head>
  <body>

    <div class="container">
      <div class="masthead">
        <h3>
          <b><a href="index.php">Employee Management System</a></b>
            <a data-toggle="modal" href="#colins" class="pull-right"><b><?php echo "Logged in as: ".$_SESSION['username'];?></b></a>
        </h3>
        <nav>
          <ul class="nav nav-justified">
            <li>
              <a href=<?php echo "user_employee_dtr.php?id=".$thisid?> >Employee</a>
            </li>
            <li>
              <a href="home_deductions.php">My Profile</a>
            </li>
            <li>
              <a href="home_salary.php">Income</a>
            </li>

			<li class="active">
              <a href="admin_leave.php">DTR</a>
            </li>
          </ul>
        </nav>
      </div>

        <br>
          <div class="well bs-component">
            <form class="form-horizontal">
              <fieldset>
                <button type="button" data-toggle="modal" data-target="#overtime" class="btn btn-success">Modify Overtime Rate</button>
                <button type="button" data-toggle="modal" data-target="#salary" class="btn btn-primary">Modify Salary Rate</button>
                <p class="pull-right">Overtime rate per hour: <big><b><?php echo 0; ?>.00</b></big></p><br>
                <p class="pull-right">Salary rate: <big><b><?php echo 0; ?>.00</b></big></p>
                <br/>
                <p align="left"><big><b>Daily Time Record</b></big></p>
                <div class="table-responsive">
                  <form method="post" action="" >
                    <table class="table table-bordered table-hover table-condensed" id="myTable">
                      <!-- <h3><b>Ordinance</b></h3> -->
                
                      <tbody>
                       <!-- <?php
                      /*    $query  = mysqli_query($conn,"SELECT * from overtime");
                          while($row=mysqli_fetch_array($query))
                          {
                            $rate   = $row['rate'];
                          }

                          $query  = mysqli_query($conn,"SELECT * from salary");
                          while($row=mysqli_fetch_array($query))
                          {
                            $salary_rate   = $row['salary_rate'];
                          }

                          $query  = mysqli_query($conn,"SELECT * from deductions");
  
  
  //print_r($_SESSION);
  //die();
  
  while($row=mysqli_fetch_array($query))
  {
    $id           = $row['deduction_id'];
    $philhealth   = $row['philhealth'];
    $bir          = $row['bir'];
    $gsis         = $row['gsis'];
    $love         = $row['pag_ibig'];
    $loans        = $row['loans'];
  $leave_rate   = $row['leave_rate'];

    $total2        = $philhealth + $bir + $gsis + $love + $loans + $leave_rate;
  }



                          $query  = mysqli_query($conn,"SELECT * from records");
                          while($row=mysqli_fetch_assoc($query))
                          {
							  
                            $emp_id = $row['emp_id'];
							              $basic_pay = $row['basic_pay'];
                            $lname           = $row['lname'];
                            $fname           = $row['fname'];
                            $deduction       = $row['deduction'];
                            $overtime        = $row['overtime'];
                            $bonus           = $row['bonus'];

                            $over     = $row['overtime'] * $rate;
                            $bonus     = $row['bonus'];
                            $deduction  = $row['deduction'];
                           // $income   = $basic_pay + $over +  $bonus;
              
						 $query22 = "SELECT sum(days) from leave_details where status='approved' and emp_id=$emp_id";
						  $results1 = mysqli_query($conn,$query22) or die(mysqli_error($conn));
						  $row = mysqli_fetch_array($results1);
						  $total1 = $row[0];
							echo $total1;
  
							$totoalNumberOfLeaves = intval($total1);
									
			  
							$total      = $total2;
								
							$netpay   = $basic_pay - $total;*/
							
							?> -->

              <?php

/*$conn = @mysqli_connect('localhost', 'root', '');
  if (!$conn)
  {
    die("Database Connection Failed" . mysql_error());
  }

  $select_db = mysqli_select_db($conn,'db');
  if (!$select_db)
  {
    die("Database Selection Failed" . mysql_error());
  }

  $query  = mysqli_query($conn,"SELECT * from overtime");
  while($row=mysqli_fetch_array($query))
  {
    @$rate = $row['rate'];
  }

  $query  = mysqli_query($conn,"SELECT * from emp_salary");
  while($row=mysqli_fetch_array($query))
  {
    $salary           = $row['basic_pay'];
  }

  if($salary )
  
 /* $query  = mysqli_query($conn,"SELECT * from deductions");
  
  
  //print_r($_SESSION);
  //die();
  
  while($row=mysqli_fetch_array($query))
  {
    $id           = $row['deduction_id'];
    $philhealth   = $row['philhealth'];
    $bir          = $row['bir'];
    $gsis         = $row['gsis'];
    $love         = $row['pag_ibig'];
    $loans        = $row['loans'];
  $leave_rate   = $row['leave_rate'];

    //$total        = $philhealth + $bir + $gsis + $love + $loans + ($leave_rate*$totoalNumberOfLeaves);
*/



error_reporting(E_ALL ^ E_WARNING);
       
// real time data
                              $conn = @mysqli_connect('localhost', 'root', '');
                              if (!$conn)
                              {
                                die("Database Connection Failed" . mysqli_error($conn));
                              }

                              
                              $select_db = mysqli_select_db($conn,'db');
                              if (!$select_db)
                              {
                                die("Database Selection Failed" . mysqli_error($conn));
                              }

     
                              $conn1 = @mysqli_connect('localhost', 'root', '');
                              if (!$conn1)
                              {
                                die("Database Connection Failed" . mysqli_error($conn1));
                              }

                              $select_db1 = mysqli_select_db($conn1,'db');
                              if (!$select_db1)
                              {
                                die("Database Selection Failed" . mysqli_error($conn1));
                              }

                             
                                 
                        ?>

                                     <tr>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>ID</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Date</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Day</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>IN</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>OUT</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Tardy</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>OT Hours (subject to filling &amp; approval)</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Undertime</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Remarks</strong></td>
                                     </tr>

                                    <?php
                                    //Check1

                                    $connectionrtd = mysqli_connect("192.168.2.117","webster","webster","realtimedata");

                                    $attsql = "SELECT * from attendanceformatted GROUP BY userid";
                                        $query11=mysqli_query($connectionrtd, $attsql)or die(mysqli_error($connectionrtd));
                      

                                              //die("SELECT * from employee WHERE emp_id=$emp_id");
                                              while($row=mysqli_fetch_array($query11))
                                              {
                                            
                                               $att_in =  $row['att_in'];
                                               $att_out =  $row['att_out'];
                                               $id = $row['userid'];




                                               $s = strtotime($att_in);
                                               $s1 = strtotime($att_out);
                                               $set = "09:00:00";
                                               $cset = strtotime($set);

                                               $date =  date('Y-m-d', $s);
                                               $day = date('D', strtotime($att_in));
                                               $diff = $s - $s1;

                                               $timeIn = date('H:i:s', $s);
                                               $timeOut = date('H:i:s', $s1);

                                               $ctimein = strtotime($timeIn);
                                               $ctimeout = strtotime($timeOut);

                                               $hours = $diff / (60 * 60);
                                               $fhours = abs($hours);
                                              

                                             //  if ($ctimein < $cset){
                                               // $finalhours = 0;
                                               //} else {
                                                if($ctimein > $cset) {

                                                  $t =   $cset - $ctimein;
                                                 $t1 = $t / (3600);
                                                 $t2 = round(abs($cset - $ctimein) / 3600,2);
                                                  $finalhours = $t2;

                                                } else {
                                                  $finalhours = 0;
                                                }
                                               
                                               //  $finalhours = number_format((float)strtotime($set), 2, '.', '');


?>
                              
     <tr>
                                             
                                             <td align="center"> <a href="DtrView.php?id=<?php echo $id ?>"> <?php echo $id ?></a></td>
                                             <td align="center"><?php echo $date ?></a></td>
                                             <td align="center"><?php echo $day ?></a></td>
                                             <td align="center"><?php echo $timeIn ?></a></td>
                                             <td align="center"><?php echo $timeOut ?></a></td>
                                            <td align="center"><?php echo $finalhours ?></a></td>




<?php 

                                        $att2 = "SELECT * from ot WHERE `userid`=".$id." AND `time_out` = '".$timeOut."'";
                                        
                                        $ut_time =  round(abs($ctimein - $ctimeout) / 3600,2);




                                        $query1=mysqli_query($conn1,$att2)or die(mysqli_error($conn1));
                                

                                         if (mysqli_num_rows($query1)==0) {

                                              $remarks = "None";
                                              $finalut = $ut_time;
                                              $ot_time1 = 0 ; 

                                             if(($ut_time < 8) && ($ut_time >4)){
                                                $finalut = $ut_time;
                                              }            else {
                                                $finalut = "No Undertime Record";
                                              }

                                           
?>
                                              <td align="center"><?php echo $ot_time1 ?></a></td>            
                                              <td align="center"><?php echo $finalut ?></a></td>
                                              <td align="center"><?php echo $remarks ?></a></td>

                                         </tr>
<?php
               
                                         }

                                         else {
 
                                       while($row = mysqli_fetch_array($query1))
                                          {

       
                                              $ot_time_in = $row['time_in'];
                                              $ot_time_out = $row['time_out'];
                                              $ctin = strtotime($ot_time_in);
                                              $ctout = strtotime($ot_time_out); 
                                              $date_from = $row['date_from'];
                                              $userid = $row['userid'];


       
                                            if (($userid == $thisid) && ($timeIn == $ot_time_in) && ($timeOut == $ot_time_out) && ($date_from == $date))
                                            {

                                           
                                            $status = $row['Status'];
                                            $otset = "17:30:00";            
                                            $oset = strtotime($otset);
                                            $otimeout = strtotime($ot_time_out);
                                          
                                            $finalut =  $ut_time;
                                   
                                            $remarks = "Overtime";


                                            $temp1 = round(abs($oset - $ctout) / 3600,2);

                                            if ($status == 1)
                                            { 
                                                $str = " [approved] ";
                                                $ot_time1 = $temp1." ".$str;
                                            } 
                                            else
                                             {
                                                $str = " [reject] ";
                                                $ot_time1 = $temp1." ".$str;
                                             }


                                          } else {
                                           
                                             
                                              $remarks = "None1";
                                              $finalut = $ut_time;
                                              $ot_time1 = 01;
                                                  
                                               
                                            }
                                     ?>

                                      <td align="center"><?php echo $ot_time1 ?></a></td>       
                                      <td align="center"><?php echo $finalut ?></a></td>
                                      <td align="center"><?php echo $remarks ?></a></td>
                                      </tr>

                                        <?php 
                                            } //end of while loop
                                        }//end of else
                                      }//end of another while loop
           
                                        
                                                        
                                  ?>

   

      <!-- this modal is for OVERTIME -->
      <div class="modal fade" id="overtime" role="dialog">
        <div class="modal-dialog modal-sm">
        
          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header" style="padding:20px 50px;">
              <button type="button" class="close" data-dismiss="modal" title="Close">&times;</button>
              <h3 align="center">Enter the amount of <big><b>Overtime</b></big> rate per hour.</h3>
            </div>
            <div class="modal-body" style="padding:40px 50px;">

              <form class="form-horizontal" action="update_overtime.php" name="form" method="post">
                <div class="form-group">
                    <input type="text" name="rate" class="form-control" value="<?php echo $rate; ?>" required="required">
                </div>

                <div class="form-group">
                    <input type="submit" name="submit" class="btn btn-success" value="Submit">
                </div>
              </form>

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

      <!-- this modal is for SALARY -->
      <div class="modal fade" id="salary" role="dialog">
        <div class="modal-dialog modal-sm">
        
          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header" style="padding:20px 50px;">
              <button type="button" class="close" data-dismiss="modal" title="Close">&times;</button>
              <h3 align="center">Enter the amount of <big><b>Salary</b></big> rate.</h3>
            </div>
            <div class="modal-body" style="padding:40px 50px;">

              <form class="form-horizontal" action="update_salary.php" name="form" method="post">
                <div class="form-group">
                    <input type="text" name="salary_rate" class="form-control" value="<?php echo $salary; ?>" required="required">
                </div>

                <div class="form-group">
                    <input type="submit" name="submit" class="btn btn-success" value="Submit">
                </div>
              </form>

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

      <!-- this modal is for my Colins -->
      <div class="modal fade" id="colins" role="dialog">
        <div class="modal-dialog modal-sm">
              
          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header" style="padding:20px 50px;">
              <button type="button" class="close" data-dismiss="modal" title="Close">&times;</button>
              <h3 align="center">You are logged in as <b><?php echo $_SESSION['username']; ?></b></h3>
            </div>
            <div class="modal-body" style="padding:40px 50px;">
              <div align="center">
                <a href="logout.php" class="btn btn-block btn-danger">Logout</a>
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <!-- <script src="assets/js/docs.min.js"></script> -->
    <script src="assets/js/search.js"></script>
    <script type="text/javascript" charset="utf-8" language="javascript" src="assets/js/dataTables.min.js"></script>

    <!-- FOR DataTable -->
    <script>
      {
        $(document).ready(function()
        {
          $('#myTable').DataTable();
        });
      }
    </script>

    <!-- this function is for modal -->
    <script>
      $(document).ready(function()
      {
        $("#myBtn").click(function()
        {
          $("#myModal").modal();
        });
      });
    </script>

  </body>
</html>