<?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'])); 
	//$refer_file = mysql_real_escape_string(stripslashes($_POST['refer_file'])); 
	
	$user_id = $_SESSION['sys_user'][0]['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);
	//$rf_path= dirname(__FILE__) . '/../files/'.$rftarget_rp;
	/*======= Enter to table =========*/
	if($h_doc_id == "" && $_GET['action'] != "edit"){
		if($target_refer =="" || $refer_type == ""){
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_title, doc_to,place_sent, signer_id, doc_comment, remark, doc_file, refer_code, refer_date, createby, doc_status, m_date)  
						values('$doc_code', '$doc_date', $doctype, '$doc_title', '$doc_to','$place_sent', $signer, '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', $user_id, 2, date(NOW()))";
		}else{
			$sql_doc = "INSERT INTO tbl_in_out_document (doc_code, doc_date, doctype_id, doc_title, doc_to,place_sent, signer_id, doc_comment, remark, doc_file, refer_code, refer_date, refer_type, refer_file, createby, doc_status, m_date)  
						values('$doc_code', '$doc_date', $doctype, '$doc_title', '$doc_to', '$place_sent', $signer, '$doc_comment', '$remark', '$target_rp', '$refer_code', '$refer_date', '$refer_type', '$rftarget_rp', $user_id, 2, date(NOW()))";
		}
	
		$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_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
			//header("Location: ?d=document/docout/docout");
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
		}
	}else{
		if($target_refer =="" || $refer_type == ""){
			$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', signer_id=$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{
			$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', signer_id=$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());
		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
			//header("Location: ?d=document/docout/docout");
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! $sql_doc</span>\"</script>";
		}
	
	}
	
}


?>