<?php
// function function gmail send email address
require_once("class/class.phpmailer.php");
require_once("class/class.smtp.php");

$section_head= mysql_query("select org.*, epp.emp_id,epp.position_id, group_concat(emp.name separator ', ') as emp_name, 
	group_concat(emp.tel separator ', ') as tel 
	from orgunit org INNER JOIN emp_employment epp ON(epp.orgunitid=org.orgunitid)
	INNER JOIN emp_personal emp ON(emp.emp_id=epp.emp_id)
	WHERE org.parentunit = 1 AND org.isdeleted <> 1 AND org.inactive <> 1 AND position_id IS NOT NULL GROUP BY org.orgunitid");
	/*-------------------  select the Head, du of ICT Division ------------------------*/
$ict_head = mysql_query("Select ptn.position_id, ptn.position_name, emp.emp_id, emp.`name`, emp.email_address, emp.tel 
			FROM emp_position ptn INNER JOIN emp_employment epp ON (ptn.position_id = epp.position_id)
			INNER JOIN emp_personal emp ON (emp.emp_id=epp.emp_id)
		WHERE ptn.position_id IN (1,2) AND epp.workingstatus = 1 ORDER BY ptn.position_id ASC ");
		
$dept_branch = mysql_query("Select  org.orgunitid, org.orgunitname, org.orgunitnameeng, org.short_name, org.unittypeid, org.parentunit,org.inactive, org.isdeleted
			FROM orgunit org WHERE org.parentunit = 0 AND org.unittypeid<>5 AND org.isdeleted=0 AND org.inactive=0 ORDER BY orgunitid, parentunit ASC ");
	
$rs_type = mysql_query("SELECT * FROM tbl_doctype");

function doctype(){
	return mysql_query("SELECT * FROM tbl_doctype ORDER BY doctype_name ASC");
}
function doc_io_type(){
	return mysql_query("SELECT * FROM tbl_doc_io_type ORDER BY io_name ASC");
}
/*--------------------------------SELECT TO EDIT ------------------------------*/
function doc(){
	$doc_id = mysql_real_escape_string(stripslashes($_GET['doc_id']));
	
	$sql ="Select d.*,dt.doctype_name, dt_r.doctype_name as refer_docname, in_t.io_name, m.m_id, org.orgunitname, SUM(if(m.receive_status=2, 1, 0)) as sum_status
	From tbl_in_out_document d LEFT JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id)
	LEFT JOIN orgunit org ON (org.orgunitid = m.orgunitid)
	INNER JOIN tbl_doctype dt ON (dt.doctype_id=d.doctype_id)
	LEFT JOIN tbl_doctype dt_r ON(dt_r.doctype_id=d.refer_type)
	LEFT JOIN tbl_doc_io_type in_t ON (in_t.doc_io_id=d.doc_io_id)
	 WHERE d.doc_id='$doc_id' GROUP BY doc_id";
	return mysql_query($sql);
}
function list_org(){
	$doc_id = mysql_real_escape_string(stripslashes($_GET['doc_id']));
	/*
	$sql = "Select  m.m_id, m.orgunitid 
	From tbl_in_out_document d INNER JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id)
	LEFT JOIN emp_personal emp ON(emp.emp_id=m.receive_person)

	LEFT JOIN emp_employment epp ON (epp.emp_id = emp.emp_id)
	LEFT JOIN emp_position ptn ON (ptn.position_id = epp.position_id)
	LEFT JOIN orgunit org ON (org.orgunitid = epp.orgunitid) WHERE d.doc_id=$doc_id";
	*/
	$sql = "Select  m.m_id, m.orgunitid 
	From tbl_in_out_document d INNER JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id)
	LEFT JOIN orgunit org ON (org.orgunitid = m.orgunitid) WHERE d.doc_id='$doc_id'";
	return mysql_query($sql);
}
function list_position(){
	$doc_id = mysql_real_escape_string(stripslashes($_GET['doc_id']));
	return  mysql_query("Select  m.m_id, m.orgunitid, ptn.position_id, ptn.position_name 
		From tbl_in_out_document d INNER JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id)
		INNER JOIN emp_position ptn ON (ptn.position_id = m.position_id)
	 WHERE d.doc_id=$doc_id ");
}
/*------------------------------------------- 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 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 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['first_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: ETLdocument.report2014@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();
	}
}
