<?php
session_start();
$session=session_id();
require("../inc/config.inc.php");
require("../inc/common.php");


if($_SESSION['login']!="admin"){	
	echo"Error";
	exit();
}
$key=$_GET['key'];	// 0 เฉพาะไม่ลงทะเบียน, 1 เฉพาะลงทะเบียน, 2 ทั้งหมดทั้งลงทะเบียนและไม่ลงทะเบียน	
if($key=="0"){ $title="ไม่ลงทะเบียน "; }
if($key=="1"){ $title="ลงทะเบียน "; }
if($key=="2"){ $title="ทั้งหมด "; }

if($_GET['ex']=='xls'){
$strExcelFileName="รายชื่อนักศึกษา $title  ปีการศึกษา $academicYear ภาคเรียน $semester (ทุกกลุ่มเรียน).xls";
header("Content-Type: application/x-msexcel; name=\"$strExcelFileName\"");
header("Content-Disposition: inline; filename=\"$strExcelFileName\"");
header("Pragma:no-cache");
}

?>
<?php //echo FCPATH."images/logo_e2e.png"; ?>
<html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40">
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8">

	
</head>
<body>
<!-- <div id="SiXhEaD_Excel" align=center x:publishsource="Excel"> -->
<div id="SiXhEaD_Excel"  x:publishsource="Excel">	
<!-------- เริ่มข้อมูล ------------------------>

<h4>สรุปข้อมูลนักศึกษา <b><?=$title?></b> ภาคเรียนที่ <b><?=$semester?></b> ปีการศึกษา <b><?=$academicYear?></b></h4>


