<?php
    if($_REQUEST['ajax']) {

        switch($_REQUEST['code']) {

            case "transno":
                if(RetRstField("SELECT fnid FROM tlsupplier WHERE ftsupplier_code='{$_REQUEST['fttrans_no']}'")) {
                    ?>
                    alert('Kode Supplier sudah dipakai... ');
                    <?php
                }
                else {
                    ?>
                    submitbutton('Simpan','','','','');
                    <?php
                }

                break;
                
            case "supplier_code":
                // Remove Company Legal Innitial
                $ftsupplier_name = strtoupper($_REQUEST['ftsupplier_name']);
                $ftsupplier_name = str_replace(array('PT ', 'CV ', 'TB '),"",$ftsupplier_name);
                
                // Delimit by multiple spaces, hyphen, underscore, comma
                $words = preg_split("/[\s,_-]+/", $ftsupplier_name);
                $str_code = "";
                foreach ($words as $w) {
                  $str_code .= $w[0];
                }
                
                // Limit 2 Character , Apabila Nama hanya terdiri dari 1 kata, gunakan huruf ke-2 sebagai initial kode
                $str_code = substr($str_code,0,2);
                if (strlen($str_code) == 1) { $str_code = substr($_REQUEST['ftsupplier_name'],0,2); }
                
                if ($_REQUEST['fnsupplier_type'] == 0) {
                    $str_code = "L" . $str_code;
                } else if ($_REQUEST['fnsupplier_type'] == 1) {
                    $str_code = "I" . $str_code;
                } 
                
                $ftsupplier_code=getAutoNumber($_SESSION['strbranch'],'pu007','tlsupplier','ftsupplier_code',$str_code);
         
                ?>
                document.getElementById('ftsupplier_code').value='<?php echo $ftsupplier_code; ?>';
                document.getElementById('ftsupplier_name').value= document.getElementById('ftsupplier_name').value.toUpperCase();
                <?php

                break;       

       }
       die("");
    }

    Getsecurity('pembelian_supplier');

    //$objAxpDB->ViewTable("vPUSupplier");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftsupplier_code, a.ftsupplier_name, CASE WHEN a.fnsupplier_type=0 THEN 'LOCAL' WHEN fnsupplier_type=1 THEN 'IMPORT' END AS fnsupplier_type, 
                            b.fttrans_status_name AS ftterm_of_payment, ftaddress1, ftphone, ftfax 
                        FROM tlsupplier a
                        INNER JOIN tstransaction_status b ON ifnull(a.ftterm_of_payment,0)=b.fntrans_status_code AND b.fntrans_group_code=75 
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')");
    $objAxpDB->ViewCaption("ID, Kode Supplier, Nama Supplier, Tipe Supplier, Syarat Pembayaran, Alamat, Telp, Fax");

    $objAxpDB->FormName("Supplier");
    $objAxpDB->FormSQL("SELECT  ftsupplier_code, ftsupplier_name,ftcurrency_code, fnsupplier_type, ftitem_supplier, ftaddress1,
                                ftcity_1, ftzip_code_1, ftstate_1,
                                ftaddress2,
                                ftcity_2, ftzip_code_2, ftstate_2,
                                ftaddress3,
                                ftcity_3, ftzip_code_3, ftstate_3,
                                ftcountry, ftnpwp,
                                ftterm_of_payment, ftbank_name,
                                ftbank_account_no, ftbank_account_name, ftphone,
                                ftoffice_phone, ftwarehouse_phone, ftfax,
                                ftemail, ftwebsite, ftmobile,
                                ftcontact_name, ftowner_name, ftnotes,
                                fnstatus,fdenter_date  FROM tlsupplier");

    $objAxpDB->FormCaption(" Kode Supplier, Nama Supplier, Mata Uang, Tipe Supplier, Kode Barang Supplier, Alamat 1, Kota 1, Kode Pos 1, Propinsi 1, Alamat 2, Kota 2, Kode Pos 2, Propinsi 2, Alamat 3, Kota 3, Kode Pos 3, Propinsi 3, Negara, NPWP,  Syarat Pembayaran, Nama Bank, No. Rekening, Nama Rekening, Telepon, Telp. Kantor, Telp. Gudang, Fax, Email, Website, HP, Nama Kontak, Nama Pemilik, Catatan, Status, Tgl. Masuk, Tampil Dilaporan Daftar Pemasok, Supplier Lama");
    $objAxpDB->Lookup("fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 9", "fntrans_status_code", "fttrans_status_name", 1,true);
    //$objAxpDB->Lookup("ftbank_name", "SELECT ftcode as  ftcoa_code,ftdescription  as ftcoa_name FROM tlmaster_bank  ", "ftcoa_code", "ftcoa_name", -1,true);
    $objAxpDB->Lookup("fnsupplier_type", "SELECT '0' AS A,'LOCAL' AS B UNION ALL SELECT '1' AS A,'IMPORT' AS B ", "A", "B", 0,true);
    //$objAxpDB->Lookup("fnsupplier_type", "SELECT '0' AS A,'LOCAL' AS B UNION ALL SELECT '1' AS A,'IMPORT' AS B UNION ALL SELECT '2' AS A,'LOCAL & IMPORT' AS B ", "A", "B", 0,true);
    $objAxpDB->Lookup("ftterm_of_payment", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 75", "fntrans_status_code", "fttrans_status_name", 5,true);
    //$objAxpDB->Lookup("ftitem_supplier", "SELECT ftsupplier_Code, ftsupplier_name FROM tlsupplier {$where_ftsupplier_Code} ORDER BY ftsupplier_Code ", "ftsupplier_Code", "ftsupplier_name", -1, False);
    //$objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);

    $objAxpDB->FormPosition("TL,TL,HD,TL,HD,TL,TL, TL,TL, TL,TL, TL,TL,TL,TL,TL, TL,TL, TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR,TR");
    
    $ajaxSupplierNo="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=supplier_code&ftsupplier_name='+document.getElementById('ftsupplier_name').value + '&fnsupplier_type='+document.getElementById('fnsupplier_type').value);";
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxSupplierNo}\"");
    $objAxpDB->FormScript(3, "onChange=\"{$ajaxSupplierNo}\"");
    
    $objAxpDB->SetStatus("id");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");

    }
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("1, 0, 0");
            
            $button_code = "<input type='button' value='Open' OnClick=\"document.getElementById('ftsupplier_code').readOnly = false;document.getElementById('ftsupplier_code').style='background-color: #FFFFFF';\">";
            $objAxpDB->FormLayout(0, $button_code);
    }
    elseif ($objAxpDB->GetStatus("id") != 0) {
            $objAxpDB->ButtonShow("update, hapus, tutup");
            $objAxpDB->FormLocked("1, 0, 0");
    }
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();

?>
