<?php
/*-------------- check user role again----------*/
$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 = 20;
$start =  (int)$_GET['start'];
$limitclause = "LIMIT $start, $pagesize";
// --------------------------------------------------------- Build Paging

$ecount = mysql_num_rows(mysql_query("SELECT m.m_id, m.doc_id,org.orgunitid, m.receive_person, org.orgunitname, org.orgunitnameeng, 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 
		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' AND m.receive_person IS NULL $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, 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' AND m.receive_person IS NULL $whereclause GROUP BY m.orgunitid,m.doc_id  ORDER BY m.receive_status ASC, 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'];
}
/*------------------------------------------- function send mail to each department od ICT , added by kongxiong -----------------------*/
/*============== function send mail after confirm // on localhost ========================*/
function send_mail($doc_id){
		
	$mail=new PHPMailer();

	$mail->IsSMTP();
	$mail->IsHTML(true);

	$mail->SMTPAuth = true;
	$mail->SMTPSecure = "ssl";
	$mail->Host = "smtp.gmail.com";
	$mail->Port = 465;

	$mail->Username = "ict.followupwork@gmail.com";
	$mail->Password = "cunning@494";
	$mail->From = "ETL document in report";
	$mail->FromName = "ETL document in report";
	$text= $part1." ETL document in report";
	$mail->Subject = $text;
	// if(mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $doc_id AND position_id IS NOT NULL")))
	$sql = "SELECT select u.sysuserid,u.first_name,u.email_address,u.orgunitid,u.orgunitid_r, u.orgunitname,u.parentunit, u.unittypeid,
			d.m_id, d.m_date as date_send, d.doc_id, d.receive_person, d.type_doc_status, dd.*
			from v_user u INNER JOIN tbl_manage_doc d ON(d.orgunitid=u.orgunitid_r)
			INNER JOIN tbl_in_out_document dd ON(dd.doc_id=d.doc_id)			
			where d.doc_id ='$doc_id' AND d.receive_status=1 AND d.type_doc_status='doc_in' GROUP BY u.sysuserid";
	$sql_doc = mysql_query($sql); 
	$rs_doc = mysql_fetch_array($sql_doc);		
	if($rs_doc['doc_status']==1){
		$mail->Body = JB_utf8_to_html("ເຂົ້າຮັບເອົາເອກະສານ: ")." <a href='http://192.168.20.48/doc/document/show_filein.php?filein=".$rs_doc['doc_file']."&type=".$rs_doc['doctype_id']."'>".JB_utf8_to_html($rs_doc['doc_title'])."</a><br>
			".JB_utf8_to_html("ເລກທີເອກະສານເຂົ້າ: ").JB_utf8_to_html($rs_doc['doc_code']).", ".$rs_doc['doc_date']."<br />
			".JB_utf8_to_html("ວັນທີເຂົ້າ: ").$rs_doc['m_date']."<br />
			".JB_utf8_to_html("ຄໍາເຫັນ: ").JB_utf8_to_html($rs_doc['doc_comment'])."<br />";
	}else{
		$mail->Body = JB_utf8_to_html("ເຂົ້າຮັບເອົາເອກະສານ: ")." <a href='http://192.168.20.48/doc/document/show_fileout.php?fileout=".$rs_doc['doc_file']."&type=".$rs_doc['doctype_id']."'>".JB_utf8_to_html($rs_doc['doc_title'])."</a><br>
			".JB_utf8_to_html("ເລກທີເອກະສານເຂົ້າ: ").JB_utf8_to_html($rs_doc['doc_code']).", ".$rs_doc['doc_date']."<br />
			".JB_utf8_to_html("ວັນທີເຂົ້າ: ").$rs_doc['m_date']."<br />
			".JB_utf8_to_html("ຄໍາເຫັນ: ").JB_utf8_to_html($rs_doc['doc_comment'])."<br />";
	}
	//======== declare array variable to get 
	$sql_doc_2 = mysql_query($sql); 
	while ($rso = mysql_fetch_array($sql_doc_2)) {
		// get employee info 
		$emp_name = $rso['name'];
		//$email_address = $rso['email_address'];
		$email_address = "phetmany.mhing@gmail.com";
		$split = stripos($email_address, ",");
		$split2 = stripos($email_address, ";");
		if(($split != false) && $split2 != false){
			continue; // --use continue to next loop and don't send the current address, it is more then two email address or incorrect
		}
		$email = array();
		if(($split == true) && ($split2 == false)){
			$email = explode(',', $email_address);
		}elseif(($split == false) && ($split2 == true)){
			$email = explode(';', $email_address);
		}else{
			$email[] = $email_address;
		} 
		// send mail here
		for($s = 0; $s<count($email); $s++){
			$mail->AddAddress($email[$s],"ETL Document");
			//$mail->AddAddress("kongxiong.12@gmail.com","ICT web admin"); 
		}
		
	}
	if(!$mail->Send()) {
		echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	 //$msg =  "<SCRIPT language='JavaScript'>alert('Mailer has been sent to $num people ..!')</SCRIPT>";			
	}
}
/*============== function send mail after send from counter // on server side ========================*/
function send_mail_server($doc_id){
	
	$sql = "SELECT select u.sysuserid,u.first_name,u.email_address,u.orgunitid,u.orgunitid_r, u.orgunitname,u.parentunit, u.unittypeid,
			d.m_id, d.m_date as date_send, d.doc_id, d.receive_person, d.type_doc_status, dd.*
			from v_user u INNER JOIN tbl_manage_doc d ON(d.orgunitid=u.orgunitid_r)
			INNER JOIN tbl_in_out_document dd ON(dd.doc_id=d.doc_id)			
			where d.doc_id ='$doc_id' AND d.receive_status=1 AND d.type_doc_status='doc_in' GROUP BY u.sysuserid";
	$sql_doc = mysql_query($sql); 
	$rs_doc = mysql_fetch_array($sql_doc);	
	if($rs_doc['doc_status']==1){
	$body = JB_utf8_to_html("ເຂົ້າຮັບເອົາເອກະສານ: ")." <a href='http://ictdemo1.etllao.com/etl_doc/document/show_filein.php?filein=".$rs_doc['doc_file']."&type=".$rs_doc['doctype_id']."'>".JB_utf8_to_html($rs_doc['doc_title'])."</a><br>
		".JB_utf8_to_html("ເລກທີເອກະສານ: ").JB_utf8_to_html($rs_doc['doc_code']).", ".$rs_doc['doc_date']."<br />
		".JB_utf8_to_html("ວັນທີເຂົ້າ: ").$rs_doc['m_date']."<br />
		".JB_utf8_to_html("ຄໍາເຫັນ: ").JB_utf8_to_html($rs_doc['doc_comment'])."<br />";
	}else{
		$body = JB_utf8_to_html("ເຂົ້າຮັບເອົາເອກະສານ: ")." <a href='http://ictdemo1.etllao.com/etl_doc/document/show_fileout.php?fileout=".$rs_doc['doc_file']."&type=".$rs_doc['doctype_id']."'>".JB_utf8_to_html($rs_doc['doc_title'])."</a><br>
		".JB_utf8_to_html("ເລກທີເອກະສານ: ").JB_utf8_to_html($rs_doc['doc_code']).", ".$rs_doc['doc_date']."<br />
		".JB_utf8_to_html("ວັນທີເຂົ້າ: ").$rs_doc['m_date']."<br />
		".JB_utf8_to_html("ຄໍາເຫັນ: ").JB_utf8_to_html($rs_doc['doc_comment'])."<br />";
	}
		
	//======== declare array variable to get 
	$sql_doc_2 = mysql_query($sql); 
	while ($rso = mysql_fetch_array($sql_doc_2)) {
		// get employee info 
		$emp_name = $rso['name'];
		$email_address_get = "";
		//$email_address = $rso['email_address'];
		$email_address = "kongxiong.12@gmail.com, xxiong42@yahoo.com, kongxiong143@yahoo.com, kongx2010@hotmail.co.uk, konginter.13@gmail.com";
		// get mail as string 
		$email_address_get = str_replace(";",",",mysql_real_escape_string(stripslashes($email_address))); // 
		// get id to guest
		if($email_address_get != "") $email_address  = $email_address.", ".$email_address_get;
	}
	// pre to send mail 
	$strTo = ltrim($email_address,","); 
	$strSubject = "ETL_document_in_report";
	$strHeader = 'From: ict.followupwork@gmail.com' . "\r\n";
	$strHeader.= "MIME-version: 1.0\n";
	$strHeader.= "Content-type: text/html; charset= utf-8\n";
	$strMessage = $body;
	$flgSend = @mail($strTo,$strSubject,$strMessage,$strHeader); // @ = No Show Error //
		
	if($flgSend){
		//echo "Email Sent.<br />".$strTo."<br />";
	}else{
		echo "not send or err";
		exit();
	}
}
