<?
  include "include/dbcon.php";
  include "include/cfunction.php";
  include "include/CSYSTEM.php";
  include "include/CHOLIDAY.php";
  include "include/CPCSETTING.php";
//  include "include/CARRAY.php";
?>
<?
  $mypcsys = new CPCSETTING();
?>
<?
  $teaid = $_POST["tea1"];
  $datefrom = $_POST["txtDatefrom"];
  $datetil = $_POST["txtDatetil"];
  $ord = $_POST["ord"];
  $pbrk = $_POST["pbrk"];
  $clp = $_POST["clp"];
  $shw = $_POST["shw"];
  $late = $_POST["late"];
  $teaids = $_POST["teaids"];
  $arrTeaid = explode(",",str_replace("\\'","",$teaids));
  $strTeaid = "'".implode("','",$arrTeaid)."'";
  $style = $_POST["style"];
  $highlight = $_POST["highlight"];
  
  if($teaid!="_s"){
    $exquery = " AND staffId='$teaid' ";
	$exquery2 = " AND id='$teaid' ";
  }else{
    $exquery = "";
	$exquery2 = "";
  }
  
  if($late!="0"){
    $exquery3 = " AND (comLate<>'00:00:00' OR levEarly<>'00:00:00') ";
  }else{
    $exquery3 = "";
  }
  
  $mysys = new CSYSTEM();
?>
<?
  function add_date($pdate,$pday){ 
    $tdate = strtotime($pdate); 
    $retDAY = date('Y-m-d', mktime(0,0,0,date('m',$tdate) , (date('d',$tdate)+$pday) , date('Y',$tdate))); 
    return $retDAY; 
  }
?>
<?
  if($shw=='0'){
    unset($arrTeaid);
    $qtea0 = "SELECT DISTINCT staffId FROM punchsys_punchrecord WHERE punchDate BETWEEN '$datefrom' AND '$datetil' $exquery3";
    $rtea0 = mysqli_query($conn,$qtea0);
    //$ntea0 = mysqli_num_rows($rtea0);
	//$i=0;
	//while($i<$ntea0){
	while($myrow = mysqli_fetch_array($rtea0)){
	
	  $arrTeaid[] = $myrow["staffId"];
	
	  $i++;
	}
	$strTeaid = "'".implode("','",$arrTeaid)."'";
    $exquery = "";
	$exquery2 = "";
	
  }
?>
<?
  $qtea = "SELECT * FROM punchsys_staff WHERE id IN ($strTeaid) $exquery2 AND userID<>'1' ORDER BY repPrint, id";
  $rtea = mysqli_query($conn,$qtea);
  $ntea = mysqli_num_rows($rtea);
  //$i=0;
  //while($i<$ntea){
  while($myrow = mysqli_fetch_array($rtea)){
  
    $tid = $myrow["id"];
	$tname = $myrow["eName"];
  
    $arrTea[$tid] = $tname;
  
    $i++;
  }
