<?php

$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" . mysql_error());
  }

   $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());
  }

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

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

  ini_set('max_execution_time', 300);

    $sql       = 'select * from records';


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

    while ($row = mysqli_fetch_array($results)) {

        $emp_id = $row['emp_id'];
    $salary = $row['basic_pay'];
   
       $sqldtr = "SELECT * FROM attendanceformatted where userid=".$emp_id;
       $querydtr  = mysqli_query($conndtr,$sqldtr);

      while ($row1 = mysqli_fetch_array($querydtr)) {
    
  
     
$att_in = $row1['att_in'];
$att_out = $row1['att_out'];

$att_out2 = date('H:i:s', strtotime($att_out));

$otdate = date('Y-m-d', strtotime($att_out));
$fix1 = $otdate." 17:30:00";

if($att_out == "0001-01-01 00:00:00"){
  $fix1 = $otdate." 17:30:00";
} else {
  $fix1 = $fix1;
}

$diff = strtotime($att_in) - strtotime($fix1);
$h = abs($diff /(60*60));
$hours = abs($h-1);

if((strtotime($att_out) > "17:30:00") && ($hours > 8) &&  (strtotime($att_out !="0001-01-01 00:00:00"))) {

$otsql       = 'select * from ot';
$otresults   = mysqli_query($conn2, $otsql);
while ($otrow = mysqli_fetch_array($otresults)) {

$userid = $otrow['userid'];
$time_out = $otrow['time_out'];
$date_from = $otrow['date_from'];
$status = $otrow['Status'];


$otdate = date('Y-m-d', strtotime($att_out));
$fix1 = $otdate." 17:30:00";



if((strtotime($att_out2) == strtotime($time_out)) && ($date_from == $otdate) && ($status == 1) && ($emp_id == $userid)){



$diff = strtotime($att_in) - strtotime($fix1);
$h = abs($diff /(60*60));
$hours = abs($h-1.00);

$diff1 = strtotime($att_out) - strtotime($fix1);
$ot_rate = abs($diff1 /(60*60));

   $dailyrate = $salary/26.16;
      $hour_rate = $dailyrate / 8;
      $temp = $hour_rate  * 1.25;
      $temp1 = $temp * $ot_rate;
      $ot = $temp1; 

$ot_hours = $ot_rate ;
} else {



$ot=0;
$ot_hours = 0;
$diff = strtotime($att_in) - strtotime($fix1);
$h = abs($diff /(60*60));
$hours = abs($h-1);
echo $att_in;
echo "<br/>";

}

}
} else {

$otdate = date('Y-m-d', strtotime($att_in));
$fix1 = $otdate." 17:30:00";

if($att_out == "0001-01-01 00:00:00"){
  $fix1 = $fix1." 17:30:00";
} else {
  $fix1 = $fix1;
}

$ot = 0;
$ot_hours = 0;
$diff = strtotime($att_in) - strtotime($fix1);
$h = abs($diff /(60*60));
$hours = abs($h-1.00);

}



$dat = date('Y-m-d', strtotime($att_in));

if($att_out == "0001-01-01 00:00:00"){
  $att_out_final = $dat." 17:30:00";
} else {
  $att_out_final = $att_out;
}

  $dailyrate = $salary/26.16;
  $hour_rate = $dailyrate / 8;
  $daily_salary1 = abs($hour_rate * $hours);
  $daily_salary = $daily_salary1 + $ot;


  if ($conn->connect_error){
    die("Connection Failed: ".$conn->connect_error);
  }


  $insert =  "INSERT into daily_pay(emp_id,basic_pay,salary,date1,date2,hours,ot_hours,ot)
  VALUES('$emp_id','$salary','$daily_salary', '$att_in','$att_out','$hours','$ot_hours','$ot')";
    

    $insertsql = mysqli_query($conn,$insert);

    if($insertsql)
    {
      ?>
        
      <?php 
    }

echo "Records";
echo "<br/>";
   echo $emp_id;
   echo "<br/>";
   echo "--- LEGIT TIME RECORD ---";
   echo "<br/>";
    echo $att_in;
    echo "<br/>";
    echo $att_out;
    echo "<br/>";
  /*  echo $att_out_final;
    echo "<br/>";*/
    echo "-------------------------";
    echo "<br/>";
    echo "Daily Salary: ". $daily_salary;
    echo "<br/>";
    echo "-------------------------";
    echo "<br/>";
    echo "Computed Hours: ".$hours;
    echo "<br/>";
      echo "Daily Rate: ".$dailyrate;
    echo "<br/>";
    echo "Daily Rate: ".$hour_rate;
    echo "<br/>";
    echo "Edited Hours: ".$fix1;
    echo "<br/>";
    echo "-------------------------";
    echo "<br/>";
    echo "OT HOURS: ". $ot_hours;
    echo "<br/>";
    echo "OT RATE: ". $ot;
    echo "<br/>";
    echo "<br/>";
    
}
}
    
    
    
