<?php

if (isset($_POST['btnsave'])) {

	$h_doc_id = (int)mysql_real_escape_string(stripslashes($_POST['h_doc_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'])); 
	$signer = mysql_real_escape_string(stripslashes($_POST['signer'])); 
	$doc_to = mysql_real_escape_string(stripslashes($_POST['doc_to'])); 
	$place_sent = mysql_real_escape_string(stripslashes($_POST['place_sent'])); 
	$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'])); 
	$type_out = (int)mysql_real_escape_string(stripslashes($_POST['type_out'])); 
	//$refer_file = mysql_real_escape_string(stripslashes($_POST['refer_file'])); 
	//if($type_out == 1)  $doc_to="";
	$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 =========*/
	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_title,doc_io_id, doc_to,place_sent, tm_signer, doc_comment, remark, doc_file, refer_code, refer_date, createby, doc_status, m_date, m_step, owner)  
						values('$doc_code', '$doc_date', $doctype, '$doc_title','$type_out', '$doc_to','$place_sent', '$signer', '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$user_id', 2, date(NOW()),'public', '".$_SESSION['orgunitid_r']."')";
		}else{
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_title,doc_io_id, doc_to,place_sent, tm_signer, 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, '$doc_title','$type_out', '$doc_to', '$place_sent', '$signer', '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$refer_type', '$rftarget_rp', '$user_id', 2, date(NOW()), 'public', '".$_SESSION['orgunitid_r']."')";
		}
	
		$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 	
		if($type_out == 2){
			mysql_query("UPDATE tbl_in_out_document SET m_step='secretary' WHERE doc_id='$doc_id'");
			$sql_m = "INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid) VALUES($doc_id, '35', '$user_id')";
			$boolean_out = mysql_query($sql_m);
		}
		/*-------------------*/
		/*=======================*/
		if (isset($_POST['type'])) {
		/*------------- Loop to check Enter data---------------*/
			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{
								
								
								
							}
							mysql_query("UPDATE tbl_manage_doc SET receive_status = 2 WHERE doc_id = '$h_doc_id' AND orgunitid IN(31)");
							//mysql_query("UPDATE tbl_in_out_document SET m_step = 'public' WHERE doc_id = '$h_doc_id'");
								
						break;
						
					}			
			}
			
		}
		if($boolean){
			// insert to owner orgunit to see the icon
			 
			if($_SESSION['orgunitid_r'] !=null){
				$real_orgid = $_SESSION['orgunitid_r'];
				mysql_query("INSERT INTO tbl_manage_doc (doc_id, orgunitid, sysuserid, receive_status, type_doc_status) VALUES('$doc_id', '$real_orgid', '$user_id', 2, 'doc_out')");
			}
			
			$dir_doc = "doc".$doctype;
			/*------ check the directory is Exist or not ------*/
			if(!is_dir("document/file_out/".$dir_doc)){
				mkdir("document/file_out/".$dir_doc,0777,TRUE);
				//$objFopen = fopen($strFileName, 'w');
				//fclose($objFopen);	
			}
					
			$path= dirname(__FILE__) . '/../file_out/'.$dir_doc.'/'.$target_rp;
			if(move_uploaded_file($_FILES['doc_file']['tmp_name'], $path  )) chmod("document/file_out/".$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);   
			}
			// header after save complete
			if($_GET['st']=='dept'){
				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
				}
				header("Location: ?d=document/docout/docout_dept");
			}else{
				header("Location: ?d=document/docout/docout");
			}
		}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_title='$doc_title',doc_io_id='$type_out', doc_to='$doc_to', tm_signer='$signer',place_sent='$place_sent', 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_title='$doc_title', doc_to='$doc_to', tm_signer='$signer',place_sent='$place_sent', 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_title='$doc_title',doc_io_id='$type_out',  doc_to='$doc_to', tm_signer='$signer',place_sent='$place_sent', 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_title='$doc_title', doc_to='$doc_to', tm_signer='$signer',place_sent='$place_sent', 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 	
					$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 !=""){
					$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 (isset($_POST['type'])) {
		/*------------- Loop to check Enter data---------------*/
			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);
								}
								
							}
							//if($type_out==2){
								 mysql_query("UPDATE tbl_manage_doc SET receive_status = 2 WHERE doc_id = '$h_doc_id' AND orgunitid IN(31)");
								// mysql_query("UPDATE tbl_in_out_document SET m_step = 'public' WHERE doc_id = '$h_doc_id'");
							//}	
						break;
						
					}			
					
				//}else{
					// $err = "<script>alert('ບໍ່ສາມາດບັນທຶກ, ກະລຸນາເລືອກຜູ້ຮັບອຸປະພ້ອມ...!')</script>";
				//}
			}
		
		}
		if($boolean){
			
			$dir_doc = "doc".$doctype;
			/*------ check the directory is Exist or not ------*/
			if(!is_dir("document/file_out/".$dir_doc)){
				mkdir("document/file_out/".$dir_doc,0777,TRUE);
				//$objFopen = fopen($strFileName, 'w');
				//fclose($objFopen);	
			}
					
			$path= dirname(__FILE__) . '/../file_out/'.$dir_doc.'/'.$target_rp;
			if(move_uploaded_file($_FILES['doc_file']['tmp_name'], $path  )) chmod("document/file_out/".$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);  
			}
			// header after save complete
			if($_GET['st']=='dept'){
				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
				}
				header("Location: ?d=document/docout/docout_dept");
			}else{
				header("Location: ?d=document/docout/docout");
			}
		
		
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! $sql_doc</span>\"</script>";
		}
	}
}

?>