 <h2>นำเข้าข้อมูลการชำระเงิน</h2> 
<?php
if($_POST['action']=="import" AND $_POST['action2']=="")
{
$target_dir = 'xls/';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
 	//อัพโหลดไฟล์ xls 
	setlocale ( LC_ALL, 'en_US.UTF-8' );
	copy( $target_file ,"xls/student.xls");
	//echo "<center><br><br>อัพโหลดไฟล์ เรียบร้อยแล้ว<br><br> ";

/** PHPExcel */
require_once 'Classes/PHPExcel.php';

/** PHPExcel_IOFactory - Reader */
include 'Classes/PHPExcel/IOFactory.php';


$inputFileName = "xls/student.xls";  
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);  
$objReader = PHPExcel_IOFactory::createReader($inputFileType);  
$objReader->setReadDataOnly(true);  
$objPHPExcel = $objReader->load($inputFileName);  

/*
// for No header
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();

$r = -1;
$namedDataArray = array();
for ($row = 1; $row <= $highestRow; ++$row) {
    $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
    if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
        ++$r;
        $namedDataArray[$r] = $dataRow[$row];
    }
}
*/

$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();

$headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
$headingsArray = $headingsArray[1];
?>
<div class="card">
    <div class="card-body">
<form action="" method="post" enctype="multipart/form-data" id="form2" >
<h4>แสดงรายชื่อนักศึกษาที่ชำระเงิน</h4>
       
  <table class="table table-hover table-condensed-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th>เลขประจำตัวประชาชน/REF.1</th>
		 <th>กลุ่ม/REF.2</th> 
        <th>รหัสประจำตัวนักศึกษา</th>
		 <th>ชื่อ</th> 
		 <th>นามสกุล</th> 
		  <th>ระดับ</th>  
		   <th>สาขางาน</th>
		  <th>วันที่ชำระเงิน</th>
		  <th>รูปแบบ</th> 
		  <th style="text-align: right">ยอดที่ต้องจ่าย</th>
		  <th style="text-align: right">ยอดที่จ่าย</th> 
		  <th>หมายเหตุ</th> 
      </tr>
    </thead>
    <tbody>
		
  <?php
