<?
  include "include/dbcon.php";
  include "include/cfunction.php";
?>
<?
  $pdate = $_POST["pdate"];
  $arrUid = $_POST["arrUid"];
  $arrIn = $_POST["arrIn"];
  $arrOut = $_POST["arrOut"];
  $arrPid = $_POST["arrPid"];
?>
<?
  $qtea = "SELECT * FROM punchsys_staff";
  $rtea = mysqli_query($conn,$qtea);
  $ntea = mysqli_num_rows($rtea);
  $i=0;
  while($myrow = mysqli_fetch_array($rtea)){
    $tid = $myrow["id"];
	$tsec = $myrow["section"];
	$arrTeasec[$tid] = $tsec;
    $i++;
  }
?>
<?
  $date = new DateTime($pdate);
  $week = $date->format("w");
  $qtimetable = "SELECT * FROM punchsys_timetable WHERE wkDay='$week'";
  $rtimetable = mysqli_query($conn,$qtimetable);
  $ntimetable = mysqli_num_rows($rtimetable);
  $i=0;
  while($myrow = mysqli_fetch_array($rtimetable)){
  
  	$tsec = $myrow["secid"];
	$tin = $myrow["pcInTime"];
	$tout = $myrow["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 punchsys_punchrecord SET punchIn='$arrIn[$tidx]', comLate='$comLate', punchOut='$arrOut[$tidx]', levEarly='$levEarly' WHERE punch_Id='$tid'";
	}else{
	  $query = "INSERT INTO punchsys_punchrecord VALUES(null,'$arrUid[$tidx]','$pdate','$arrIn[$tidx]','$pdate','$arrOut[$tidx]','','$comLate','$levEarly')";
	}
//	echo $query . "<br>";
	mysqli_query($conn,$query);
  }
?>
<?
  header("Location: vb_punchimport2.php?fle=mydata2.txt&pdate=$pdate");
?>