<?php

   include('connect.php');
   include('logging.php');

if( isset( $_POST['payrollid'] ) )
{

$payrollid=$_POST['payrollid'];
$employeeid=$_POST['employeeid'];
$overtime1_5=$_POST['overtime1_5'];
$overtime2_0=$_POST['overtime2_0'];
$overtime3_0=$_POST['overtime3_0'];
$totalOvertime=$_POST['totalOvertime'];

$checkEntry = "SELECT * FROM overtimes WHERE employeeid = $employeeid AND periodid = $payrollid;";

$resultCheck = mysqli_query($db,$checkEntry);

$resultCheck = mysqli_num_rows($resultCheck);

if ($resultCheck == 0) {

	$insertdata=" INSERT INTO overtimes (
		periodid,
		employeeid,
		1_5_units,
		2_0_units,
		3_0_units,
		totalOvertime
		)
		VALUES( 
		'$payrollid',
		'$employeeid',
		'$overtime1_5',
		'$overtime2_0',
		'$overtime3_0',
		'$totalOvertime'

		) ";


		    if(mysqli_query($db, $insertdata)) {
		      	echo 'Overtime Data for employee inserted successfully';
		      } else {
		         echo "Error: " . mysqli_error($db);
		    }


} else {

	$updatedate=" UPDATE overtimes 
		SET 1_5_units = '$overtime1_5',
		2_0_units = '$overtime2_0',
		3_0_units = '$overtime3_0',
		totalOvertime = '$totalOvertime'
		WHERE periodid = '$payrollid' AND employeeid = $employeeid;";
	
	if(mysqli_query($db, $updatedate)) {
		echo 'Overtime Data for employee updated successfully';
	} else {
	   echo "Error: " . mysqli_error($db);
  }


}




} 
?>