<?php
    session_start
();
    
/**  Define Validate Access  */
    
define'_VALID_ACCESS');

    
/**  Check Session User Login  */
    
if (! session_is_registered("valid_user") &&session_is_registered("Priority") ) {
        echo 
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-874\" />";
        echo 
"<p style=padding-top:115px><p align=center><br /><font color=red><strong>กรุณาทำการ Login ก่อน</strong></font></p></p>";
        echo 
"<meta http-equiv=\"refresh\" content=\"1; URL=../login.php\" />";
        exit();
    } 
    else {
        
/**  Configuration  */
        
require_once( "../configuration.php" );
        require_once( 
$_Config_absolute_path "/includes/framework.php" );
        include(
"../include/Function.php");
    
        
/**  Create Database Object  */
        
$dbObj = new DBConn;
        
        
/**  Config Table for This Page  */
    
$myTable "faculty_tb";
    
$myTableFK "Faculty_code";
    
$myTable1 " accessories_tb";
    
    
$query " SELECT *  FROM $myTable   Where   FacultyStatus ='Y'   ";
    
$result $dbObj->execQuery($query);
    
$numrows $dbObj->_numrows;

        
/**  Paging */
        
$page $_GET['page'];
        if( 
$page == "" ) { $page 1; }
            
        
/**  จำนวนข้อมูล ต่อ 1 หน้า  */
        
$perpage $_REQUEST['perpage'];
        if( 
$perpage == "" ) { $perpage 20; }
        
        
/**  Searching */
        
$keyword $_REQUEST['keyword'];
        


        
    } 
# else
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<link href="../css/default.css" rel="stylesheet" type="text/css" />
<title><?=$_Config_sitename;?></title>
<script type="text/javascript" src="../js/utilities.js"></script>

</head>

<body topmargin="0" rightmargin="0" bottommargin="0" leftmargin="0">
<?php
    
include("../templates/incHeader.php");
?>
<table width="1003" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="203" align="left" valign="top" style="padding:10px 0px 0px 5px"><?php include("./_incMainMenu.php");?></td>
    <td width="800" height="440" align="center" valign="top" style="padding:10px 0px 5px 10px"><?php
        
/**  Config Paging  */
        
if( $perpage == "" ) { $limit $_Config_limit; }
        else { 
$limit $perpage; }
        
        
$scroll $_Config_scroll
        
        
        
/**  ตั้งค่า Default สำหรับการทำ Order By  */
        
if( $OrderBy == "" ) { $OrderBy $myTableFK; }
                        
        if( !isset(
$_GET['ASCDESC']) ) { $ASCDESC "ASC"; }
                        
        if( 
$_GET['ASCDESC'] == "DESC" ) { $ASCDESC "ASC"; }
        elseif( 
$_GET['ASCDESC'] == "ASC" ) { $ASCDESC "DESC"; } 
        
        
/**  Query เพื่อหา Numrows ก่อน  */
        
if( $keyword != "" ) { 
        
            
$query " SELECT *  FROM $myTable  Where  FacultyStatus ='Y'  ";
        } 
        else {
            
$query " SELECT *  FROM $myTable   Where  FacultyStatus ='Y'   ";
        }
        
        
$result $dbObj->execQuery($query);
        
$numrows $dbObj->_numrows;
        
        
/**  Paging  */
        
$display = ( !isset ($_GET['page']) ) ? $_GET['page'];
        
$start = ( ($display $limit) - $limit );
        
/**  Paging  */
        
        //###  Search
        
if( $keyword != "" ) { 
            if( isset(
$_GET['OrderBy']) ) {
            
                
$query " SELECT *  FROM $myTable  Where  FacultyStatus ='Y'   ";
            } 
            elseif( !isset(
$_GET['OrderBy']) ) {
                
                
$query " SELECT *  FROM $myTable  Where  FacultyStatus ='Y'   ";
            }
        } 
        else {
            if( isset(
$_GET['OrderBy']) ) {
                
$query " SELECT *  FROM $myTable  Where  FacultyStatus ='Y'  ";
            } 
            elseif( !isset(
$_GET['OrderBy']) ) {
                
$query " SELECT *  FROM $myTable  Where  FacultyStatus ='Y'    ";
            }
        }
        
        
$query .= " LIMIT $start, $limit ";
        