<?php
//กลุ่มเรียนทั้งหมด	
$sql_regis_group="Select * From regis_group where academicYear='$academicYear' AND semester='$semester' AND status='1' order by level_no,groupCode ASC"; 
$db_query_regis_group=mysqli_query($connect,$sql_regis_group);
$num_rows_regis_group=mysqli_num_rows($db_query_regis_group); /* นับ Reccord ที่พบ */	
$a_regis_group=0;
while($a_regis_group < $num_rows_regis_group)
{
	$result_regis_group = mysqli_fetch_array($db_query_regis_group);
	$groupCode=$result_regis_group['groupCode'];
	$cost_credit=$result_regis_group['cost_credit'];
	$cost_value=$result_regis_group['cost_value'];
	$cost_value2=$result_regis_group['cost_value'];
	//ยอดเงินที่ต้องลงทะเบียน
	$total_cost=$cost_credit+$cost_value;

	
//ดึงข้อมูลกลุ่มเรียน	
$result_std2018=select("std2018_studentgroup","where groupCode='$groupCode'");
	$grade=$result_std2018['grade'];
	$groupCode=$result_std2018['groupCode'];
	$groupName=$result_std2018['groupName'];
	$groupAbbr=$result_std2018['groupAbbr'];
	$teacherFirstname=$result_std2018['teacherFirstname'];
	$teacherLastname=$result_std2018['teacherLastname'];	
	  
	 $num_student=num_record("std2018_student","where groupCode='$groupCode' AND studentStatusCode='03'"); 
	 $num_student_all=num_record("std2018_student","where groupCode='$groupCode' "); 
	 $num_out=$num_student_all-$num_student;	
	
	 $num_payment=num_record("regis_payment_log","where academicYear='$academicYear' AND semester='$semester' AND groupCode='$groupCode'  ");
	
	
		//ตรวจสอบเงื่อนไขว่าจะแสดงกลุ่มนี้หรือไม่
	if($key=="0"){ if($num_student>$num_payment){ $show_group=1; } }
	if($key=="1"){  if($num_payment!=0){ $show_group=1; } }
	if($key=="2"){  $show_group=1; }
	
	//ตรวจสอบว่าลงทะเบียนครบไม่ไม่ ถ้าครบไม่ต้องแสดงผล
	if($show_group=="1"){

?>
ระดับ   <b><?=$grade?></b>    รหัสกลุ่ม <b><?=$groupCode?></b>  <b><?=$groupName?></b><br>
ครูที่ปรึกษา <b><?php if($teacherFirstname!=""){ echo "ครู$teacherFirstname    $teacherLastname"; }?></b><br>
จำนวนนักศึกษาทั้งหมด  <b><?=$num_student?></b> คน  ชำระเงินค่าลงทะเบียน <b><?=$num_payment?></b> คน  ยังไม่ได้ชำระเงินค่าลงทะเบียน <b><?php $num_student_lost=$num_student-$num_payment;  echo $num_student_lost;?></b> คน  <br>
ร้อยละของนักศึกษาที่ชำระเงินค่าลงทะเบียน  <b><?php  $percent=$num_payment/$num_student*100; $percent=number_format($percent,2); echo $percent; ?></b><br>
ยอดเงินค่าลงทะเบียน <b><?php echo number_format($total_cost);?></b> บาทต่อคน <br>	
	(รายชื่อนักศึกษา <b><?=$title?></b>)	
<?php	
//ดึงค่านักศึกษาในกลุ่ม
	$sql="Select * From std2018_student,prefix where std2018_student.groupCode='$groupCode' AND std2018_student.studentStatusCode='03' AND std2018_student.prefixID=prefix.prefix_id  order by std2018_student.studentCode ASC"; 

$db_query=mysqli_query($connect,$sql);
$num_rows=mysqli_num_rows($db_query); /* นับ Reccord ที่พบ */
//echo "มีจำนวน <b>$num_rows</b> คน";		
if($num_rows==0)
{
	echo " ไม่พบข้อมูลนักศึกษา";
}else{
?>
         
<table width="779" border="1">
    <thead>
      <tr>
        <th width="52" bgcolor="#E7E7E7"><center>
          ลำดับ
        </center></th>
        <th width="169" bgcolor="#E7E7E7"><center>รหัสประจำตัวนักศึกษา</center></th>
        <th width="152" bgcolor="#E7E7E7">ชื่อ</th>
		<th width="127" bgcolor="#E7E7E7">นามสกุล</th>
		<th width="127" bgcolor="#E7E7E7">เบอร์โทรศัพท์</th>  
		<th width="120" bgcolor="#E7E7E7">จำนวนเงินที่ต้องชำระ</th>   
		  <th width="75" bgcolor="#E7E7E7"><center>จำนวนเงินที่ชำระ</center></th>  
		 <th width="47" bgcolor="#E7E7E7"><center>TranC</center></th>  
		  <th width="95" bgcolor="#E7E7E7"><center>วันที่ชำระเงิน</center></th>  
        <th width="55" bgcolor="#E7E7E7"><center>สถานะ </center></th>  
	  </tr>
    </thead>
    <tbody>
<?php
$a=0;
$no=1;	
while($a < $num_rows)
{
	$result = mysqli_fetch_array($db_query);
	$prefix_name=$result['prefix_name'];
	$firstname=$result['firstname'];
	$surname=$result['surname'];
	$studentCode=$result['studentCode'];
	$studentStatusCode=$result['studentStatusCode'];
	$gpax=$result["gpax"];
	$tel=$result["tel"];
	$tel="&nbsp;$tel";
	 if($studentStatusCode=='03'){ $studentStatus="<font color=\"#0A7E02\">กำลังศึกษา</<font>"; }else{ $studentStatus="<font color=\"#FB4447\">พ้นสภาพ</<font>"; }
//------- ดึงข้อมูลค่าหน่วยกิต และค่าธรรมเนียมการศึกษา ------
$result_regis_student_log=select("regis_student_log","where studentCode='$studentCode'  AND academicYear='$academicYear' AND semester='$semester' ");
$cost_total=$result_regis_student_log["cost_total"];
$cost_total2=$result_regis_student_log["cost_total"];
$confirm=$result_regis_student_log['confirm'];	
if($cost_total==""){
	$cost_total="-";
}else{
	$cost_total=number_format($cost_total);	
}


	//ตรวจสอบว่ามีการชำระเงินหรือไม่
	$num_payment=num_record("regis_payment_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode'  ");
	if($num_payment!=0){
		//ดึงยอดการชำระเงิน
		$result_regis_payment_log=select("regis_payment_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode'  ");
		$PaymentDate=$result_regis_payment_log["PaymentDate"];
		$TransactionCode=$result_regis_payment_log["TransactionCode"];
		$cost_total3=$result_regis_payment_log["cost_total"];
		$cost_totalx=$result_regis_payment_log["cost_total"];
		$cost_totalx=number_format($cost_totalx);
		$comment_pay="/";
		
	}else{
		$PaymentDate="-";
		$TransactionCode="-";
		$comment_pay="x";
		$cost_totalx="-";
		$cost_total3="0";
	}
	
//ทำไฮไล กรณ์ที่ยอดเงินไม่ตรงกันในกลุ่ม คนที่เลือกค่าธรรมเนียมเพิ่มเติม
if($cost_total2!=""){
	if($cost_total2==$cost_total3){
		$high_ling_bg="#C5FFE8";
	}else{ 
		$high_ling_bg="#EEEED6";
	}	
}else{
	$high_ling_bg="#FFFFFF";
} 	
//แสดงเฉพาะนักศึกษาที่ไม่ลงทะเบียน
if($key=="0" AND $num_payment==0){
?>	
      <tr>
        <td bgcolor="<?=$high_ling_bg?>"><center>
          <?php echo $no;?>
        </center></td>
        <td bgcolor="<?=$high_ling_bg?>"><center><?=$studentCode?></center></td>
        <td bgcolor="<?=$high_ling_bg?>"><?php echo "$prefix_name$firstname";?></td>
        <td bgcolor="<?=$high_ling_bg?>"><?=$surname?></td>
		<td bgcolor="<?=$high_ling_bg?>"><?=$tel?></td>
		<td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$cost_total?> </td>
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$cost_totalx?> </td>
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$TransactionCode?> </td> 
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$PaymentDate?> </td>  
        <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$comment_pay?></td>
      </tr>
<?php
		$no++;
	}
	
//แสดงเฉพาะนักศึกษาที่ลงทะเบียน
if($key=="1" AND $num_payment==1){
?>	
      <tr>
        <td bgcolor="<?=$high_ling_bg?>"><center>
          <?php echo $no;?>
        </center></td>
        <td bgcolor="<?=$high_ling_bg?>"><center><?=$studentCode?></center></td>
        <td bgcolor="<?=$high_ling_bg?>"><?php echo "$prefix_name$firstname";?></td>
        <td bgcolor="<?=$high_ling_bg?>"><?=$surname?></td>
		<td bgcolor="<?=$high_ling_bg?>"><?=$tel?></td>
		<td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$cost_total?> </td>
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$cost_totalx?> </td>
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$TransactionCode?> </td> 
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$PaymentDate?> </td>  
        <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$comment_pay?></td>
      </tr>
<?php
		$no++;
	}
//แสดงนักศึกษทั้งหมด
if($key=="2" ){
?>	
      <tr>
        <td bgcolor="<?=$high_ling_bg?>"><center>
          <?php echo $no;?>
        </center></td>
        <td bgcolor="<?=$high_ling_bg?>"><center><?=$studentCode?></center></td>
        <td bgcolor="<?=$high_ling_bg?>"><?php echo "$prefix_name$firstname";?></td>
        <td bgcolor="<?=$high_ling_bg?>"><?=$surname?></td>
		<td bgcolor="<?=$high_ling_bg?>"><?=$tel?></td>
		<td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$cost_total?> </td>
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$cost_totalx?> </td>
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$TransactionCode?> </td> 
		 <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$PaymentDate?> </td>  
        <td style="text-align: center" bgcolor="<?=$high_ling_bg?>"><?=$comment_pay?></td>
      </tr>
<?php
		$no++;
	}
?>
	

		
<?php		
	$a++;
}
?>	
    </tbody>
  </table>	
<h4></h4>	
<!------ สิ้นสุดข้อมูล ------------------------>		
<?php
}
	}
	$a_regis_group++;
}
$login_name=$_SESSION['login_name'];	
$Publisher_day=thai3($today);
echo"ผู้พิมพ์   $login_name     วันที่พิมพ์รายงาน  $Publisher_day ";
echo"<br>$dev";
	?>	

</div>	
</body>
</html>
	