<?
  include "../../mainfile.php";
  include "include/lang.php";
  include "../admsys/include/CSETTING.php";
  include "../admsys/include/CTEACHER.php";
  include "include/CTIMETABLE.php";
  include "include/cfunction.php";
  include "include/CPCSETTING.php";
  include "include/CFUNCTION2.php";
  date_default_timezone_set('Asia/Kuala_Lumpur');
?>
<?
  //-- 08:15:24       2009-08-31
//  $mynowtime = date("H:i:s");
//  $mynowdate = date("Y-m-d");
  $pfr = $_GET["fr"];
  $pid = $_GET["pid"];
  $pty = $_GET["ty"];
  $ptme = $_GET["tme"];
  $pdte = $_GET["dte"];
?>
<?
//  $fr = $_GET["fr"];
//  $pid = $_GET["pid"];
  $mysys = new CPCSETTING();
  $myuser = new CTEACHER($pid);


  //$nowT = date("H:i:s");
  $startT = $myuser->getStartT( date("w") );
  $endT  = $myuser->getEndT( date("w") );

//  $query = "SELECT * FROM xoops_pc_punchrecord WHERE staffId='$pid' AND punchDate='$pdte' AND punchOut='00:00:00'";
//  $result = mysql_query($query);
//  $num = mysql_num_rows($result);

  //--- punch in -----------  
  if($pty==1){

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

    //--- come late -----------------
	if( $difSec>0 ){
      $comLate = secConvTime($difSec);
	  if($mysys->autolate==1){
	    updateLate($staffId,$mysys->lateId,$punchDate);
	  }
	//--- 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."], $WELCOME2 ".$punchIn;
	//$exqval = "?v1=1&v2=" . $myuser->eName;
	
  }else if($pty==2){
  
    $staffId = $pid;
    $punchOutDate = $pdte;
    $punchOut = $ptme;
    $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='$pdte' AND punchOut='00:00:00'";
//				echo $query;
    mysql_query($query);
	
    $_SESSION["msg"] = "[".$myuser->eName."], $WELCOME3 ".$punchOut;
//	$exqval = "?v1=2&v2=" . $myuser->eName;
  
  }
?>
<?
  if($fr=="v"){
    header("Location: vb_index_report.php?msg=" . $_SESSION["msg"]."&v2=1");
  }elseif($fr=="mb"){
    header("Location: index.php?msg=" . $_SESSION["msg"]);
  }else{
    header("Location: index.php");
  }
?>