<?php
 include("auth.php");
?>


   <?php
                          if(isset($_POST["filter"]))
                          {

                            $date_from = $_POST['date_from'];
                            $date_to = $_POST['date_to'];

                            $dateFrom = date('Y-m-d',$date_from);
                            $dateTo = date('Y-m-d',$dateTo);

                           echo $dateFrom;
                           echo "<br/>";
                           echo $dateTo;
                           echo "hello";
                         
                          }


        //Else statement for the default values without the filtered results




?>




<!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 class="active">
              <a href="">Income</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">Configure Employee View</button>
                <button type="button" class="btn btn-primary">13th Month Pay</button>
                <br/>
                <br/>
                
                                      <label for="">From</label>
                                                <input type="date" name="date_from" id="date_from" required>
                                            
                                                <label for="">To</label>
                                                <input type="date" name="date_to" id="date_to" required>
                                            
                                                <input type="submit" name= "filter" value="Search" style="margin-left: 100px !important; margin-right: 150px !important;" class="btn">
                                                <br/>
                

                <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>
                <p align="center"><big><b>Payroll System</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> -->
                      <thead>
                        <tr class="info">
                          <th><p align="center">Name</p></th>
						  <th><p align="center">Basic Pay</p></th>
                          <th><p align="center">Deduction</p></th>
                          <th><p align="center">Overtime</p></th>
                          <th><p align="center">Bonus</p></th>
                          <th><p align="center">Net Pay</p></th>
                          <th><p align="center">Action</p></th>
                        </tr>
                      </thead>
                      <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);
 $conn = @mysqli_connect('localhost', 'root', '');
  if (!$conn)
  {
    die("Database Connection Failed" . mysql_error());
  }

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

   $conndtr = @mysqli_connect('192.168.2.117', 'webster', 'webster');
  if (!$conndtr)
  {
    die("Database Connection Failed" . mysql_error());
  }

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


  $query1  = mysqli_query($conn,"SELECT * from records");
  
  
    while($row1=mysqli_fetch_array($query1))
  {

    $emp_id = $row1['emp_id'];

    $sql = "SELECT * FROM ot WHERE userid =".$emp_id;
     $sqldtr = "SELECT * FROM attendanceformatted WHERE userid =".$emp_id;
     $querydtr  = mysqli_query($conndtr,$sqldtr);
    $query  = mysqli_query($conn,$sql);


    $salary  = $row1['basic_pay'];
    $emp_lastname = $row1['lname'];
    $fname = $row1['fname'];
    
  if(($salary >=1000) && ($salary <=1249.99)) {
 //pagibig, philhealth, sss
     $totaldeductions = (100 + 137.50 + 36.30);

  } else if (($salary >=1250) && ($salary <=1749.99)) {
  $totaldeductions = (100 + 137.50 + 54.50);
  } else if (($salary >=1750) && ($salary <=2249.99)) {
   $totaldeductions = (100 + 137.50 + 72.70);
  }else if (($salary >=2250) && ($salary <=2749.99)) {
    $totaldeductions = (100 + 137.50 + 90.80);
  }else if (($salary >=2750) && ($salary <=3249.99)) {
    $totaldeductions = (100 + 137.50 + 109.00);
  } else if (($salary >=3250) && ($salary <=3749.99)) {
    $totaldeductions = (100 + 137.50 + 127.20);
  } else if (($salary >=3750) && ($salary <=4249.99)) {
    $totaldeductions = (100 + 137.50 + 145.30);
  }  else if (($salary >=4250) && ($salary <=4749.99)) {
    $totaldeductions = (100 + 137.50 + 163.50);
  }  else if (($salary >=4750) && ($salary <=5249.99)) {
    $totaldeductions = (100 + 137.50 + 181.70);
  }  else if (($salary >=5250) && ($salary <=5749.99)) {
    $totaldeductions = (100 + 137.50 + 199.80);
  }  else if (($salary >=5750) && ($salary <=6249.99)) {
    $totaldeductions = (100 + 137.50 + 218.00);
  }  else if (($salary >=6250) && ($salary <=6749.99)) {
    $totaldeductions = (100 + 137.50 + 236.20);
  }  else if (($salary >=6750) && ($salary <=7249.99)) {
    $totaldeductions = (100 + 137.50 + 254.30);
  } else if (($salary >=7250) && ($salary <=7749.99)) {
    $totaldeductions = (100 + 137.50 + 272.50);
  }   else if (($salary >=7750) && ($salary <=8249.99)) {
    $totaldeductions = (100 + 137.50 + 290.70);
  }  else if (($salary >=8250) && ($salary <=8749.99)) {
    $totaldeductions = (100 + 137.50 + 308.08);
  }  else if (($salary >=8750) && ($salary <=9249.99)) {
    $totaldeductions = (100 + 137.50 + 327.00);
  }  else if (($salary >=9250) && ($salary <=9749.99)) {
    $totaldeductions = (100 + 137.50 + 345.20);
  }  else if (($salary >=9750) && ($salary <=10249.99)) {
    $totaldeductions = (100 + 137.50 + 363.30);
  }  else if (($salary >=10250) && ($salary <=10749.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 381.50);
  } else if (($salary >=10750) && ($salary <=11249.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 399.70);
  }  else if (($salary >=11250) && ($salary <=11749.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 417.80);
  } else if (($salary >=11750) && ($salary <=12249.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) +  436.00);
  } else if (($salary >=12450) && ($salary <=12949.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 454.20);
  } else if (($salary >=12950) && ($salary <=13449.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 472.30);
  } else if (($salary >=13450) && ($salary <=13949.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 490.50);
  } else if (($salary >=13950) && ($salary <=14449.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 508.70);
  }  else if (($salary >=14450) && ($salary <=14949.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 526.80);
  } else if (($salary >=14950) && ($salary <=15249.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 545.00);
  } else if (($salary >=15250) && ($salary <=15749.99)) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 563.20);
  } else if ($salary >= 15750) {
    $totaldeductions = (100 + (($salary*0.0275)/2) + 581.30);
  } 
 else {
    $totaldeductions = 0;
  }


  //Daily Time Computation



  $netpay = $salary - $totaldeductions;

    if (mysqli_num_rows($query)==0){
     $ot = 0;
     $salary = $row1['basic_pay'];
     ?>
   <tr>
                          <td align="center"><?php echo $emp_lastname?>, <?php echo $fname?></td>
              <td align="center"><big><b><?php echo $salary?></b></big></td>
                          <td align="center"><big><b><?php echo $totaldeductions;?></b></big></td>
                          <td align="center"><big><b><?php echo $ot;?></b></big> hrs</td>
                          <td align="center"><big><b><?php echo 0;?></b></big></td>
                          <td align="center"><big><b><?php echo $netpay;?></b></big></td>
                            <td align="center">
                            <a class="btn btn-primary" href="payslip.php?emp_id=<?php echo $emp_id; ?>">View Details</a>
                            <a class="btn btn-danger" href="delete.php?emp_id=<?php echo $row["emp_id"]; ?>">Edit Basic Pay</a>
                          </td>
                        </tr>
                        <?php
    } 
    else {
        
      $dailyrate = $salary/26.16;
      $hour_rate = $dailyrate / 8;
      $temp = $hour_rate  * 1.25;
      $temp1 = $temp;
      $ot = $temp1; 

     $ot =  number_format((float)$ot, 2, '.', '')
      ?>

        <tr>
                          <td align="center"><?php echo $emp_lastname?>, <?php echo $fname?></td>
                          <td align="center"><big><b><?php echo $salary?></b></big></td>
                          <td align="center"><big><b><?php echo $totaldeductions;?></b></big></td>
                          <td align="center"><big><b><?php echo $ot;?></b></big> hrs</td>
                          <td align="center"><big><b><?php echo 0;?></b></big></td>
                          <td align="center"><big><b><?php echo $netpay;?></b></big></td>
       </tr>
                     
                        <?php 
                        } // 1st while
                         // 2nd while
                      } // else
                         ?>
                      </tbody>

                        <tr class="info">
                          <th><p align="center">Name</p></th>
						             <th><p align="center">Basic Pay</p></th>
                          <th><p align="center">Deduction</p></th>
                          <th><p align="center">Overtime</p></th>
                          <th><p align="center">Bonus</p></th>
                          <th><p align="center">Net Pay</p></th>
                           <th><p align="center">Action</p></th>  
                        </tr>
                    </table>
                  </form>
                </div>
           
            </form>
          </div>

      <!-- 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>

      <!-- 13th Month Pay -->
      <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>1
    </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>