<?php
// HTTP/1.0
if($_REQUEST['ajax']) {
    switch($_REQUEST['code']) {
        case "transno":

            if(RetRstField("SELECT fnid FROM tluser WHERE ftusername='{$_REQUEST['fttrans_no']}'")) {
                ?>
                alert('User Name sudah dipakai... ');
                <?php
            }
            else {
                ?>
                submitbutton('Simpan','','','','');
                <?php
            }
            break;
        /*     
        case "security":
            echo GetGridUser('security');
            break;
        case "setting":
            echo GetGridUser('setting');
            break;
        case "saldoawal":
            echo GetGridUser('saldoawal');
            break;
        case "pembelian":
            echo GetGridUser('pembelian');
            break;
        case "Inventory_Bahan_Baku":
            echo GetGridUser('Inventory_Bahan_Baku');
            break;
        case "inventory_setengah_jadi":
            echo GetGridUser('inventory_setengah_jadi');
            break;
        case "inventory_sparepart":
            echo GetGridUser('inventory_sparepart');
            break;
        case "inventory":
            echo GetGridUser('inventory');
            break;
        case "penjualan":
            echo GetGridUser('penjualan');
            break;
        case "keuangan":
            echo GetGridUser('keuangan');
            break;
        case "akunting":
            echo GetGridUser('akunting');
            break;
        case "preproduksi":
            echo GetGridUser('preproduksi');
            break;
        case "produksi":
            echo GetGridUser('produksi');
            break;
        case "ppic":
            echo GetGridUser('ppic');
            break;
        case "tracking":
            echo GetGridUser('tracking');
            break; */
        case "department":
            /*if($_REQUEST['ftdepartment']=='admin'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='ADM'";
                $rs=mysql_query($ssql);
            }else if($_REQUEST['ftdepartment']=='cs'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='CS'";
                $rs=mysql_query($ssql);
            }else if($_REQUEST['ftdepartment']=='hadmin'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='HADM'";
                $rs=mysql_query($ssql);
            }else if($_REQUEST['ftdepartment']=='mgr'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='MGR'";
                $rs=mysql_query($ssql);
            }else if($_REQUEST['ftdepartment']=='sp'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='SP'";
                $rs=mysql_query($ssql);
            }else if($_REQUEST['ftdepartment']=='spv'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='SPV'";
                $rs=mysql_query($ssql);
            }else if($_REQUEST['ftdepartment']=='staff'){
                $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='STAF'";
                $rs=mysql_query($ssql);
            }*/
            $ssql="SELECT ftdepartment_code, ftdepartment_name FROM tldepartment WHERE ftdepartment_type='{$_REQUEST['ftdepartment']}'";
            $rs=mysql_query($ssql);

            ?>
            var ObjSelect=document.getElementById('<?php echo $_REQUEST['ObjLokasi']; ?>');
            var len = ObjSelect.length;
            for (var idx=0; idx < len; idx++)
            {
                    if (ObjSelect.options[ObjSelect.length-1].value!='') {
                          ObjSelect.remove(ObjSelect.length-1);
                    }
            }
            <?php

            $i=0;
            while($r=mysql_fetch_assoc($rs)) {
                $i++;
                echo "  try {
                            ObjSelect.add(new Option('{$r['ftdepartment_code']}--{$r['ftdepartment_name']}','{$r['ftdepartment_code']}'),null,null);
                        }
                        catch(ex) {
                            ObjSelect.add(new Option('{$r['ftdepartment_code']}--{$r['ftdepartment_name']}','{$r['ftdepartment_code']}'), $i, $i);
                        } ";
            }
            break;

        default: 
            echo GetGridUser("{$_REQUEST['code']}");
            break;
    }
    die("");
}

