<?php
    Getsecurity("pembelian_purchase_order_local");

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

    if($_REQUEST['ajax']) {

            switch($_REQUEST['code']) {
                    case "transno":
                            if(RetRstField("SELECT fnid FROM txpurchase_order_hdr WHERE ftpo_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 "termpayment":
                            $ssql="select ifnull(ftterm_of_payment,'') as ftterm_of_payment from tlsupplier  where ftsupplier_code='{$_REQUEST['ftsupplier_code']}' ";
                            $ftterm_payment=RetRstField($ssql);
                            
                            if($ftterm_payment=="") {
                                $ftterm_payment="0";
                            }

                            ?>
                                document.getElementById('ftterm_payment').value='<?php echo ($ftterm_payment); ?>';
                            <?php

                            /*
                            $ssql="SELECT DATE_ADD('".CDate($_REQUEST['fdtrans_date'])."',INTERVAL {$ftterm_payment} DAY)";
                            ?>
                                    document.getElementById('fddue_date').value='<?php echo CDateRev(RetRstField($ssql)); ?>';
                            <?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 "item":
                            $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);
                                                    } ";
                            }

                            $fcprice=RetRstField("SELECT fcbuying_price FROM tlbuying_price WHERE ftitem_code='{$_REQUEST['ftitem_code']}' and ftstock_type_code = '{$ftstock_type_code}' and ftcurrency_code = '{$_REQUEST['ftcurrency_code']}' ");
                            $ffdiscount=RetRstField("SELECT ffbuying_discount FROM tlbuying_price  WHERE ftitem_code='{$_REQUEST['ftitem_code']}' and ftstock_type_code = '{$ftstock_type_code}' and ftcurrency_code = '{$_REQUEST['ftcurrency_code']}' ");
                            $ffdefault='1';

                            ?>
                            document.getElementById('fcpriceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fcprice; ?>';
                            document.getElementById('ffdiscountD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffdiscount; ?>';
                            document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftUOM_Code1; ?>';
                            document.getElementById('ffformulaD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffdefault; ?>';
                            document.getElementById('fnstatusD0<?php echo $_REQUEST['index']; ?>').value='1';

                            PurchaseOrderLocalRecalPrice();
                            PurchaseOrderLocalRecalDiscPercent();
                            ElementCurrencyALL();
                            <?php

                            die("");
                            break;

                    case "pr_item":
                            
                            ?>
                            ajax_do('pembelian.php?form=<?php echo $_REQUEST['form']?>&mode=<?php echo $_REQUEST['mode']?>&id=0&ajax=1&code=order&ftproses_pr=' + document.getElementById('ftproses_pr').value + '&ftitem_code_pr=' + document.getElementById('ftitem_code_pr').value + '&fnpo_type3=' + document.getElementById('fnpo_type3').value + '&ftcurrency_code=' + document.getElementById('ftcurrency_code').value);
                            <?php

                            die("");
                            break;
                   case "idr":
                        $ssql="SELECT fcrate FROM tlcurrency_hdr WHERE ftsimbol='{$_REQUEST["ftcurrency_code"]}'";
                        $mysql=mysql_query($ssql) or die ($ssql);
                        $r=mysql_fetch_assoc($mysql);
                        ?>
                        var i=document.getElementsByName('fnindexD0[]').length;

                        for(var j=0;j < i;j++) {
                            ajax_do('pembelian.php?form=<?php echo $_REQUEST['form']; ?>&mode=<?php echo $_REQUEST['mode']; ?>&id=0&ajax=1&code=item&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0' + j + '&ftitem_code=' + document.getElementsByName('ftitem_codeD0[]')[j].value + '&index=' + j + '&fnpo_type3='+ document.getElementById('fnpo_type3').value);
                        }

                        PurchaseOrderLocalRecalPrice();
                        PurchaseOrderLocalRecalDiscPercent();
                            
                        ElementCurrencyALL();
                        document.getElementById('fcrate').value='<?php echo number_format($r["fcrate"], 2); ?>';
                        <?php
                        break;
                  case "hdr_category":
                          if($_REQUEST['fnhdr_category']==2){
                        ?>
                          document.getElementById('fnpo_type3').value='3';
                          ajax_do_grid0('pembelian.php?form=<?php echo $_REQUEST['form'] ;?>&mode=<?php echo $_REQUEST['mode'] ;?>&id=0&ajax=1&code=grid&fnpo_type3=3&fnhdr_category=<?php echo $_REQUEST['fnhdr_category'] ;?>');
                          ajax_do('pembelian.php?form=<?php echo $_REQUEST['form'] ;?>&mode=<?php echo $_REQUEST['mode'] ;?>&id=0&ajax=1&code=purchaserequest&fnpo_type3=3&fnhdr_category=<?php echo $_REQUEST['fnhdr_category'] ;?>');
                      <?php
                          }else{
                        ?>
                          ajax_do_grid0('pembelian.php?form=<?php echo $_REQUEST['form'] ;?>&mode=<?php echo $_REQUEST['mode'] ;?>&id=0&ajax=1&code=grid&fnpo_type3=<?php echo $_REQUEST['fnpo_type3'];?>&fnhdr_category=<?php echo $_REQUEST['fnhdr_category'] ;?>');
                          ajax_do('pembelian.php?form=<?php echo $_REQUEST['form'] ;?>&mode=<?php echo $_REQUEST['mode'] ;?>&id=0&ajax=1&code=purchaserequest&fnpo_type3=<?php echo $_REQUEST['fnpo_type3'];?>&fnhdr_category=<?php echo $_REQUEST['fnhdr_category'] ;?>');
                      <?php
                          }
                    break;
                  case "typestock":
                        if($_REQUEST["fnhdr_category"] == 2 && $_REQUEST["fnpo_type3"]!=3){
                        ?>
                          document.getElementById('fnhdr_category').value='0';
                      <?php
                        }
                    break;
            }
    }

    function GetRequest() {

        $x="";
                    
        $ssql="SELECT a.ftbranch_code,a.ftpr_no,a.fnpr_type,c.fttrans_status_name as ftpr_type_name,a.fdtrans_date,b.fnindex,b.ftitem_code,b.fnqty-ifnull(b.fnqty_accept,0) as fnqty
                    FROM txpurchase_request_hdr a inner join txpurchase_request_det b
                    ON a.ftpr_no = b.ftpr_no inner join tstransaction_status c
                    ON a.fnpr_type = c.fntrans_status_code
                    WHERE a.ftbranch_code='{$_SESSION['strbranch']}' AND a.fnstatus=1 and b.fnstatus=1 AND fntrans_group_code = 35 AND fnpr_type=".$_REQUEST['fnpo_type3'];
        //echo $ssql;
        
        $rs=mysql_query($ssql);
        $x.= "<BR>
            <table class='ewTable' id='TransactionDetail2' name='TransactionDetail2' border='0' width='100%' cellspacing='0' celpadding='0'>
            <tr class='ewTableHeader'><td colspan=7>Purchase Request</td></tr>
            <tr class='ewTableHeader'>
             <td  valign='top'>No</th>
             <td  valign='top'>No. PR</th>
             <td  valign='top'>Tipe PR</th>
             <td  valign='top'>Tgl. PR</th>
             <td  valign='top'>Kode Barang</th>
             <td  valign='top'>Qty</th>
             <td  valign='top'>#</th>
            </tr>";

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

            $x.= "<tr class='$class1'>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='fnindexD2[]' id='fnindexD2[]' value='{$j}' size=3></td>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='ftpr_noD2[]' id='ftpr_noD2[]' value='" . $r['ftpr_no'] . "'  size=25></td>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='hidden' name='fnpr_typeD2[]' id='fnpr_typeD2[]' value='" . $r['fnpr_type'] . "'  size=25>
                                        <input readonly style='background-color: #E0E0E0'  type='text' name='ftpr_type_nameD2[]' id='ftpr_type_nameD2[]' value='" . $r['ftpr_type_name'] . "'  size=25></td>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='fdtrans_dateD2[]' id='fdtrans_dateD2[]' value='" . $r['fdtrans_date'] . "'   size=20></td>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='ftitem_codeD2[]' id='ftitem_codeD2[]' value='" . $r['ftitem_code'] . "'  size=20></td>
                    <td class='$class2'><input readonly style='background-color: #E0E0E0'  type='text' name='fnqtyD2[]' id='fnqtyD2[]' value='" . $r['fnqty'] . "'  size=50></td>
                    <td class='$class2'><input type='checkbox' name='ftpr_listD2[]' id='ftpr_listD2[]' value=\"{$r['ftpr_no']}|{$r['ftitem_code']}\"</td>
                  </tr>
                  ";

            $i++;
         }

         $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=pr_item&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value);";
         $ajaxPR="  document.getElementById('ftproses_pr').value = '';
                    document.getElementById('ftitem_code_pr').value = '';
                    for (var idx=0; idx < document.getElementsByName('ftpr_listD2[]').length; idx++)
                            {   
                                 if (document.getElementsByName('ftpr_listD2[]')[idx].checked) {
                                    var vprlist = document.getElementsByName('ftpr_listD2[]')[idx].value;
                                    var res = vprlist.split('|',2);
                                    document.getElementById('ftproses_pr').value=document.getElementById('ftproses_pr').value+',\'' + res[0] + '\'';
                                    document.getElementById('ftitem_code_pr').value=document.getElementById('ftitem_code_pr').value+',\'' + res[1] + '\'';
                                 }
                            }";
         $ajax1=" OnClick=\"{$ajaxPR}{$ajaxItem}\"";

         $x.= " <tr class='$class1'>
                    <td class='$class2' colspan=7 align='right'><input type=button name='proses_pr' id='proses_pr' value='Proses PR' {$ajax1}></td>
                             <input type='hidden' name='ftproses_pr' id='ftproses_pr' value=''>
                             <input type='hidden' name='ftitem_code_pr' id='ftitem_code_pr' value=''></td>
                    </td></tr>
                </table>
                <input id='TransactionDetailCount2' name='TransactionDetailCount2' value='$num_rows' type='hidden'>";

         return $x;
    }

    $objAxpDB->ViewName("Daftar Purchase Order Local");

    $objAxpDB->ViewSQL("select a.fnid,a.fdtrans_date, a.ftpo_no,e.fttrans_status_name as fttype,
                        b.ftsupplier_name, a.fcamount, a.ftcurrency_code, a.ftnotes, d.fttrans_status_name as ftshipment_status,
                        c.fttrans_status_name as ftstatus
                        from txpurchase_order_hdr a
                        left join tlsupplier b on a.ftsupplier_code = b.ftsupplier_code
                        left join tstransaction_status c on a.fnstatus = c.fntrans_status_code and c.fntrans_group_code = 2
                        left join tstransaction_status d on a.fnreceipt_status = d.fntrans_status_code and d.fntrans_group_code = 18
                        left join tstransaction_status e on a.fnpo_type3 = e.fntrans_status_code and e.fntrans_group_code = 35
                        where a.fnpo_type=0
                        AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                        order by a.fdtrans_date desc, a.ftpo_no desc");
    $objAxpDB->ViewCaption("ID, Tgl. PO, No. PO, Tipe PO, Nama Supplier, Jumlah, Mata Uang, Catatan, Status Kirim, Status");
    $objAxpDB->ViewPeriod(TRUE);
    
    $objAxpDB->FormName("Purchase Order Local");
    $objAxpDB->FormSQL("SELECT  ftpo_no, fnpo_type3, fnpo_typelpb, ftsupplier_code, ftship_address, fnhdr_print,
                        fnhdr_category, ftreference, ftdo_no, fdtrans_date, ftterm_payment,
                        fddelivery_date, ftcurrency_code, fcrate, fnreceipt_status, fntax_code,
                        ftnotes, fdclosing_date, fdcancel_date, ftmemo_no_cancel, ftreason_cancel,
                        fcsub_amount, ffdiscount, fcdiscount, fcother_cost, ffvat,
                        fcvat, fcamount, fnstatus,'0' AS fnpo_type
                        FROM txpurchase_order_hdr");
    $objAxpDB->FormCaption("No. PO, Tipe PO,Tipe LPB, Supplier, Alamat, Header Cetakan, Kategori PO, No. Reff, No. DO,
                            Tgl. PO, Syarat Pembayaran, Tgl. Kirim,Mata Uang, Kurs, Status Pengiriman,Kode Pajak,
                            Catatan,Tgl. Closing/Final, Tgl Cancel/Batal,No. Memo Batal, Alasan batal,
                            Sub Total, Disc (%), Disc,Biaya Lain-Lain,Pajak (%), Pajak,Grand Total, Status,
                            Type PO");
    $objAxpDB->FormPosition("TL, TL,  HD, TL, TL, TL,TL,TR,HD,TR, TR, HD, TR, TR, TR, TR,BL,BL,BL,BL,BL,BR, BR, BR, BR, BR,BR, BR, BR,HD");
    $objAxpDB->FormLayout(10,"&nbsp;Hari <input type='hidden' id='ftitem_supplier' name='ftitem_supplier'>");
    
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,fnqty,ffformula,fnqty_base,fnqty_limit, CAST(fnqty-ifnull(fnqty_accept,0) AS DECIMAL(19,2)) as fnqty_accept,CAST(fnqty_base-ifnull(fnqty_accept_base,0) AS DECIMAL(19,2)) as fnqty_accept_base, ftuom_code, fcprice, ffdiscount, fcdiscount, fcamount, fnvat,ftdescription,fdclosing_date,fnstatus FROM txpurchase_order_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty,Qty Formula, Qty Base,Qty Limit Outsource,Qty Sisa,Qty Sisa Base, Satuan, Harga, Discount(%),Discount,Jumlah, Include Pajak, Keterangan, Tgl. Status, Status");

    $objAxpDB->GridName(1, "Schedule Delivery");
    $objAxpDB->GridSQL(1, "SELECT fnindex,fdschedule_delivery_date, fnlock FROM txpurchase_order_schedule_delivery");
    $objAxpDB->GridCaption(1, "No., Schedule Delivery, Sudah Dikirim");

    if($_REQUEST['id']=='0') {
        $where_ftsupplier_Code=" WHERE fnstatus=1 and fnsupplier_type=0 ";
        $where_ftitem_code=" WHERE fnstatus=1";

        $objAxpDB->GridName(2, "Purchase Request");
        $objAxpDB->GridSQL(2, "select '','','','' from txpurchase_order_det ");
        $objAxpDB->GridCaption(2, "No., No. PR, Tipe PR, Tgl. PR");
        $objAxpDB->GridLocked(2,"1,1,1,1");
        $objAxpDB->GridAutoAdd(2, False);
    } 

    $ajaxKurs="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=idr&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);";
    
    if(!$_REQUEST['id'])
    {
                $strAutoNumber1="if(document.getElementById('fntax_code').value=='1')
                                    {
                                            ajax_do('query2.php?code=2&ftmodule_code=pu002&fnpo_typelpb=' + document.getElementById(\"fnpo_typelpb\").value + '&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftpo_no&no_trans=ftpo_no&table_name=txpurchase_order_hdr&type_po=local&fnpo_type3=' + document.getElementById(\"fnpo_type3\").value + '&vat=' + document.getElementById(\"fntax_code\").value + '&ftbranch_code={$_SESSION['strbranch']}');
                                    }
                                    else
                                    {
                                            ajax_do('query2.php?code=2&ftmodule_code=pu003&fnpo_typelpb=' + document.getElementById(\"fnpo_typelpb\").value + '&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftpo_no&no_trans=ftpo_no&table_name=txpurchase_order_hdr&type_po=local&fnpo_type3=' + document.getElementById(\"fnpo_type3\").value + '&vat=' + document.getElementById(\"fntax_code\").value + '&ftbranch_code={$_SESSION['strbranch']}');
                                    }";
               

		$strAutoNumber0=" function OnChangeDate_fdtrans_date()
                                    {
                                            {$strAutoNumber1}
                                    }

                                    try
                                    {
                                            document.getElementById('ftterm_payment').value='0';
                                    }

                                    catch (er)
                                    {

                                    }

                                    document.getElementById('fnpo_type').value='0';                                  
                                    OnChangeDate_fdtrans_date();
                                    document.getElementById('ftcurrency_code').value='IDR';
                                            {$ajaxKurs}
                                    ElementCurrencyOnBlur(document.getElementById('fcrate'));
                                    ";
                                            
		$objAxpDB->FormJS($strAutoNumber0);
    } else {
            $objAxpDB->FormJS("ElementCurrencyALL();");
    }
        
    $objAxpDB->Lookup("ftsupplier_code", "SELECT ftsupplier_Code, ftsupplier_name FROM tlsupplier {$where_ftsupplier_Code} ORDER BY ftsupplier_Code ", "ftsupplier_Code", "ftsupplier_name", -1, False);
    $objAxpDB->Lookup("fnhdr_print", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 57", "fntrans_status_code", "fttrans_status_name",0, True);
    $objAxpDB->Lookup("fnhdr_category", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 58", "fntrans_status_code", "fttrans_status_name",0, 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",0, True);
    $objAxpDB->Lookup("fnreceipt_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 18", "fntrans_status_code", "fttrans_status_name", 1, True);
    $objAxpDB->Lookup("fntax_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 15", "fntrans_status_code", "fttrans_status_name", 1,true);
    $objAxpDB->Lookup("fnpo_type3", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 35 AND fntrans_status_code='3'", "fntrans_status_code", "fttrans_status_name", 0,true);
    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
    
    $objAxpDB->LookupGrid(0,"fnvat", "SELECT '1' AS A,'TRUE' AS B UNION ALL SELECT '0' AS A,'FALSE' AS B ", "A", "B", 1,true);
    $objAxpDB->LookupGrid(0,"fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2 and (fntrans_status_code=1 or fntrans_status_code=9 or fntrans_status_code=5)", "fntrans_status_code", "fttrans_status_name",1, True);
    $objAxpDB->LookupGrid(1,"fnlock", "SELECT '1' AS A,'SUDAH' AS B UNION ALL SELECT '0' AS A,'BELUM' AS B ", "A", "B", 1,true);

    $ajaxAddress="ajax_do('query2.php?code=bb6&ftsupplier_code=' + this.value + '&objAddress=ftship_address');";
    $ajaxtermpayment="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=termpayment&ftsupplier_code='+ document.getElementById('ftsupplier_code').value + '&fttrans_date='+ document.getElementById('fdtrans_date').value);";
    $ajaxGrid1="ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&fnhdr_category='+ document.getElementById('fnhdr_category').value);";
    $ajaxGrid2="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=hdr_category&fnhdr_category='+ this.value + '&fnpo_type3=' + document.getElementById('fnpo_type3').value);";
    $ajaxGrid3="ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=typestock&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&fnhdr_category='+ document.getElementById('fnhdr_category').value);";
    $ajaxPR="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=purchaserequest&fnpo_type3='+ document.getElementById('fnpo_type3').value);";

    $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid3}{$ajaxGrid1}{$ajaxPR}{OnChangeDate_fdtrans_date()}\"");
    //$objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid1}{$ajaxPR}{OnChangeDate_fdtrans_date()}\"");
    $objAxpDB->FormScript(2,"onChange=\"OnChangeDate_fdtrans_date();\"");
    $objAxpDB->FormScript(3,"onChange=\"{$ajaxtermpayment}{$ajaxAddress}\"");
    $objAxpDB->FormScript(6,"onChange=\"{$ajaxGrid2}\"");
    $objAxpDB->FormScript(15,"onChange=\"OnChangeDate_fdtrans_date();\"");
    $objAxpDB->FormScript(22, "onChange=\"PurchaseOrderLocalRecalDiscPercent();\"");
    $objAxpDB->FormScript(23, "onChange=\"PurchaseOrderLocalRecalDiscRp();\"");
    $objAxpDB->FormScript(24, "onChange=\"PurchaseOrderLocalRecalQty0();\"");
    $objAxpDB->FormScript(25, "onChange=\"PurchaseOrderLocalRecalQty0();\"");
    $objAxpDB->FormScript(12, "OnChange=\"{$ajaxKurs}\"");
    
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
    $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_');";
    $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");
    
    //$ajax1=" onChange=\"PurchaseOrderLocalRecalPrice();PurchaseOrderLocalRecalDiscPercent();\"  ";
//    $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_&fnpo_type3='+ document.getElementById('fnpo_type3').value');";
//    $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");
    $ajax1=" onChange=\"PurchaseOrderLocalRecalDiscDetailPercent(_N_);PurchaseOrderLocalRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,2, $ajax1);
    $objAxpDB->GridScript(0,3, $ajax1);
    $ajaxFormula="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&fnpo_type3='+ document.getElementById('fnpo_type3').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);\"";
    $objAxpDB->GridScript(0,8, $ajaxFormula );
    $objAxpDB->GridScript(0,9, $ajax1);
    $objAxpDB->GridScript(0,10, $ajax1);
    $ajax1="   onChange=\"PurchaseOrderLocalRecalDiscDetailRp(_N_);PurchaseOrderLocalRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,11, $ajax1);
    $ajax1="   onChange=\"PurchaseOrderLocalRecalDiscDetailPercent(_N_);PurchaseOrderLocalRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,12, $ajax1);
    $ajax1="   onChange=\" if((document.getElementById('fnstatusD0_N_').value=='9')&&(document.getElementById('fnqtyD0_N_').value.replace(/,/g,'')*1==document.getElementById('fnqty_acceptD0_N_').value.replace(/,/g,'')*1 )) { document.getElementById('fcpriceD0_N_').value=0; PurchaseOrderLocalRecalDiscDetailRp(_N_); }\"  ";
    $objAxpDB->GridScript(0,13, $ajax1);
    $ajax1=" onChange=\"PurchaseOrderLocalRecalPrice();\"  ";
    $objAxpDB->GridScript(0,16, $ajax1);
    $objAxpDB->GridDelJS(0,"PurchaseOrderLocalRecalQty0();");

    $objAxpDB->SetStatus("id, fnreceipt_status, fnstatus,fntax_code");

    if ($objAxpDB->GetStatus("id") == "") 
    {
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel, tutup");
    } 
    elseif ($objAxpDB->GetStatus("id") == 0)
    {
            $objAxpDB->FormLocked("0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,0,0,0,0,1,1,1");
            $objAxpDB->GridLocked(0,"1,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0");
            $objAxpDB->GridHidden(0,"0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->GridLocked(1,"1,0,0");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE);
    } 
    elseif ($objAxpDB->GetStatus("fnstatus") == 0)
    {
        if(isset($_REQUEST['revisi'])) 
        {
            $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
            $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_');";
            $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");

            if($objAxpDB->GetStatus("fnreceipt_status")==1)
            {
                $objAxpDB->FormLocked("1,1,1,1,0,0,0,0,1,1,0,1,0,0,1,0,0,1,1,1,1,1,0,0,0,0,1,1,1");
                $objAxpDB->GridSQL(0,"SELECT fnindex, ftitem_code,fnqty,ffformula,fnqty_base, fnqty_limit, fnqty-ifnull(fnqty_accept,0) as fnqty_accept,fnqty_base-ifnull(fnqty_accept_base,0) as fnqty_accept_base, ftuom_code, fcprice, ffdiscount, fcdiscount, fcamount, fnvat,ftdescription,fdclosing_date,fnstatus FROM txpurchase_order_det");
                $objAxpDB->GridLocked(0,"1,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0");
                $objAxpDB->GridLocked(1, "1,0,0");
                $objAxpDB->GridHidden(0,"0,0,0,1,1,1,0,1,0,0,0,0,0,1,0,1,1");
                $objAxpDB->GridAllowAddNew(0, TRUE);
                $objAxpDB->GridAllowDelete(0, TRUE); 
            }
            else
            {
                $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1");
                $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,fnqty,ffformula,fnqty_base, fnqty_limit, fnqty-ifnull(fnqty_accept,0) as fnqty_accept, fnqty_base-ifnull(fnqty_accept_base,0) as fnqty_accept_base, ftuom_code, fcprice, ffdiscount, fcdiscount, fcamount, fnvat,ftdescription,fdclosing_date,fnstatus  FROM txpurchase_order_det");
                $objAxpDB->GridLocked(0,"1,1,1,1,1,1,1,1,1,1,1,1,1,1,0");
                $objAxpDB->GridHidden(0, "0,0,0,1,1,1,0,1,0,0,0,0,0,1,0,1,1");
                $objAxpDB->GridLocked(1, "1,1,1");
                $objAxpDB->GridAllowAddNew(0, FALSE);
                $objAxpDB->GridAllowDelete(0, FALSE);
            }

            $objAxpDB->ButtonShow("simpan_revisi,tutup");
        }
        else 
        {
            $fntax_code="cetak, cetak_non_harga, ";

            if($objAxpDB->GetStatus("fnreceipt_status") == 3)
            {
                    $objAxpDB->ButtonShow("{$fntax_code}baru, batal, tutup");
                    $objAxpDB->GridLocked(1,"1,1,1,1");
            }
            else
            {
                    $objAxpDB->ButtonShow("{$fntax_code}baru, update, update_schedule_delivery,  final, batal, tutup");
                    $objAxpDB->GridLocked(1,"1,0,0");
            }

            $objAxpDB->FormLocked("1,1,1,1,0,0,0,0,1,1,0,1,0,0,1,0,0,1,1,1,1,1,0,0,0,0,1,1,1");
            $objAxpDB->GridLocked(0,"1,0,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0");
            $objAxpDB->GridHidden(0,"0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE);


        }
    } 
    elseif ($objAxpDB->GetStatus("fnstatus") == 1)
    {
            if($objAxpDB->GetStatus("fnreceipt_status") == 3)
            {
                    $objAxpDB->ButtonShow("baru, cetak, cetak_non_harga, update_schedule_delivery, batal, tutup");
            }
            // Status item masih bisa dirubah manual menjadi close kalau diperlukan
            else
            {
                    $objAxpDB->ButtonShow("baru, cetak, cetak_non_harga, close_po, update_schedule_delivery, batal, tutup");
            }

            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,1,1,1,1");
            $objAxpDB->GridHidden(0,"0,0,0,1,1,1,0,1,0,0,0,0,0,1,0,0,0");
            $objAxpDB->GridLocked(1,"1,0,0");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 2) 
    {
            $objAxpDB->ButtonShow("baru, cetak, cetak_non_harga, tutup");
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,1,1,1,1");
            $objAxpDB->GridHidden(0,"0,0,0,1,1,1,0,1,0,0,0,0,0,1,0,0,0");
            $objAxpDB->GridLocked(1,"1, 1, 1");
    }
    elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 5) || ($objAxpDB->GetStatus("fnstatus") == 9))
    {
            $objAxpDB->ButtonShow("baru, tutup");
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,1,1,1,1");
            $objAxpDB->GridHidden(0,"0,0,0,1,1,1,0,1,0,0,0,0,0,1,0,0,0");
            $objAxpDB->GridLocked(1,"1, 1, 1");
    }

    if($_REQUEST['ajax'])
    {
		switch($_REQUEST['code']) 
		{
                    case "grid":
                        ob_start();
                        $_REQUEST['id']=0;

                        $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
                        $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_');";
                        $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");

                        $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,fnqty,ffformula,fnqty_base,fnqty_limit, fnqty-ifnull(fnqty_accept,0) as fnqty_accept,fnqty_base-ifnull(fnqty_accept_base,0) as fnqty_accept_base, ftuom_code, fcprice, ffdiscount, fcdiscount, fcamount, fnvat,ftdescription,fdclosing_date,fnstatus FROM txpurchase_order_det");
                        $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;
                            RowBuild0();
                        <?php
                        break;

                     case "purchaserequest":

                            $html = GetRequest();

                            ?>
                            div = document.getElementById('txtHint2');
                            var m='<?php echo js_esc($html); ?>';
                            m= m.replace(/^\s*|\s*$/g,"");
                            div.innerHTML =m;
                            <!--reSize();-->
                            <?php
                           

                       break;

                       case "order":
                            $_REQUEST['id']=0.5;
                                
                            ob_start();
                           
                            $ssql="SELECT a.fnid as fnindex, a.ftitem_code, SUM(a.fnqty) AS fnqty ,ffformula,SUM(fnqty_base) AS fnqty_base  ,SUM(fnqty_base) AS fnqty_limit,
                                        0.0 AS fnqty_accept,0.0 AS fnqty_accept_base,
                                        a.ftuom_code AS ftuom_code,b.fcbuying_price AS fcprice,b.ffbuying_discount AS ffdiscount,0.0 AS fcdiscount,0.0 AS fcamount, 0 AS fnvat, a.ftdescription, DATE(NOW()) AS fdclosing_date,'1' AS fnstatus
                                    FROM txpurchase_request_det a INNER JOIN txpurchase_request_hdr c ON a.ftpr_no = c.ftpr_no
                                    LEFT JOIN tlbuying_price b
                                        ON a.ftitem_code = b.ftitem_code AND b.ftcurrency_code = '{$_REQUEST['ftcurrency_code']}'
                                    WHERE a.fnstatus = 1  
                                            AND (a.ftpr_no in ('0'{$_REQUEST['ftproses_pr']})) and (a.ftitem_code in ('0'{$_REQUEST['ftitem_code_pr']}))
                                            AND CASE c.fnpr_type WHEN '0' THEN  b.ftstock_type_code = 'invbb'
                                                                 WHEN '1' THEN  b.ftstock_type_code = 'invsj'
                                                                 WHEN '2' THEN  b.ftstock_type_code = 'invsp'
                                                                 WHEN '3' THEN  b.ftstock_type_code = 'inv'
                                                END     
                                    GROUP BY a.ftitem_code #";
                            // echo $ssql;
                            $objAxpDB->GridSQL(0,$ssql);
                            $objAxpDB->GridCaption(0, "No., Kode Barang, Qty,Qty Formula, Qty Base,Qty Limit Outsource,Qty Terima,Qty Terima Base, Satuan, Harga, Discount(%),Discount,Jumlah, Include Pajak, Keterangan, Tgl. Status, Status, Uom Code Hidden");
                            //$objAxpDB->GridAutoAdd(0, False);
   
                            $objAxpDB->GridLocked(0,"1,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0");
                            $objAxpDB->GridHidden(0,"0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0");
                            $objAxpDB->GridAllowAddNew(0, TRUE);
                            $objAxpDB->GridAllowDelete(0, TRUE);

                            $objAxpDB->LookupGrid(0,"ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr WHERE fnstatus=1 ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
                            $objAxpDB->LookupGrid(0,"fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2 and (fntrans_status_code=1 or fntrans_status_code=9 or fntrans_status_code=5)", "fntrans_status_code", "fttrans_status_name",1, True);

                            $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_&fnpo_type3='+ document.getElementById('fnpo_type3').value);";
                            $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");
                            $ajax1=" onChange=\"PurchaseOrderLocalRecalDiscDetailPercent(_N_);PurchaseOrderLocalRecalDiscPercent();\"  ";
                            $objAxpDB->GridScript(0,2, $ajax1);
                            $objAxpDB->GridScript(0,3, $ajax1);
                            $ajaxFormula="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&fnpo_type3='+ document.getElementById('fnpo_type3').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);\"";
                            $objAxpDB->GridScript(0,8, $ajaxFormula );
                            $ajax1=" onChange=\"PurchaseOrderLocalRecalDiscDetailPercent(_N_);PurchaseOrderLocalRecalDiscPercent();\"  ";
                            $objAxpDB->GridScript(0,9, $ajax1);
                            $ajax1=" onChange=\"PurchaseOrderLocalRecalDiscDetailPercent(_N_);\"   ";
                            $objAxpDB->GridScript(0,10, $ajax1);
                            $ajax1="   onChange=\"PurchaseOrderLocalRecalDiscDetailRp(_N_);\"  ";
                            $objAxpDB->GridScript(0,11, $ajax1);
                            $ajax1="   onChange=\"PurchaseOrderLocalRecalDiscDetailRp(_N_);\"  ";
                            $objAxpDB->GridScript(0,12, $ajax1);
                            $ajax1="   onChange=\" if((document.getElementById('fnstatusD0_N_').value=='9')&&(document.getElementById('fnqtyD0_N_').value.replace(/,/g,'')*1==document.getElementById('fnqty_acceptD0_N_').value.replace(/,/g,'')*1 )) { document.getElementById('fcpriceD0_N_').value=0; PurchaseOrderLocalRecalDiscDetailRp(_N_); }\"  ";
                            $objAxpDB->GridScript(0,13, $ajax1);
                            $ajax1=" onChange=\"PurchaseOrderLocalRecalPrice();\"  ";
                            $objAxpDB->GridScript(0,16, $ajax1);

                            $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;
                            TransactionDetailRowID0 =document.getElementsByName('fnindexD0[]').length;
                            RowIndex0();
                            <!--reSize();-->


                            var i=document.getElementsByName('fnindexD0[]').length;

                            for(var j=0;j < i;j++) {
                                ajax_do('pembelian.php?form=<?php echo $_REQUEST['form']; ?>&mode=<?php echo $_REQUEST['mode']; ?>&id=0&ajax=1&code=item&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0' + j + '&ftitem_code=' + document.getElementsByName('ftitem_codeD0[]')[j].value + '&index=' + j + '&fnpo_type3='+ document.getElementById('fnpo_type3').value);
                            }

                            PurchaseOrderLocalRecalPrice();
                            PurchaseOrderLocalRecalDiscPercent();

                            <?php
                    break;
                    
                }
                die("");
                exit;
    }
    
   if(isset($_REQUEST['id'])&&($_REQUEST['id'])!=0)
    {
        $ssql="SELECT fnpo_type3, fnhdr_category
                    FROM txpurchase_order_hdr
                    WHERE fnid='{$_REQUEST['id']}'";
        $mysql=mysql_query($ssql);
        $r=mysql_fetch_assoc($mysql);
        $fnpo_type3=$r["fnpo_type3"];
        $fnhdr_category=$r["fnhdr_category"];

        if ($fnpo_type3=="0")
            $objAxpDB->LookupGrid(0, "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
        elseif ($fnpo_type3=="1")   
            $objAxpDB->LookupGrid(0, "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
        elseif ($fnpo_type3=="2")
            $objAxpDB->LookupGrid(0, "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_sparepart ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
        elseif ($fnpo_type3=="3")
            $objAxpDB->LookupGrid(0, "ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);

        $ajaxItem="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&fnpo_type3='+ document.getElementById('fnpo_type3').value +'&ftcurrency_code='+ document.getElementById('ftcurrency_code').value +'&fcrate='+ document.getElementById('fcrate').value +'&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_');";
        $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");
    } 

    if($objAxpDB->GetStatus("fnstatus") == 1) 
    {
        if(isset($_REQUEST['revisi'])) 
        {
                $ssql="SELECT ftsupplier_code
                            FROM txpurchase_order_hdr
                            WHERE fnid='".$_REQUEST['id']."'";
                $ftsupplier_code=RetRstField($ssql);
           
                $ssql="select CASE WHEN ifnull(ftitem_supplier,'')='' THEN ftsupplier_code ELSE  ifnull(ftitem_supplier,'') END as ftitem_supplier
                            from tlsupplier
                            where ftsupplier_code='{$ftsupplier_code}' ";
                $ftitem_supplier=RetRstField($ssql);
                $strAutoNumber0="document.getElementById('ftitem_supplier').value='{$ftitem_supplier}';";
                $objAxpDB->FormJS($strAutoNumber0);
        }
    }

    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();