$result $dbObj->execQuery($query);

    
?>
      <fieldset>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="2"></td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <form id="myForm" name="myForm" method="post" action="<?=$PHP_SELF;?>?page=<?=$page;?>">
      <tr>
        <td width="70%" height="30" align="left" background="../images/background/bg-head-topic.gif" class="PADDING-LEFT-10"><strong><a href="index.php">หน้าหลัก</a> &raquo; <span class="NOTE">ข้อมูลครุภัณฑ์รายหน่วยงาน</a></span></strong></td>
        <td width="30%" align="right" class="PADDING-LEFT-10 PADDING-RIGHT-15">&nbsp;</td>
      </tr>
      <tr>
        <td height="385" colspan="2" align="center" valign="top">
          <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td height="10"></td>
            </tr>
            <tr>
              <td height="10"><span style="padding:5px 0px 5px 0px"><span class="PADDING-TOP-10"><img src="../images/icons/arrow-circle-225-left.png" width="16" height="16" border="0" align="absmiddle" /> <a href="javascript:;" onclick="window.history.back();"><strong>&lsaquo; ย้อนกลับ</strong></a></span>&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
            </tr>
          </table>
          <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td align="right" style="padding:5px 0px 5px 0px">จำนวนข้อมูลต่อหน้า
                  <select name="perpage" id="perpage" onchange="document.myForm.perpage.value=this.value; document.myForm.submit();" style="font-size:9pt; color:darkblue">
                    <option value="10" <?php if( $perpage=="10" ) echo 'selected'?>>10</option>
                    <option value="20" <?php if( $perpage=="20" ) echo 'selected'?>>20</option>
                    <option value="50" <?php if( $perpage=="50" ) echo 'selected'?>>50</option>                
                  </select>
             </td>
            </tr>
          </table>
          <?php if( $numrows ) { ?>
          <table width="97%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF" style="background-color:#eeeeee; border:0px solid gray">
            <tr bgcolor="#F8F8F8">
              <td colspan="2" rowspan="2" align="center" bordercolor="#999966" bgcolor="#F8F8F8" class="PADDING-LEFT-10">รหัสหน่วยงาน/หน่วยงาน</td>
              <td height="22" colspan="5" align="center" bordercolor="#999966">จำนวนครุภัณฑ์</td>
              </tr>
            <tr bgcolor="#F8F8F8">
              <td width="10%" height="22" align="center" bordercolor="#999966">
                ทั้งหมด</td>
              <td width="11%" bordercolor="#999966" align="center">
                หมดอายุ</td>
              <td width="10%" bordercolor="#999966" align="center">
ใช้งานอยู่</td>
              <td width="9%" bordercolor="#999966" align="center">บริจาค</td>
              <td width="10%" bordercolor="#999966" align="center">
จำหน่าย</td>
            </tr>
            <?php
                $no 
1+($display-1)*$limit;
                while( 
$rs $dbObj->fetchArray($result) ) { 
                    
                    
$bgColor = ( $bgColor == "#FFFFFF" ) ? "#F9FBFB" "#FFFFFF";
                    
                     
$Faculty_code$rs['Faculty_code'];
              
?>
            <tr bgcolor="<?=$bgColor;?>" onmouseover="this.style.backgroundColor='#F5F9F9'" onmouseout="this.style.backgroundColor=''">
              <td height="21" align="center" bgcolor="<?=$bgColor;?>" >
                <?=$rs["Faculty_code"]?>
            </td>
              <td align="left" bgcolor="<?=$bgColor;?>" class="PADDING-LEFT-5"><strong><?=$rs['Faculty_name'];?></strong></td>
              <td align="center" bgcolor="<?=$bgColor;?>" class="PADDING-RIGHT-30" style="padding-right:30px;">&nbsp;</td>
              <td align="right" bgcolor="<?=$bgColor;?>" class="PADDING-RIGHT-30" style="padding-right:30px;">&nbsp;</td>
              <td align="right" bgcolor="<?=$bgColor;?>" class="PADDING-RIGHT-30" style="padding-right:30px;">&nbsp;</td>
              <td align="right" bgcolor="<?=$bgColor;?>" class="PADDING-RIGHT-30" style="padding-right:30px;">&nbsp;</td>
              <td align="right" bgcolor="<?=$bgColor;?>" class="PADDING-RIGHT-30" style="padding-right:30px;">&nbsp;</td>
            <?php
               $query 
" SELECT *  FROM  accessories_tb at, accessories_type_tb att   WHERE   at.Acc_type_code = att.Acc_type_code   and  at.Acc_type_code  !='0'  and  at.Faculty_code ='$rs[Faculty_code]'   Group  By   at.Acc_type_code ";
               
$result5 $dbObj->execQuery($query);
             
$no 1+($display-1)*$limit;
            while( 
$rss $dbObj->fetchArray($result5) ) { 
                
              
$bgColor = ( $bgColor == "#FFFFFF" ) ? "#F9FBFB" "#FFFFFF";
                    
                 
$Acc_type_code$rss['Acc_type_code'];
              
?>
              </tr>
              <tr >
                <td width="3%" height="21" align="center" bgcolor="#FFFFFF" >&nbsp;</td>
                <td width="47%" align="left" bgcolor="#FFFFFF" class="PADDING-LEFT-5"><a href="printExcelAcc.php?Dura=Y&Faculty_code=<?=$rss["Faculty_code"]?>&Acc_type_code=<?=$rss['Acc_type_code'];?>"  title="Click to see Detail" class="d"><img src="../picture/linesub.gif" width="16" height="16" border="0" align="absmiddle" /><?php 
                $sql 
" SELECT *  FROM  accessories_type_tb    Where    Acc_type_code='$rss[Acc_type_code]' ";
                
$result6 $dbObj->execQuery($sql);
                
$rs6 $dbObj->fetchArray($result6);
                echo 
$rs6['Acc_type'] ; 
                
                
?>
                  </a></td>
                <td align="center" bgcolor="#FFFFFF" class="PADDING-RIGHT-5">
                  <?php
               
              $query1 
" SELECT *  FROM  accessories_tb at, accessories_type_tb att   WHERE  at.Dura='Y'  and   at.Acc_type_code = att.Acc_type_code   and  at.Acc_type_code  !='0'  and  at.Acc_type_code='$rss[Acc_type_code]'  and  at.Faculty_code ='$rss[Faculty_code]'  ";
                    
$result1 $dbObj->execQuery($query1);
                    
$countnum $dbObj->_numrows;
                
$dbObj->freeresult($result1);
                        echo 
number_format$countnum );
                     
?>
                </td>
                <td align="center" bgcolor="#FFFFFF" class="PADDING-RIGHT-15"><?php                
                  $now 
date('Y-m-d');
                  
$sday $now;
                
$yearthai explode"-"$sday );
                
$day intval$yearthai[2])-1;
                
