<?php
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                if(RetRstField("SELECT fnid FROM txstock_out_hdr WHERE ftstock_out_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
                }
                die("");
                break;
            case "gudang":
                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' and fnstatus = 1";
                $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['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),null,null);
                               }
                              catch(ex) {
                                ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),$i,$i);

                           } ";
                }
                die("");
                break;

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det
                        WHERE ftitem_code='{$_REQUEST['ftitem_code']}'
                        and ftuom_code_multi='{$_REQUEST['ftuom_code']}'
                        ";
                $ffformula=RetRstField($ssql);
                $ffQtyMulti=(double)$ffformula*(double)CCur($_REQUEST['fnqty']);
                ?>
                        document.getElementById('ffformulaD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffformula; ?>';
                        document.getElementById('fnqty_baseD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffQtyMulti; ?>';
                <?php

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

                ?>
                document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';
                ajax_do('query2.php?code=qty&ftwarehouse_code=<?php echo $ftwarehouse_code; ?>&ftlocation_code=<?php echo $_REQUEST['ftlocation_code']; ?>&ftitem_code=' + document.getElementById('ftitem_codeD0<?php echo $_REQUEST['index']; ?>').value+ '&objQty=fnqty_availableD0<?php echo $_REQUEST['index']; ?>');
                <?php

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

                $ssql="SELECT ifnull(fnqty,0) as fnqty FROM txstock_out_det 
                        WHERE ftstock_out_no='{$_REQUEST['ftstock_out_no']}'  AND ftitem_code='{$_REQUEST['ftitem_code']}' AND ftlocation_code='{$_REQUEST['ftlocation_code']}'
                        ";
                $fnqty=RetRstField($ssql);
                if ($fnqty=='') { $fnqty = 0; }
                
                ?>
                document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';
                ajax_do('query2.php?code=qty&ftwarehouse_code=<?php echo $ftwarehouse_code; ?>&ftlocation_code=<?php echo $_REQUEST['ftlocation_code']; ?>&ftitem_code=' + document.getElementById('ftitem_codeD0<?php echo $_REQUEST['index']; ?>').value+ '&objQty=fnqty_availableD0<?php echo $_REQUEST['index']; ?>&qty_revisi=<?php echo $fnqty; ?>');
                <?php
                
                die("");
                break;
            
            case "cattype":
                // Tidak perlu input company name / kolom dikunci
                if(($_REQUEST["ftcategory_code"]==7) or ($_REQUEST["ftcategory_code"]==8) or ($_REQUEST["ftcategory_code"]==10) or ($_REQUEST["ftcategory_code"]==11)) {
                    ?>
                    document.getElementById("ftstock_out_company").readOnly=true;
                    document.getElementById("ftstock_out_company").style='background-color: #E0E0E0';
                    document.getElementById("ftdocument_no").readOnly=true;
                    document.getElementById("ftdocument_no").style='background-color: #E0E0E0';
                    <?php
                // Harus input company name / kolom dibuka
                } else {
                ?>
                    document.getElementById("ftstock_out_company").readOnly=false;
                    document.getElementById("ftstock_out_company").style='background-color: #FFFFFF';
                    document.getElementById("ftdocument_no").value='';
                    <?php
                    // Harus input document no
                    if(($_REQUEST["ftcategory_code"]==5)) {
                    ?>    
                        document.getElementById("ftdocument_no").readOnly=false;
                        document.getElementById("ftdocument_no").style='background-color: #FFFFFF';
                        // document.getElementById('span_ftdocument_no').innerHTML='';
                    <?php
                    } else {
                    ?>
                        document.getElementById("ftdocument_no").readOnly=true;
                        document.getElementById("ftdocument_no").style='background-color: #E0E0E0';
                    <?php
                    }
                }
                ?> 
                document.getElementById("ftstock_out_company").value='';
                document.getElementById('span_ftstock_out_company').innerHTML='';
                document.getElementById("ftdocument_no").value='';
                document.getElementById('span_ftdocument_no').innerHTML='';
                <?php
                break;  
              
            case "qty_available_revisi":
                ?>
                var i=document.getElementsByName('fnindexD0[]').length;

                for(var j=0;j < i;j++) {
                    ajax_do('inventory.php?form=<?php echo $_REQUEST['form']; ?>&mode=<?php echo $_REQUEST['mode']; ?>&id=0&ajax=1&code=lokasi_revisi&index='+j+'&ftlocation_code='+document.getElementsByName('ftlocation_codeD0[]')[j].value + '&ftstock_out_no=' + document.getElementById('ftstock_out_no').value + '&ftitem_code='+ document.getElementsByName('ftitem_codeD0[]')[j].value);            
                }

                <?php
                break;  
        }       
      
    }
    
    Getsecurity('barang_keluar');
    $objAxpDB->ViewName("Daftar Stock Keluar");   

    // join supplier (outsource)
    $ssql="SELECT A.fnid, A.fdtrans_date, A.ftstock_out_no,  c.fttrans_status_name as ftcategory_code, d.ftsupplier_name as ftstock_out_company, A.ftdocument_no, A.fntotal_qty, A.ftnotes, B.fttrans_status_name 
           FROM txstock_out_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=4
           LEFT JOIN tlsupplier d on A.ftstock_out_company=d.ftsupplier_code
           WHERE (a.ftcategory_code = 6) and ftstock_type_code='inv' 
           AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')";

    // join customer 
    $ssql.=" UNION ALL SELECT A.fnid, A.fdtrans_date, A.ftstock_out_no,  c.fttrans_status_name as ftcategory_code, D.ftcustomer_name as ftstock_out_company, A.ftdocument_no, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
           FROM txstock_out_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=4
           LEFT JOIN tlcustomer d on A.ftstock_out_company=d.ftcustomer_code 
           WHERE (a.ftcategory_code IN (1,2,3,5,9)) 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_out_no,  c.fttrans_status_name as ftcategory_code, '' as ftstock_out_company, A.ftdocument_no, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
           FROM txstock_out_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=4
           WHERE (a.ftcategory_code IN (7,8,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_out_no DESC, fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID, Tgl. Masuk ,No. Stock Keluar, Kategori, Penerima, No. Dokumen, Total Qty, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);
   
    if(!$_REQUEST['id']) {
        $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=TN002&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_out_no&no_trans=ftstock_out_no&table_name=txstock_out_hdr&ftbranch_code={$_SESSION['strbranch']}');";
        $strAutoNumber0="function OnChangeDate_fdtrans_date() {                                                    
                            {$strAutoNumber1} 
                        } 
                        OnChangeDate_fdtrans_date();
                        
                        function OnChangeKategory() {
                            var m=document.getElementById('ftcategory_code').value;
                            
                            /* retur pembelian & outsource */
                            if(m=='0' || m=='6') {
                                 return \"SELECT ftsupplier_code AS ftcompany_code, ftsupplier_name AS ftcompany_name FROM tlsupplier\";
                            }
                            else if(m=='1' || m=='2' || m=='3' || m=='4' || m=='5' || m=='9') {
                                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;
                            
                            /* tukar barang */
                            if(m=='5') {
                                return \"SELECT a.ftstock_in_no, a.fdtrans_date FROM txstock_in_hdr a WHERE a.fnstatus=1 AND a.ftcategory_code = 5 AND a.ftbranch_code='{$_SESSION['strbranch']}' \";
                            }                           
                            /* keperluan internal */
                            else {
                                return \"select * from (SELECT \'\' AS ftstock_in_no, \'\'  AS txstock_in_hdr) x where 0=1 \";
                            }
                       
                        }    
                        ";
        //ajax_do('query2.php?code=15&value=' + document.getElementById(\"ftcategory_code\").value + '');
                                           
        $objAxpDB->jsPageOnLoad="OnChangeDate_fdtrans_date(); ";
        $objAxpDB->FormJS($strAutoNumber0);
      
    } else {
        if(isset($_REQUEST['revisi'])) {
            // reload qty available
            $ajax_qty="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=qty_available_revisi');";
            $objAxpDB->FormJS($ajax_qty);    
        }        
    }
    
    $objAxpDB->FormName("Stock Keluar");
    $objAxpDB->FormSQL("SELECT ftstock_out_no, ftcategory_code, fdtrans_date, ftstock_out_company,ftdocument_no, ftreff_no, ftnotes, fntotal_qty, fnstatus,ftstock_type_code FROM txstock_out_hdr");
    $objAxpDB->FormCaption("No. Stock Keluar, Kategori, Tgl. Keluar, Penerima, No. Document, No Referensi, Catatan,  Total Qty, Status, Tipe Stock");
    $objAxpDB->FormPosition("TL, TL, TR, TL, TL, TR, 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);";
    //$objAxpDB->FormScript(1, "onChange=\"ajax_do('query2.php?code=15&value=' + this.value + '&ftstock_type_code=' + document.getElementById('ftstock_type_code').value);\"");
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxType}\"");
    $ajaxDocument="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=grid&ftcategory_code='+ document.getElementById('ftcategory_code').value + '&ftdocument_no='+ document.getElementById('ftdocument_no').value);";
    $objAxpDB->FormScript(4, "onChange=\"{$ajaxDocument}\"");
                
    $objAxpDB->SetStatus("id, fnstatus,ftcategory_code");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code, fnqty_available, fnqty, ftuom_code, fnqty_base, ffformula, '' as ftactive FROM txstock_out_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty Avail, Qty, Satuan, Qty Base, Formula, Status");
    $objAxpDB->GridHidden(0,'0,0,1,0,0,0,0,0,1,1,1');
    $objAxpDB->GridHidden(1,'1');
    $objAxpDB->GridSize(0,',,,,,10,10,8,,,');
    
    $ajaxOUM_Code="ajax_do('query2.php?code=uom&obj1=' + document.getElementById('ftitem_codeD0_N_').value + '&obj2=ftuom_codeD0_N_');";
    $ajaxExp_Date="ajax_do('query2.php?code=exp_date&obj1=' + this.value + '&obj2=ftactiveD0_N_');";
    $ajaxQtyAvail=" onChange=\"{$ajaxOUM_Code}ajax_do('query2.php?code=qty&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value +'&ftcategory_code=' + document.getElementById('ftcategory_code').value+ '&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxOUM_CodeAndQtyAvail=" onChange=\"{$ajaxOUM_Code}{$ajaxExp_Date};ajax_do('query2.php?code=qty&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + encodeURIComponent(document.getElementById('ftitem_codeD0_N_').value)+ '&objQty=fnqty_availableD0_N_');\"";
    
    $ajaxGudang="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=fnqty_availableD0_N_');\"";    
    // $ajaxLokasi="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi&index=_N_&ftlocation_code='+this.value);\"";
    $objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxGudang);
    $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
    
    $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);";
    $objAxpDB->GridScript(0,6,"onChange=\"{$ajaxFormula}StockKeluarRecalQty();\"");
    $objAxpDB->GridDelJS(0,"StockKeluarRecalQty();");

    if(!$_REQUEST['id']) {
        $sql_where="WHERE fnstatus=1 ";
        $objAxpDB->Lookup("ftcategory_code","SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 4 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("ftstock_out_company","'+ OnChangeKategory() +'", "ftcompany_code", "ftcompany_name", -1,false);
        $objAxpDB->Lookup("ftdocument_no", "'+ OnChangeCompany() +'", "ftstock_in_no", "fdtrans_date", -1,false,"' AND ftstock_in_company=\''+ document.getElementById('ftstock_out_company').value + '\' ORDER BY ftstock_in_no '");
        
    } else {
        $objAxpDB->LookupMagicQuotes(TRUE);
        
        $objAxpDB->Lookup("ftcategory_code","SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 4 AND fnactive_status=1 ORDER BY fntrans_status_code ", "fntrans_status_code", "fttrans_status_name", 0,true);
        
        //if (($objAxpDB->GetStatus("ftcategory_code")=='0') or ($objAxpDB->GetStatus("ftcategory_code")=='6')) {
        //    $objAxpDB->Lookup("ftstock_out_company", "SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier", "ftsupplier_code", "ftsupplier_name", -1,false);
        //} else {
        //    $objAxpDB->Lookup("ftstock_out_company", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", -1,false);
        //}
    }

    $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 a inner join tlwarehouse b on a.ftwarehouse_code = b.ftwarehouse_code where a.fnstatus = 1 ", "ftlocation_code", "ftlocation_name", -1,true,"' AND a.ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
    $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->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom WHERE fnstatus = 1 order by ftuom_code", "ftuom_code", "ftuom_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} ORDER BY ftitem_code,ftpart_no ", "ftitem_code", "ftitem_name", -1,false);
    $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} ORDER BY ftitem_code,ftpart_no ", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftitem_code_change", "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} ORDER BY ftitem_code,ftpart_no ", "ftitem_code", "ftitem_name", -1,false);
    
    $ftcategory_code=RetRstField("select ftcategory_code from txstock_out_hdr where fnid='{$_REQUEST['id']}'");
  
    if ($objAxpDB->GetStatus("id") == "")
        $objAxpDB->ButtonShow("baru");
    elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
        $objAxpDB->GridLocked(0,'1,0,0,0,0,1,0,1,1,1,1,1');
        $objAxpDB->ButtonShow("reset, simpan, tutup");
        $objAxpDB->FormLocked("1, 0, 0, 0, 0, 0, 0,1, 1, 1, 1");
        
        $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->ButtonShow("cetak,tutup");
        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
        $objAxpDB->GridLocked(0,'1,0,0,0,0,1,0,1,1,1');
        $objAxpDB->ButtonShow("cetak, update, baru, batal, final, tutup");
        $objAxpDB->FormLocked("1, 0, 0, 0, 0, 0, 1, 1, 1");
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
        // revisi
        if(isset($_REQUEST['revisi'])) {
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0,'1,0,0,0,0,1,0,1,1,1,1,1');    
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE); 
        
            $ajaxLokasiRevisi="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_revisi&index=_N_&ftlocation_code='+this.value + '&ftstock_out_no=' + document.getElementById('ftstock_out_no').value + '&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value);\"";
            $objAxpDB->GridScript(0,3, $ajaxLokasiRevisi);
    
            $objAxpDB->Lookup("ftcategory_code","SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 4 AND fnactive_status=1  AND fntrans_status_code <> 0 AND fntrans_status_code <> 4  ORDER BY fntrans_status_code ", "fntrans_status_code", "fttrans_status_name", -1,true);
        
            $objAxpDB->ButtonShow("simpan_revisi,tutup");
            
        } else {
            $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1,1');
        }
        
    // Status approved tidak bisa batal karena ada sales invoice
    } elseif ($objAxpDB->GetStatus("fnstatus") == 2) {
        $objAxpDB->ButtonShow("cetak, tutup");
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1,1');
    } elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 9)) {
        $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1');
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->ButtonShow("tutup");
    }

    
    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           /*case "return":
                ob_start();

                $ssql="SELECT b.fnindex,b.ftitem_code AS ftitem_code_change,b.ftitem_code AS ftitem_code,
                        '' AS  ftwarehouse_code, '' AS ftlocation_code, b.fnqty-IFNULL(c.fnqty,0) as fnqty_sisa, 0.0 AS fnqty_available,  0.0 AS fnqty, '' AS ftuom_code
                        FROM txstock_in_hdr a INNER JOIN txstock_in_det b ON  a.ftstock_in_no=b.ftstock_in_no
                        LEFT JOIN (SELECT a.ftdocument_no,b.ftitem_code,IFNULL(SUM(b.fnqty),0) AS fnqty
                        FROM txstock_out_hdr a,txstock_out_det b
                        WHERE a.ftstock_out_no=b.ftstock_out_no
                            AND a.fnstatus=1 AND
                             a.ftdocument_no='{$_REQUEST['ftdocument_no']}' AND a.ftcategory_code='1'
                        GROUP BY a.ftdocument_no,b.ftitem_code
                        ) c
                        ON a.ftstock_in_no=c.ftdocument_no AND b.ftitem_code=c.ftitem_code
                        WHERE
                            a.ftstock_in_no='{$_REQUEST['ftdocument_no']}'  AND
                            a.fnstatus=1 AND
                            a.ftcategory_code='2' AND
                            b.fnqty-IFNULL(c.fnqty,0)>0 #";

                $objAxpDB->GridSQL(0,$ssql);
                $objAxpDB->GridCaption(0, "No., Kode Barang ,Kode Barang Pengganti, Gudang, Lokasi,Qty Sisa, Qty Avail, Qty, Satuan");
                $objAxpDB->GridLocked(0,'1,1,0,0,0,1,1,0,1');
                $objAxpDB->GridScript(0,3, $ajaxGudang);
                $objAxpDB->GridScript(0,2, $ajaxOUM_CodeAndQtyAvail);
                #$objAxpDB->GridScript(0,2, $ajaxQtyAvail);
                $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
                $objAxpDB->GridScript(0,7,"onChange='StockKeluarRecalQty();'");
                $objAxpDB->GridDelJS(0,"StockKeluarRecalQty();");
                $objAxpDB->GridAllowAddNew(0,FALSE);
                $objAxpDB->GridAllowDelete(0,true);
             
                $objAxpDB->Process();
                $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;
                   
                    var i=document.getElementsByName('fnindexD0[]').length;

                    RowBuild0();
                     document.getElementById("TransactionDetail0").deleteRow(i+1);
                    reSize();
               <?

               break;*/

            case "grid":
                ob_start();

                // tukar barang
                if($_REQUEST["ftcategory_code"] == 5){
                    $ssql=" SELECT b.fnindex, b.ftitem_code, b.fdexp_date, '' AS ftwarehouse_code, '' as ftlocation_code,
                                CAST(0 AS DECIMAL(19,2)) AS fnqty_available,  CAST(0 AS DECIMAL(19,2)) AS fnqty, b.ftuom_code AS ftuom_code,
                                CAST(0 AS DECIMAL(19,2)) AS fnqty_base, b.ffformula, '' as ftactive
                            FROM txstock_in_hdr a INNER JOIN txstock_in_det b ON a.ftstock_in_no = b.ftstock_in_no
                            WHERE a.ftstock_in_no='".$_REQUEST['ftdocument_no']."' AND a.ftcategory_code = 5 AND a.fnstatus <> 3 AND a.fnstatus <> 9
                            GROUP BY b.fnindex, b.ftitem_code, b.fnqty ,b.ftuom_code, b.ffformula #";

                    $objAxpDB->GridLocked(0,"1,0,0,0,0,1,0,0,1,1,1");
                    $objAxpDB->GridHidden(0,"0,0,1,0,0,0,0,0,1,1,1");
                    
                }     
                // outsource
                /*else if($_REQUEST["ftcategory_code"] == 6){    
                $ssql="SELECT b.fnindex,b.ftitem_code AS ftitem_code, '' as fdexp_date,
                        '' AS  ftwarehouse_code, '' AS ftlocation_code, CAST(0 AS DECIMAL(19,4)) AS fnqty_available,  CAST(0 AS DECIMAL(19,4)) AS fnqty, b.ftuom_code AS ftuom_code,
                        CAST(0 AS DECIMAL(19,4)) AS fnqty_base, 1 AS ffformula, '' as ftactive
                        FROM txpurchase_order_hdr a INNER JOIN txpurchase_order_det b ON  a.ftpo_no=b.ftpo_no
                        WHERE a.ftpo_no='{$_REQUEST['ftdocument_no']}'  AND
                            a.fnstatus=1 AND a.fnreceipt_status <> 3 and a.fnpo_type3=3 # ";
                } */
                
                $objAxpDB->GridSQL(0,$ssql);
                //$objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty Avail, Qty, Satuan, Qty Base, Formula, Exp. Date Status");
                //$objAxpDB->GridLocked(0,'1,1,0,0,0,1,0,1,1,1');
                //$objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
                //$objAxpDB->GridScript(0,3, $ajaxGudang);
                //$objAxpDB->GridScript(0,4, $ajaxQtyAvail);
                //$objAxpDB->GridScript(0,6,"onChange=\"{$ajaxFormula}StockKeluarRecalQty();\"");
                //$objAxpDB->GridDelJS(0,"StockKeluarRecalQty();");
                $objAxpDB->LookupMagicQuotes(TRUE);
                //$objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(ftpart_no) != '' THEN ' PART NO: ' ELSE '' END,ftpart_no) AS ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_name,ftpart_no", "ftitem_code", "ftitem_name",-1,false);

                $objAxpDB->GridAllowAddNew(0,true);
                $objAxpDB->GridAllowDelete(0,true);

                $objAxpDB->Process();
                $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();
               <?php

               break;
           }
           
           die("");
           exit;
    }
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();