<?php
    // initialize variable for multiple tables
    $tablename = "";
    $fnpo_type3 = "";
   
    if($_REQUEST['ftstock_type_code']=='invbb') {
        $tablename = "_bahan_baku";
        $fnpo_type3 = "0";
    } elseif ($_REQUEST['ftstock_type_code']=='invsj') {
        $tablename = "_setengah_jadi";
        $fnpo_type3 = "1";
    } elseif ($_REQUEST['ftstock_type_code']=='invsp') {
        $tablename = "_sparepart";
        $fnpo_type3 = "2";
    } elseif ($_REQUEST['ftstock_type_code']=='inv') {
        $tablename = "";
        $fnpo_type3 = "3";
    }

    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 "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);
                               } ";
                }
                
                echo " try {
                            ObjSelect.selectedIndex=1;
                       } catch(ex) {

                       } ";
               die("");
            break;

            case "uom":
                //$ftUOM_Code1=RetRstField("SELECT UCASE(ftUOM_Code) as ftUOM_Code  FROM tlstock_hdr{$tablename} 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{$tablename} 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{$tablename} 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);
                                        } ";
                }

                ?>
                        ObjSelect.value=document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index']; ?>').value;
                <?php

                die("");
                break;
                
            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det{$tablename}
                        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 "fdschedule_delivery_date":
                $ssql1="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_schedule_delivery
                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' ";
 
                $ssql2="SELECT fdschedule_delivery_date
                    FROM txpurchase_order_schedule_delivery
                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                            fnlock='0'
                    ORDER BY fnindex";

                $ssql3="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_schedule_delivery
                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' ";
             
                if(RetRstField($ssql1)==1) {
                    $ssql="SELECT fdschedule_delivery_date
                            FROM txpurchase_order_schedule_delivery
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}' ";
                    $date=RetRstField($ssql);
                    ?>
                            document.getElementById('fdschedule_delivery_date').value='<?php echo CDateRev($date); ?>';
                    <?php
                }
                elseif(RetRstField($ssql2)!="") {
                    $ssql2="SELECT fdschedule_delivery_date
                            FROM txpurchase_order_schedule_delivery
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND fnlock='0'
                            ORDER BY fnindex";
                    $date=RetRstField($ssql2);
                    ?>
                            document.getElementById('fdschedule_delivery_date').value='<?php echo CDateRev($date); ?>';
                    <?php
                }
                else {
                    $ssql2="SELECT fdschedule_delivery_date
                            FROM txpurchase_order_schedule_delivery
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}'
                            ORDER BY fnindex DESC";
                    $date=RetRstField($ssql2);
                    ?>
                            document.getElementById('fdschedule_delivery_date').value='<?php echo CDateRev($date); ?>';
                    <?php
                }
                die("");
            break;

            case "grid":
                    $ssql="SELECT ftsupplier_code 
                            FROM txpurchase_order_hdr
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}'
                            ";
                    $ftsupplier_code=RetRstField($ssql);
                    
                    $ssql="SELECT fdtrans_date 
                            FROM txpurchase_order_hdr
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}'
                            ";
                    $fdtrans_date=RetRstField($ssql);
                    
                    ?>
                    
                        document.getElementById('fnpo_type3').value='<?php echo $fnpo_type3; ?>';
                        document.getElementById('ftstock_in_company').value='<?php echo $ftsupplier_code; ?>';
                        
                    <?php
                    
                    $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=pu004&ftstock_type_code=' + document.getElementById(\"ftstock_type_code\").value + '&ftcategory_code=' + document.getElementById(\"ftcategory_code\").value + '&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']}');" ;
                    echo $strAutoNumber1;
            break;

        }
    }
    
    Getsecurity('pembelian_stock_masuk');
    $objAxpDB->LookupMagicQuotes(True);
    
    $objAxpDB->ViewName("Daftar Penerimaan Barang");
    $ssql="SELECT A.fnid, A.fdtrans_date,  A.ftstock_in_no, e.ftstock_type_code as fttype, d.ftsupplier_name as ftstock_in_company, A.ftdocument_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=20
                left JOIN tlsupplier d on a.ftstock_in_company=d.ftsupplier_code
                left join tlstock_type e on a.ftstock_type_code = e.ftstock_type_code
            WHERE a.ftcategory_code='0' AND d.fnsupplier_type= 0
                    AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
            ORDER BY ftstock_in_no DESC,fdtrans_date DESC ";
    $objAxpDB->ViewSQL($ssql);
    $objAxpDB->ViewCaption("ID, Tgl Transaksi,No. Transaksi,Tipe PO, Supplier, No. PO, Total Qty, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

    $objAxpDB->FormName("Penerimaan Barang");
    $objAxpDB->FormSQL("SELECT ftstock_in_no, ftstock_type_code, ftcategory_code, ftdocument_no, ftstock_in_company, fdschedule_delivery_date, fdtrans_date, ftreff_no, fttime_start, fttime_end, ftchecked_user, ftcurrency_code, fcrate, ftnotes,  fntotal_qty, fnstatus FROM txstock_in_hdr");
    $objAxpDB->FormCaption("No. Transaksi,Tipe Stock, Kategori, No. PO, Supplier,Tgl. Schedule Delivery, Tgl Transaksi, No. Surat Jalan, Jam Mulai Pemeriksaan, Jam akhir Pemeriksaan, Pengecek Barang, Mata Uang, Kurs, Catatan,  Total Qty, Status");
    $objAxpDB->FormPosition("TL,TL, TL, TL, TL,HD, TR, TR, HD, HD, HD, HD, HD, BL, BR, BR");

    $x="document.getElementById('ftstock_in_company').value='';";
    $x.="document.getElementById('hidden_ftstock_in_company').value='';";
    $x.="document.getElementById('span_ftstock_in_company').innerHTML='';";
    $x.="document.getElementById('ftdocument_no').value='';";
    $x.="document.getElementById('hidden_ftdocument_no').value='';";
    $x.="document.getElementById('span_ftdocument_no').innerHTML='';";
    $x.="$ajaxGrid";
    
    $objAxpDB->FormScript(1, "onChange=\"{$x}\"");
    $objAxpDB->FormScript(2, "onChange=\"{$x}OnChangeDate_fdtrans_date();\"");
    $hiddentext="<input type='hidden' id='fnpo_type3' name='fnpo_type3' value=''>";
    $objAxpDB->FormLayout(0,"{$hiddentext}");
    
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,ftdescription, ftwarehouse_code, ftlocation_code,ffformula, fnqtypo,fnqtypo_base,fnqtysisapo,fnqtysisapo_base,ftuom_code as ftuom_code_po,fnqty,fnqty_base, ftuom_code,fnindex_po,fnqty_reject, ifnull(fcprice,0) as fcprice, fcselling_price FROM txstock_in_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang,Keterangan, Gudang, Lokasi, Formula, Qty PO, Qty PO Base, Qty Sisa PO, Qty Sisa PO Base, Satuan PO, Qty Masuk, Qty Masuk Base, Satuan,Index PO, Qty Reject, Harga Beli (IDR), Harga Jual (IDR)");
    $objAxpDB->GridLocked(0, '1,1,0,0,0,1,1,1,1,1,1,0,1,1,1,0,1,0');
    $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,1,1');
    $objAxpDB->GridSize(0, '3,10,10,5,5,,10,10,10,10,10,10,10,5,,,10,10');
                
    $objAxpDB->GridAutoAdd(0, False);
    //$objAxpDB->Lookup( "ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
    //$objAxpDB->Lookup( "ftuom_code_po", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
    $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", 0,true);
    $objAxpDB->Lookup( "ftstock_in_company", "SELECT  ftsupplier_code, ftsupplier_name from tlsupplier WHERE 0=0", "ftsupplier_code", "ftsupplier_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( "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr{$tablename} $m", "ftitem_code", "ftitem_name",-1,false);
    //$objAxpDB->Lookup( "ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse", "ftwarehouse_code", "ftwarehouse_name", 0,true);
    //$objAxpDB->Lookup( "ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation ", "ftlocation_code", "ftlocation_name", 0,true);
    $objAxpDB->Lookup( "ftstock_type_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type WHERE ftstock_type_code IN ('inv','srv') AND 0=0 ", "ftstock_type_code", "ftstock_type_name", 0,true);

    $ajaxGudang="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=ffformulaD0_N_&ObjItem=ftuom_codeD0_N_&ftpo_no='+ document.getElementById('ftdocument_no').value+'&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value +'');\"";
    // $js2="OnChange=\"StockMasukRecalQtyPembelianMulti();StockMasukRecalQtyPembelian();\"";
    $js2="OnChange=\"StockMasukRecalQtyPembelian();\"";
    $ajaxFormula="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&ftstock_type_code='+ document.getElementById('ftstock_type_code').value +'&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value+'&ffformula='+ document.getElementById('ffformulaD0_N_').value);StockMasukRecalQtyPembelian();\"";
    
    if($_REQUEST['id']==0)
    {
        // Bila ada qty PO di bawah qty limit juga dimunculkan
        // $objAxpDB->Lookup( "ftdocument_no", "SELECT ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3 FROM (SELECT distinct ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3 FROM (SELECT b.ftitem_code, a.ftpo_no, a.fdtrans_date ,a.ftsupplier_code,a.fnpo_type3, COALESCE(SUM(b.fnqty_accept_base),0) AS ttlqtyaccept, CASE WHEN COALESCE(SUM(b.fnqty_limit),0) = 0 THEN SUM(b.fnqty_base) ELSE SUM(b.fnqty_limit) END AS ttlqtylimit FROM txpurchase_order_hdr a INNER JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no WHERE a.fnstatus=1 and a.fnhdr_category = 0 AND a.ftbranch_code='{$_SESSION['strbranch']}' GROUP BY b.ftitem_code, a.ftpo_no, a.fdtrans_date ,a.ftsupplier_code,a.fnpo_type3 HAVING  ttlqtyaccept < ttlqtylimit) x ORDER BY ftpo_no) a where 0=0 ", "ftpo_no", "fdtrans_date", -1,false,"' AND fnpo_type3=\''+ document.getElementById('fnpo_type3').value + '\' AND ftsupplier_code=\''+ document.getElementById('ftstock_in_company').value + '\''");
        $objAxpDB->Lookup( "ftdocument_no", "SELECT ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3 FROM (SELECT distinct ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3 FROM (SELECT b.ftitem_code, a.ftpo_no, a.fdtrans_date ,a.ftsupplier_code,a.fnpo_type3, COALESCE(SUM(b.fnqty_accept_base),0) AS ttlqtyaccept, CASE WHEN COALESCE(SUM(b.fnqty_limit),0) = 0 THEN SUM(b.fnqty_base) ELSE SUM(b.fnqty_limit) END AS ttlqtylimit FROM txpurchase_order_hdr a INNER JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no WHERE a.fnstatus=1 and a.fnhdr_category = 0 AND fnpo_type=0 AND a.ftbranch_code='{$_SESSION['strbranch']}' GROUP BY b.ftitem_code, a.ftpo_no, a.fdtrans_date ,a.ftsupplier_code,a.fnpo_type3 HAVING  ttlqtyaccept < ttlqtylimit) x ORDER BY ftpo_no) a where 0=0 ", "ftpo_no", "fdtrans_date", -1,true,"'AND fnpo_type3=\''+ document.getElementById('fnpo_type3').value + '\''");
       
    }
    else
    {
        //   $objAxpDB->Lookup( "ftdocument_no", "SELECT ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3 FROM txpurchase_order_hdr ", "ftpo_no", "fdtrans_date", -1,false,"' AND fnpo_type3=\''+ document.getElementById('ftstock_type_code').value + '\'  AND ftsupplier_code=\''+ document.getElementById('ftstock_in_company').value + '\''");
        $objAxpDB->Lookup( "ftdocument_no", "SELECT ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3 FROM txpurchase_order_hdr ", "ftpo_no", "fdtrans_date", -1,true,"' AND fnpo_type3=\''+ document.getElementById('fnpo_type3').value + '\'  AND ftsupplier_code=\''+ document.getElementById('ftstock_in_company').value + '\''");

    }
    
    $objAxpDB->SetStatus("id, fnstatus,ftcategory_code");

    if((!$_REQUEST['id']) && ($_REQUEST['id']!="")) {
      
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=pu004&ftstock_type_code=' + document.getElementById(\"ftstock_type_code\").value + '&ftcategory_code=' + document.getElementById(\"ftcategory_code\").value + '&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(); ";

                                       
            $objAxpDB->FormJS($strAutoNumber0);
            //$ajaxpo="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=fdschedule_delivery_date&index=_N_&ObjItem=ftuom_code_1D0_N_&ftpo_no='+ document.getElementById('ftdocument_no').value);";
            $objAxpDB->FormScript(2, "onChange=\"OnChangeDate_fdtrans_date();\"");
            //$ajaxGrid="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=stocktype&index=_N_&ftstock_in_company=' + document.getElementById('ftstock_in_company').value);";
            //$objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");
            //$ajaxGrid="{$ajaxpo}ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&index=_N_&objQty=ffformulaD0_N_&ObjItem=ftuom_code_1D0_N_&ftpo_no='+ document.getElementById('ftdocument_no').value + '&ftstock_in_company=' + document.getElementById('ftstock_in_company').value + '&fnpo_type3=' + document.getElementById('fnpo_type3').value);";
            
            $ajaxGrid="{$ajaxpo}ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftpo_no='+ document.getElementById('ftdocument_no').value + '&ftstock_type_code='+ document.getElementById('ftstock_type_code').value);";
            $objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");         
    } 

    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru");
    
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->FormLocked("1,1,1,0,1,0,0,0,0,0,0,1,1,0,1,1");
            $objAxpDB->GridLocked(0, '1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0');    
            $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,1,1');
            
            $objAxpDB->ButtonShow("reset, simpan, tutup");
    }
    
    elseif (($objAxpDB->GetStatus("fnstatus") == 1)||($objAxpDB->GetStatus("fnstatus") == 2) || ($objAxpDB->GetStatus("fnstatus") == 0)) {
            if(isset($_REQUEST['revisi'])) {
                    $objAxpDB->ButtonShow("simpan_revisi,tutup");
                    $objAxpDB->FormLocked("1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1");
                    $objAxpDB->GridLocked(0, '1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0');
                    $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,1,1');
                    
                    $objAxpDB->GridScript(0,11, $ajaxFormula);
                    $objAxpDB->GridScript(0,13, $ajaxFormula );                    
                    
            }
            else {  
                    //$objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,ftdescription, ftwarehouse_code, ftlocation_code,ffformula, fnqtypo,fnqtypo_base,fnqtysisapo,fnqtysisapo_base,'' as ftuom_code_po,fnqty,fnqty_base, ftuom_code,fnindex_po,fnqty_reject,fcselling_price FROM txstock_in_det ");
                    //$objAxpDB->GridCaption(0, "No., Kode Barang,Keterangan, Gudang, Lokasi, Formula, Qty PO, Qty PO Base, Qty Sisa PO, Qty Sisa PO Base, Satuan PO, Qty Masuk, Qty Masuk Base, Satuan,Index PO, Qty Reject, Harga Jual");
                    $objAxpDB->GridLocked(0, '1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1');
                    $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,1,1');
                                
                    $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
                    $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            }
    }

    elseif ($objAxpDB->GetStatus("fnstatus") == 3) {
            $objAxpDB->GridLocked(0, '1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1');
            $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,1,1');
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->ButtonShow("baru, tutup");
    }

    elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->GridLocked(0, '1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1');
            $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,1,0,1,1,1,1');
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->ButtonShow("baru, tutup");
    }
    
    if($_REQUEST['ajax']) {
		switch($_REQUEST['code']) {
			case "grid":
                                ob_start();
				$_REQUEST['id']=0.5;
                                $ssql="SELECT fnpo_type3
                                        FROM txpurchase_order_hdr
                                        WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
				$fnpo_type3=RetRstField($ssql);
                                $ftcurrency_code=RetRstField("SELECT ftcurrency_code FROM txpurchase_order_hdr WHERE ftpo_no='{$_REQUEST['ftpo_no']}'");
                                $fcrate=RetRstField("SELECT fcrate FROM txpurchase_order_hdr WHERE ftpo_no='{$_REQUEST['ftpo_no']}'");
                                
                                $tablename = "";
                                if($fnpo_type3==0) {
                                    $tablename = "_bahan_baku";
                                    $fnwarehouse_type = '3';
                                } elseif ($fnpo_type3==1) {
                                    $tablename = "_setengah_jadi";
                                    $fnwarehouse_type = '2';
                                } elseif ($fnpo_type3==2) {
                                    $tablename = "_sparepart";
                                    $fnwarehouse_type = '4';
                                } elseif ($fnpo_type3==3) {
                                    $tablename = "";
                                    $fnwarehouse_type = '1';
                                }

                                $ssql="SELECT  DISTINCT a.fnindex, a.ftitem_code,a.ftdescription, '' AS ftwarehouse_code, '' AS ftlocation_code,
                                            a.ffformula AS ffformula,a.fnqty AS fnqtypo,a.fnqty_base AS fnqtypo_base,
                                            round(a.fnqty-IFNULL(a.fnqty_accept,0),2) AS fnqtysisapo,round(a.fnqty_base-IFNULL(a.fnqty_accept_base,0),2) AS fnqtysisapo_base, a.ftuom_code as ftuom_code_po,
                                            (a.fnqty_base-a.fnqty_base) AS fnqty, (a.fnqty_base-a.fnqty_base) AS fnqty_base, a.ftuom_code,
                                            a.fnindex AS  fnindex_po, 1*0 as fnqty_reject, (a.fcprice * d.fcrate) AS fcprice, e.fcselling_price
                                        FROM txpurchase_order_det a
                                        INNER JOIN txpurchase_order_hdr d on a.ftpo_no = d.ftpo_no 
                                        LEFT JOIN tlstock_hdr{$tablename} b ON a.ftitem_code=b.ftitem_code
                                        LEFT JOIN tlstock_group{$tablename} c ON b.ftstock_group_code=c.ftstock_group_code
                                        LEFT JOIN tlselling_price e on a.ftitem_code = e.ftitem_code and e.ftcurrency_code = 'IDR'
                                        LEFT JOIN txstock_in_det z ON 1=0
                                        WHERE a.ftpo_no='".$_REQUEST['ftpo_no']."' AND a.fnstatus != 5
                                                AND ((a.fnqty_base-IFNULL(a.fnqty_accept_base,0)>0) OR (a.fnqty_limit-IFNULL(a.fnqty_accept_base,0)>0))
                                                AND fnpo_type3='{$fnpo_type3}'
                                        ORDER BY a.fnindex #";
                                               
                                $objAxpDB->GridSQL(0,$ssql);
                                $objAxpDB->GridCaption(0, "No., Kode Barang,Keterangan, Gudang, Lokasi, Formula, Qty PO, Qty PO Base, Qty Sisa PO, Qty Sisa PO Base, Satuan PO, Qty Masuk, Qty Masuk Base, Satuan,Index PO, Qty Reject, Harga Beli (IDR), Harga Jual (IDR)");
                                $objAxpDB->GridLocked(0, '1,1,0,0,0,1,1,1,1,1,1,0,1,1,1,0,1,0');
                                $objAxpDB->GridHidden(0, '0,0,0,0,0,1,0,1,0,1,1,0,0,0,1,1,1,1');
                                $objAxpDB->GridSize(0, '3,10,10,5,5,,10,10,10,10,10,10,10,5,,,10,10');
                                    
                                // $ajaxUom="ajax_do('pembelian.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 + '&ftstock_type_code=' + document.getElementById('ftstock_type_code').value + '&index=_N_');";
                                // $ajaxGudangUOM="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=ffformulaD0_N_&ObjItem=ftuom_codeD0_N_&ftpo_no='+ document.getElementById('ftdocument_no').value+'&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value +'');{$ajaxUom}\"";
                                $ajaxGudangUOM="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=ffformulaD0_N_&ObjItem=ftuom_codeD0_N_&ftpo_no='+ document.getElementById('ftdocument_no').value+'&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value +'');\"";

                                $objAxpDB->GridScript(0,3, $ajaxGudangUOM);
                                $objAxpDB->GridScript(0,11, $ajaxFormula);
                                $objAxpDB->GridScript(0,13, $ajaxFormula );

                                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr{$tablename} $m", "ftitem_code", "ftitem_name",-1,false);
                                $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnstatus = 1 ", "ftwarehouse_code", "ftwarehouse_name",-1,true);
				$objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true,"' AND ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
                                //$objAxpDB->Lookup("ftuom_code_po", "SELECT ftuom_code, ftuom_name FROM tluom ", "ftuom_code", "ftuom_name", -1,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();
                                StockMasukRecalQtyPembelian();
                                // StockMasukPembelian_warehouse_onchange();
                    
                                document.getElementById('ftcurrency_code').value='<?php echo $ftcurrency_code; ?>';
                                document.getElementById('fcrate').value='<?php echo $fcrate; ?>';
                                ElementCurrencyALL();
                                <?php

                                

			break;
		}
            die("");
            exit;
    }
    
    if(isset($_REQUEST['id'])&&($_REQUEST['id'])!=0) {
        $ssql="SELECT ftstock_type_code
                FROM txstock_in_hdr
                WHERE fnid='{$_REQUEST['id']}'";
        $ftstock_type_code=RetRstField($ssql);

        if($ftstock_type_code=='invbb') {
            $tablename = "_bahan_baku";
        } elseif ($ftstock_type_code=='invsj') {
            $tablename = "_setengah_jadi";
        } elseif ($ftstock_type_code=='invsp') {
            $tablename = "_sparepart";
        } elseif ($ftstock_type_code=='inv') {
            $tablename = "";
        }
        
        $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr{$tablename} $m", "ftitem_code", "ftitem_name",-1,false);
        $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,ftdescription, ftwarehouse_code, ftlocation_code,ffformula, fnqtypo,fnqtypo_base,fnqtysisapo,fnqtysisapo_base,ftuom_code as ftuom_code_po,fnqty,fnqty_base, ftuom_code,fnindex_po,fnqty_reject,fcprice,fcselling_price FROM txstock_in_det");
        $objAxpDB->GridCaption(0, "No., Kode Barang,Keterangan, Gudang, Lokasi, Formula, Qty PO, Qty PO Base, Qty Sisa PO, Qty Sisa PO Base, Satuan PO, Qty Masuk, Qty Masuk Base, Satuan,Index PO, Qty Reject, Harga Beli (IDR), Harga Jual (IDR)");
        //$ajaxGudang="OnChange=\"ajax_do('pembelian.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);
        //$js1="OnChange=\"StockMasukRecalQtyPembelianMulti();StockMasukRecalQtyPembelian();\"";
        //$objAxpDB->GridScript(0,11, $js1);

    }
    
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();
    
?>
    