<?php
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                if(RetRstField("SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['fttrans_no']}'")) {
                    ?>
                    alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                    OnChangeDate_fdtrans_date();
                    <?php
                }
                else {
                    ?>
                    submitbutton('Simpan','','','','');
                    <?php
                }
                break;
            /* case "qty":
                $qty=$_REQUEST["qty"];
                if($qty > 0){
                  ?>
                    //document.getElementById("ftlocation_codeD0<?php echo $_REQUEST["index"];?>").disabled=false;
                    //document.getElementById("ftwarehouse_codeD0<?php echo $_REQUEST["index"];?>").disabled=false;
                  <?php
                }else{
                  ?>
                    //document.getElementById("ftwarehouse_codeD0<?php echo $_REQUEST["index"];?>").value='';
                    //document.getElementById("ftlocation_codeD0<?php echo $_REQUEST["index"];?>").value='';
                    //document.getElementById("ftlocation_codeD0<?php echo $_REQUEST["index"];?>").disabled=true;
                    //document.getElementById("ftwarehouse_codeD0<?php echo $_REQUEST["index"];?>").disabled=true;
                  <?php
                }
              break; */

            case "cattype":
                // clear text
                ?> 
                document.getElementById("ftstock_in_company").value='';
                document.getElementById('span_ftstock_in_company').innerHTML='';
                document.getElementById("ftdocument_no").value='';
                document.getElementById('span_ftdocument_no').innerHTML='';
                <?php
                
                // Tidak perlu input company name / kolom dikunci
                if(($_REQUEST["ftcategory_code"]==8) or ($_REQUEST["ftcategory_code"]==9) or ($_REQUEST["ftcategory_code"]==10) or ($_REQUEST["ftcategory_code"]==12)) {
                    ?> 
                    document.getElementById("ftstock_in_company").readOnly=true;
                    document.getElementById("ftstock_in_company").style='background-color: #E0E0E0';
                    <?php
                // Harus input company name / kolom dibuka
                } else { 
                    ?>     
                    document.getElementById("ftstock_in_company").readOnly=false;
                    document.getElementById("ftstock_in_company").style='background-color: #FFFFFF';
                    <?php
                }    
                
                // Harus input document no 
                if(($_REQUEST["ftcategory_code"]==2) or ($_REQUEST["ftcategory_code"]==3) or ($_REQUEST["ftcategory_code"]==6) or ($_REQUEST["ftcategory_code"]==7)  or ($_REQUEST["ftcategory_code"]==8) or ($_REQUEST["ftcategory_code"]==9) or ($_REQUEST["ftcategory_code"]==12)) {
                    ?>     
                    document.getElementById("ftdocument_no").readOnly=false;
                    document.getElementById("ftdocument_no").style='background-color: #FFFFFF';
                    <?php
                // Tidak perlu input document no / kolom dikunci
                } else { 
                    ?> 
                    document.getElementById("ftdocument_no").readOnly=true;
                    document.getElementById("ftdocument_no").style='background-color: #E0E0E0';
                    <?php
                }    
                
                echo "ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=grid&ftdocument_no='+ document.getElementById('ftdocument_no').value + '&ftcategory_code='+ document.getElementById('ftcategory_code').value);";

                break;  
            
            case "item":
                ?>
                document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value='';
                document.getElementById('ftlocation_codeD0<?php echo $_REQUEST['index']; ?>').value='';
                document.getElementById('fnqtyD0<?php echo $_REQUEST['index']; ?>').value='0';
                document.getElementById('fnqty_baseD0<?php echo $_REQUEST['index']; ?>').value='0';
                <?php

                die("");
                break;    
        
            case "price":
                $ssql="SELECT ftwarehouse_code FROM tllocation 
                        WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}' ";
                $ftwarehouse_code=RetRstField($ssql);

                $ftcategory_code = $_REQUEST['ftcategory_code'];

                // HPP dari fifo document no
                if (($ftcategory_code==2) OR ($ftcategory_code==3) OR ($ftcategory_code==7) OR ($ftcategory_code==9) OR ($ftcategory_code==12)) {
                    $fcPrice = GetFIFOPriceValue($_SESSION['strbranch'],$_REQUEST['ftdocument_no'],$_REQUEST['ftitem_code'],"");
                    
                } else {       
                    $fcPrice = GetLastPriceValue("{$_SESSION['strbranch']}","{$_REQUEST['ftitem_code']}");

                    if ($fcPrice == 0) {
                        $fcPrice = RetRstField("SELECT fcbuying_price FROM tlbuying_price WHERE ftcurrency_code = 'IDR' AND ftitem_code = '{$_REQUEST['ftitem_code']}' ");
                        if ($fcPrice == '') $fcPrice = 0;         
                    }
                }
                
                ?>
                document.getElementById('fcpriceD0<?  echo $_REQUEST['index']; ?>').value='<?php echo CCur($fcPrice); ?>';
                ElementCurrencyALL();   
                <?php    
                
                die("");
                break;  
        }
    }

    Getsecurity('barang_masuk');
    $objAxpDB->ViewName("Daftar Stock Masuk");
    // join customer (retur sample, retur pinjaman, tukar barang)
    $ssql=" SELECT A.fnid, A.fdtrans_date,  A.ftstock_in_no, c.fttrans_status_name as ftcategory_code, d.ftcustomer_name as ftstock_out_company, A.ftdocument_no, A.ftreff_no, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
                        FROM txstock_in_hdr A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        inner join tstransaction_status c on A.ftcategory_code=c.fntrans_status_code AND c.fntrans_group_code=3
                        LEFT JOIN tlcustomer d on A.ftstock_in_company=d.ftcustomer_code
                        WHERE (a.ftcategory_code IN (2,3,5)) and ftstock_type_code = 'inv'
                                AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')";

    // join supplier 
    $ssql.=" UNION ALL SELECT A.fnid, A.fdtrans_date,  A.ftstock_in_no, c.fttrans_status_name as ftcategory_code, d.ftsupplier_name as ftstock_out_company, A.ftdocument_no, A.ftreff_no, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
                        FROM txstock_in_hdr A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        inner join tstransaction_status c on A.ftcategory_code=c.fntrans_status_code AND c.fntrans_group_code=3
                        LEFT JOIN tlsupplier d on A.ftstock_in_company=d.ftsupplier_code
                        WHERE (a.ftcategory_code IN (1,6,7)) and ftstock_type_code = 'inv'
                                AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')";

    // lain2
    $ssql.=" UNION ALL SELECT A.fnid, A.fdtrans_date,  A.ftstock_in_no, c.fttrans_status_name as ftcategory_code, '' as ftstock_out_company, A.ftdocument_no, A.ftreff_no, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
                        FROM txstock_in_hdr A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        inner join tstransaction_status c on A.ftcategory_code=c.fntrans_status_code AND c.fntrans_group_code=3
                        LEFT JOIN tlsupplier d on A.ftstock_in_company=d.ftsupplier_code
                        WHERE (a.ftcategory_code IN (8,9,10)) and ftstock_type_code = 'inv'
                                AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')";

    $objAxpDB->ViewSQL("SELECT * FROM ({$ssql}) X ORDER BY  ftstock_in_no DESC, fdtrans_date DESC" );
    $objAxpDB->ViewCaption("ID, Tgl. Masuk ,No. Stock Masuk, Kategori, Pemberi, No. Dokumen, No. Referensi, Total Qty, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

    if(!$_REQUEST['id']) {
        $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=TN001&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_in_no&no_trans=ftstock_in_no&table_name=txstock_in_hdr&ftbranch_code={$_SESSION['strbranch']}');";
        $strAutoNumber0="function OnChangeDate_fdtrans_date() {
                                {$strAutoNumber1}
                         }

                        OnChangeDate_fdtrans_date();

                        function OnChangeKategory() {
                            var m=document.getElementById('ftcategory_code').value;
                            
                            if(m=='1' || m=='6' || m=='7') {
                                return \"SELECT ftsupplier_code AS ftcompany_code, ftsupplier_name AS ftcompany_name FROM tlsupplier \";
                            }
                            /* kembali sample, pinjaman & tukar barang */
                            else if(m=='2' || m=='3' || m=='5') {
                                return \"select * from (SELECT ftcustomer_code as ftcompany_code, ftcustomer_name as ftcompany_name FROM tlcustomer) m \";
                            }
                            /* keperluan internal */
                            else {
                                return \"select * from (SELECT \'\' AS ftcompany_code, \'\'  AS ftcompany_name) x where 1=2 \";
                            }
                        }
                        
                        function OnChangeCompany() {
                            var m=document.getElementById('ftcategory_code').value;
                            var ftstock_in_company=document.getElementById('ftstock_in_company').value;
                            
                            /* retur sample */
                            if(m=='2') {
                                return \"SELECT a.ftstock_out_no, a.fdtrans_date FROM txstock_out_hdr a WHERE a.fnstatus=1 AND ftcategory_code = 2 AND a.ftbranch_code='{$_SESSION['strbranch']}' \";
                            }
                            /* retur pinjaman */
                            else if(m=='3') {
                                return \"SELECT a.ftstock_out_no, a.fdtrans_date FROM txstock_out_hdr a WHERE a.fnstatus=1 AND ftcategory_code = 3 AND a.ftbranch_code='{$_SESSION['strbranch']}' \";
                            }
                            /* hasil outsource dan retur outsource */
                            else if(m=='6' || m=='7') {
                                return \"SELECT a.ftstock_out_no, a.fdtrans_date FROM txstock_out_hdr a WHERE a.fnstatus=1 AND ftcategory_code = 6 AND a.ftbranch_code='{$_SESSION['strbranch']}' \";
                            }
                            /* hasil produksi dan retur produksi */
                            else if(m=='8' || m=='9') {
                                return \"SELECT a.ftstock_out_no, a.fdtrans_date FROM txstock_out_hdr a WHERE a.fnstatus=1 AND ftcategory_code = 8 AND a.ftbranch_code='{$_SESSION['strbranch']}' \";
                            }
                            /* retur pemakaian */
                            else if(m=='12') {
                                return \"SELECT a.ftstock_out_no, a.fdtrans_date FROM txstock_out_hdr a WHERE a.fnstatus=1 AND ftcategory_code = 7 AND a.ftbranch_code='{$_SESSION['strbranch']}' \";
                            }
                            /* keperluan internal */
                            else {
                                return \"select * from (SELECT \'\' AS ftstock_out_no, \'\'  AS txstock_out_hdr) x where 1=2 \";
                            }
                       
                        }    
                        ";
                                
        $objAxpDB->FormJS($strAutoNumber0);
     }
    
    $objAxpDB->FormName("Stock Masuk");
    $objAxpDB->FormSQL("SELECT ftstock_in_no, ftcategory_code, ftstock_in_company, ftdocument_no, fdtrans_date, ftreff_no, ftchecked_user, ftnotes,  fntotal_qty, fnstatus, ftstock_type_code FROM txstock_in_hdr ");
    $objAxpDB->FormCaption("No. Stock Masuk, Kategori, Diterima Dari, No. Document, Tgl. Masuk, No. Referensi, Pengecek Barang, Catatan,  Total Qty, Status, Tipe Stock");
    $objAxpDB->FormPosition("TL, TL, TL, TL, TR, TR, HD, BL, BR, BR, TR");
        
    $ajaxType="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=cattype&ftcategory_code='+ document.getElementById('ftcategory_code').value);";
    // $ajaxClear="document.getElementById('ftstock_in_company').value='';document.getElementById('span_ftstock_in_company').innerHTML='';document.getElementById('hidden_ftstock_in_company').value='';document.getElementById('ftdocument_no').value='';document.getElementById('span_ftdocument_no').innerHTML='';";
    //$ajaxClear="document.getElementById('ftdocument_no').value='';document.getElementById('span_ftdocument_no').innerHTML='';";
    $ajaxClear2="document.getElementById('ftdocument_no').value='';document.getElementById('span_ftdocument_no').innerHTML='';document.getElementById('hidden_ftdocument_no').value='';";
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxClear2}{$ajaxType}\"");
    $objAxpDB->FormScript(2, " onChange=\"{$ajaxClear2}\"");
    $ajaxDocNo="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=grid&ftdocument_no='+ document.getElementById('ftdocument_no').value + '&ftcategory_code='+ document.getElementById('ftcategory_code').value);";
    $objAxpDB->FormScript(3, " onChange=\"{$ajaxDocNo}\"");
    
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code, fnqtypo, fnqtysisapo, fnqty, fnqty_base, fnqty_broken, ftuom_code, ffformula, fcprice FROM txstock_in_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty PO, Qty Sisa, Qty, Qty Base, Qty Rusak, Satuan, Formula, Harga HPP");
    $objAxpDB->GridHidden(0,'0,0,1,0,0,1,1,0,1,1,0,1,0,1');
    $objAxpDB->GridHidden(1,'1');
    $objAxpDB->GridSize(0,',,,,,5,5,5,,,10,,10,');
               
    //$objAxpDB->GridScript(0,1, "onChange=\"ajax_do('query2.php?code=uom&obj1=' + this.value + '&obj2=ftuom_codeD0_N_');\"");
    //$ajaxUom="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value + '&index=_N_');";
    $ajaxUom="ajax_do('query2.php?code=uom_list&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ObjLokasi=ftuom_codeD0_N_');";
    $ajaxItem="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value + '&index=_N_');";
    $objAxpDB->GridScript(0,1, "onChange=\"{$ajaxUom}{$ajaxItem};\"");

    $ajaxGudang="ajax_do('query2.php?code=location_list&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');";
    $objAxpDB->GridScript(0,3, "onChange=\"{$ajaxGudang};\"");
    
    $ajaxPrice="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=price&index=_N_&ftitem_code='+document.getElementById('ftitem_codeD0_N_').value+'&ftcategory_code='+document.getElementById('ftcategory_code').value+'&ftdocument_no='+document.getElementById('ftdocument_no').value);";
    $objAxpDB->GridScript(0,4, "onChange=\"{$ajaxPrice};\"");

    // $ajaxLock="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=qty&qty='+ this.value + '&index=_N_');";
    // $ajaxFormula="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
    $ajaxFormula="ajax_do('query2.php?code=formula&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
    $objAxpDB->GridScript(0,7,"onChange=\"{$ajaxLock}{$ajaxFormula}StockMasukRecalQty();\"");
    $objAxpDB->GridDelJS(0,"StockMasukRecalQty();");
    
    $ftcategory_code="";
    $ssql="SELECT ftcategory_code FROM txstock_in_hdr WHERE fnid='{$_REQUEST['id']}'";
    $rs=mysql_query($ssql);
    $r=mysql_fetch_assoc($rs);
    $ftcategory_code=$r['ftcategory_code'];

    if($_REQUEST['id']) {
        $sql_where="WHERE fnstatus=1 ";
        $sql_where2="AND ftstock_category_code='inv' ";
        $sql_where3="WHERE a.fnstatus=1 ";
    
    } else {
        $objAxpDB->Lookup( "ftdocument_no", "'+ OnChangeCompany() +'", "ftstock_out_no", "fdtrans_date", -1,false,"' AND ftstock_out_company=\''+ document.getElementById('ftstock_in_company').value + '\' ORDER BY ftstock_out_no '");
        $objAxpDB->Lookup("ftstock_in_company","'+ OnChangeKategory() +'", "ftcompany_code", "ftcompany_name", -1,false);
    
    }

    $objAxpDB->LookupMagicQuotes(TRUE);
    $objAxpDB->Lookup("ftcategory_code", "SELECT * FROM tstransaction_status WHERE fntrans_group_code = 3 AND fnactive_status=1 AND fntrans_status_code NOT IN (0,4,11) ORDER BY fntrans_status_code ", "fntrans_status_code", "fttrans_status_name", -1,true);
    $objAxpDB->Lookup("fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2", "fntrans_status_code", "fttrans_status_name", 1,true);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse {$sql_where} ", "ftwarehouse_code", "ftwarehouse_name",-1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation {$sql_where} ", "ftlocation_code", "ftlocation_name", -1,true);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(IFNULL(ftpart_no,'')) != '' THEN ' ,PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) as ftitem_name  FROM tlstock_hdr {$sql_where} {$sql_where2} ORDER BY ftitem_code,ftpart_no ", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftstock_type_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type WHERE ftstock_type_code='inv' ", "ftstock_type_code", "ftstock_type_name", 0,true);
    	    
    $objAxpDB->SetStatus("id, fnstatus,ftcategory_code");
    if ($objAxpDB->GetStatus("id") == "")
        //$objAxpDB->ButtonShow("baru,  cetak_daftar, export_excel , tutup");
        $objAxpDB->ButtonShow("baru");
    elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->FormLocked("1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1");
        
        $objAxpDB->GridLocked(0,'1,0,1,0,0,1,1,0,1,1,0,1,0,1');
        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
    
        $objAxpDB->ButtonShow("reset, simpan, tutup");
        
        $objAxpDB->GridSQL(1, "SELECT REPLACE(fbaccess,1,'aktif') AS fbaccess  FROM tluser_access_setting #");
        $objAxpDB->GridCaption(1, "Access");
        $objAxpDB->GridAutoAdd(1, FALSE);
	
    } elseif ($objAxpDB->GetStatus("fnstatus") == 0){
        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
        $objAxpDB->ButtonShow("baru, reset, final, update, tutup");
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1");
        if(($objAxpDB->GetStatus("ftcategory_code") == 6) or ($objAxpDB->GetStatus("ftcategory_code") == 7)){
          $objAxpDB->GridLocked(0,'1,1,0,0,0,0,1');
          $objAxpDB->GridAllowAddNew(0,false);
          $objAxpDB->GridAllowDelete(0,false);
        }else{
          $objAxpDB->GridLocked(0,'1,0,0,0,0,0,1');
          $objAxpDB->GridAllowAddNew(0,true);
          $objAxpDB->GridAllowDelete(0,true);
        }
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
        // revisi
        if(isset($_REQUEST['revisi'])) {
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1");
            $objAxpDB->GridLocked(0,'1,0,1,0,1,1,1,0,1,1,0,1,0,1');
        
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE); 
        
            $objAxpDB->Lookup("ftcategory_code", "SELECT * FROM tstransaction_status WHERE fntrans_group_code = 3 AND fnactive_status=1 AND fntrans_status_code IN (1, 2, 3, 6, 7, 8, 9, 10, 13)  ORDER BY fntrans_status_code ", "fntrans_status_code", "fttrans_status_name", -1,true);
    
            $objAxpDB->ButtonShow("simpan_revisi,tutup");
            
        } else {
        
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1,1,1,1,1');

            // barang masuk pembelian harus dibatalkan melalui penerimaan barang
            if (($objAxpDB->GetStatus("ftcategory_code") == 0) || ($objAxpDB->GetStatus("ftcategory_code") == 4) || ($objAxpDB->GetStatus("ftcategory_code") == 5) || ($objAxpDB->GetStatus("ftcategory_code") == 11)) {
                $objAxpDB->ButtonShow("baru, cetak, tutup");
            } else {
                $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
            }
            
        }
    // Status Approved tidak bisa batal karena sudah ada purchase invoice
    } elseif ($objAxpDB->GetStatus("fnstatus") == 2) {
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1,1,1,1,1');
        
        $objAxpDB->ButtonShow("baru, cetak, tutup");
        
    } elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 9)) {
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1,1,1,1,1');
        
        $objAxpDB->ButtonShow("tutup");
        
    }

    
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
           case "grid":
                ob_start();
                // retur sample
                if($_REQUEST["ftcategory_code"] == 2){
                    $ssql="SELECT a.fnindex, a.ftitem_code, d.fdexp_date, '' AS ftwarehouse_code, '' as ftlocation_code, a.fnqty AS fnqtypo, a.fnqty-SUM(IFNULL(d.fnqty_base,0)) AS fnqtysisapo,
                             CAST(0 AS DECIMAL(19,2)) AS fnqty, CAST(0 AS DECIMAL(19,2)) AS fnqty_base, CAST(0 AS DECIMAL(19,2)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,2)) AS fcprice
                             FROM txstock_out_det a INNER JOIN txstock_out_hdr b ON a.ftstock_out_no = b.ftstock_out_no
                             LEFT JOIN txstock_in_hdr c ON c.ftdocument_no = b.ftstock_out_no AND c.fnstatus <> 3 AND c.fnstatus <> 9 AND c.ftcategory_code = 2
                             LEFT JOIN txstock_in_det d ON c.ftstock_in_no = d.ftstock_in_no AND a.ftitem_code = d.ftitem_code
                             WHERE b.ftstock_out_no='".$_REQUEST['ftdocument_no']."' AND b.ftcategory_code = 2 AND b.fnstatus <> 3 AND b.fnstatus <> 9
                             GROUP BY a.fnindex, a.ftitem_code, a.fnqty ,a.ftuom_code, a.ffformula,b.ftdocument_no
                             HAVING fnqtysisapo <> 0  #";

                    $objAxpDB->GridLocked(0,"1,1,0,0,0,1,1,0,1,0,1,1,0,1");
                    $objAxpDB->GridHidden(0,'0,0,1,0,0,1,0,0,1,1,0,1,0,1');
                    $objAxpDB->GridAllowAddNew(0,false);
                    $objAxpDB->GridAllowDelete(0,true);
                    
                // retur pinjaman
                } elseif($_REQUEST["ftcategory_code"] == 3){
                    $ssql="SELECT a.fnindex, a.ftitem_code, d.fdexp_date, '' AS ftwarehouse_code, '' as ftlocation_code, a.fnqty AS fnqtypo, a.fnqty-SUM(IFNULL(d.fnqty_base,0)) AS fnqtysisapo,
                             CAST(0 AS DECIMAL(19,2)) AS fnqty, CAST(0 AS DECIMAL(19,2)) AS fnqty_base, CAST(0 AS DECIMAL(19,2)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,2)) AS fcprice
                             FROM txstock_out_det a INNER JOIN txstock_out_hdr b ON a.ftstock_out_no = b.ftstock_out_no
                             LEFT JOIN txstock_in_hdr c ON c.ftdocument_no = b.ftstock_out_no AND c.fnstatus <> 3 AND c.fnstatus <> 9 AND c.ftcategory_code = 3
                             LEFT JOIN txstock_in_det d ON c.ftstock_in_no = d.ftstock_in_no AND a.ftitem_code = d.ftitem_code
                             WHERE b.ftstock_out_no='".$_REQUEST['ftdocument_no']."' AND b.ftcategory_code = 3 AND b.fnstatus <> 3 AND b.fnstatus <> 9
                             GROUP BY a.fnindex, a.ftitem_code, a.fnqty ,a.ftuom_code, a.ffformula,b.ftdocument_no
                             HAVING fnqtysisapo <> 0  #";

                    $objAxpDB->GridLocked(0,"1,1,0,0,0,1,1,0,1,0,1,1,0,1");
                    $objAxpDB->GridHidden(0,'0,0,1,0,0,1,0,0,1,1,0,1,0,1');
                    $objAxpDB->GridAllowAddNew(0,false);
                    $objAxpDB->GridAllowDelete(0,true);
                    
                // retur outsource    
                } elseif($_REQUEST["ftcategory_code"] == 7){
                    $ssql="SELECT a.fnindex, a.ftitem_code, d.fdexp_date, '' AS ftwarehouse_code, '' as ftlocation_code, a.fnqty AS fnqtypo, a.fnqty-SUM(IFNULL(d.fnqty_base,0)) AS fnqtysisapo,
                            CAST(0 AS DECIMAL(19,2)) AS fnqty, CAST(0 AS DECIMAL(19,2)) AS fnqty_base, CAST(0 AS DECIMAL(19,2)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,2)) AS fcprice
                            FROM txstock_out_det a INNER JOIN txstock_out_hdr b ON a.ftstock_out_no = b.ftstock_out_no
                            LEFT JOIN txstock_in_hdr c ON c.ftdocument_no = b.ftstock_out_no AND c.fnstatus <> 3 AND c.fnstatus <> 9 AND c.ftcategory_code = 6
                            LEFT JOIN txstock_in_det d ON c.ftstock_in_no = d.ftstock_in_no AND a.ftitem_code = d.ftitem_code
                            WHERE b.ftstock_out_no='".$_REQUEST['ftdocument_no']."' AND b.ftcategory_code = 6 AND b.fnstatus <> 3 AND b.fnstatus <> 9
                            GROUP BY a.fnindex, a.ftitem_code, a.fnqty ,a.ftuom_code, a.ffformula,b.ftdocument_no
                            HAVING fnqtysisapo <> 0  #";

                    $objAxpDB->GridLocked(0,"1,1,0,0,0,1,1,0,1,0,1,1,0,1");
                    $objAxpDB->GridHidden(0,'0,0,1,0,0,1,0,0,1,1,0,1,0,1');
                    $objAxpDB->GridAllowAddNew(0,false);
                    $objAxpDB->GridAllowDelete(0,true);
                    
                // retur produksi    
                } elseif($_REQUEST["ftcategory_code"] == 9){
                    $ssql="SELECT a.fnindex, a.ftitem_code, d.fdexp_date, '' AS ftwarehouse_code, '' as ftlocation_code, a.fnqty AS fnqtypo, a.fnqty-SUM(IFNULL(d.fnqty_base,0)) AS fnqtysisapo,
                            CAST(0 AS DECIMAL(19,2)) AS fnqty, CAST(0 AS DECIMAL(19,2)) AS fnqty_base, CAST(0 AS DECIMAL(19,2)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,2)) AS fcprice
                            FROM txstock_out_det a INNER JOIN txstock_out_hdr b ON a.ftstock_out_no = b.ftstock_out_no
                            LEFT JOIN txstock_in_hdr c ON c.ftdocument_no = b.ftstock_out_no AND c.fnstatus <> 3 AND c.fnstatus <> 9 AND c.ftcategory_code = 9
                            LEFT JOIN txstock_in_det d ON c.ftstock_in_no = d.ftstock_in_no AND a.ftitem_code = d.ftitem_code
                            WHERE b.ftstock_out_no='".$_REQUEST['ftdocument_no']."' AND b.ftcategory_code = 8 AND b.fnstatus <> 3 AND b.fnstatus <> 9
                            GROUP BY a.fnindex, a.ftitem_code, a.fnqty ,a.ftuom_code, a.ffformula,b.ftdocument_no
                            HAVING fnqtysisapo <> 0  #";

                    $objAxpDB->GridLocked(0,"1,1,0,0,0,1,1,0,1,0,1,1,0,1");
                    $objAxpDB->GridHidden(0,'0,0,1,0,0,1,0,0,1,1,0,1,0,1');
                    $objAxpDB->GridAllowAddNew(0,false);
                    $objAxpDB->GridAllowDelete(0,true);
                    
                // retur pemakaian
                }elseif($_REQUEST["ftcategory_code"] == 12){
                   $ssql="SELECT a.fnindex, a.ftitem_code, d.fdexp_date, '' AS ftwarehouse_code, '' as ftlocation_code, a.fnqty AS fnqtypo, a.fnqty-SUM(IFNULL(d.fnqty_base,0)) AS fnqtysisapo,
                            CAST(0 AS DECIMAL(19,2)) AS fnqty, CAST(0 AS DECIMAL(19,2)) AS fnqty_base, CAST(0 AS DECIMAL(19,2)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,2)) AS fcprice
                            FROM txstock_out_det a INNER JOIN txstock_out_hdr b ON a.ftstock_out_no = b.ftstock_out_no
                            LEFT JOIN txstock_in_hdr c ON c.ftdocument_no = b.ftstock_out_no AND c.fnstatus <> 3 AND c.fnstatus <> 9 AND c.ftcategory_code = 12
                            LEFT JOIN txstock_in_det d ON c.ftstock_in_no = d.ftstock_in_no AND a.ftitem_code = d.ftitem_code
                            WHERE b.ftstock_out_no='".$_REQUEST['ftdocument_no']."' AND b.ftcategory_code = 7 AND b.fnstatus <> 3 AND b.fnstatus <> 9
                            GROUP BY a.fnindex, a.ftitem_code, a.fnqty ,a.ftuom_code, a.ffformula,b.ftdocument_no
                            HAVING fnqtysisapo <> 0  #";

                    $objAxpDB->GridLocked(0,"1,1,0,0,0,1,1,0,1,0,1,1,0,1");
                    $objAxpDB->GridHidden(0,'0,0,1,0,0,1,0,0,1,1,0,1,0,1');
                    $objAxpDB->GridAllowAddNew(0,false);
                    $objAxpDB->GridAllowDelete(0,true);
               }else{
                    $ssql="SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code, fnqtypo,fnqtysisapo, fnqty, fnqty_base, fnqty_broken, ftuom_code, ffformula, fcprice FROM txstock_in_det";
                    $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
                    $objAxpDB->GridLocked(0, "1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0");
                    $objAxpDB->GridAllowAddNew(0,true);
                    $objAxpDB->GridAllowDelete(0,true);
                }

               $objAxpDB->GridSQL(0,$ssql);
               //$objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Lokasi, Gudang, Qty PO, Qty Sisa PO, Qty, Qty Base, Qty Rusak, Satuan, Formula, Harga HPP, Exp. Date Status");
               //$objAxpDB->GridHidden(0,'0,0,1,0,1,1,1,0,1,1,0,1,0,1');
               //$objAxpDB->GridSize(0,',,,,,,,10,,,10,,10,');me),CASE WHEN TRIM(IFNULL(ftpart_no,'')) != '' THEN ' ,PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) as ftitem_name  FROM tlstock_hdr {$sql_where} ORDER BY ftitem_code,ftpart_no ", "ftitem_code", "ftitem_name", -1,false);
        
               $objAxpDB->Process();
               
               $objAxpDB->LookupMagicQuotes(TRUE);
               // $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_na
               $objAxpDB->setGrid2(0);
               $html = ob_get_contents();
               ob_end_clean();
               ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");
                    div.innerHTML =m;
                    RowIndex0();
                    <!--reSize();-->
                    StockMasukRecalQtyPembelian();
               <?php
                if(($_REQUEST["ftcategory_code"] != 2) AND ($_REQUEST["ftcategory_code"] != 3) AND ($_REQUEST["ftcategory_code"] != 7) AND ($_REQUEST["ftcategory_code"] != 9) AND ($_REQUEST["ftcategory_code"] != 12)){
               ?>
                    TransactionDetailAddRow0();
                    if(document.getElementById('TransactionDetail0').rows.length==4){
                      <!--reSize();-->
                    }
               <?php
                }
               break;
           }
           die("");
           exit;
    }
    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();