$r = -1;
$nodata=0;
$nodataOk=0;
$nodataErr=0;
$nodataErr2=0;
$errorCost=0;
$againData=0;
$no=1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
    $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
    if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
        ++$r;
		
        //foreach($headingsArray as $columnKey => $columnHeading) {
         //   $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
        //}
		/*
		  $namedDataArray[$r] = $dataRow[$row];
		  echo $dataRow[$row]['A'];
		  echo $dataRow[$row]['B'];
		  echo $dataRow[$row]['C'];
		  echo "<br>";
		  */
		  //ตรวจสอบ error เลขประจำตัวประชาชน
		$paymentDate=($dataRow[$row]['A']);
		$PIN= ($dataRow[$row]['B']);
		$ref2= ($dataRow[$row]['C']);
		$ref2 = substr($ref2, 0, 9) ;
		$TransactionCode= ($dataRow[$row]['D']);
		$cost_pay=($dataRow[$row]['E']);
		$cost_pay=$cost_pay/100;
		$Count_PIN=strlen($PIN);
		//echo $Count_PIN; 
		if($Count_PIN==11){
			$ref1="00$PIN";
		}
		if($Count_PIN==12){
			$ref1="0$PIN";
		}
		if($Count_PIN==13){
			$ref1="$PIN";
		}
		if($Count_PIN>13){
			$ref1="$PIN";
		}	
		//echo $ref1;	
		$Count_paymentDate=strlen($paymentDate);
		if($Count_paymentDate==7){
			$paymentDate="0$paymentDate";
		}
		//จัดรูปแบบวันที่ชำระเงิน
		$paymentDate=paymentDate($paymentDate);
		
		  $std2018_student=select("std2018_student,prefix,std2018_studentgroup","where std2018_student.idcard='$ref1' AND std2018_student.studentStatusCode='03' AND std2018_student.prefixID=prefix.prefix_id AND std2018_student.groupCode=std2018_studentgroup.groupCode ");
			$prefix_name=$std2018_student["prefix_name"];
		 	$firstname=$std2018_student["firstname"];
			$surname=$std2018_student["surname"];
			$studentCode=$std2018_student["studentCode"];
			$grade=$std2018_student["grade"];
			$groupName=$std2018_student["groupName"];
			$groupCode=$std2018_student["groupCode"];
			//echo "$groupCode";
			/*
			$regis_group=select("regis_group","where groupCode='$groupCode' ");
			$cost_credit=$regis_group["cost_credit"];
			$cost_value=$regis_group["cost_value"];
			$cost_total=$cost_credit+$cost_value;
			if($cost_total==0){  $cost_total="-";}
			*/
			//ดึงข้อมูลยอดเงินที่ต้องชำระ
			$regis_group=select("regis_student_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode' AND confirm='1'  ");
			$cost_total=$regis_group["cost_total"];
			if($cost_total==0){  $cost_total="-";}
			//echo "$cost_total $cost_pay<br> ";

						
		//$PIN= strlen($dataRow[$row]['A']);
		if($firstname=='')
		{
			$BG_ERROR="#FFB7B7";
			$nodataErr2++;
		}
		else
		{
			//ตรวจสอบการนำเข้าข้อมูลซ้ำ
			$num_regis_payment_log=num_record("regis_payment_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode' ");
			//echo $num_regis_payment_log;
			if($num_regis_payment_log==1){
				//ดึงค่าข้อมูลการชำระเงิน
				$result_regis_payment_log=select("regis_payment_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode' ");
				$PaymentDate_old=$result_regis_payment_log["PaymentDate"];
				if($PaymentDate_old==$paymentDate){ 
					$BG_ERROR="#D5FFD5";
					$Comment="";
					$nodataOk++;
				}
				else
				{
					$BG_ERROR="#D99E4B";
					$Comment="ชำระเงินซ้ำ ($PaymentDate_old)";	
					$againData++;
				}

			}
			else
			{



				if($cost_total==$cost_pay){
							$BG_ERROR="#D5FFD5";
							$Comment="";
							$nodataOk++;
				}
				else
				{
							$BG_ERROR="#FFFFA8";
							$Comment="ยอดเงินไม่ตรงกับยอดที่ต้องจ่าย";
							//อัพเดทยอดเงินให้ตรงกับยอดที่จ่าย
							//บันทึกการเช้าระบบ ของนักศึกษา
							$num_regis_student_log=num_record("regis_student_log","where  academicYear='$academicYear' AND semester='$semester'   AND studentCode='$studentCode' ");
							//เช็คสถานะของนักเรียน กำลังศึกษาอยู่หรือไม่
							$num_std2018_student=num_record("std2018_student","where  studentCode='$studentCode' AND studentStatusCode='03' ");
							if($num_regis_student_log==0 AND $num_std2018_student==1){
								insert("NULL,'$academicYear','$semester','$studentCode','$day_log',1,'$cost_pay' ","regis_student_log");
							}
							/*
							else
							{
								update("regis_student_log","confirm='1',cost_total='$cost_pay' ","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode'");
							}
							*/

							$errorCost++;
				}
			}	
		}
	
		

		
		$nodata++;
?>		
      <tr>
        <td bgcolor="<?=$BG_ERROR;?>"><center><?=$no?></center></td>
        <td bgcolor="<?=$BG_ERROR;?>"><?=$ref1?></td>
		<td bgcolor="<?=$BG_ERROR;?>"><?=$ref2?></td>  
        <td bgcolor="<?=$BG_ERROR;?>"><?=$studentCode?></td>
		<td bgcolor="<?=$BG_ERROR;?>"><?php echo"$prefix_name$firstname";?></td>  
		<td bgcolor="<?=$BG_ERROR;?>"><?=$surname?></td> 
		<td bgcolor="<?=$BG_ERROR;?>"><?=$grade?></td> 
		<td bgcolor="<?=$BG_ERROR;?>"><?=$groupName?></td> 
		<td bgcolor="<?=$BG_ERROR;?>"><?=$paymentDate?></td> 
		 <td bgcolor="<?=$BG_ERROR;?>"><?=$TransactionCode?></td>  
		 <td bgcolor="<?=$BG_ERROR;?>" style="text-align: right"><?php echo number_format($cost_total);?></td>  
		  <td bgcolor="<?=$BG_ERROR;?>" style="text-align: right"><?php echo number_format($cost_pay);?></td>   
		  <td bgcolor="<?=$BG_ERROR;?>" ><?=$Comment?></td>
      </tr>
    <?php
		$no++;
    }
    }

