<?
  include "../../mainfile.php";
  include "include/cfunction.php";
?>
<?
  $pdate = $_POST["pdate"];
  $arrUid = $_POST["arrUid"];
  $arrIn = $_POST["arrIn"];
  $arrOut = $_POST["arrOut"];
  $arrPid = $_POST["arrPid"];

  $pfle = $_POST["pfle"];
  $pdate = $_POST["pdate"];

?>
<?
  $qtea = "SELECT * FROM xoops_adm_teacher";
  $rtea = mysql_query($qtea);
  $ntea = mysql_num_rows($rtea);
  $i=0;
  while($i<$ntea){
    $tid = mysql_result($rtea,$i,"id");
	$tsec = mysql_result($rtea,$i,"section");
	$arrTeasec[$tid] = $tsec;
    $i++;
  }
?>
<?
  $date = new DateTime($pdate);
  $week = $date->format("w");
  $qtimetable = "SELECT * FROM xoops_pc_timetable WHERE wkDay='$week'";
  $rtimetable = mysql_query($qtimetable);
  $ntimetable = mysql_num_rows($rtimetable);
  $i=0;
  while($i<$ntimetable){
  
  	$tsec = mysql_result($rtimetable,$i,"secid");
	$tin = mysql_result($rtimetable,$i,"pcInTime");
	$tout = mysql_result($rtimetable,$i,"pcOutTime");
	$arrSec[$tsec]["in"] = $tin;
	$arrSec[$tsec]["out"] = $tout;
  
    $i++;
  }
?>
<?
  foreach($arrPid as $tidx=>$tid){


	if($arrIn[$tidx]!="00:00:00"){
      //$arrIn[$tidx] = $ptme;
	}else{
      $arrIn[$tidx] = "00:00:01";
	}
	
    $difSec = getDifSec2($arrSec[$arrTeasec[$arrUid[$tidx]]]["in"],$pdate,$arrIn[$tidx],$pdate);
//	echo $arrSec[$arrTeasec[$arrUid[$tidx]]]["in"].",".$pdate.",".$arrIn[$tidx].",".$pdate."<br>";
//	echo $difSec . " " . secConvTime($difSec) . "<br><br>";

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

	if($arrOut[$tidx]!=""){
	
      $difSec2 = getDifSec2($arrSec[$arrTeasec[$arrUid[$tidx]]]["out"],$pdate,$arrOut[$tidx],$pdate);
      //--- come late -----------------
	  if( $difSec2<0 ){
        $levEarly = secConvTime($difSec2);	
	  //--- come early -----------------
	  }else{
	    $levEarly = "00:00:00";
	  }
	
	}else{
	
	  $levEarly = "00:00:00";
	
	}
//	echo $arrSec[$arrTeasec[$arrUid[$tidx]]]["out"].",".$pdate.",".$arrOut[$tidx].",".$pdate."<br>";
//	echo $difSec2 . " " . secConvTime($difSec2) . " " . $levEarly ."<br><br>";


    if($tid!=""){
	  $query = "UPDATE xoops_pc_punchrecord SET punchIn='$arrIn[$tidx]', comLate='$comLate', punchOut='$arrOut[$tidx]', levEarly='$levEarly' WHERE punch_Id='$tid'";
	}else{
	  $query = "INSERT INTO xoops_pc_punchrecord VALUES(null,'$arrUid[$tidx]','$pdate','$arrIn[$tidx]','$pdate','$arrOut[$tidx]','','$comLate','$levEarly')";
	}
//	echo $query . "<br>";
	mysql_query($query);
  }
?>
<?
  header("Location: vb_punchimport3.php?fle=$pfle&pdate=$pdate");
?>