$month intval$yearthai[1] );
                
$year1 intval$yearthai[0] )+543;
                
$sday1 =  $year1."-".$month."-".$day;
                
                
$query1 " SELECT *  FROM  accessories_tb at, accessories_type_tb att   WHERE   at.Dura='Y'  and  at.Acc_type_code = att.Acc_type_code   and  at.Acc_type_code  !='0'  and  at.Acc_type_code='$rss[Acc_type_code]'  and  at.Faculty_code ='$rss[Faculty_code]'  AND at.Date_finish BETWEEN at.Date_finish AND '$sday1' ";
                    
$result1 $dbObj->execQuery($query1);
                    
$countnum $dbObj->_numrows;
                
$dbObj->freeresult($result1);
                        echo 
number_format$countnum );
                     
?></td>
                <td align="center" bgcolor="#FFFFFF" class="PADDING-RIGHT-15">
                  <?php
               
              $query1 
" SELECT *  FROM  accessories_tb at, accessories_type_tb att   WHERE  at.Dura='Y'  and  at.Status='Y'  and   at.Acc_type_code = att.Acc_type_code   and  at.Acc_type_code  !='0'  and  at.Acc_type_code='$rss[Acc_type_code]'  and  at.Faculty_code ='$rss[Faculty_code]'  ";
                    
$result1 $dbObj->execQuery($query1);
                    
$countnum $dbObj->_numrows;
                
$dbObj->freeresult($result1);
                        echo 
number_format$countnum );
                     
?>
               </td>
                <td align="center" bgcolor="#FFFFFF" class="PADDING-RIGHT-15"><?php
               
              $query1 
" SELECT *  FROM  accessories_tb at, accessories_type_tb att   WHERE  at.Dura='Y'  and  at.Fund='บริจาค'  and   at.Acc_type_code = att.Acc_type_code   and  at.Acc_type_code  !='0'  and  at.Acc_type_code='$rss[Acc_type_code]'  and  at.Faculty_code ='$rss[Faculty_code]'  ";
                    
$result1 $dbObj->execQuery($query1);
                    
$countnum $dbObj->_numrows;
                
$dbObj->freeresult($result1);
                        echo 
number_format$countnum );
                     
?></td>
                <td align="center" bgcolor="#FFFFFF" class="PADDING-RIGHT-15"><?php
               
              $query1 
