<?
  include "../../mainfile.php";
  include "../admsys/include/CSETTING.php";
  include "../admsys/include/CTEACHER.php";
  include "include/CTIMETABLE.php";
  include "include/cfunction.php";
  include "include/CSETTING.php";
  
  date_default_timezone_set('Asia/Kuala_Lumpur');
  
  $mynowdate = date("Y-m-d");
  $mysys = new CSETTING();
?>
<?
  //-- 08:15:24       2009-08-31
  $mynowtime = date("H:i:s");
  $mynowdate = date("Y-m-d");
?>
<?
  $pid = $_GET["id"];
  $myuser = new CTEACHER($pid);
  
  $nowT = date("H:i:s");
  $startT = $myuser->getStartT( date("w") );
  $endT  = $myuser->getEndT( date("w") );
?>
<?
  //********* check max punch time of the day **********************
  $query = "SELECT * FROM xoops_pc_punchrecord WHERE staffId='$pid' AND punchDate='$mynowdate' AND punchOut<>'00:00:00'";
  $result = mysql_query($query);
  $num = mysql_num_rows($result);
  //--- max punch limit is over
  if( $num >= $mysys->maxpunchtime ){
  	$_SESSION["msg"] = "Sorry, you punch limit is over.";
  ?>
    <script language="javascript">
      //alert("Sorry, you punch limit is over");
	  window.location = 'vb_index_report.php';
    </script>
  <?
  }else{
?>
<?
    $query = "SELECT * FROM xoops_pc_punchrecord WHERE staffId='$pid' AND punchDate='$mynowdate' AND punchOut='00:00:00'";
    $result = mysql_query($query);
    $num = mysql_num_rows($result);

    //--- punch in -----------  
    if($num==0){

      $staffId = $pid;
      $punchDate = $mynowdate;
	  if($punchIn!="00:00:00"){
        $punchIn = $nowT;
	  }else{
        $punchIn = "00:00:01";
	  }
      $difSec = getDifSec($startT,$punchDate,$punchIn,$punchDate);

      //--- come late -----------------
	  if( $difSec>0 ){
        $comLate = secConvTime($difSec);
		
	  //--- come early -----------------
	  }else{
	    $comLate = "00:00:00";
	  }

      $query = "INSERT INTO xoops_pc_punchrecord 
	              (staffId,punchDate,punchIn,comLate)
		  		  VALUES
			  	  ('$staffId','$punchDate','$punchIn','$comLate')";
    //				echo $query;
      mysql_query($query);

      //$_SESSION["msg"] = "Welcome, [".$myuser->eName."], you have punch in at ".$punchIn;
	
    }else{
  
      $staffId = $pid;
      $punchOutDate = $mynowdate;
      $punchOut = $nowT;
      $difSec = getDifSec($endT,$punchOutDate,$punchOut,$punchOutDate);

      //--- come late -----------------
	  if( $difSec<0 ){
        $levEarly = secConvTime($difSec);	
	  //--- come early -----------------
	  }else{
	    $levEarly = "00:00:00";
	  }
	
	  $query = "UPDATE xoops_pc_punchrecord
	            SET punchOutDate='$punchOutDate' , punchOut='$punchOut' , levEarly='$levEarly'
				WHERE staffId='$pid' AND punchDate='$mynowdate' AND punchOut='00:00:00'";
    //				echo $query;
      mysql_query($query);
	
    //    $_SESSION["msg"] = "[".$myuser->eName."], you have punch out at ".$punchOut;
  
    }
    header("Location: vb_index_report.php");
	
  }
?>