<?
//  include "../../mainfile.php";
  include "include/dbcon.php";
  date_default_timezone_set('Asia/Kuala_Lumpur');
  include "include/cfunction.php";
  include "include/CTIMETABLE.php";
//  include "../admsys/include/CTEACHER.php";
  include "include/CTEACHER.php";
?>
<?
  $arrTimetable = getTimeTableList();
  $arrStaff = getTeaList();
//  echo "<pre>";
//  print_r($arrStaff);
//  echo "</pre>";

  $handle = fopen("mydata.txt", "rb");
  if($handle==true){
  
    $contents = '';
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
  ?>
  <?
    $arrCont = explode("\r\n",$contents);
    foreach($arrCont as $tcont){
	  $arr = explode("_",$tcont);//[0]=userid,[1]=date,[2]=time
	  //echo $arr[0] . " " . $arr[1] . " " . $arr[2] . "<br>";
	
	  if($arr[0]!=""){
	
	    $tarr0 = explode("-",$arr[1]);
	    if( strlen($tarr0[1])<2 ){
	      $tmth = "0".$tarr0[1];
	    }else{
	      $tmth = $tarr0[1];
	    }
	    if( strlen($tarr0[2])<2 ){
	      $tday = "0".$tarr0[2];
	    }else{
	      $tday = $tarr0[2];
	    }
	    $tempdate = $tarr0[0]."-".$tmth."-".$tday;
	  
	    $arrTempDate[$tempdate] = $tempdate;
	    $arrTempUid[$arr[0]] = $arr[0];
	
	    $arrTemp[ $tempdate ][ $arr[0] ]++;
	
	    if( $arrTemp[ $tempdate ][ $arr[0] ] != "1" ){
	      $arrMydata[$tempdate][$arr[0]]["out"] = $arr[2];
	    }else{
	      $arrMydata[$tempdate][$arr[0]]["in"] = $arr[2];
	    }
	
	  }//--if($arr[0]!=""){

    }
?>
<?
  $strDate = "'".implode("','",$arrTempDate)."'";
  $strUid = "'".implode("','",$arrTempUid)."'";
  $query = "SELECT * FROM punchsys_punchrecord WHERE punchDate IN ($strDate) AND staffId IN ($strUid)";
  
  $result = mysqli_query($conn,$query);
  $num = mysqli_num_rows($result);
  $i=0;
  if($num>0){
  while($myrow = mysqli_fetch_array($result)){
  
    $tpdate = $myrow["punchDate"];
	$tstaffId = $myrow["staffId"];
	$arrChk[$tpdate][$tstaffId] = "1";
  
  }
  }
?>
<?
  foreach($arrMydata as $tdate=>$tarr1){
    foreach($tarr1 as $tuid=>$tarr2){
	  
      $difSecIn = getDifSec2($tarr2["in"],$tdate,$arrTimetable[$arrStaff[$tuid]['section']][getWeek2($tdate)]["IN"],$tdate);
	  if( $difSecIn<0 ){
        $comelate = secConvTime($difSecIn);	
	  }else{
	    $comelate = "00:00:00";
	  }

      if(isset($tarr2["out"])){
        $difSecOut = getDifSec2($tarr2["out"],$tdate,$arrTimetable[$arrStaff[$tuid]['section']][getWeek2($tdate)]["OUT"],$tdate);
	    if( $difSecOut<0 ){
	      $levEarly = "00:00:00";
	    }else{
          $levEarly = secConvTime($difSecOut);
	    }
	  }else{
	    $levEarly = "00:00:00";
	  }
	  
	  if( $arrChk[$tdate][$tuid]==1 ){
	    if( $tarr2["out"]!=""){
	      $q2 = "UPDATE punchsys_punchrecord SET punchOut='".$tarr2["out"]."', levEarly='$levEarly'
		           WHERE staffId='$tuid' AND punchDate='$tdate'";
		}else{
	      $q2 = "UPDATE punchsys_punchrecord SET punchOut='".$tarr2["in"]."', levEarly='$levEarly'
		           WHERE staffId='$tuid' AND punchDate='$tdate'";
		}
	  }else{
	    $q2 = "INSERT INTO punchsys_punchrecord (staffId,punchDate,punchIn,punchOut,comLate,levEarly) 
		          VALUES ('$tuid','$tdate','".$tarr2["in"]."','".$tarr2["out"]."','$comelate','$levEarly')";
	  }
	  mysqli_query($conn,$q2);
//	  echo $q2 . "<br>";
//    echo "date: " . $tdate . " [".getWeek2($tdate)."] <br>user: " . $tuid . " <br>IN:" . $tarr2["in"] . "[".$arrTimetable[$arrStaff[$tuid]['section']][getWeek2($tdate)]["IN"]."][$comelate]";
//	if(isset($tarr2["out"])){
//	  echo "<br>OUT:" . $tarr2["out"] . "[".$arrTimetable[$arrStaff[$tuid]['section']][getWeek2($tdate)]["OUT"]."][$levEarly]";
//	}
//	  echo "<br><br>";
//	}
    }
  }
?>
<?
//  print_r($arrChk);
//  print_r($arrMydata)
?>
<?
  }
?>
<?
//  header("Location: vb_index_report.php");
?>
<script>
	//window.location = "vb_index_report.php";
	window.location = "vb_punchupload.php";
</script>