<?php
    GetSecurity('customer');

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                if(RetRstField("SELECT fnid FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST['fttrans_no']}'")) {
                    ?>
                    alert('Kode Customer sudah dipakai...');
                    OnChangeDate_fdtrans_date();
                    <?php
                }
                else {
                    ?>
                    submitbutton('Simpan','','','','');
                    <?php
                }
                break;
                
            case "customer_no":
                // Remove Company Legal Innitial
                $ftcustomer_name = strtoupper($_REQUEST['ftcustomer_name']);
                $ftcustomer_name = str_replace(array('PT ', 'CV ', 'TB '),"",$ftcustomer_name);
                
                // Delimit by multiple spaces, hyphen, underscore, comma
                $words = preg_split("/[\s,_-]+/", $ftcustomer_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['ftcustomer_name'],0,2); }
                
                $ftcustomer_code=getAutoNumber($_SESSION['strbranch'],'sls005','tlcustomer','ftcustomer_code',$str_code);
         
                ?>
                document.getElementById('ftcustomer_code').value='<?php echo $ftcustomer_code; ?>';
                document.getElementById('ftcustomer_name').value= document.getElementById('ftcustomer_name').value.toUpperCase();
                <?php

                break;          
        }
        die("");
    }

    $objAxpDB->ViewName("Daftar Customer");
    //    $objAxpDB->ViewTable("vSACustomer");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftcustomer_code, a.ftcustomer_name, a.ftcustomer_type_code,
                                a.ftaddress,a.ftcity, a.ftphone, 
                                a.ftcontact_person, a.ftterm_payment, a.fccredit_limit, c.fttrans_status_name as fnstatus
                        FROM tlcustomer a left join  tlarea b on a.ftarea_code=b.ftarea_code 
                        LEFT JOIN tstransaction_status c ON a.fnstatus=c.fntrans_status_code AND fntrans_group_code = 9
                ");
    $objAxpDB->ViewCaption("ID, Kode Customer, Nama Customer, Tipe Customer, Alamat1, Kota1, Telepon1, Nama Kontak, Pembayaran (Hari), Batas Kredit, Status");
            
    // KHUSUS ABRINDO TIDAK MENGGUNAKAN CUSTOMER TYPE KARENA AUTO CREATE TIPE DI SELLLING PRICE JADI BERMASALAH
    $objAxpDB->FormName("Customer");
    $objAxpDB->FormSQL("SELECT  ftcustomer_code, ftcustomer_name,ftparent_code,ftfiktif,ftharga_potong_pajak, ftsales_code, ftaddress,
                                ftcity, ftzip_code, ftstate, ftphone, ftfax, ftmobile, ftemail,
                                ftaddress_2, ftcity_2, ftzip_code_2, ftstate_2,
                                ftphone_2, ftfax_2, ftmobile_2, ftemail_2,
                                ftaddress_3, ftcity_3, ftzip_code_3, ftstate_3,
                                ftphone_3, ftfax_3, ftmobile_3, ftemail_3,
                                ftwebsite, ftcountry,  ftcustomer_type_code, ftdistributor_code,
                                ftarea_code, fbExport, ftcontact_person, ftnpwp,ftnppkp, fttax_code, 
                                ftterm_payment, ffdiscount, fccredit_limit, fnstatus
                        FROM tlcustomer");

    $objAxpDB->FormCaption("Kode Customer, Nama Customer,Parent, Surat Jalan dan Invoice Fiktif,Harga Potong Pajak (Invoice), Kode Sales, Alamat Perusahaan (1), 
                            Kota (1), Kode Pos (1), Propinsi (1), Telepon (1), Fax (1), HP (1), Email (1),
                            Alamat (2), Kota (2), Kode Pos (2), Propinsi (2),  
                            Telepon (2), (2), HP (2), Email (2),
                            Alamat (3), Kota (3), Kode Pos (3), Propinsi (3),  
                            Telepon (3), Fax (3), HP (3), Email, 
                            Website, Negara, Tipe Customer, Distributor, 
                            Wilayah, Ekspor/Impor, Nama Kontak, No. NPWP, No. NPPKP,Kode Pajak,  
                            Syarat Pembayaran (Hari), Discount, Batas Kredit,  Status");
    $objAxpDB->FormPosition("TL,TL,HD,HD,HD,TL,TL, 
                             TL,TL,TL,TL,TL,TL,TL,
                             TL,TL,TL,TL,
                             TL,TL,TL,TL,
                             TR,TR,TR,TR,
                             TR,TR,TR,TR,
                             TR,TR,TR,HD,
                             HD,HD,TR,TR,TR,TR, 
                             TR,HD,TR,TR");
    
    $objAxpDB->Lookup("ftsales_code", "SELECT ftsales_code, ftsales_name FROM tlsales WHERE fnstatus=1 ", "ftsales_code", "ftsales_name", -1,true);
    $objAxpDB->Lookup("ftparent_code", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer WHERE ifnull(ftparent_code,'')=''", "ftcustomer_code", "ftcustomer_name", -1,true);
    $objAxpDB->Lookup("ftcustomer_type_code", "SELECT ftcustomer_type_code, ftcustomer_type_name FROM tlcustomer_type", "ftcustomer_type_code", "ftcustomer_type_name", 1,true);
    $objAxpDB->Lookup("ftarea_code", "SELECT ftarea_code, ftarea_name FROM tlarea", "ftarea_code", "ftarea_name", 0,true);
    $objAxpDB->Lookup("ftdistributor_code", "SELECT '' AS ftcustomer_code,'' AS ftcustomer_name UNION ALL SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer WHERE fnstatus = 1 AND ftcustomer_type_code = 'Distributor'", "ftcustomer_code", "ftcustomer_name", 0,true);
    $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("fttax_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("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", 0,true);
    
    $js1="if (this.value=='Agen') {SetIndexSeleted(2,document.getElementById('ftdistributor_code')) } else {SetIndexSeleted(-1,document.getElementById('ftdistributor_code')) };";
    $objAxpDB->FormScript(31, "onChange=\"{$js1}\"");

    $ajaxCustNo="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=customer_no&ftcustomer_name='+document.getElementById('ftcustomer_name').value);";
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxCustNo}\"");
 
    $objAxpDB->SetStatus("id");
    
    //$ssql="SELECT ftcustomer_code FROM tlcustomer WHERE fnid='{$_REQUEST['id']}'";
    //$rs=mysql_query($ssql);
    //$r=mysql_fetch_assoc($rs);
    
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");
    }
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("1,0,1,1,1,0,0, 
                             0,0,0,0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,1,
                             1,1,0,0,0,0, 
                             0,1,1,1");
            
            $button_code = "<input type='button' value='Open' OnClick=\"document.getElementById('ftcustomer_code').readOnly = false;document.getElementById('ftcustomer_code').style='background-color: #FFFFFF';\">";
            $objAxpDB->FormLayout(0, $button_code);
            
    }
    elseif ($objAxpDB->GetStatus("id") != 0) {
            
            $objAxpDB->ButtonShow("update, hapus, tutup");
            
            // Special Access for update status and credit limit
            $update_status_access = GetSecurity('customer_update_status');
            if ($update_status_access) {
                $objAxpDB->FormLocked("1,0,1,1,1,0,0, 
                             0,0,0,0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,1,
                             1,1,0,0,0,0, 
                             0,1,0,0");
            } else {
                $objAxpDB->FormLocked("1,0,1,1,1,0,0, 
                             0,0,0,0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,0,
                             0,0,0,1,
                             1,1,0,0,0,0, 
                             1,1,1,1");
            }    
    }
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();

