<?php
// in linux 
if (isset($_POST['type'])) {
	$h_doc_id = mysql_real_escape_string(stripslashes($_POST['h_doc_id']));
	//$sp_id = (int)mysql_real_escape_string(stripslashes($_POST['sp_id']));
	$now = date('Y-m-d G:i:s');
	$today = date('Ymd');
	/*-------------------------- document info -------------*/
	$doc_code = mysql_real_escape_string(stripslashes($_POST['doc_code'])); 
	$doc_date = mysql_real_escape_string(stripslashes($_POST['doc_date'])); 
	$doctype = mysql_real_escape_string(stripslashes($_POST['doctype'])); 
	$io_type = mysql_real_escape_string(stripslashes($_POST['io_type'])); 
	$etl_code = mysql_real_escape_string(stripslashes($_POST['etl_code'])); 
	$doc_from = mysql_real_escape_string(stripslashes($_POST['doc_from'])); 
	$doc_title = mysql_real_escape_string(stripslashes($_POST['doc_title'])); 
	$doc_comment = mysql_real_escape_string(stripslashes($_POST['doc_comment'])); 
	$remark = mysql_real_escape_string(stripslashes($_POST['remark'])); 
	//$doc_file = mysql_real_escape_string(stripslashes($_POST['doc_file'])); 
	/*-------------------refer document ---------------------*/
	$refer_code = mysql_real_escape_string(stripslashes($_POST['refer_code'])); 
	$refer_date = mysql_real_escape_string(stripslashes($_POST['refer_date'])); 
	$refer_type = mysql_real_escape_string(stripslashes($_POST['refer_type'])); 
	//$refer_file = mysql_real_escape_string(stripslashes($_POST['refer_file'])); 
	
	$user_id = $_SESSION['sys_id']; 
	
	/*-------------------------- upload file ----------------------------------*/
	$target = basename( $_FILES['doc_file']['name']) ;
	$target_refer = basename( $_FILES['refer_file']['name']) ;
	$random_digit=rand(0000,9999);
	$new_file_name=$today.$random_digit.$target;
	$target_rp=str_replace(" ","_",$new_file_name);
	//$path= dirname(__FILE__) . '/../files/'.$target_rp;
	/*-------the refer file --------*/
	$new_file_rfname=$today.$random_digit.$target_refer;
	$rftarget_rp=str_replace(" ","_",$new_file_rfname);
	
	/*======= Enter to table =========*/
	/*================ Check to Add OR Edition ======================================*/
	
	if($h_doc_id == "" && $_GET['action'] != "edit"){
		if($target=="") $target_rp ="";
		if($target_refer =="" || $refer_type == ""){
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_io_id, etl_code, doc_title, doc_from, doc_comment, remark, doc_file, refer_code, refer_date,  createby, doc_status, m_date, m_step, owner)  
						values('$doc_code', '$doc_date', $doctype, $io_type, '$etl_code', '$doc_title', '$doc_from', '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$user_id', 1, date(NOW()), 'secretary', '31')";
		}else{
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_io_id, etl_code, doc_title, doc_from, doc_comment, remark, doc_file, refer_code, refer_date, refer_type, refer_file, createby, doc_status, m_date, m_step)  
						values('$doc_code', '$doc_date', $doctype, $io_type, '$etl_code', '$doc_title', '$doc_from', '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$refer_type', '$rftarget_rp', '$user_id', 1, date(NOW()), 'secretary', '31')";
		}
		//header("Location: ?d=$sql_doc");
		$boolean = mysql_query($sql_doc) ;//or die (mysql_error());
		if($boolean){
			
			$dir_doc = "doc".$doctype;
			/*------ check the directory is Exist or not ------*/
			if(!is_dir("document/file_in/".$dir_doc)){
				mkdir("document/file_in/".$dir_doc,0777,TRUE);
				//$objFopen = fopen($strFileName, 'w');
				//fclose($objFopen);	
			}
					
			$path= dirname(__FILE__) . '/../file_in/'.$dir_doc.'/'.$target_rp;
			/*============= change the upload file mod =========*/
			
			if(move_uploaded_file($_FILES['doc_file']['tmp_name'], $path  )) chmod($path, 0755); // chmod("document/file_in/".$dir_doc.'/'.$target_rp, 0755);
			/*---------if refer file is attached --------------*/
			if($target_refer !=""){
				$dir_doc = "doc".$refer_type;
				/*------ check the directory is Exist or not ------*/
				if(!is_dir("document/refer_file/".$dir_doc)){
					mkdir("document/refer_file/".$dir_doc,0777,TRUE);
					//$objFopen = fopen($strFileName, 'w');
					//fclose($objFopen);	
				}
				$rf_path= dirname(__FILE__) . '/../refer_file/'.$dir_doc.'/'.$rftarget_rp;
				
				if(move_uploaded_file($_FILES['refer_file']['tmp_name'], $rf_path )) chmod("document/refer_file/".$dir_doc.'/'.$rftarget_rp, 0755); 
			}
			
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
		}
	
	}else{
		 
		if($target_refer =="" || $refer_type == ""){
			if($target==""){
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', refer_code='$refer_code', refer_date='$refer_date',  createby=$user_id WHERE doc_id = $h_doc_id";
			}else{
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', doc_file='$target_rp', refer_code='$refer_code', refer_date='$refer_date',  createby=$user_id WHERE doc_id = $h_doc_id";
			}
			
		}else{
			if($target==""){
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', refer_code='$refer_code', refer_date='$refer_date', refer_type='$refer_type', refer_file='$rftarget_rp', createby=$user_id WHERE doc_id = $h_doc_id";
			}else{
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', doc_file='$target_rp', refer_code='$refer_code', refer_date='$refer_date', refer_type='$refer_type', refer_file='$rftarget_rp', createby=$user_id WHERE doc_id = $h_doc_id";
			}
			
			
		}
		$boolean = mysql_query($sql_doc) ;//or die (mysql_error());
		if($boolean){
			
			$dir_doc = "doc".$doctype;
			/*------ check the directory is Exist or not ------*/
			if(!is_dir("document/file_in/".$dir_doc)){
				mkdir("document/file_in/".$dir_doc,0777,TRUE);
				//$objFopen = fopen($strFileName, 'w');
				//fclose($objFopen);	
			}
					
			$path= dirname(__FILE__) . '/../file_in/'.$dir_doc.'/'.$target_rp;
			if(move_uploaded_file($_FILES['doc_file']['tmp_name'], $path  )) chmod("document/file_in/".$dir_doc.'/'.$target_rp, 0755); 
			/*---------if refer file is attached --------------*/
			if($target_refer !=""){
				$dir_doc = "doc".$refer_type;
				/*------ check the directory is Exist or not ------*/
				if(!is_dir("document/refer_file/".$dir_doc)){
					mkdir("document/refer_file/".$dir_doc,0777,TRUE);
					//$objFopen = fopen($strFileName, 'w');
					//fclose($objFopen);	
				}
				$rf_path= dirname(__FILE__) . '/../refer_file/'.$dir_doc.'/'.$rftarget_rp;
				if(move_uploaded_file($_FILES['refer_file']['tmp_name'], $rf_path )) chmod("document/refer_file/".$dir_doc.'/'.$rftarget_rp, 0755); 
			}
			
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
		}
	}
	
	
	/*------------------------------------- CHECK TO UPDATE DOCUMENT ---------------------------------------*/
	
	/*------------- Loop to Enter data , the ICT head --------------- 
	for ($i = 0; $i < count($_POST['type_ict']); $i++) {	
				
		//$emp_id = mysql_real_escape_string(stripslashes($_POST['id'][$i]));
		$position_id = mysql_real_escape_string(stripslashes($_POST['position_id'][$i])); // the head id
			
			switch ($_POST['type_ict'][$i]) {				
				case "added" :	
					
					
				break;
				
				case "edited":
					
					if($h_doc_id == ""){ 
						//==== check if the head of ICT is Checked ====
						if (mysql_num_rows(mysql_query("SELECT * FROM tbl_in_out_document d INNER JOIN tbl_manage_doc m ON (m.doc_id = d.doc_id) WHERE d.doc_code = '$doc_code' AND m.m_date = '$now' AND m.position_id = $position_id ")) == 0) {
							$rs_m = mysql_fetch_array(mysql_query("SELECT MAX(doc_id) as mid FROM tbl_in_out_document "));
							$doc_id = $rs_m['mid'];
							//=========== inser ony the position in Head, Duputy of ICT====
							$sql_m = "INSERT INTO tbl_manage_doc (doc_id, position_id, sysuserid) VALUES($doc_id, $position_id, $user_id)";
							$boolean_in = mysql_query($sql_m);
						}
					}else{
							
						if (mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND position_id = $position_id ")) == 0) {
							//=========== inser ony the position in Head, Duputy of ICT====
							$sql_m = "INSERT INTO tbl_manage_doc (doc_id, position_id, sysuserid) VALUES($h_doc_id, $position_id, $user_id)";
						}else{
							$sql_m = "DELETE FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND position_id = $position_id ";
						}
						$boolean_in = mysql_query($sql_m);
					}
						
				break;
				
			}			
			
	}
	*/
	/*------------- Loop to check Enter data, the section head---------------*/
	for ($i = 0; $i < count($_POST['type']); $i++) {	
				
		$orgunitid = mysql_real_escape_string(stripslashes($_POST['id'][$i])); // the section id
		$m_id = mysql_real_escape_string(stripslashes($_POST['m_id'][$i]));
		//echo $_POST['type'][$i]	;		
		//if ($_POST['id'][$i] != "") {
			//echo $_POST['type'][$i]	;
			
			switch ($_POST['type'][$i]) {				
				case "added" :	
					
					
				break;
				
				case "edited":
					
					if($h_doc_id == ""){ 
						//if (mysql_num_rows(mysql_query("SELECT * FROM tbl_in_out_document d INNER JOIN tbl_manage_doc m ON (m.doc_id = d.doc_id) WHERE d.doc_code = '$doc_code' AND m.m_date = '$now' ")) == 0) {
							$rs_m = mysql_fetch_array(mysql_query("SELECT MAX(doc_id) as mid FROM tbl_in_out_document "));
							$doc_id = $rs_m['mid'];
							
							$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid, type_doc_status) VALUES($doc_id, '$orgunitid', '$user_id','doc_in')";
							$boolean_in = mysql_query($sql_m);
							
						//}
					}else{
						
						if (mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND orgunitid = $orgunitid ")) == 0) {	
							$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid, type_doc_status) VALUES('$h_doc_id', '$orgunitid', '$user_id', 'doc_in')";
							$boolean_out = mysql_query($sql_m);
						}else{
							$sql_m = "DELETE FROM tbl_manage_doc WHERE doc_id = '$h_doc_id' AND orgunitid = '$orgunitid' ";
							$boolean_out = mysql_query($sql_m);
						}
						
					}
					mysql_query("UPDATE tbl_manage_doc SET receive_status = 2 WHERE doc_id = '$h_doc_id' AND orgunitid IN(31, 35)");
					mysql_query("UPDATE tbl_in_out_document SET m_step = 'public' WHERE doc_id = '$h_doc_id' ");
						
				break;
				
			}			
			
		//}else{
			// $err = "<script>alert('ບໍ່ສາມາດບັນທຶກ, ກະລຸນາເລືອກຜູ້ຮັບອຸປະພ້ອມ...!')</script>";
		//}
	}
	// header after save complete
	if($boolean==true|| $boolean_in==true || $boolean_out==true){
		/*=========== check to send mail ====================*/
			if($h_doc_id ==""){
				//send_mail($doc_id); // on local
				//send_mail_server($doc_id); // on server 
			}else{
				//send_mail($h_doc_id);  // on local
				//send_mail_server($h_doc_id); // on server
			}
		// after complete
		if($_SESSION['sys_type']=="secretary"){
			if($_GET['st']=="scan"){
				header("Location: ?d=document/list_docin/addfile_doc&orgunitid=35");
			}else{
				header("Location: ?d=document/docin/docin");
			}
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class=''>ເພີ່ມ ຫຼື ແກ້ໄຂຂໍ້ມູນສໍາເລັດ.....! </span>\"</script>";
			//header("Location: ?d=document/docin/docin");
			if($_GET['st']=="assign"){
				header("Location: ?d=document/list_docin/send_doc&orgunitid=31&status=1");
			}
		}
	}else{
		$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
	}
}else{
	if(isset($_POST['btnsave'])){
	//$err = "<script>document.getElementById('errEq').innerHTML = \"<span class='errChk' >(ກະລຸນາເລືອກຢ່າງໜ້ອຍ 1 ພະແນກ/ສາຂາ)</span>\"</script>";
	$h_doc_id = mysql_real_escape_string(stripslashes($_POST['h_doc_id']));
	//$sp_id = (int)mysql_real_escape_string(stripslashes($_POST['sp_id']));
	$now = date('Y-m-d G:i:s');
	$today = date('Ymd');
	/*-------------------------- document info -------------*/
	$doc_code = mysql_real_escape_string(stripslashes($_POST['doc_code'])); 
	$doc_date = mysql_real_escape_string(stripslashes($_POST['doc_date'])); 
	$doctype = mysql_real_escape_string(stripslashes($_POST['doctype'])); 
	$io_type = mysql_real_escape_string(stripslashes($_POST['io_type'])); 
	$etl_code = mysql_real_escape_string(stripslashes($_POST['etl_code'])); 
	$doc_from = mysql_real_escape_string(stripslashes($_POST['doc_from'])); 
	$doc_title = mysql_real_escape_string(stripslashes($_POST['doc_title'])); 
	$doc_comment = mysql_real_escape_string(stripslashes($_POST['doc_comment'])); 
	$remark = mysql_real_escape_string(stripslashes($_POST['remark'])); 
	//$doc_file = mysql_real_escape_string(stripslashes($_POST['doc_file'])); 
	/*-------------------refer document ---------------------*/
	$refer_code = mysql_real_escape_string(stripslashes($_POST['refer_code'])); 
	$refer_date = mysql_real_escape_string(stripslashes($_POST['refer_date'])); 
	$refer_type = mysql_real_escape_string(stripslashes($_POST['refer_type'])); 
	//$refer_file = mysql_real_escape_string(stripslashes($_POST['refer_file'])); 
	
	$user_id = $_SESSION['sys_id']; 
	
	/*-------------------------- upload file ----------------------------------*/
	$target = basename( $_FILES['doc_file']['name']) ;
	$target_refer = basename( $_FILES['refer_file']['name']) ;
	$random_digit=rand(0000,9999);
	$new_file_name=$today.$random_digit.$target;
	$target_rp=str_replace(" ","_",$new_file_name);
	//$path= dirname(__FILE__) . '/../files/'.$target_rp;
	/*-------the refer file --------*/
	$new_file_rfname=$today.$random_digit.$target_refer;
	$rftarget_rp=str_replace(" ","_",$new_file_rfname);
	
	/*======= Enter to table =========*/
	/*================ Check to Add OR Edition ======================================*/
	
	if($h_doc_id == "" && $_GET['action'] != "edit"){
		if($target=="") $target_rp ="";
		if($target_refer =="" || $refer_type == ""){
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_io_id, etl_code, doc_title, doc_from, doc_comment, remark, doc_file, refer_code, refer_date,  createby, doc_status, m_date, m_step, owner)  
						values('$doc_code', '$doc_date', $doctype, $io_type, '$etl_code', '$doc_title', '$doc_from', '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$user_id', 1, date(NOW()),'secretary', '31')";
		}else{
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_io_id, etl_code, doc_title, doc_from, doc_comment, remark, doc_file, refer_code, refer_date, refer_type, refer_file, createby, doc_status, m_date, m_step, owner)  
						values('$doc_code', '$doc_date', $doctype, $io_type, '$etl_code', '$doc_title', '$doc_from', '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$refer_type', '$rftarget_rp', '$user_id', 1, date(NOW()), 'secretary', '31')";
		}
		//header("Location: ?d=$sql_doc");
		$boolean = mysql_query($sql_doc) ;//or die (mysql_error());
		// add to user counter 
		$rs_m = mysql_fetch_array(mysql_query("SELECT MAX(doc_id) as mid FROM tbl_in_out_document "));
		$doc_id = $rs_m['mid'];
		// fixed destination orgunitid as 	
		$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid) VALUES($doc_id, '35', '$user_id')";
		/*-------------------------*/
		$boolean_in = mysql_query($sql_m);
		if($boolean){
			
			$dir_doc = "doc".$doctype;
			/*------ check the directory is Exist or not ------*/
			if(!is_dir("document/file_in/".$dir_doc)){
				mkdir("document/file_in/".$dir_doc,0777,TRUE);
				//$objFopen = fopen($strFileName, 'w');
				//fclose($objFopen);	
			}
					
			$path= dirname(__FILE__) . '/../file_in/'.$dir_doc.'/'.$target_rp;
			/*============= change the upload file mod =========*/
			
			if(move_uploaded_file($_FILES['doc_file']['tmp_name'], $path  )) chmod($path, 0755); // chmod("document/file_in/".$dir_doc.'/'.$target_rp, 0755);
			/*---------if refer file is attached --------------*/
			if($target_refer !=""){
				$dir_doc = "doc".$refer_type;
				/*------ check the directory is Exist or not ------*/
				if(!is_dir("document/refer_file/".$dir_doc)){
					mkdir("document/refer_file/".$dir_doc,0777,TRUE);
					//$objFopen = fopen($strFileName, 'w');
					//fclose($objFopen);	
				}
				$rf_path= dirname(__FILE__) . '/../refer_file/'.$dir_doc.'/'.$rftarget_rp;
				
				if(move_uploaded_file($_FILES['refer_file']['tmp_name'], $rf_path )) chmod("document/refer_file/".$dir_doc.'/'.$rftarget_rp, 0755); 
			}
			
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
		}
	
	}else{
		 
		if($target_refer =="" || $refer_type == ""){
			if($target==""){
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', refer_code='$refer_code', refer_date='$refer_date',  createby=$user_id WHERE doc_id = $h_doc_id";
			}else{
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', doc_file='$target_rp', refer_code='$refer_code', refer_date='$refer_date',  createby=$user_id WHERE doc_id = $h_doc_id";
			}
			
		}else{
			if($target==""){
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', refer_code='$refer_code', refer_date='$refer_date', refer_type='$refer_type', refer_file='$rftarget_rp', createby=$user_id WHERE doc_id = $h_doc_id";
			}else{
				$sql_doc = "UPDATE tbl_in_out_document SET  doc_code= '$doc_code', doc_date = '$doc_date', doctype_id=$doctype, doc_io_id=$io_type, etl_code='$etl_code', doc_title='$doc_title', doc_from='$doc_from', doc_comment='$doc_comment', remark='$remark', doc_file='$target_rp', refer_code='$refer_code', refer_date='$refer_date', refer_type='$refer_type', refer_file='$rftarget_rp', createby=$user_id WHERE doc_id = $h_doc_id";
			}
			
			
		}
		$boolean = mysql_query($sql_doc) ;//or die (mysql_error());
		// assing to counter or secretary room
		/* 
			if (mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND orgunitid IN('35','31') ")) == 0) {	
				if($_SESSION['sys_type']=='counter'){
				// fixed destination orgunitid as 	
					$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid) VALUES('$doc_id', '35', '$user_id')";	
				}elseif($_SESSION['sys_type']=='secretary' && $target !=""){
					$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid) VALUES('$doc_id', '31', '$user_id')";
				}
			}else{
				if($_SESSION['sys_type']=='counter'){
				// fixed destination orgunitid as 	
					if (mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND orgunitid ='35' ")) == 0) {	
						$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid) VALUES('$doc_id', '35', '$user_id')";
					}else{
						$sql_m = "UPDATE tbl_manage_doc SET orgunitid = '35', sysuserid = '$user_id' WHERE doc_id='$doc_id' AND orgunitid IN('35','31')";
					}
				}elseif($_SESSION['sys_type']=='secretary' && $target !=""){
					if (mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND orgunitid ='31' ")) == 0) {	
						$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid) VALUES('$doc_id', '31', '$user_id')";
					}else{
						$sql_m = "UPDATE tbl_manage_doc SET orgunitid = '31', sysuserid = '$user_id' WHERE doc_id='$doc_id' AND orgunitid IN('35','31')";	
					}
				}
			}
			
			$boolean_out = mysql_query($sql_m);	
			*/
		
		if($boolean){
			
			$dir_doc = "doc".$doctype;
			/*------ check the directory is Exist or not ------*/
			if(!is_dir("document/file_in/".$dir_doc)){
				mkdir("document/file_in/".$dir_doc,0777,TRUE);
				//$objFopen = fopen($strFileName, 'w');
				//fclose($objFopen);	
			}
					
			$path= dirname(__FILE__) . '/../file_in/'.$dir_doc.'/'.$target_rp;
			if(move_uploaded_file($_FILES['doc_file']['tmp_name'], $path  )) chmod("document/file_in/".$dir_doc.'/'.$target_rp, 0755); 
			/*---------if refer file is attached --------------*/
			if($target_refer !=""){
				$dir_doc = "doc".$refer_type;
				/*------ check the directory is Exist or not ------*/
				if(!is_dir("document/refer_file/".$dir_doc)){
					mkdir("document/refer_file/".$dir_doc,0777,TRUE);
					//$objFopen = fopen($strFileName, 'w');
					//fclose($objFopen);	
				}
				$rf_path= dirname(__FILE__) . '/../refer_file/'.$dir_doc.'/'.$rftarget_rp;
				if(move_uploaded_file($_FILES['refer_file']['tmp_name'], $rf_path )) chmod("document/refer_file/".$dir_doc.'/'.$rftarget_rp, 0755); 
			}
			
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
		}
	}


	// header after save complete
	if($boolean_in==true || $boolean==true){
		/*=========== check to send mail ====================*/
			if($h_doc_id ==""){
				//send_mail($doc_id); // on local
				//send_mail_server($doc_id); // on server 
			}else{
				//send_mail($h_doc_id);  // on local
				//send_mail_server($h_doc_id); // on server
			}
			// after kk
		if($_SESSION['sys_type']=="secretary"){
			if($_GET['st']=="scan"){
				header("Location: ?d=document/list_docin/addfile_doc&orgunitid=35");
			}else{
				header("Location: ?d=document/docin/docin");
			}
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class=''>ເພີ່ມ ຫຼື ແກ້ໄຂຂໍ້ມູນສໍາເລັດ.....! </span>\"</script>";
			//header("Location: ?d=document/docin/docin");
			if($_GET['st']=="assign"){
				header("Location: ?d=document/list_docin/send_doc&orgunitid=31&status=1");
			}
		}
		
	}else{
		$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
	}
	
	}
}
if($_GET["action"]=="del"){
	$doc_id = mysql_real_escape_string(stripslashes($_GET['doc_id']));
	$emp_id = mysql_real_escape_string(stripslashes($_GET['emp_id']));
	$m_id = mysql_real_escape_string(stripslashes($_GET['m_id']));
	
	$rs=mysql_query("DELETE FROM tbl_manage_doc WHERE receive_person = $emp_id AND m_id = $m_id ");

	if($rs){
		header("Location: ?d=document/docin/docin_add&doc_id=$doc_id&action=edit");
	}else{
		echo "<h2>ERROR Delete</h2>";
	}
	
}

?>