<?php
Getsecurity("pembelian_purchase_invoice_import");

if($_REQUEST['ajax']) {
    switch($_REQUEST['code']) {
        case "transno":
            if(RetRstField("SELECT fnid FROM txpurchase_invoice_hdr WHERE ftpurchase_invoice_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 "tgl":
            $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL ".intval($_REQUEST['ftterm_payment'])." DAY)";
            $date=RetRstField($ssql);
            ?>
            document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
            <?php
            break;
        case "tgltempo":
            $ssql="select date_add('".CDate($_REQUEST['fdtrans_date'])."',INTERVAL ".intval($_REQUEST['ftterm_payment'])." DAY)";
            $date=RetRstField($ssql);
            ?>
            document.getElementById('<?php echo $_REQUEST["fddue_date"]; ?>').value='<?php echo CDateRev($date); ?>';
            <?php
            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);
            ?>
            document.getElementById('fcrate').value='<?php echo number_format($r["fcrate"], 2); ?>';
            ElementCurrencyALL();
            <?php
            break;
        case "supplier":
            // $ssql="SELECT a.ftpo_no, a.fdtrans_date, a.ftsupplier_code, a.fnstatus FROM txpurchase_order_hdr a WHERE (a.fnstatus=1 OR a.fnstatus=2) AND a.fnpo_type=1 AND a.fnreceipt_status != 3 AND a.fnreceipt_status !=9 AND ftsupplier_code='{$_REQUEST['ftsupplier_code']}' ";
            // Purchase Invoice di Hutang Awal , qty tidak diperhitungkan sebagai penerimaan. Hanya digunakan utk informasi harga
            $ssql=" SELECT ftpo_no, fdtrans_date ,ftsupplier_code,fnpo_type3, ftreference 
                    FROM   (SELECT DISTINCT ftpo_no, fdtrans_date , ftsupplier_code, fnpo_type3, ftreference 
                            FROM (  SELECT a.ftpo_no, b.ftitem_code, a.fdtrans_date ,a.ftsupplier_code,a.fnpo_type3, a.ftreference, 
                                        COALESCE(SUM(d.fnqty_base),0) AS ttlqtyaccept, COALESCE(SUM(b.fnqty_base),0) AS ttlqtylimit 
                                    FROM txpurchase_order_hdr a LEFT JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no 
                                    LEFT JOIN txpurchase_invoice_hdr c ON c.ftpo_no = a.ftpo_no AND c.fnstatus != 3 AND c.fnstatus != 9 AND c.fnsaldo_awal = 0
                                    LEFT JOIN txpurchase_invoice_det d ON c.ftpurchase_invoice_no = d.ftpurchase_invoice_no AND b.ftitem_code=d.ftitem_code 
                                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnpo_type=1 
                                            AND a.ftbranch_code='{$_SESSION['strbranch']}' AND a.ftsupplier_code='{$_REQUEST['ftsupplier_code']}'
                                    GROUP BY a.ftpo_no, b.ftitem_code, a.fdtrans_date ,a.ftsupplier_code,a.fnpo_type3 
                                    HAVING  ttlqtyaccept < ttlqtylimit) X 
                            ORDER BY ftpo_no) a ";
            $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['ftpo_no']} -- {$r['fdtrans_date']} -- {$r['ftreference']}','{$r['ftpo_no']}'),null,null);
                               }
                           catch(ex) {
                                ObjSelect.add(new Option('{$r['ftpo_no']} -- {$r['fdtrans_date']} -- {$r['ftreference']}','{$r['ftpo_no']}'), $i, $i);
                           } ";
            }

            echo " try {
                        ObjSelect.selectedIndex=0;
                   } catch(ex) {

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

$objAxpDB->ViewName("Daftar Purchase Invoice Import");
// $objAxpDB->ViewTable("vPUPurchaseInvoiceImport");
$objAxpDB->ViewSQL("select a.fnid,a.fdtrans_date, a.ftpurchase_invoice_no,a.ftpo_no, a.ftsupplier_code,
                    b.ftsupplier_name,d.fttrans_status_name as ftshipment_status,
                     c.fttrans_status_name as ftstatus
                    from txpurchase_invoice_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.fnpayment_status = d.fntrans_status_code and d.fntrans_group_code = 1
                    where a.fnpo_type=1
                    AND (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                    order by a.fdtrans_date desc, a.ftpurchase_invoice_no desc");
$objAxpDB->ViewCaption("ID, Tgl. PI, No. PI,No. PO, Kode Supplier, Nama Supplier, Status Pembayaran, Status");
$objAxpDB->ViewPeriod(TRUE);

$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="ajax_do('query2.php?code=2&ftmodule_code=pu009&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftpurchase_invoice_no&no_trans=ftpurchase_invoice_no&table_name=txpurchase_invoice_hdr&ftbranch_code={$_SESSION['strbranch']}');";
        $ajaxTransDate="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById(\"fdtrans_date\").value + '&ftterm_payment='+ document.getElementById(\"ftterm_payment\").value);";
        $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1} {$ajaxTransDate}
                                 }
                                 OnChangeDate_fdtrans_date();
                                   // var i=document.getElementsByName('fnindexD0[]').length;

                              document.getElementById('fnpo_type').value='1';
                              document.getElementById('ftcurrency_code').value='IDR';
                              {$ajaxKurs}
                            ";

        $objAxpDB->FormJS($strAutoNumber0);
} else {
        // revisi
        if(isset($_REQUEST['revisi'])) {
            $ssql="select a.ftstock_in_no from txstock_in_hdr a inner join txpurchase_invoice_hdr b ON a.ftdocument_no = b.ftpurchase_invoice_no 
                    WHERE b.fnid = '{$_REQUEST['id']}' AND a.fnstatus !=3 AND a.fnstatus != 9  AND b.fnstatus !=3 AND b.fnstatus != 9 ";

            $ftstock_in_no = RetRstField($ssql);
        
            if ($ftstock_in_no != "") {
                $msg="alert('Sudah ada transaksi Penerimaan Barang. {$ftstock_in_no} harus dibatalkan terlebih dahulu.');window.history.back();";
                $objAxpDB->FormJS($msg);
            } else {
                $objAxpDB->FormJS("ElementCurrencyALL();");
            }
        
        } else {
            $objAxpDB->FormJS("ElementCurrencyALL();");
        }    
}

