<?php
/*-------------- check user role again----------*/
$cnt = 0;
$ptn_id = $_SESSION['position_id'];
$user_role = $_SESSION['role_id'];
	$orgunitid = mysql_real_escape_string(stripslashes($_GET['orgunitid']));
	//$status= mysql_real_escape_string(stripslashes($_GET['status']));
	$type_io= mysql_real_escape_string(stripslashes($_GET['type_io']));
	$doc_code = mysql_real_escape_string(stripslashes($_GET['doc_code']));
	$doc_title = mysql_real_escape_string(stripslashes($_GET['doc_title']));
	$doctype_id = mysql_real_escape_string(stripslashes($_GET['doctype_id']));
	$startdate = mysql_real_escape_string(stripslashes($_GET['startdate']));
	$enddate = mysql_real_escape_string(stripslashes($_GET['enddate']));
	
	
	//------------------------------------------------ Building search clause // 
	
$whereclause = "";
// check role the where clause 
if($user_role == 1 || $ptn_id == 1 || $ptn_id == 2 || $ptn_id == 3 || $ptn_id == 4 || $ptn_id == 9){
	
}else{
	//$whereclause .= "AND dt.doctype_id IN(1,5) ";
}
if ($orgunitid != null) $whereclause .= " AND org.orgunitid = $orgunitid ";
//if ($status != null) $whereclause .= " AND m.receive_status = $status ";
if ($type_io != null) $whereclause .= " AND d.doc_status = $type_io ";
if ($doc_code != null) $whereclause .= "AND d.doc_code LIKE '%$doc_code%' ";
if ($doc_title != null) $whereclause .= "AND d.doc_title LIKE '%$doc_title%' ";
if ($doctype_id != null) $whereclause .= "AND d.doctype_id=$doctype_id  ";
if ($startdate != null && $enddate != null) $whereclause .= "AND (d.doc_date BETWEEN '$startdate' AND '$enddate')  ";

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

$params = "";

if ($orgunitid != null) $params .= "orgunitid=$orgunitid&";
//if ($status != null) $params .= "status=$status&";
if ($type_io != null) $params .= "type_io=$type_io&";
if ($doc_code != null) $params .= "doc_code=$doc_code&";
if ($doc_title != null) $params .= "doc_title=$doc_title&";
if ($doctype_id != null) $params .= "doctype_id=$doctype_id&";
if ($startdate != null) $params .= "startdate=$startdate&";
if ($enddate != null) $params .= "enddate=$enddate&";


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

$ecount = mysql_num_rows(mysql_query("SELECT m.m_id
		FROM tbl_in_out_document d 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)
		LEFT JOIN tbl_manage_doc m ON (m.doc_id=d.doc_id)
		LEFT JOIN orgunit org  ON(org.orgunitid = m.orgunitid)
		LEFT JOIN sys_user u ON(u.orgunitid=org.orgunitid)
		
		WHERE d.m_step='secretary' $whereclause GROUP BY m.orgunitid,m.doc_id "));
$pagecount = ceil($ecount / $pagesize);
$pagenum = floor($start/$pagesize) +1;
$starttext = $start+1;
$endtext = min($start + $pagesize, $ecount);
$pagedescription = "ຫນ້າທີ $pagenum, ສະແດງລາຍການທີ $starttext ຫາ $endtext, ຈາກທັງຫມົດ $ecount ລາຍການ";

function docin($whereclause,$limitclause) {
	$sql = "SELECT m.m_id, m.doc_id,org.orgunitid, m.receive_person, org.orgunitname, org.orgunitnameeng, org.short_name, m.receive_status, m.m_date, d.doctype_id,
		 dt.doctype_name, d.doc_code, d.doc_date, d.doc_file,d.doc_comment, d.doc_title, d.refer_code, d.refer_date, d.refer_file, d.refer_type, d.remark, d.doc_status, d.m_step 
		FROM tbl_in_out_document d 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)
		LEFT JOIN tbl_manage_doc m ON (m.doc_id=d.doc_id)
		LEFT JOIN orgunit org  ON(org.orgunitid = m.orgunitid)
		LEFT JOIN sys_user u ON(u.orgunitid=org.orgunitid)
		
		WHERE d.m_step='secretary' $whereclause GROUP BY m.orgunitid,m.doc_id  ORDER BY d.doc_id DESC $limitclause";
	return mysql_query($sql);
}
/*----------- select doctype by the position and role access--------------*/

function doctype(){
	global $user_role, $ptn_id;
	//if($user_role == 1 || $ptn_id == 1 || $ptn_id == 2 || $ptn_id == 3 || $ptn_id == 4){
		//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 orgunit($orgunitid){
	$org = mysql_query("SELECT * FROM orgunit WHERE orgunitid=$orgunitid");
	$rs = mysql_fetch_array($org, MYSQL_ASSOC);
	return $rs['orgunitname'];
}
