<?php
/////////////

$sql = "select * from bookpv_sync";
$dbquery = mysqli_query($connect,$sql);
$result = mysqli_fetch_array($dbquery);
$sync_code=md5($result['sync_code']);
$url=($result['pmss_url']);
$pmss_url=($result['pmss_url']);

////////////
require_once "modules/bookpv/time_inc.php";	

//**ส่วนการลงทะเบียนหนังสือ
if($index==1){
$day_now=date("Y-m-d H:i:s");
for($i=0;$i<count($_POST['ms_id']);$i++){
			$ms_id=$_POST['ms_id'][$i];
			$bookno=$_POST['bookno'][$i];
			$subject=$_POST['subject'][$i];
			$detail=$_POST['detail'][$i];
			$signdate=$_POST['signdate'][$i];
			$level=$_POST['level'][$i];
			$ref_id=$_POST['ref_id'][$i];
			$send_date=$_POST['send_date'][$i];
			$office=$_POST['office'][$i];
			
			if(isset($_POST[$ms_id])){
					//ส่วนการบันทึกทะเบียนรับส่งหนังสือราชการ
					$sql_check_bookregister = mysqli_query($connect,"SELECT * FROM  bookregister_year WHERE year_active='1' and  start_receive_num!='0' and school_code is null ") ;
					if($result_start=mysqli_fetch_array($sql_check_bookregister)){
					//เลขทะเบียน
					$sql_number="select  max(register_number) as number_max from bookregister_receive where year='$result_start[year]' ";
					$query_number=mysqli_query($connect,$sql_number);
					$result_number=mysqli_fetch_array($query_number);
					
							if($result_number['number_max']<$result_start['start_receive_num']){
							$register_number=$result_start['start_receive_num'];
							}
							else{
							$register_number=$result_number['number_max']+1;
							}
							
					$sender=$office;	
					$sql = "insert into bookregister_receive(year, register_number, book_no, signdate, book_from, book_to, subject, register_date, ref_id, officer, book_link, secret) values ('$result_start[year]', '$register_number', '$bookno', '$signdate', '$sender', 'สำนักงานเขตพื้นที่การศึกษา', '$subject', '$day_now', '$ref_id', '$_SESSION[login_user_id]', '15', '0')";
					$dbquery = mysqli_query($connect,$sql);
					}

					//บันทึกในระบบรับส่งหนังสือราชการ
			$sql_book = "insert into book_main (book_type, office, sender, level, secret, bookno, signdate, subject, detail, ref_id, send_date, bookregis_link) values ('5', '', '$_SESSION[login_user_id]', '$level', '0', '$bookno', '$signdate','$subject','$detail','$ref_id','$day_now','15')";
			$dbquery = mysqli_query($connect,$sql_book);
			$sql_sendto=	"insert into book_sendto_answer (send_level, ref_id, send_to, answer, answer_time) values ('2', '$ref_id','saraban','1','$day_now')";
			$dbquery = mysqli_query($connect,$sql_sendto);
			} //if isset
}
?>
<script type="text/JavaScript">
setTimeout("document.location.href='?option=book&task=main/receive';",1000);
</script>
<?php

}
//**


if(!(isset($_POST['urlGET']))){
echo "<br />";
echo "<form method='post' action='' id='frm1' name='frm1'>";
echo "<div style='display:none'>";
echo "<table align='center'>";
echo "<input type='hidden' name='urlGET[]' value='$url/modules/book/book_area/bookpv.php?sync_code=$sync_code&person=$_SESSION[login_user_id]'><br />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo "<br />";
echo "<div align='center'><b>การรับหนังสือพร้อมลงทะเบียน</b><font color='#000099'> เมื่อคลิกตกลงจะเป็นการรับหนังสือทั้งหมดที่ยังไม่ได้รับจาก PMSS</font></div>";
echo "<br />";
echo "<div align='center'><font color='#000099'>อีกทั้งสามารถลงทะเบียนในทะเบียนหนังสือรับ และส่งเข้าระบบรับส่งหนังสือราชการในระบบ AMSS++</font></div>";

echo "<br />";
echo "<div align='center'><input type='submit' value='ตกลง'></div>";
echo "</form>";
}