function GetGridUser($n) {
    $x='';
    $ftusername='';

    $ssql="SELECT ftusername FROM tluser WHERE fnid='{$_REQUEST['id']}'";
    $rs=mysql_query($ssql) or die($ssql);
    $r=mysql_fetch_assoc($rs);
    $ftusername=$r['ftusername'];

    $ssql=" SELECT a.fx, A.ftsecurity_code as fnsecurity_access,IFNULL(B.ftusername,'{$ftusername}') AS ftusername,a.ftmodule_code, A.ftparent_code as ftsecurity_description,a.ftsecurity_name as ftsecurity_subdescription,
                CASE WHEN ((b.ftusername = '') OR (b.ftusername IS NULL)) THEN 0 ELSE IFNULL(b.fbaccess,0) END AS fnsecurity, a.ftmenu_code
            FROM (
                    SELECT 0 AS fx,ftsecurity_code,ftmodule_code,ftparent_code,ftsecurity_name,a.ftmenu_code, fnlevel FROM tlsecurity a INNER JOIN tlmenu b ON a.ftmenu_code = b.ftmenu_code 
                    WHERE ftmodule_code='{$n}' AND b.fnstatus = 1 
                    UNION ALL
                    select 1 as fx,concat('laporan_',ftreport_group,'_',ftreport_code) as ftsecurity_code,ftreport_group as ftmodule_code, concat('Laporan ',ftreport_group) as ftparent_code,ftreport_name,'' as ftmenu_code, '' as fnlevel
                    FROM tlreport WHERE ftreport_group='{$n}' and fnstatus = 1
            ) A
            LEFT JOIN tluser_access B ON A.ftsecurity_code=B.ftsecurity_code AND (B.ftusername ='{$ftusername}' OR B.ftusername IS NULL)
            WHERE ftmodule_code='{$n}' ";

    // echo ($ssql);
    $rs=mysql_query("select distinct ftsecurity_description, fx, ftmenu_code from ($ssql) m order by fx ASC, ftmenu_code asc, fnsecurity_access asc") or die($ssql) ;

    $x.= "<table class='ewTable' id='TransactionDetail{$n}' name='TransactionDetail{$n}' border='0' width='100%' cellspacing='0' celpadding='0'>
            <tr class='is_edit_header_row' width='100%'>
                <th valign=top width=30 align=center>No</th>
                <th valign=top align=center width='31%'>Keterangan</th>
                <th valign=top width=30 align=center>No</th>
                <th valign=top align=center width='31%'>Keterangan</th>
                <th valign=top width=30 align=center>No</th>
                <th valign=top align=center width='31%'>Keterangan</th>
            </tr>";

    $i=0;
    $j=0;
    $row1=1;

    while($r = mysql_fetch_assoc($rs))
    {
        if ($i%2) {
                $class1="ewTableRow";
                $class2="ISGridTDeven";
        } else {
                $class1="ISGridTRodd";
                $class2="ISGridTDodd";
        }
        $j++;

        if($row1==1) {
            $x.= "<tr class='$class1'>";
        }

        $x.="<td class='$class2' valign=top>{$j}</td>
             <td class='$class2' valign=top>
                <b>".ucwords(str_replace("_"," ",$r['ftsecurity_description'])) ."</b>";

        $rs1=mysql_query($ssql." and A.ftparent_code='".$r['ftsecurity_description']."' order by fx ASC, ftmenu_code asc, fnsecurity_access asc" ) or die("m");

        $i=0;

        while($r1 = mysql_fetch_assoc($rs1)) {
            if($i==0) {
                 $namex="cAll_{$r1['fnsecurity_access']}";
                 $checkx= " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type=checkbox id='{$namex}' name='{$namex}' value='0' onClick=\"CheckSecurityAll('{$namex}')\">ALL";
                 $x.="$checkx<table class='ewTable'  border='0'  cellspacing='0' celpadding='0'  width='100%'>";

            }

             $i++;
                if($r1['fnsecurity']==0){
                        $c="";
                }
                else {
                        $c=" checked ";
                }
            $x.="<tr  class='$class1'>";
            $x.="<td class='$class2'>";
            $x.="   <input type=hidden name=fnS[] value='{$r1['fnsecurity_access']}'>
                    <input type=checkbox id='{$namex}_{$i}' name='c{$r1['fnsecurity_access']}' value='1' $c ></td>
                 <td width='100%'>".ucwords(str_replace("_"," ",$r1['ftsecurity_subdescription'])) ." ";
            $x.="</td>";
            $x.="</tr>";

         }

         $x.="</table>";
         $x.="<input type=hidden name='{$namex}_count'  id='{$namex}_count' value={$i}>";
                     $x.="</td>";
                         if((mysql_num_rows($rs)==$j)||($row1==3)) {
                                if(mysql_num_rows($rs)==$j) {
                                        if($row1==1) {
                                                $x.="<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
                                        }
                                        elseif($row1==2) {
                                                $x.="<td>&nbsp;</td><td>&nbsp;</td>";
                                        }
                                }
                                $x.="</tr>
                                     <tr><td colspan=6>&nbsp;</td></tr>";
                         }
                         if($row1==3) $row1=0;
                         $row1++;
             $i++;
    }
    $x.="</table>";

    return $x;

}
    