?>
<?
  if($late!="0"){
    $exquery4 = "";
  }else{
    $exquery4 = " AND staffid IN ($strTeaid) ";
  }

  $qrcd = "SELECT * FROM punchsys_punchrecord WHERE punchDate BETWEEN '$datefrom' AND '$datetil' $exquery4 $exquery $exquery3";
  $rrcd = mysqli_query($conn,$qrcd);
  $nrcd = mysqli_num_rows($rrcd);
  while($myrow = mysqli_fetch_array($rrcd)){
//  $i = 0;
//  while($i<$nrcd){
  
    $tteaid = $myrow["staffId"];
	$tpdate = $myrow["punchDate"];
	$tpunchIn = $myrow["punchIn"];
	$tpunchOut = $myrow["punchOut"];
	$tmessage = $myrow["message"];
	$tcomLate = $myrow["comLate"];
	$tlevEarly = $myrow["levEarly"];
	
	if( $tcomLate=="00:00:00" ){
	  $acolors1 = "";
	  $acolore1 = "";
	}else{
	  if($highlight=="1"){
	    $acolors1 = " ( ";
	    $acolore1 = " ) ";
	  }else{
	    $acolors1 = " <font color='red'> ";
	    $acolore1 = " </font> ";
	  }

	  $tpunchIn = $acolors1 . $tpunchIn . $acolore1;
	}
	
	if( $tlevEarly=="00:00:00" ){
	  $acolors2 = "";
	  $acolore2 = "";
	}else{
	  if($highlight=="1"){
	    $acolors2 = " ( ";
	    $acolore2 = " ) ";
	  }else{
	    $acolors2 = " <font color='red'> ";
	    $acolore2 = " </font> ";
	  }

	  $tpunchOut = $acolors2 . $tpunchOut . $acolore2;
	}

	$arrPunchintime[$tpdate][$tteaid] = $tpunchIn;
	$arrPunchouttime[$tpdate][$tteaid] = $tpunchOut;
	
//	$arrComlate[$tpdate][$tteaid] = $tcomLate;
//	$arrLevearly[$tpdate][$tteaid] = $tlevEarly;
//	$arrMessage[$tpdate][$tteaid] = $tmessage;
  
    $i++;
  }
?>

<style>
  .rpt_tbl{
  font-family:Arial;
  font-size:14px;
  width:700px;
  border-collapse:collapse;
  border-spacing:0;

  }
	
.myh1{
  font-size:16px;
  font-family:Arial;
}
.lsttable{
font-family:Arial;
border-collapse:collapse;
margin: 5px 5px 5px 5px ;
}
</style>
<center>
<div style=" page-break-after:always">

<table width="800px" border="0">
<tr>
  <th rowspan="2">
    <img src="img/<?= $mysys->imgPath ?>" height="50px" />
  </th>
  <th class="myh1">LAPORAN KEHADIRAN <?= strtoupper($mysys->schName)  ?></th>
</tr>
<tr>
  <th colspan="2" class="myh1">
     TARIKH : <u><?= $datefrom ?> ~ <?= $datetil ?></u></th>
</tr>
</table>
      
<hr width="800px" />


<table class="lsttable" width="800px"  border="1">
  <tr>
    <th width="25px">&nbsp;</th>
    <th>NAME</th>
    <?
    $tdate = $datefrom;
    while($tdate<=$datetil){
    ?>
    <th width="65px"><?= substr($tdate,5) ?></th>
    <?
    $tdate=add_date($tdate,1);
    }
    ?>
  </tr>
  <?
  $numrowofpage = 34;
  $i=1;
  foreach($arrTea as $tid=>$ttea){
  ?>
  <tr style="font-size:10px; text-align:center; height:28px">
    <td><?= $i ?></td>
    <td align="left"><?= $ttea ?></td>
    <?
    $tdate = $datefrom;
    while($tdate<=$datetil){
    ?>
    <td><?= $arrPunchintime[$tdate][$tid] . "<br>" . $arrPunchouttime[$tdate][$tid] ?></td>
    <?
    $tdate=add_date($tdate,1);
    }
    ?>
  </tr>
  <?
  if(count($arrTea)>$numrowofpage){
  if( ($i%$numrowofpage)==0 ){
  ?>
</table>
</div>
<div style=" page-break-after:always">
<table class="lsttable" width="800px"  border="1">
  <tr>
    <th width="25px">&nbsp;</th>
    <th>NAME</th>
    <?
    $tdate = $datefrom;
    while($tdate<=$datetil){
    ?>
    <th width="65px"><?= substr($tdate,5) ?></th>
    <?
    $tdate=add_date($tdate,1);
    }
    ?>
  </tr>
  <?
  }
  }

  $i++;
  }//foreach($arrTea as $tid=>$ttea){
  ?>

<?
  if($i<$numrowofpage){
  $ii=$i;
  while($ii<$numrowofpage){
  ?>
  <tr style="font-size:10px; text-align:center; height:28px">
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr>
  <?
  $ii++;
  }
  ?>
  <?
  $i++;
  }//if(count($arrTea)>=$numrowofpage){
  ?>
</table>

</div>
</center>