if(isset($_POST['urlGET'])){
echo "<form id='frm1' name='frm1'>";
$urlGET=$_POST['urlGET'];
foreach ($urlGET as &$value) {
   		$url = $value;
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$announce = $xmlDoc->getElementsByTagName('item');
		
echo "<br />";

///////////////////////////แจ้งข้อความ
$obecbook_alert_text=base64_decode($xmlDoc->getElementsByTagName('office_code')->item(0)->nodeValue);
echo "<div align='center' class='tagline2'>";
echo $obecbook_alert_text;
echo "</div>";
////////////////////////
$book_active=base64_decode($xmlDoc->getElementsByTagName('book_active')->item(0)->nodeValue);
//***
if($book_active==1){
echo "<table width='100%' border='0' align='center'>";
echo "<tr align='center'><td><font color='#006666' size='3'><strong>รับหนังสือราชการจากสำนักงานศึกษาธิการจังหวัด</strong></font></td></tr>";
echo "</table>";
			//**** 
			echo "<table align='center' width='98%' border='1' borderColor='#FF0033' style='border-collapse: collapse'>";
			echo "<tr align='center' bgcolor='#99ccff'><td width='70'>ที่</td><td width='200'>เลขที่หนังสือ</td><td>เรื่อง</td><td width='50'>ราย<br>ละเอียด</td><td width='120'>ลงวันที่</td><td width='200'>จาก</td><td width='160'>วันเวลาที่ส่ง</td></tr>";
					$i=1;
					foreach ($announce as $ann)
					{
					$ms_id=base64_decode($ann->getElementsByTagName('ms_id')->item(0)->nodeValue);
					$bookno=base64_decode($ann->getElementsByTagName('bookno')->item(0)->nodeValue);
					$subject=base64_decode($ann->getElementsByTagName('subject')->item(0)->nodeValue);
					$detail=base64_decode($ann->getElementsByTagName('detail')->item(0)->nodeValue);
					$signdate=base64_decode($ann->getElementsByTagName('signdate')->item(0)->nodeValue);
					$level=base64_decode($ann->getElementsByTagName('level')->item(0)->nodeValue);
					$ref_id=base64_decode($ann->getElementsByTagName('ref_id')->item(0)->nodeValue);
					$send_date=base64_decode($ann->getElementsByTagName('send_date')->item(0)->nodeValue);
					$office=base64_decode($ann->getElementsByTagName('office')->item(0)->nodeValue);
					
					if(($i%2) == 0)
					$color="#ffffff";
					else $color="#E5E5FF";
					echo "<tr bgcolor=$color align='left'>";
						echo "<td align='center'>$i </td>";
						echo "<td align='left'><input type='checkbox' name='$ms_id' checked>";
						echo $bookno;
						echo "</td>";
						echo "<td align='left'>";
						echo $subject;
						echo "</td>";
						echo "<td align='center'>";
						?><A HREF="javascript:void(0)"
onclick="window.open('<?php echo $pmss_url?>/modules/book/book_area/bookpv_detail2.php?b_id=<?php echo $ms_id ?>', 'bookdetail','width=500,height=500,scrollbars')" title="คลิกเพื่อดูรายละเอียด"><span style="text-decoration: none">คลิก</span></A></a><?php
						echo "</td>";
						echo "<td align='center'>";
						echo thai_date_3($signdate);
						echo "</td>";
						echo "<td align='left'>";
						echo $office;
						echo "</td>";
						echo "<td>";
						echo thai_date_4($send_date);
						echo "</td>";
						////
						echo "<input type='hidden' name='ms_id[]' value='$ms_id'>";
						echo "<input type='hidden' name='bookno[]' value='$bookno'>";
						echo "<input type='hidden' name='subject[]' value='$subject ($send_date)'>";
						echo "<input type='hidden' name='detail[]' value='$detail'>";
						echo "<input type='hidden' name='signdate[]' value='$signdate'>";
						echo "<input type='hidden' name='level[]' value='$level'>";
						echo "<input type='hidden' name='ref_id[]' value='$ref_id'>";
						echo "<input type='hidden' name='send_date[]' value='$send_date'>";
						echo "<input type='hidden' name='office[]' value='$office'>";
						////
						$i++;
					echo "</tr>";
					}
				echo "</table>";
				//****
		} //***			
	echo "<br>";
	}
unset($value); // break the reference with the last element
	if($book_active==1){
	echo "<div align='center'><INPUT TYPE='button' name='smb' value='ลงทะเบียนหนังสือ' onclick='goto_url(1)'></div>";
	}
echo "</form>";
}

?>
<script>
function goto_url(val){
callfrm("?option=bookpv&task=main/receive_register&index=1");   //page ประมวลผล
}
</script>