function GetUser() {

    $x.="<link type='text/css' href='jquery/themes/base/ui.all.css' rel='stylesheet' />
        <script type='text/javascript' src='jquery/jquery-1.3.2.js'></script>
        <script type='text/javascript' src='jquery/ui/ui.core.js'></script>
        <script type='text/javascript' src='jquery/ui/ui.tabs.js'></script>
        <link type='text/css' href='jquery/demos.css' rel='stylesheet' />
        <script type='text/javascript'>
        $(function() {
                $('#tabs').tabs();
        });
        </script> 

        <div class='demo'> 
            <div id='tabs'>
                    <ul>
                            <li><a href='#tabs-1'>System</a></li>
                            <li><a href='#tabs-2'>Setting</a></li>
                            <li><a href='#tabs-3'>Security</a></li>
                            <li><a href='#tabs-4'>Saldo Awal</a></li>
                            <li><a href='#tabs-5'>Pembelian</a></li>
                            <li><a href='#tabs-6'>Inventory</a></li>
                            <li><a href='#tabs-7'>Penjualan</a></li>
                            <li><a href='#tabs-8'>Keuangan</a></li>
                            <li><a href='#tabs-9'>Akunting</a></li>
                            <li><a href='#tabs-10'>Absensi</a></li>
                            <li><a href='#tabs-11'>Hrd</a></li>
                    </ul>


                    <div id='tabs-1'>
                    ".GetGridUser('system')."
                    </div>
                    <div id='tabs-2'>
                    ".GetGridUser('setting')."
                    </div>
                    <div id='tabs-3'>
                    ".GetGridUser('security')."
                    </div>
                    <div id='tabs-4'>
                    ".GetGridUser('saldo_awal')."
                    </div>
                    <div id='tabs-5'>
                    ".GetGridUser('pembelian')."
                    </div>
                    <div id='tabs-6'>
                     ".GetGridUser('inventory')."
                    </div>
                    <div id='tabs-7'>
                     ".GetGridUser('penjualan')."
                    </div>
                    <div id='tabs-8'>
                     ".GetGridUser('keuangan')."
                    </div>
                    <div id='tabs-9'>
                     ".GetGridUser('akunting')."
                    </div>
                    <div id='tabs-10'>
                     ".GetGridUser('absensi')."
                    </div>
                    <div id='tabs-11'>
                     ".GetGridUser('hrd')."
                    </div>

            </div>

            </div><!-- End demo -->

            <div class='demo-description'>



            </div><!-- End demo-description -->";
    return $x;

}

Getsecurity("user");
$objAxpDB->ViewName("Daftar User");
$objAxpDB->ViewSQL('SELECT fnid,ftusername,ftfirst_name,ftuser_group_code FROM tluser ORDER BY ftusername,ftfirst_name,ftuser_group_code');
$objAxpDB->ViewCaption('ID, User Name, Nama, Group User');

$objAxpDB->FormName("User");
$objAxpDB->FormSQL('SELECT ftusername,ftpassword,ftpassword as password_confirm,ftuser_group_code,ftfirst_name,
                    ftmiddle_name,ftlast_name,ftdepartment, fnstatus, ftsales_code, ftwarehouse_code 
                    FROM tluser');
$objAxpDB->FormCaption(' User Name,Password, Confirm Password , Group User, Nama Depan, Nama Tengah, Nama Belakang,  Departemen, Status, Kode Sales, Kode Gudang ');

$ajaxDepartment="OnChange=\"ajax_do('security.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=department&ftdepartment='+this.value+'&ObjLokasi=ftdepartment');\"";
$objAxpDB->FormScript(3,$ajaxDepartment);
$objAxpDB->Lookup("ftdepartment","SELECT ftdepartment_code, ftdepartment_name from tldepartment","ftdepartment_code","ftdepartment_name",-1,true);
$objAxpDB->Lookup('ftuser_group_code', 'SELECT ftuser_group_code, ftuser_group_name FROM tluser_group', 'ftuser_group_code', 'ftuser_group_name', -1,true);
$objAxpDB->Lookup('fnstatus', 'SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status where fntrans_group_code=9', 'fntrans_status_code', 'fttrans_status_name', 1,true);
$objAxpDB->Lookup('ftsales_code', 'SELECT ftsales_code, ftsales_name FROM tlsales where fnstatus=1', 'ftsales_code', 'ftsales_name', -1,true);
// $objAxpDB->Lookup('ftwarehouse_code', 'SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnstatus=1', 'ftwarehouse_code', 'ftwarehouse_name', -1,true);

$objAxpDB->FormPosition('TL, TL, TL, TR ,TL,TL,TL,TR,TR,TR,TR');
$password="document.getElementById('ftpassword').type='password';
          document.getElementById('password_confirm').type='password';";

$objAxpDB->FormJS("{$password}");

$objAxpDB->GridSQL(0, "SELECT fnid FROM tluser");
$m=GetUser();
#die($m);
$objAxpDB->GridReplace(0, $m);

$objAxpDB->SetStatus("id");
if ($objAxpDB->GetStatus("id") == "") {
        $objAxpDB->ButtonShow("baru");

}
elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->ButtonShow("reset, simpan, tutup");

}
elseif ($objAxpDB->GetStatus("id") != 0) {
        $objAxpDB->ButtonShow("update, hapus, tutup");
        $objAxpDB->FormLocked("1, 0, 0");
}
$objAxpDB->Process();
$objAxpDB->WriteHtmlTable();


?>