//echo '<pre>';
//var_dump($namedDataArray);
//echo '</pre><hr />';

?>    
    </tbody>
  </table>
<ui>	
	<li>จำนวนข้อมูล <strong>การชำระเงินทั้งหมด     <?=$nodata;?> รายการ</strong></li>
	<li> ไม่พบข้อมูลนักศึกษา <strong><?=$nodataErr2;?></strong> คน</li>
	<li> ยอดเงินที่ชำระไม่ถูกต้อง <strong><?=$errorCost;?></strong> คน</li>
	<li> ตรวจสอบข้อมูลนักศึกษาชำระเงินซ้ำ <strong><?=$againData;?></strong> คน</li>
	<li> ตรวจสอบข้อมูลนักศึกษาสมบูรณ์ <strong><?=$nodataOk;?></strong> คน</li>
	
</ui>
<div align="center">	
	<div class="alert alert-info">
    <strong>ข้อแนะนำ </strong> ระบบจะนำเข้าเฉพาะข้อมูลที่สมบูรณ์ (สีเขียว) เท่านั้น.
  </div>
  <button type="button" class="btn btn-primary" onClick="window.location.reload();"><i class="fa fa-refresh"></i> Refresh</button>&nbsp;&nbsp;
	<button type="submit" class="btn btn-warning" id="btnSearch" ><i class="fa fa-file-excel-o fa-fw"></i> ยืนยันการนำเข้าข้อมูล</button>&nbsp;&nbsp;
	<a href="?op=money&module=cancel" class="btn btn-secondary" role="button"><i class="fa fa-share" aria-hidden="true"></i> ยกเลิก</a>
    <input name="action" type="hidden" id="action" value="import">
	<input name="action2" type="hidden" id="action2" value="ok">
</div>	
</form> 
  </div>
</div>	


<?php 
}
elseif($_POST['action']=="import" AND $_POST['action2']=="ok"){
	//ยืนยันการนำเข้า

/** PHPExcel */
require_once 'Classes/PHPExcel.php';

/** PHPExcel_IOFactory - Reader */
include 'Classes/PHPExcel/IOFactory.php';


$inputFileName = "xls/student.xls";  
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);  
$objReader = PHPExcel_IOFactory::createReader($inputFileType);  
$objReader->setReadDataOnly(true);  
$objPHPExcel = $objReader->load($inputFileName);  

/*
// for No header
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();

$r = -1;
$namedDataArray = array();
for ($row = 1; $row <= $highestRow; ++$row) {
    $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
    if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
        ++$r;
        $namedDataArray[$r] = $dataRow[$row];
    }
}
*/

$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();

$headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
$headingsArray = $headingsArray[1];
?>
<div class="card">
    <div class="card-body">
<form action="" method="post" enctype="multipart/form-data" id="form2" >
<h4>นำเข้ารายชื่อนักศึกษาที่ชำระเงิน</h4>
       
  <table class="table table-hover table-condensed-sm">
    <thead>
      <tr>
        <th><center>#</center></th>
        <th>เลขประจำตัวประชาชน/ref.1</th>
		 <th>กลุ่ม/ref.2</th> 
        <th>รหัสประจำตัวนักศึกษา</th>
		 <th>ชื่อ</th> 
		 <th>นามสกุล</th> 
		  <th>ระดับ</th>  
		   <th>สาขางาน</th>  
		  <th>วันที่ชำระเงิน</th>
		  <th>รูปแบบ</th> 
		  <th style="text-align: right">ยอดที่ต้องจ่าย</th>
		  <th style="text-align: right">ยอดที่จ่าย</th>  
      </tr>
    </thead>
    <tbody>
		
  <?php
