<?php
header("Cache-Control: no-store, no-cache, must-revalidate");             
header("Cache-Control: post-check=0, pre-check=0", false);   
echo '<?xml version="1.0" encoding="utf-8"?>';

require_once "../../../amssplus_connect.php";	

/////////////////////////
if(isset($_GET['sync_code'])){
					
			//ตรวจสอบการ Sync
					$sql = "select * from  book_sync_amssplus where sync_code='$_GET[sync_code]'";
					$dbquery = mysqli_query($connect,$sql);
					$result = mysqli_fetch_array($dbquery);
					if($result){
					$office_code=$result['office_code'];
					$office_code=trim($office_code);
					$_SESSION['bookpv_user_office']=$office_code;
					}
					else{
					echo "<br />";
					echo "<div align='center'>รหัสเชื่อมระบบไม่ถูกต้อง โปรดแจ้งผู้ดูและระบบ</div>";
					exit();
					}
					

			//ตรวจสอบการเป็นบุคลากร สพท. หรือสถานศึกษานอกสังกัด สพท.
			$sql_person = "select * from person_edoffice_main where office_code='$office_code' and person_id='$_GET[person]' and status='0'";
			$dbquery_person = mysqli_query($connect,$sql_person);
			$result_person = mysqli_fetch_array($dbquery_person);
			if($result_person){
			$_SESSION['bookpv_user_id']=$_GET['person'];
			$_SESSION['bookpv_user_group']=3;
			}
			else{
			$sql_person = "select * from person_sch_main2 where school_code='$office_code' and person_id='$_GET[person]' and status='0'";
			$dbquery_person = mysqli_query($connect,$sql_person);
			$result_person = mysqli_fetch_array($dbquery_person);
					if($result_person){
					$_SESSION['bookpv_user_id']=$_GET['person'];
					$_SESSION['bookpv_user_group']=2;
					}
					else{
							echo "<div align='center'><br>";
							echo "คุณไม่มีชื่อในระบบ PMSS โปรดตรวจสอบกับเจ้าหน้าที่";
							echo "</div>";
							exit();
					}
			}
}
else{
			if($_SESSION['bookpv_user_id']!=$_REQUEST['person']){
			echo "คุณไม่ได้รับสิทธิ์";
			exit();
			}
}
 
//////////////////////////////////

$sync_ok=1; $person_ok=1;  // ชั่วคราว

if($sync_ok==1 and $person_ok==1){

// อาเรย์ชื่อหน่วยงาาน
$office_name_ar['saraban']="สารบรรณกลาง";
$sql_work_group = mysqli_query($connect,"SELECT * FROM  system_workgroup") ;
while ($row_work_group= mysqli_fetch_array($sql_work_group)){
$office_name_ar[$row_work_group['workgroup']]=$row_work_group['workgroup_desc']."(ศธจ)";
}
$sql_sch = mysqli_query($connect,"SELECT * FROM  system_school2") ;
while ($row_sch= mysqli_fetch_array($sql_sch)){
$office_name_ar[$row_sch['school_code']]=$row_sch['school_name'];
}
$sql_office = mysqli_query($connect,"SELECT * FROM system_sub_department") ;
while ($row_office= mysqli_fetch_array($sql_office)){
$office_name_ar[$row_office['code']]=$row_office['short_sub_dpm'];
}

$sql="select * from book_main, book_sendto_answer where book_main.ref_id=book_sendto_answer.ref_id  and  book_sendto_answer.send_to='$office_code'  and book_sendto_answer.status is null and book_sendto_answer.answer is null and  book_sendto_answer.school is null  order by book_main.ms_id";

$objQuery = mysqli_query($connect,$sql);
$num_row=mysqli_num_rows($objQuery);
		if($num_row<1){
		$text="ไม่มีหนังสือราชการใหม่ที่ยังไม่ได้รับ";
		$book_actice=0;
		}
		else{
		$text="";
		$book_actice=1;
		}
$day_now=date("Y-m-d H:i:s");
?>
<info>
	<office_code><?php echo base64_encode($text);?></office_code>
	<book_active><?php echo base64_encode($book_actice);?></book_active>
	<bookobec>
			<?php
			while($obResult=mysqli_fetch_array($objQuery))
			{
			$e_office_code=$obResult['office'];
			$e_office_name=$office_name_ar[$e_office_code];
			
			//ลงทะเบียนหนังสือรับpmss
			$sql_answer = mysqli_query($connect,"update book_sendto_answer set answer='1', answer_time='$day_now' where ref_id='$obResult[ref_id]' and send_to='$office_code'") ;
			?>
			<item>
				<ms_id><?php echo base64_encode($obResult['ms_id']);?></ms_id>
				<bookno><?php echo base64_encode($obResult['bookno']);?></bookno>
				<ref_id><?php echo base64_encode($obResult['ref_id']);?></ref_id>
				<level><?php echo base64_encode($obResult['level']);?></level>
				<signdate><?php echo base64_encode($obResult['signdate']);?></signdate>
				<subject><?php echo base64_encode($obResult['subject']);?></subject>
				<detail><?php echo base64_encode($obResult['detail']);?></detail>
				<send_date><?php echo base64_encode($obResult['send_date']);?></send_date>
				<office><?php echo base64_encode($e_office_name);?></office>
			</item>
		<?php
		}
		?>
	</bookobec>
</info>
<?php
}
?>