" SELECT *  FROM  accessories_tb at, accessories_type_tb att   WHERE  at.Dura='Y'  and  at.Fund='จำหน่าย'  and   at.Acc_type_code = att.Acc_type_code   and  at.Acc_type_code  !='0'  and  at.Acc_type_code='$rss[Acc_type_code]'  and  at.Faculty_code ='$rss[Faculty_code]'  ";
                    
$result1 $dbObj->execQuery($query1);
                    
$countnum $dbObj->_numrows;
                
$dbObj->freeresult($result1);
                        echo 
number_format$countnum );
                     
?></td>
                <?php
                    $no
++;
                } 
# while
                 
?>
                <?php
                    $no
++;
                } 
# while
              
?>
              </tr>
            </table>
          <?php #if ( $numrows > 0 ) ?>
          
<table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td>
          <?php if( $numrows ) { ?>
          <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td height="10" colspan="2"></td>
            </tr>
            <tr>
                <td width="80%" align="left"><span style="font-size:12px">Page : 
                    <?php
                        
/**  ตรวจสอบการ Order By ของ Query */
                        
if( $ASCDESC=="ASC") { $ASCDESC "DESC"; }  else { $ASCDESC="ASC"; } 
                        
                        
$paging ceil($numrows/$limit);
                                
                        
//=== Display the navigation
                        
if ($display 1) {    
                            
$previous $display 1;                    
                    
?><a href="<?=$PHP_SELF;?>?page=1&amp;perpage=<?=$perpage;?>&amp;OrderBy=<?=$OrderBy;?>&amp;ASCDESC=<?=$ASCDESC;?>&amp;keyword=<?=$keyword;?>"><font color="#FE830B">|&laquo; หน้าแรก</font></a>&nbsp; <a href="<?=$PHP_SELF;?>?page=<?=$previous;?>&amp;perpage=<?=$perpage;?>&amp;OrderBy=<?=$OrderBy;?>&amp;ASCDESC=<?=$ASCDESC;?>&amp;keyword=<?=$keyword;?>"><font color="#FE830B">&laquo; ก่อนหน้า</font></a>
                    <?php
                        
// end..if
                        
if($numrows != $limit) {                 
                            if(
$paging $scroll) { 
                                
$first $_GET['page']; 
                                
$last = ($scroll 1) + $_GET['page']; 
                            } else {                         
                                
$first 1;                        
                                
$last $paging;                        
                            } 
                            
                            if(
$last $paging ) {                            
                                
$first $paging - ($scroll 1);                            
                                
$last $paging;                            
                            }
                                            
                            for(
$i=$first$i<=$last$i++) {                        
                                if(
$display == $i) {        
                                    echo 
"<font color=red size=2> [<strong>$i</strong>] </font>";
                                } 
                                else {
                                    
//echo " <a href=\"$PHP_SELF?page=$i&user_id=$user_id&user_login=$user_login\">$i</a> ";
                                    
printf(" <a href=\"%s?page=%s&perpage=%s&OrderBy=%s&ASCDESC=%s&keyword=%s\">%s</a> "$_SERVER['PHP_SELF'], $i$perpage$OrderBy$ASCDESC$keyword$i);
                                }        
                            } 
// end..for
                        
// end..if
                            
                        
if($display $paging) {
                            
$next $display 1;                        
                    
?>&nbsp;<a href="<?=$PHP_SELF;?>?page=<?=$next;?>&amp;perpage=<?=$perpage;?>&amp;OrderBy=<?=$OrderBy;?>&amp;ASCDESC=<?=$ASCDESC;?>&amp;keyword=<?=$keyword;?>"><font color="#FE830B">ถัดไป &raquo;</font></a> &nbsp;<a href="<?=$PHP_SELF;?>?page=<?=$paging;?>&amp;perpage=<?=$perpage;?>&amp;OrderBy=<?=$OrderBy;?>&amp;ASCDESC=<?=$ASCDESC;?>&amp;keyword=<?=$keyword;?>"><font color="#FE830B">หน้าสุดท้าย &raquo;|</font></a><?php
                    
// end..if
                
?></span></td>
                <td width="20%" align="right">Total: <strong><?=$numrows;?></strong> <?php if( $numrows==1) echo 'Record'; else echo 'Records'?></td>
            </tr>
            </table>
          <?php #if ?></td>
        </tr>
      </table>
     <br /></td></tr>
     </form>
     </table>
    </fieldset></td>
  </tr>
</table>
<?php include("../templates/incFooter.php"); ?>
</body>
</html>
<?php
    
/**  Close the Database  */
    
$dbObj->disconn();
    
    
/**  Unset Class  */
    
unset($dbObj);
?>