<?php
   if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                if(RetRstField("SELECT fnid FROM txadvanced_payment WHERE fttrans_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
                }
            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 $r["fcrate"]; ?>';
                <?php
            break;
        
            // Status Giro
            case "statusgiro":
                // Metode Giro atau cek
                if (($_REQUEST['status'] == '0') || ($_REQUEST['status'] == '1')) {
                    $statusgiro = "0";
                } else {
                    $statusgiro = "1";
                }
                ?>
                document.getElementById('fngiro_status').value='<?php echo $statusgiro; ?>';
                <?php
            break; 
        }
        die("");
    }
    
    Getsecurity('uang_muka_pembelian');
    $objAxpDB->LookupMagicQuotes(True);
    $objAxpDB->ViewName("Daftar Uang Muka Pembelian");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.fttrans_no, a.fdtrans_date, c.ftsupplier_name, a.fcamount,
                               d.fttrans_status_name as fngiro_status, b.fttrans_status_name as fnstatus
                        FROM txadvanced_payment a
                        INNER JOIN tstransaction_status b ON a.fnstatus=b.fntrans_status_code AND b.fntrans_group_code=2
                        LEFT JOIN TLSupplier c ON a.ftsupplier_code = c.ftsupplier_code
                        LEFT JOIN tstransaction_status d ON a.fngiro_status = d.fntrans_status_code AND d.fntrans_group_code=11
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')       
                        ORDER BY a.fttrans_no DESC, a.fdtrans_date DESC");
    $objAxpDB->ViewPeriod(TRUE);
    $objAxpDB->ViewCaption("ID, No. transaksi, Tgl. transaksi, Nama Supplier, Jumlah, Status Pembayaran, Status");

    $objAxpDB->FormName("Uang Muka Pembelian");
    $objAxpDB->FormSQL("SELECT fttrans_no, fdtrans_date, ftsupplier_code, ftcurrency_code, fcrate, fcamount, fcoutstanding_amount, ftacc_no, fnpayment_method, ftgiro_no, fdgiro_date, fdcliring_date, fngiro_status, ftremarks, fnstatus
                        FROM txadvanced_payment");
    $objAxpDB->FormCaption("No. Transaksi, Tgl. transaksi, Supplier, Mata Uang, Kurs, Jumlah Total, Sisa Pembayaran, No Perkiraan, Metode Pembayaran, No Giro, Tgl. Giro, Tgl. Cliring, Status Pembayaran, Keterangan, Status");
    $objAxpDB->FormPosition("TL, TL, TL, TL, TL, TL, TL, TR, TR, TR, TR, TR, TR, TL, TL");

    $ajaxKurs="ajax_do('keuangan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=idr&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);";
    $objAxpDB->FormScript(3, "OnChange=\"{$ajaxKurs}\"");
    $objAxpDB->FormScript(5, "onChange=\"document.getElementById('fcoutstanding_amount').value = document.getElementById('fcamount').value; \"");
    $ajaxPayment="ajax_do('keuangan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=statusgiro&status='+ document.getElementById('fnpayment_method').value);";
    $objAxpDB->FormScript(8, "OnChange=\"{$ajaxPayment}\"");

    $ajaxKurs="ajax_do('keuangan.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=fin002&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=fttrans_no&no_trans=fttrans_no&table_name=txadvanced_payment&ftbranch_code={$_SESSION['strbranch']}');";

        $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1}
                            }
                             OnChangeDate_fdtrans_date();
                             document.getElementById('ftcurrency_code').value='IDR';
                             {$ajaxKurs}
                            ";

        $objAxpDB->FormJS($strAutoNumber0);
    } 

    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    //$objAxpDB->Lookup("ftacc_no", "SELECT a.ftcoa_code, a.ftcoa_name FROM tlcoa a INNER JOIN tlcoa_subgroup b ON a.ftcoa_subgroup_code=b.ftcoa_subgroup_code WHERE (a.ftcoa_subgroup_code=16 OR a.ftcoa_subgroup_code=17) AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,true);
    $objAxpDB->Lookup("ftacc_no", "SELECT a.ftcoa_code, a.ftcoa_name FROM tlcoa a INNER JOIN tlcoa_subgroup b ON a.ftcoa_subgroup_code=b.ftcoa_subgroup_code WHERE (a.ftcoa_subgroup_code=16 OR a.ftcoa_subgroup_code=17 OR ftcoa_name LIKE '%uang muka pembelian%' OR ftcoa_name LIKE '%piutang lain - lain%' OR ftcoa_name LIKE '%hutang lain - lain%' ) AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_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("fnpayment_method", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 8 AND ( (fntrans_status_code<=4) or fntrans_status_code=7)", "fntrans_status_code", "fttrans_status_name", -1,true);
    $objAxpDB->Lookup("fngiro_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 11", "fntrans_status_code", "fttrans_status_name", 0, true);
    $objAxpDB->Lookup("ftsupplier_code", "SELECT  ftsupplier_code, ftsupplier_name  FROM tlsupplier ORDER BY ftsupplier_code", "ftsupplier_code", "ftsupplier_name", -1, False);
    
    $objAxpDB->SetStatus("id, fnstatus,ftgiro_no");
    if ($objAxpDB->GetStatus("id") == "")
        $objAxpDB->ButtonShow("baru,  cetak_daftar, export_excel , tutup");
    elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->ButtonShow("reset, simpan, tutup");
        $objAxpDB->FormLocked("0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
           $fnstatusgiro = RetRstField("SELECT COUNT(fnid) FROM txadvanced_payment WHERE fngiro_status <> 0 AND (fnpayment_method = 0 OR fnpayment_method = 1) AND fttrans_no = '{$_REQUEST['$fttrans_no']}'");

           if(isset($_REQUEST['revisi'])) {
                $objAxpDB->ButtonShow("simpan_revisi, tutup");
                $objAxpDB->FormLocked("1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1");

           } elseif ($objAxpDB->GetStatus("ftgiro_no") != '') {
                if ($fnstatusgiro == 0) {
                    $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
                } else {
                    $objAxpDB->ButtonShow("baru, cetak, batal, tutup");
                }
                $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
           } else {
                if ($fnstatusgiro == 0) {
                    $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
                    $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
                } else {
                    $objAxpDB->ButtonShow("baru, cetak, update, batal, tutup");
                    $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1");
                }
                
           }

    } elseif ($objAxpDB->GetStatus("fnstatus") == 2) {
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->ButtonShow("baru, cetak, tutup");

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

    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->ButtonShow("tutup");
    }
    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();