<?php
/*-------------- check user role again----------*/
$user_role = $_SESSION['role_id'];
$ptn_id = $_SESSION['position_id'];

	$doc_code = mysql_real_escape_string(stripslashes($_GET['doc_code']));
	$etl_code = mysql_real_escape_string(stripslashes($_GET['etl_code']));
	$doc_title = mysql_real_escape_string(stripslashes($_GET['doc_title']));
	$doc_from = mysql_real_escape_string(stripslashes($_GET['doc_from']));
	$doctype_id = mysql_real_escape_string(stripslashes($_GET['doctype_id']));
	$doc_io_id = (int)mysql_real_escape_string(stripslashes($_GET['doc_io_id']));
	$startdate = mysql_real_escape_string(stripslashes($_GET['startdate']));
	$enddate = mysql_real_escape_string(stripslashes($_GET['enddate']));
	
	$orgunitid = $_SESSION['orgunitid_r'];
	

	//------------------------------------------------ Building search clause // 
	
$whereclause = "";

if($_SESSION['orgunitid_r'] != null || $_SESSION['sys_type'] !=null){
	if($_SESSION['sys_type'] != "counter" && $_SESSION['sys_type'] != "secretary" ) $whereclause .=" AND m.orgunitid='$orgunitid'   " ;
}
// check role the where clause 
if($user_role == 1 || $ptn_id == 1 || $ptn_id == 2 || $ptn_id == 3 || $ptn_id == 4){
	
}else{
	// $whereclause .= "AND dt.doctype_id IN(1,5) ";
}
//if ($tid != null) $whereclause .= "AND  t_id = $tid  ";
if ($doc_code != null) $whereclause .= "AND  d.doc_code LIKE '%$doc_code%'  ";
if ($etl_code != null) $whereclause .= "AND  d.etl_code LIKE '%$etl_code%'  ";
if ($doc_title != null) $whereclause .= "AND d.doc_title LIKE '%$doc_title%'  ";
if ($doc_from != null) $whereclause .= "AND d.doc_from LIKE '%$doc_from%'  ";
if ($doctype_id != null) $whereclause .= "AND d.doctype_id=$doctype_id  ";
if ($doc_io_id != null) $whereclause .= "AND d.doc_io_id=$doc_io_id  ";
if ($startdate != null && $enddate != null) $whereclause .= "AND (d.m_date BETWEEN '$startdate' AND '$enddate')  ";

//if ($whereclause != "") $whereclause = "WHERE  " . substr($whereclause, 0, strlen($whereclause)-5);

$params = "";

if ($doc_code != null) $params .= "doc_code=$doc_code&";
if ($etl_code != null) $params .= "etl_code=$etl_code&";
if ($doc_title != null) $params .= "doc_title=$doc_title&";
if ($doc_from != null) $params .= "doc_from=$doc_from&";
if ($doctype_id != null) $params .= "doctype_id=$doctype_id&";
if ($doc_io_id != null) $params .= "doc_io_id=$doc_io_id&";
if ($startdate != null) $params .= "startdate=$startdate&";
if ($enddate != null) $params .= "enddate=$enddate&";


// ------------------------------------- PAGE ------------------------------ //
$pagesize = 20;
$start =  (int)$_GET['start'];
$limitclause = "LIMIT $start, $pagesize";
// --------------------------------------------------------- Build Paging

$ecount = mysql_num_rows(mysql_query("SELECT d.doc_id FROM tbl_in_out_document d LEFT JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id) 
	LEFT JOIN tbl_doctype dt ON (dt.doctype_id = d.doctype_id) 
	LEFT JOIN tbl_doc_io_type io ON(io.doc_io_id = d.doc_io_id) WHERE d.doc_status = 1  $whereclause GROUP BY d.doc_id  ")); // AND d.doc_file !=''
$pagecount = ceil($ecount / $pagesize);
$pagenum = floor($start/$pagesize) +1;
$starttext = $start+1;
$endtext = min($start + $pagesize, $ecount);
$pagedescription = "ຫນ້າທີ $pagenum, ສະແດງລາຍການທີ $starttext ຫາ $endtext, ຈາກທັງຫມົດ $ecount ລາຍການ";


// ------------------------------ Create function -------------------------- //

function getDocin($whereclause,$limitclause){	 
	return mysql_query("SELECT d.*, m.m_id,m.receive_person,m.orgunitid,m.sysuserid,
 m.receive_status,m.type_doc_status,dt.*, io.* FROM tbl_in_out_document d LEFT JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id) 
		LEFT JOIN tbl_doctype dt ON (dt.doctype_id = d.doctype_id) LEFT JOIN tbl_doc_io_type io ON(io.doc_io_id = d.doc_io_id)  
		WHERE d.doc_status = 1 $whereclause GROUP BY d.doc_id ORDER BY d.m_date DESC, CAST(d.doc_code AS SIGNED) DESC $limitclause");
		// AND d.doc_file !=''
}
function orgunit($orgunitid, $doc_id){
	
	  $sql_org = "SELECT  m.orgunitid, org.orgunitname,org.orgunitnameeng, org.short_name, m.receive_status
			from tbl_manage_doc m INNER JOIN orgunit org ON(org.orgunitid = m.orgunitid)
	  WHERE m.doc_id = '$doc_id' AND org.orgunitid NOT IN(31,35) AND org.unittypeid<>5 GROUP BY m.orgunitid";
	return mysql_query($sql_org);
}
/*----------- select doctype by the position and role access--------------*/

function doctype(){
	global $user_role, $ptn_id;
	//if($user_role == 1){
		//return "SELECT * FROM tbl_doctype ORDER BY doctype_name ASC";
		return nr_query("SELECT * FROM tbl_doctype ORDER BY doctype_name ASC");
	//}else{
		//return "SELECT * FROM tbl_doctype WHERE doctype_id IN(1,5) ORDER BY doctype_name ASC";
		//return nr_query("SELECT * FROM tbl_doctype WHERE doctype_id IN(1,5) ORDER BY doctype_name ASC");
	//}
}
function doctype_in(){
	return nr_query("SELECT * FROM tbl_doc_io_type ORDER BY doc_io_id ASC");
}

	
		