<?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
              }
              die("");
            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":
              if(($_REQUEST["ftcategory_code"]==6) or ($_REQUEST["ftcategory_code"]==7) or ($_REQUEST["ftcategory_code"]==9) ){
              ?>
                document.getElementById("ftdocument_no").readOnly=false;
                document.getElementById("ftdocument_no").style='background-color: #FFFFFF';
              <?php
              
              // retur pemakaian
              } else if($_REQUEST["ftcategory_code"]==8){
                ?>
                document.getElementById("ftdocument_no").readOnly=false;
                document.getElementById("ftstock_in_company").readOnly=true;
                document.getElementById("ftdocument_no").style='background-color: #FFFFFF';
                document.getElementById("ftstock_in_company").style='background-color: #E0E0E0';
                <?php

              }else{
              ?>
                document.getElementById("ftdocument_no").readOnly=true;
                document.getElementById("ftdocument_no").style='background-color: #E0E0E0';
              <?php
              }
              ?>
              ajax_do('inventory_bahan_baku.php?form=<?php echo $_REQUEST['form'];?>&mode=<?php echo $_REQUEST['mode'];?>&id=0&ajax=1&code=grid&ftpo_no=<?php echo $_REQUEST['ftpo_no'];?>&ftcategory_code=<?php echo $_REQUEST["ftcategory_code"];?>');
              <?php
              break;
            case "gudang":
                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}'";
                $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 "uom":
                $ftUOM_Code1=RetRstField("SELECT UCASE(ftUOM_Code) as ftUOM_Code  FROM tlstock_hdr_bahan_baku WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");
                $ftUOM_Code="SELECT UCASE(a.ftUOM_Code) AS ftuom_code ,UCASE(b.ftuom_name) AS ftuom_name FROM tlstock_hdr_bahan_baku a
                                INNER JOIN tluom b ON a.ftuom_code=b.ftuom_code
                                WHERE a.ftitem_code='{$_REQUEST['ftitem_code']}'
                                GROUP BY a.ftUOM_Code
                                UNION ALL
                                SELECT UCASE(a.ftuom_code_multi) AS ftuom_code,UCASE(b.ftuom_name) AS ftuom_name FROM tlstock_multi_det_bahan_baku a
                                        INNER JOIN tluom b ON a.ftuom_code_multi=b.ftuom_code
                                WHERE a.ftitem_code='{$_REQUEST['ftitem_code']}'
                                GROUP BY a.ftuom_code_multi";
                $rs=mysql_query($ftUOM_Code);
                ?>
                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['ftuom_code']}--{$r['ftuom_name']}','{$r['ftuom_code']}'),null,null);
                                        }
                                        catch(ex)
                                        {
                                                ObjSelect.add(new Option('{$r['ftuom_code']}--{$r['ftuom_name']}','{$r['ftuom_code']}'),$i,$i);
                                        } ";
                }
                $ftactive=RetRstField("SELECT fdexp_date  FROM tlstock_hdr_bahan_baku WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");

                ?>
                        // ObjSelect.value=document.getElementById('ftuom_code_D0<?php echo $_REQUEST['index']; ?>').value;
                        ObjSelect.value='<?php echo $ftUOM_Code1; ?>';
                        document.getElementById('ftactiveD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftactive; ?>';
                <?php

                die("");
            break;

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det_bahan_baku
                        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;
        }
       
    }

    Getsecurity("inventorybb_stock_masuk");
    $objAxpDB->ViewName("Daftar Stock Masuk (Bahan Baku)");
    $ssql="SELECT A.fnid,  A.ftstock_in_no, c.fttrans_status_name as ftcategory_code, d.ftsupplier_name as ftstock_in_company, A.ftdocument_no, A.fdtrans_date, /*A.ftreff_no,A.ftchecked_user,*/ 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.ftstock_type_code='invbb'
                ORDER BY a.ftstock_in_no DESC,a.fdtrans_date DESC ";

    $objAxpDB->ViewSQL($ssql);
    $objAxpDB->ViewCaption("ID,No. Stock Masuk, Kategori, Supplier, No. PO, Tgl. Masuk, Total Qty, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

    $ajaxType="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=cattype&ftcategory_code='+ document.getElementById('ftcategory_code').value);";
	
     if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=bbtn001&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();
                              {$ajaxType}
                              
                              function get_ftsupplier_code() {
                                        return  \" and ftsupplier_code='\"+ document.getElementById('ftstock_in_company').value + \"'\";
                                }

                                var i=document.getElementsByName('fnindexD0[]').length;
                                // document.getElementById(\"TransactionDetail0\").deleteRow(2);
                                       ";
             $objAxpDB->FormJS($strAutoNumber0);
             $ajaxpo="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=fdschedule_delivery_date&ftpo_no='+ document.getElementById('ftdocument_no').value);";

     }

    $objAxpDB->FormName("Stock Masuk");
    $objAxpDB->FormSQL("SELECT ftstock_in_no, ftcategory_code, ftstock_in_company, fdtrans_date, ftdocument_no,fdschedule_delivery_date, ftreff_no, ftchecked_user, ftnotes, fntotal_qty, fnstatus, ftstock_type_code FROM txstock_in_hdr");
    $objAxpDB->FormCaption("No. Stock Masuk, Kategori, Supplier, Tgl. Masuk, No. Document,Tgl. Schedule Delivery, No. SJ, Pengecek Barang, Catatan,  Total Qty, Status, Tipe Stock");
    $objAxpDB->FormPosition("TL, TL, TL, TR, TR, HD, HD, TR, BL, BR, BR, TR");
    $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='';document.getElementById('hidden_ftdocument_no').value='';";
    $objAxpDB->FormScript(1, " onChange=\"{$ajaxClear}{$ajaxType}\"");

    $ajaxType="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=grid&ftpo_no='+ document.getElementById('ftdocument_no').value + '&ftcategory_code='+ document.getElementById('ftcategory_code').value);";
    $objAxpDB->FormScript(4, " onChange=\"{$ajaxType}\"");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code, fnqtypo,fnqtysisapo, fnqty, fnqty_base, fnqty_broken, ftuom_code, ffformula, fcprice, '' as ftactive FROM txstock_in_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty PO, Qty Sisa PO, Qty, Qty Base, Qty Rusak, Satuan, Formula, Harga HPP, Exp. Date Status");

    $ajaxUom="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=uom&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value + '&index=_N_');";
    $objAxpDB->GridScript(0,1, "onChange=\"{$ajaxUom};\"");
    //$objAxpDB->GridScript(0,1, "onChange=\"ajax_do('query2.php?code=uom_bahan_baku&obj1=' + this.value + '&obj2=ftuom_codeD0_N_');\"");

    $ajaxGudang="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');\"";
    $objAxpDB->GridScript(0,3, $ajaxGudang);

    $ajaxLock="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=qty&qty='+ this.value + '&index=_N_');";
    $ajaxFormula="ajax_do('inventory_bahan_baku.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,7,"onChange=\"{$ajaxLock}{$ajaxFormula}StockMasukRecalQty();\"");
    $objAxpDB->GridDelJS(0,"StockMasukRecalQty();");

    if(!$_REQUEST['id']) {
        $ssql=" SELECT ftstock_out_no, fdtrans_date, ftstock_out_company, ftcategory_code FROM ( ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 6 as ftcategory_code FROM txstock_out_hdr a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9  UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 6 as ftcategory_code FROM txstock_out_hdr_bahan_baku a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9  UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 6 as ftcategory_code FROM txstock_out_hdr_setengah_jadi a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9  UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 6 as ftcategory_code FROM txstock_out_hdr_sparepart a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9 UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 7 as ftcategory_code FROM txstock_out_hdr a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9  UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 7 as ftcategory_code FROM txstock_out_hdr_bahan_baku a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9  UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 7 as ftcategory_code FROM txstock_out_hdr_setengah_jadi a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9  UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, a.ftstock_out_company, 7 as ftcategory_code FROM txstock_out_hdr_sparepart a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no = b.ftpo_no WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND b.fnreceipt_status <> 3 AND b.fnreceipt_status <> 9 AND b.fnstatus <> 3 AND b.fnstatus <> 9 UNION ALL ";
        $ssql.=" SELECT a.ftstock_out_no, a.fdtrans_date, \'\' as ftstock_out_company, 8 as ftcategory_code FROM txstock_out_hdr_bahan_baku a WHERE a.fnstatus=1 AND a.ftcategory_code=5 ";
        $ssql.="  ) txstock WHERE 1=1 ";

        $objAxpDB->Lookup( "ftdocument_no", $ssql, "ftstock_out_no", "fdtrans_date", -1,false,"' AND ftstock_out_company=\''+ document.getElementById('ftstock_in_company').value + '\' AND ftcategory_code = ' + document.getElementById('ftcategory_code').value");
    }

    $objAxpDB->Lookup( "ftstock_in_company", "SELECT  ftsupplier_code,  ftsupplier_name from tlsupplier", "ftsupplier_code", "ftsupplier_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);
    $ssql2="SELECT ftitem_code, ftitem_name FROM (";
    $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr UNION ALL ";
    $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku UNION ALL ";
    $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_sparepart UNION ALL ";
    $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi";
    $ssql2.=") tlstock WHERE 1=1";
    $objAxpDB->Lookup("ftitem_code", $ssql2, "ftitem_code", "ftitem_name",-1,false);
    $objAxpDB->Lookup( "ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type=0 or fnwarehouse_type=3 ", "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 b.fnwarehouse_type=0 or b.fnwarehouse_type=3 ", "ftlocation_code", "ftlocation_name", -1,true,"' AND a.ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
    $objAxpDB->Lookup( "ftcategory_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 20", "fntrans_status_code", "fttrans_status_name", -1,true);
    $objAxpDB->Lookup("ftstock_type_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type where ftstock_type_code='invbb' ", "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");

    elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->Lookup( "ftcategory_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 3 and fnactive_status = 1 and fntrans_status_code <> 0  and fntrans_status_code <> 4  and fntrans_status_code <> 5 ", "fntrans_status_code", "fttrans_status_name", -1,true);

        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
        $objAxpDB->GridLocked(0,'1,0,0,0,0,1,1,0,0,1,0,0,0');
        $objAxpDB->ButtonShow("reset, simpan, tutup");
        $objAxpDB->FormLocked("0, 0, 0, 0, 0, 0, 0, 0, 0, 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") == 1) {
        $objAxpDB->FormLocked("1, 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');
            
        // barang masuk pembelian harus dibatalkan melalui penerimaan barang
        if ($objAxpDB->GetStatus("ftcategory_code") == 0) {
            $objAxpDB->ButtonShow("cetak, baru, tutup");
        } else {
            $objAxpDB->ButtonShow("cetak, baru, 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, 1, 1");
            $objAxpDB->GridLocked(0,'1,1,1,1,1,1,1,1,1,1,1,1,1');
            $objAxpDB->ButtonShow("baru, cetak, tutup");
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->FormLocked("1, 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');
            $objAxpDB->ButtonShow("tutup");
    }

    $objAxpDB->GridHidden(0,'0,0,0,0,0,1,1,0,0,0,0,1,0,1');
    $objAxpDB->GridHidden(1,'1');

    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();
                if($_REQUEST["ftcategory_code"] == 6){
                   $ssql="SELECT ftdocument_no, ftstock_out_no FROM (";
                   $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr UNION ALL ";
                   $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku UNION ALL ";
                   $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_sparepart UNION ALL ";
                   $ssql.="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_setengah_jadi";
                   $ssql.=") txstock WHERE ftstock_out_no='".$_REQUEST['ftpo_no']."'";
                   $mysql=mysql_query($ssql);
                   $row=mysql_fetch_assoc($mysql);
                   $ftpo_no=$row["ftdocument_no"];

                   // $ssql="SELECT fnindex, ftitem_code, '' as ftwarehouse_code, '' as ftlocation_code, fnqty as fnqtypo, ifnull(fnqty_accept,0) as fnqtysisapo, CAST(0 AS DECIMAL(19,4)) as fnqty, CAST(0 AS DECIMAL(19,4)) as fnqty_base, CAST(0 AS DECIMAL(19,4)) AS fnqty_broken, ftuom_code, ffformula,CAST(0 AS DECIMAL(19,4)) as fcprice FROM txpurchase_order_det WHERE ftpo_no='{$ftpo_no}' AND (fnqty - ifnull(fnqty_accept,0)) > 0 #";
                   $ssql="SELECT fnindex, ftitem_code, '' as fdexp_date, '' as ftwarehouse_code, '' as ftlocation_code, fnqty as fnqtypo, ifnull(fnqty_accept,0) as fnqtysisapo, CAST(0 AS DECIMAL(19,4)) as fnqty, CAST(0 AS DECIMAL(19,4)) as fnqty_base, CAST(0 AS DECIMAL(19,4)) AS fnqty_broken, ftuom_code, ffformula,CAST(0 AS DECIMAL(19,4)) as fcprice, '' as ftactive FROM txpurchase_order_det WHERE ftpo_no='{$ftpo_no}' #";

                   $ssql2="SELECT ftitem_code, ftitem_name FROM (";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr UNION ALL ";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku UNION ALL ";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_sparepart UNION ALL ";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi";
                   $ssql2.=") tlstock WHERE 1=1";
                   $objAxpDB->Lookup( "ftitem_code", $ssql2, "ftitem_code", "ftitem_name",-1,false);
                  
                   $objAxpDB->GridLocked(0, "1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0");
                   $objAxpDB->GridAllowAddNew(0,false);
                   $objAxpDB->GridAllowDelete(0,true);

                }elseif($_REQUEST["ftcategory_code"] == 7){
                   $ssql="SELECT a.fnindex, a.ftitem_code, '' as 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,4)) AS fnqty, CAST(0 AS DECIMAL(19,4)) AS fnqty_base, CAST(0 AS DECIMAL(19,4)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,4)) AS fcprice, '' as ftactive
                            FROM txstock_out_det_bahan_baku a INNER JOIN txstock_out_hdr_bahan_baku 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 = 7
                            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['ftpo_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  #";
                  
                   $ssql2="SELECT ftitem_code, ftitem_name FROM (";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr UNION ALL ";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku UNION ALL ";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_sparepart UNION ALL ";
                   $ssql2.="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi";
                   $ssql2.=") tlstock WHERE 1=1";
                   $objAxpDB->Lookup( "ftitem_code", $ssql2, "ftitem_code", "ftitem_name",-1,false);

                   $objAxpDB->GridLocked(0, "1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0");
                   $objAxpDB->GridAllowAddNew(0,false);
                   $objAxpDB->GridAllowDelete(0,true);

                // retur pemakaian
                }elseif($_REQUEST["ftcategory_code"] == 8){
                   $ssql="SELECT ftdocument_no, ftstock_out_no FROM txstock_out_hdr_bahan_baku WHERE ftstock_out_no='".$_REQUEST['ftpo_no']."'";
                   $mysql=mysql_query($ssql);
                   $row=mysql_fetch_assoc($mysql);
                   $ftpo_no=$row["ftdocument_no"];

                   $ssql="SELECT a.fnindex, a.ftitem_code, '' as 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,4)) AS fnqty, CAST(0 AS DECIMAL(19,4)) AS fnqty_base, CAST(0 AS DECIMAL(19,4)) AS fnqty_broken, a.ftuom_code, a.ffformula,CAST(0 AS DECIMAL(19,4)) AS fcprice, '' as ftactive
                            FROM txstock_out_det_bahan_baku a INNER JOIN txstock_out_hdr_bahan_baku 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 = 8
                            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['ftpo_no']."' AND b.ftcategory_code = 5 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  #";

                   $ssql2="SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku ";
                   $objAxpDB->Lookup( "ftitem_code", $ssql2, "ftitem_code", "ftitem_name",-1,false);

                   $objAxpDB->GridLocked(0, "1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0");
                   $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, '' as ftactive FROM txstock_in_det";
                  $objAxpDB->GridLocked(0, "1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0");
                  $objAxpDB->Lookup( "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku $m", "ftitem_code", "ftitem_name",-1,false);
                  $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
                  $objAxpDB->GridAllowAddNew(0,true);
                  $objAxpDB->GridAllowDelete(0,true);
                }
               
               $objAxpDB->GridSQL(0,$ssql);
               $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty PO, Qty Sisa PO, Qty, Qty Base, Qty Rusak, Satuan, Formula, Harga HPP, Exp. Date Status");
               
               $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();
                    StockMasukRecalQtyPembelian();
               <?php
                if(($_REQUEST["ftcategory_code"] != 6) and ($_REQUEST["ftcategory_code"] != 7) and ($_REQUEST["ftcategory_code"] != 8)){
               ?>
                    TransactionDetailAddRow0();
                    if(document.getElementById('TransactionDetail0').rows.length==4){
                      reSize();
                    }
               <?php
                }
               break;
           }
           die("");
           exit;
    }
    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();