$r = -1;
$nodata=0;
$nodataOk=0;
$nodataErr=0;
$nodataErr2=0;
$errorCost=0;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
    $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
    if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
        ++$r;
		
        //foreach($headingsArray as $columnKey => $columnHeading) {
         //   $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
        //}
		/*
		  $namedDataArray[$r] = $dataRow[$row];
		  echo $dataRow[$row]['A'];
		  echo $dataRow[$row]['B'];
		  echo $dataRow[$row]['C'];
		  echo "<br>";
		  */
		  //ตรวจสอบ error เลขประจำตัวประชาชน
		$paymentDate=($dataRow[$row]['A']);
		$PIN= ($dataRow[$row]['B']);
		$ref2= ($dataRow[$row]['C']);
		$ref2 = substr($ref2, 0, 9) ;
		$TransactionCode= ($dataRow[$row]['D']);
		$cost_pay=($dataRow[$row]['E']);
		$cost_pay=$cost_pay/100;
		$Count_PIN=strlen($PIN);
		if($Count_PIN==11){
			$ref1="00$PIN";
		}
		if($Count_PIN==12){
			$ref1="0$PIN";
		}
		if($Count_PIN==13){
			$ref1="$PIN";
		}
		if($Count_PIN>13){
			$ref1="$PIN";
		}	
		$Count_paymentDate=strlen($paymentDate);
		if($Count_paymentDate==7){
			$paymentDate="0$paymentDate";
		}
		//จัดรูปแบบวันที่ชำระเงิน
		$paymentDate=paymentDate($paymentDate);
		
		 
		 $std2018_student=select("std2018_student,prefix,std2018_studentgroup","where std2018_student.idcard='$ref1' AND std2018_student.studentStatusCode='03' AND std2018_student.prefixID=prefix.prefix_id AND std2018_student.groupCode=std2018_studentgroup.groupCode ");
			$prefix_name=$std2018_student["prefix_name"];
		 	$firstname=$std2018_student["firstname"];
			$surname=$std2018_student["surname"];
			$studentCode=$std2018_student["studentCode"];
			$grade=$std2018_student["grade"];
			$groupName=$std2018_student["groupName"];
			$groupCode=$std2018_student["groupCode"];
			//echo "$groupCode";
			/*
			$regis_group=select("regis_group","where groupCode='$groupCode' ");
			$cost_credit=$regis_group["cost_credit"];
			$cost_value=$regis_group["cost_value"];
			$cost_total=$cost_credit+$cost_value;
			if($cost_total==0){  $cost_total="-";}
			*/
			//ดึงข้อมูลยอดเงินที่ต้องชำระ
			$regis_group=select("regis_student_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode' AND confirm='1'  ");
			$cost_total=$regis_group["cost_total"];
			if($cost_total==0){  $cost_total="-";}
			//echo "$cost_total $cost_pay<br> ";

						
		//$PIN= strlen($dataRow[$row]['A']);
		if($firstname=='')
		{
			$BG_ERROR="#FFB7B7";
			$nodataErr2++;
		}
		else
		{

			if($cost_total==$cost_pay){
						$BG_ERROR="#D5FFD5";
						$nodataOk++;
						//ตรวจสอบว่ามีการเพิ่มข้อมูลการชำระเงินนักศึกษคนนี้เข้าไปหรือยัง
						$num_regis_payment_log=num_record("regis_payment_log","where academicYear='$academicYear' AND semester='$semester' AND studentCode='$studentCode' AND groupCode='$groupCode'  ");
						//echo "num_regis_payment_log=$num_regis_payment_log $nodataOk $paymentDate<br>";
						if($num_regis_payment_log==0){
							insert("NULL,'$academicYear','$semester','$studentCode','$groupCode','$paymentDate','$TransactionCode','$cost_total','$day_log' ","regis_payment_log");
						}
				
			}
			else
			{
						$BG_ERROR="#FFFFA8";
						$errorCost++;
			}
		}
	
		

		
		$nodata++;