$objAxpDB->LookupMagicQuotes(True);

// Deklarasi Header & Footer
$objAxpDB->FormName("Purchase Invoice Import");
$objAxpDB->FormSQL("SELECT  ftpurchase_invoice_no, ftsupplier_code, ftstock_in_no, ftpo_no,
                                fdtrans_date,ftterm_payment, fddue_date,
                                ftcurrency_code,fcrate,fnpayment_status, fntax_code, ftnotes,
                                fcsub_amount,ffdiscount,fcdiscount,
                                fcother_cost,ffvat,fcvat,
                                fcamount,fnstatus,
                                '1' AS fnpo_type,
                                ftexpedition_code,fcexpedition_cost,fnexpedition_auto_inv,
                                fcreturn_amount,fcpayment_amount,fcoutstanding_amount
                        FROM txpurchase_invoice_hdr");
$objAxpDB->FormCaption("No. PI, Supplier, No. Terima, No. PO,
                            Tgl. PI, Syarat Pembayaran, Tgl. Jatuh Tempo, Mata Uang, Kurs, Status Pembayaran,Kode Pajak, Catatan,
                            Sub Total, Disc (%), Disc,Biaya Lain-Lain,Pajak (%), Pajak,Grand Total, Status,
                            Type PO, No Perkiraan Ekspedisi, Biaya Ekspedisi, Invoice Ekspedisi, Total Retur, Total Pembayaran, Sisa Pembayaran");

$objAxpDB->FormPosition("TL, TL, HD, TL, TR, TR, TR, TR, TR, TR, TL, BL, BR, BR, BR, BR, BR, BR, BR, BR,HD,BL,BL,HD,BL,BL,BL");
// $objAxpDB->FormLayout(5,"&nbsp;Hari");

// Deklarasi Detail
$objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fnqty, ftuom_code,ftwarehouse_code,ftlocation_code, fcprice, ffdiscount, fcdiscount, fcamount, fcprice_cogs,fnvat, fnqty_base, ffformula, fnqty_return FROM txpurchase_invoice_det");
$objAxpDB->GridCaption(0, "No., Kode Barang, Qty, Satuan,Gudang, Lokasi, Harga, Discount(%),Discount,Jumlah, Harga Modal,Include Pajak, Qty Base, Formula, Qty Retur");
$objAxpDB->GridHidden(0,"0,0,0,0,1,1,0,0,0,0,0,1,1,1,0");
$objAxpDB->GridSize(0, "5,5,5,5,10,10,10,5,10,10,10,5,5,5,5");
$objAxpDB->GridAutoAdd(0, False);

if(!$_REQUEST['id']) {
    //$ssql_ftstock_in_no="SELECT ftstock_in_no, fdtrans_date, ftstock_in_company FROM (SELECT ftstock_in_no, fdtrans_date, ftstock_in_company FROM (SELECT a.ftstock_in_no, a.fdtrans_date, a.ftstock_in_company, a.fnstatus FROM txstock_in_hdr a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no=b.ftpo_no WHERE a.fnstatus=1 AND b.fnpo_type=1 AND a.ftcategory_code=0 AND (SELECT COUNT(c.fnid) FROM txpurchase_invoice_hdr c WHERE c.ftstock_in_no=a.ftstock_in_no AND c.fnstatus<>9) = 0 UNION ALL SELECT a.ftstock_in_no, a.fdtrans_date, a.ftstock_in_company, a.fnstatus FROM txstock_in_hdr a WHERE a.fnstatus=1 AND a.ftcategory_code=6 AND (SELECT COUNT(b.fnid) FROM txpurchase_invoice_hdr b WHERE b.ftstock_in_no=a.ftstock_in_no AND b.fnstatus<>9) = 0 UNION ALL SELECT ftpo_no AS ftstock_in_no, fdtrans_date, ftsupplier_code AS ftstock_in_company, fnstatus FROM txpurchase_order_hdr a WHERE fnhdr_category=2 AND fnstatus=1 AND (SELECT COUNT(b.fnid) FROM txpurchase_invoice_hdr b WHERE b.ftpo_no=a.ftpo_no AND b.fnstatus<>9) = 0 ) z ORDER BY ftstock_in_no) a WHERE 1=1";
    //$ssql_ftpo_no="SELECT a.ftpo_no, a.fdtrans_date, a.ftsupplier_code, a.fnstatus FROM txpurchase_order_hdr a WHERE (a.fnstatus=1 OR a.fnstatus=2) AND a.fnpo_type=1 AND a.fnreceipt_status != 3 AND a.fnreceipt_status !=9 ";
    $ssql_ftpo_no="SELECT a.ftpo_no, a.fdtrans_date, a.ftsupplier_code, a.fnstatus FROM txpurchase_order_hdr a WHERE 0=1 ";
    // $objAxpDB->Lookup("ftpo_no", $ssql_ftpo_no, "ftpo_no", "fdtrans_date", -1, False, "' AND ftsupplier_code=\''+ document.getElementById('ftsupplier_code').value + '\''");
    //$objAxpDB->Lookup("ftpo_no", $ssql_ftpo_no, "ftpo_no", "fdtrans_date", -1, True, "' AND ftsupplier_code=\''+ document.getElementById('ftsupplier_code').value + '\''");
    $objAxpDB->Lookup("ftpo_no", $ssql_ftpo_no, "ftpo_no", "fdtrans_date", -1, True, " ");
      
}else{
    // $ssql_ftstock_in_no="SELECT ftstock_in_no, fdtrans_date, ftstock_in_company FROM (SELECT a.ftstock_in_no, a.fdtrans_date, a.ftstock_in_company FROM txstock_in_hdr a INNER JOIN txpurchase_order_hdr b ON a.ftdocument_no=b.ftpo_no WHERE a.fnstatus=1 AND b.fnpo_type=1 UNION ALL SELECT ftstock_in_no, fdtrans_date, ftstock_in_company FROM txstock_in_hdr WHERE fnstatus=1 AND ftcategory_code=6 UNION ALL SELECT ftpo_no AS ftstock_in_no, fdtrans_date, ftsupplier_code AS ftstock_in_company FROM txpurchase_order_hdr WHERE fnhdr_category=2) z WHERE 1=1";
}

$objAxpDB->Lookup("ftsupplier_code", "SELECT ftsupplier_Code, ftsupplier_name FROM tlsupplier WHERE fnsupplier_type IN (1, 2) ORDER BY ftsupplier_Code", "ftsupplier_Code", "ftsupplier_name", -1, False);
//$objAxpDB->Lookup("ftstock_in_no", $ssql_ftstock_in_no, "ftstock_in_no", "fdtrans_date", -1, False, "' AND ftstock_in_company=\''+ document.getElementById('ftsupplier_code').value + '\''");
// $objAxpDB->Lookup("ftpo_no", $ssql_ftstock_in_no, "ftstock_in_no", "fdtrans_date", -1, False, "' AND ftstock_in_company=\''+ document.getElementById('ftsupplier_code').value + '\''");
$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("fnpayment_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 1", "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", 0,true);
$objAxpDB->Lookup("ftexpedition_code", "SELECT a.ftcoa_code, a.ftcoa_name FROM tlcoa a WHERE  RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,false);
$objAxpDB->Lookup("fnexpedition_auto_inv", "SELECT '0' AS A,'No' AS B UNION ALL SELECT '1' AS A,'Yes' AS B ", "A", "B", 0,true);
$objAxpDB->Lookup("ftterm_payment", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 75 ORDER BY fntrans_status_code ", "fntrans_status_code", "fttrans_status_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,"ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom WHERE fnstatus=1", "ftuom_code", "ftuom_name", -1,true);
    
$ssql_ftitem_code="SELECT a.ftitem_code, a.ftitem_name FROM (
                        SELECT ftitem_code, ftitem_name FROM tlstock_hdr
                        UNION ALL
                        SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku
                        UNION ALL
                        SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi
                        UNION ALL
                        SELECT ftitem_code, ftitem_name FROM tlstock_hdr_sparepart
                        ) a
                        ORDER BY a.ftitem_code";
$objAxpDB->Lookup("ftitem_code", $ssql_ftitem_code, "ftitem_code", "ftitem_name", -1, False);

$objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);


//$ajaxClear="document.getElementById('ftstock_in_no').value='';document.getElementById('span_ftstock_in_no').innerHTML='';document.getElementById('hidden_ftstock_in_no').value='';";
//$ajaxClear="if (document.getElementById('ftpo_no').value!='') { document.getElementById('ftpo_no').value='';document.getElementById('span_ftpo_no').innerHTML='';document.getElementById('hidden_ftpo_no').value=''; }";
$ajaxClear="document.getElementById('ftpo_no').value='';";
//$ajaxGrid="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftstock_in_no=' + document.getElementById('ftstock_in_no').value + '&fdtrans_date=' + document.getElementById('fdtrans_date').value);";
$ajaxSupplier="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=supplier&ftsupplier_code=' + document.getElementById('ftsupplier_code').value+'&ObjLokasi=ftpo_no');";
$ajaxGrid="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftpo_no=' + document.getElementById('ftpo_no').value + '&fdtrans_date=' + document.getElementById('fdtrans_date').value);";
$objAxpDB->FormScript(1, "onChange=\"{$ajaxSupplier}{$ajaxClear}{$ajaxGrid}\"");
//$objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");
$objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");

$ajax_due="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&code=tgltempo&ajax=1&fdtrans_date=' + document.getElementById('fdtrans_date').value + '&ftterm_payment=' + document.getElementById('ftterm_payment').value + '&fddue_date=fddue_date');";
$objAxpDB->FormScript(4, "onChange=\"{$ajax_due}\"");
$objAxpDB->FormScript(5, "onChange=\"{$ajax_due}\"");
$objAxpDB->FormScript(7, "OnChange=\"{$ajaxKurs}\"");
$objAxpDB->FormScript(13, "onChange=\"PurchaseInvoiceImportRecalDiscPercent();\"");
$objAxpDB->FormScript(14, "onChange=\"PurchaseInvoiceImportRecalDiscRp();\"");
$objAxpDB->FormScript(15, "onChange=\"PurchaseInvoiceImportRecalQty0();\"");
$objAxpDB->FormScript(16, "onChange=\"PurchaseInvoiceImportRecalQty0();\"");
$objAxpDB->FormScript(22, "onChange=\"PurchaseInvoiceImportRecalQty0();\"");

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

//$ajax1=" onChange=\"PurchaseInvoiceImportRecalPrice();\"  ";
$ajaxFormula="ajax_do('query2.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);";   
$ajax1=" onChange=\"{$ajaxFormula}PurchaseInvoiceImportRecalDiscDetailPercent(_N_);PurchaseInvoiceImportRecalDiscPercent();\"  ";
$objAxpDB->GridScript(0,2, $ajax1);

$objAxpDB->GridScript(0,6, $ajax1);
$ajax1=" onChange=\"PurchaseInvoiceImportRecalDiscDetailPercent(_N_);\"   ";
$objAxpDB->GridScript(0,7, $ajax1);

$ajax1="   onChange=\"PurchaseInvoiceImportRecalDiscDetailRp(_N_);\"  ";
$objAxpDB->GridScript(0,8, $ajax1);

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

if ($objAxpDB->GetStatus("id") == "") {
        $objAxpDB->ButtonShow("baru");
} elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->ButtonShow("reset, simpan, tutup");
        $objAxpDB->FormLocked("1, 0, 0, 0,
                               0, 0, 0, 0, 0, 1,1, 0,
                               1,0,0,0,0,1,1,1,1,0,0,0,1,1,1,1");
        $objAxpDB->GridLocked(0,"1,1,0,1,1,1,0,0,0,1,1,1,1,1,0");

} elseif (($objAxpDB->GetStatus("fnstatus") == 1) OR ($objAxpDB->GetStatus("fnstatus") == 2)) {
        
        $ajaxInquiry="window.open('pembelian.php?form=history_bayar_purchase&mode=".$_REQUEST['mode']."&id=".$_REQUEST['id']."','win_inquiry','height='+250+',width='+700+',top='+100+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen=1');";

        $button_pembayaran = "<input type='button' value='History Bayar' OnClick=\"{$ajaxInquiry}\">";
        $objAxpDB->FormLayout(26, $button_pembayaran);

        // revisi
        if(isset($_REQUEST['revisi'])) 
        {   
            $objAxpDB->FormLocked("1,1,1,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,1,1,1,0,0,1,1,1,1");
            $objAxpDB->GridLocked(0,"1,1,0,1,1,1,0,0,0,1,1,0,1,1,0");
            
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE); 
            
            $objAxpDB->ButtonShow("simpan_revisi,tutup");
        } else {
            //$objAxpDB->ButtonShow("cetak, cetak_non_harga, batal, tutup");
            $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,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");
        }
        
} else if (($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");
}


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

            $ftpo_no=$_REQUEST['ftpo_no'];
            /*$ssql_grid="SELECT a.fnindex, a.ftitem_code,a.fnqty - ifnull(a.fnqty_accept,0) as fnqty, a.ftuom_code,'' as ftwarehouse_code,'' as ftlocation_code, a.fcprice, a.ffdiscount, a.fcdiscount, a.fcamount, a.fcprice as fcprice_cogs, a.fnvat, a.fnqty_base, a.ffformula,'0.00' as fnqty_return FROM
                          txpurchase_order_det a WHERE a.ftpo_no='{$ftpo_no}' #"; */

            $ssql_grid="SELECT DISTINCT fnindex, ftitem_code,(ttlqtylimit-ttlqtyaccept) AS fnqty, ftuom_code, ftwarehouse_code, ftlocation_code, 
                                        fcprice, ffdiscount, fcdiscount, ((ttlqtylimit-ttlqtyaccept) * fcprice) AS fcamount, fcprice_cogs, fnvat, fnqty_base, 
                                        ffformula,fnqty_return
                        FROM (  SELECT b.fnindex, b.ftitem_code,b.ftuom_code,'' as ftwarehouse_code,'' as ftlocation_code, 
                                        b.fcprice, b.ffdiscount, b.fcdiscount, b.fcamount, b.fcprice as fcprice_cogs, b.fnvat, b.fnqty_base, 
                                        b.ffformula,'0.00' as fnqty_return, a.ftpo_no,
                                        COALESCE(SUM(d.fnqty_base),0) AS ttlqtyaccept, COALESCE(SUM(b.fnqty_base),0) AS ttlqtylimit 
                                FROM txpurchase_order_hdr a LEFT JOIN txpurchase_order_det b ON a.ftpo_no = b.ftpo_no 
                                LEFT JOIN txpurchase_invoice_hdr c ON c.ftpo_no = a.ftpo_no AND c.fnstatus != 3 AND c.fnstatus != 9 
                                LEFT JOIN txpurchase_invoice_det d ON c.ftpurchase_invoice_no = d.ftpurchase_invoice_no AND b.ftitem_code=d.ftitem_code and c.fnsaldo_awal = 0
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnpo_type=1 AND a.ftbranch_code='{$_SESSION['strbranch']}' 
                                        AND a.ftpo_no='{$_REQUEST['ftpo_no']}'
                                GROUP BY a.ftpo_no, b.ftitem_code, d.fnindex
                                HAVING  ttlqtyaccept < ttlqtylimit) X 
                        ORDER BY fnindex #";              
            /*               
            $ftstock_in_no=$_REQUEST['ftstock_in_no'];
            $ssql="SELECT fnid, ftdocument_no, ftstock_in_company, ftcategory_code FROM txstock_in_hdr WHERE ftstock_in_no='{$ftstock_in_no}'";
            $rs=mysql_query($ssql) or die($ssql);
            $r=mysql_fetch_assoc($rs);
            if(isset($r["fnid"])):
                $ftpo_no=$r["ftdocument_no"];
                $ftsupplier_code=$r["ftstock_in_company"];
                $ftcategory_code=$r["ftcategory_code"];

                if($ftcategory_code == 6){
                  $ssql="SELECT ftdocument_no FROM txstock_out_hdr WHERE ftstock_out_no='{$ftpo_no}' UNION ALL ";
                  $ssql.="SELECT ftdocument_no FROM txstock_out_hdr_bahan_baku WHERE ftstock_out_no='{$ftpo_no}' UNION ALL ";
                  $ssql.="SELECT ftdocument_no FROM txstock_out_hdr_setengah_jadi WHERE ftstock_out_no='{$ftpo_no}' UNION ALL ";
                  $ssql.="SELECT ftdocument_no FROM txstock_out_hdr_sparepart WHERE ftstock_out_no='{$ftpo_no}'";
                  $ssql="SELECT ftdocument_no FROM ({$ssql}) txstock";
                  $rs=mysql_query($ssql) or die($ssql);
                  $r=mysql_fetch_assoc($rs);
                  $ftpo_no=$r["ftdocument_no"];
                  //$m=" AND (SELECT COUNT(*) FROM txstock_out_det b WHERE b.ftitem_code=a.ftitem_code AND b.ftstock_out_no='{$ftpo_no}') > 0";
                  //$ssql_grid="SELECT a.fnindex, a.ftitem_code,a.fnqty - ifnull(a.fnqty_accept,0) as fnqty, a.ftuom_code,'' as ftwarehouse_code,'' as ftlocation_code, a.fcprice, a.ffdiscount, a.fcdiscount, a.fcamount, a.fnvat FROM
                  //           txpurchase_order_det a WHERE a.ftpo_no='{$ftpo_no}'{$m} #";
                  $ssql_grid="SELECT a.fnindex, a.ftitem_code, a.fnqty, a.ftuom_code, a.ftwarehouse_code,a.ftlocation_code, b.fcprice, b.ffdiscount, (a.fnqty * b.fcprice * b.ffdiscount) / 100 AS fcdiscount, ((a.fnqty * b.fcprice) - (a.fnqty * b.fcprice * b.ffdiscount) / 100) AS fcamount, b.fcprice as fcprice_cogs, b.fnvat, a.fnqty_base, a.ffformula,'0.00' as fnqty_return FROM
                             txstock_in_det a INNER JOIN txpurchase_order_det b ON a.ftitem_code=b.ftitem_code AND b.ftpo_no='{$ftpo_no}' WHERE ftstock_in_no='{$ftstock_in_no}'{$m} #";
                }else{
                  //$m=" AND (SELECT COUNT(*) FROM txstock_in_det b WHERE b.ftitem_code=a.ftitem_code AND b.ftstock_in_no='{$ftstock_in_no}') > 0";
                  $ssql_grid="SELECT a.fnindex, a.ftitem_code, a.fnqty, a.ftuom_code, a.ftwarehouse_code,a.ftlocation_code, b.fcprice, b.ffdiscount, (a.fnqty * b.fcprice * b.ffdiscount) / 100 AS fcdiscount, ((a.fnqty * b.fcprice) - (a.fnqty * b.fcprice * b.ffdiscount) / 100) AS fcamount, b.fcprice as fcprice_cogs, b.fnvat, a.fnqty_base, a.ffformula,'0.00' as fnqty_return FROM
                             txstock_in_det a INNER JOIN txpurchase_order_det b ON a.ftitem_code=b.ftitem_code AND b.ftpo_no='{$ftpo_no}' WHERE ftstock_in_no='{$ftstock_in_no}'{$m} #";
                }
            else:
               $ftpo_no=$_REQUEST['ftstock_in_no'];
               $ssql_grid="SELECT a.fnindex, a.ftitem_code,a.fnqty - ifnull(a.fnqty_accept,0) as fnqty, a.ftuom_code,'' as ftwarehouse_code,'' as ftlocation_code, a.fcprice, a.ffdiscount, a.fcdiscount, a.fcamount, a.fcprice as fcprice_cogs, a.fnvat, a.fnqty_base, a.ffformula,'0.00' as fnqty_return FROM
                             txpurchase_order_det a WHERE a.ftpo_no='{$ftpo_no}'{$m} #";
            endif;
            */

            $objAxpDB->GridSQL(0, $ssql_grid);
            $objAxpDB->Process();
            $objAxpDB->setGrid2(0);
            $html = ob_get_contents();
            ob_end_clean();

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

            div.innerHTML =m;
            RowIndex0();
            reSize();
            <?php

            //$ftsupplier_name=RetRstField("SELECT ftsupplier_name FROM tlsupplier  WHERE ftsupplier_code='{$ftsupplier_code}'");
            //$ftpo_no=RetRstField("SELECT ftdocument_no FROM txstock_in_hdr_bahan_baku  WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'");
            $ssql="SELECT ffvat,ffdiscount,fcother_cost,fntax_code,ftterm_payment,ftcurrency_code,fcrate FROM txpurchase_order_hdr WHERE ftpo_no='{$ftpo_no}'";
            $rs=mysql_query($ssql) or die($ssql);
            $r=mysql_fetch_assoc($rs);
            ?>  
            <!-- document.getElementById('ftpo_no').value='<?php // echo $ftpo_no; ?>'; -->
            <!-- document.getElementById('hidden_ftsupplier_code').value='<?php //echo $ftsupplier_name; ?>'; -->
            <!-- document.getElementById('span_ftsupplier_code').innerHTML='<?php //echo $ftsupplier_name; ?>'; -->
            document.getElementById('ftterm_payment').value='<?php echo $r['ftterm_payment']; ?>';
            document.getElementById('fcrate').value='<?php echo $r['fcrate']; ?>';
            document.getElementById('ffvat').value='<?php echo $r['ffvat']; ?>';
            document.getElementById('ffdiscount').value='<?php echo $r['ffdiscount']; ?>';
            document.getElementById('fcother_cost').value='<?php echo$r['fcother_cost']; ?>';
            <?php
            $ssql="select date_add('".CDate($_REQUEST['fdtrans_date'])."',INTERVAL ".intval($r['ftterm_payment'])." DAY)";
            $date=RetRstField($ssql);
            ?>
            document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';

            var len1 = document.getElementById('ftcurrency_code').length;
            for (var idx1=0; idx1 < len1; idx1++)
            {
                if (document.getElementById('ftcurrency_code').options[idx1].value=='<?php echo $r['ftcurrency_code']; ?>') {
                    document.getElementById('ftcurrency_code').selectedIndex=idx1;
                    break;
                }
            }

            var len2 = document.getElementById('fntax_code').length;
            for (var idx2=0; idx2 < len2; idx2++)
            {
                if (document.getElementById('fntax_code').options[idx2].value=='<?php echo $r['fntax_code']; ?>') {
                    document.getElementById('fntax_code').selectedIndex=idx2;
                    break;
                }
            }

            ElementCurrencyALL();
            PurchaseInvoiceImportRecalPrice();

            <?php

            break;

    }
    die("");
    exit;
}


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