?>		
      <tr>
        <td bgcolor="<?=$BG_ERROR;?>"><center><?php if($BG_ERROR=="#D5FFD5"){  echo"<i class=\"fa fa-check\" aria-hidden=\"true\"></i>";}else{ echo"<i class=\"fa fa-ban\" aria-hidden=\"true\"></i>"; }   ?></center></td>
        <td bgcolor="<?=$BG_ERROR;?>"><?=$ref1?></td>
		<td bgcolor="<?=$BG_ERROR;?>"><?=$ref2?></td>  
        <td bgcolor="<?=$BG_ERROR;?>"><?=$studentCode?></td>
		<td bgcolor="<?=$BG_ERROR;?>"><?php echo"$prefix_name$firstname";?></td>  
		<td bgcolor="<?=$BG_ERROR;?>"><?=$surname?></td> 
		<td bgcolor="<?=$BG_ERROR;?>"><?=$grade?></td> 
		<td bgcolor="<?=$BG_ERROR;?>"><?=$groupName?></td> 
		<td bgcolor="<?=$BG_ERROR;?>"><?=$paymentDate?></td> 
		 <td bgcolor="<?=$BG_ERROR;?>"><?=$TransactionCode?></td>  
		 <td bgcolor="<?=$BG_ERROR;?>" style="text-align: right"><?php echo number_format($cost_total);?></td>  
		  <td bgcolor="<?=$BG_ERROR;?>" style="text-align: right"><?php echo number_format($cost_pay);?></td>   
      </tr>
    <?php
    }
    }

//echo '<pre>';
//var_dump($namedDataArray);
//echo '</pre><hr />';

?>    
    </tbody>
  </table>
<ui>	
	<li>จำนวนข้อมูล <strong>การชำระเงินทั้งหมด     <?=$nodata;?> รายการ</strong></li>
	<li> ไม่พบข้อมูลนักศึกษา <strong><?=$nodataErr2;?></strong> คน</li>
	<li> ยอดเงินที่ชำระไม่ถูกต้อง <strong><?=$errorCost;?></strong> คน</li>
	<li> ตรวจสอบข้อมูลนักศึกษาสมบูรณ์ <strong><?=$nodataOk;?></strong> คน</li>
	
</ui>
	<div class="alert alert-success">
  <strong>Success!</strong> นำเข้าข้อมูลการชำระเงินของนักศึกษาตามรายการที่สมบูรณ์  <i class="fa fa-check" aria-hidden="true"></i> เรียบร้อยแล้ว</div>
<div align="center"> <a href="?op=money" class="btn btn-success" role="button">นำเข้าข้อมูลเสร็จสมบูรณ์</a></div>	
</form> 
  </div>
</div>		
<?php	
	unlink("xls/student.xls");
	
}
else
{
?>
<div class="card">
    <div class="card-body">
<form action="" method="post" enctype="multipart/form-data" id="form1" >
  <div class="form-group">	
<div align="center"><img src="img/import-payment-Excel4.png"  class="img-fluid" /><br>ตัวอย่างไฟล์ xls นำเข้าข้อมูล xls </div>	
</div>
	<hr>	
 <div class="form-group" align="center">	
  <label for="fileToUpload">ไฟล์ต้องเป็น xls   &nbsp;</label>
    <input name="fileToUpload" type="file" required="required" id="fileToUpload">&nbsp;&nbsp;
    <button type="submit" class="btn btn-primary" id="btnSearch" ><i class="fa fa-file-excel-o fa-fw"></i> นำเข้า</button>
    <input name="action" type="hidden" id="action" value="import">
	 <input name="action2" type="hidden" id="action2" value="">
</div>	 
</form> 
  </div>
</div>


<?php
}
?>	