<?php
    
    GetSecurity('harga_jual');
    
    $objAxpDB->ViewName("Daftar Harga Jual");
//    $objAxpDB->ViewTable("vSASellingPrice");
/*    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftitem_code, CONCAT(b.ftitem_name,' PART NO: ',b.ftpart_no),b.ftstock_group_code, a.fcold_selling_price, a.fcselling_price, a.ffold_selling_discount, a.ffselling_discount
                            FROM tlselling_price a inner join tlstock_hdr b
                            where a.ftitem_code=b.ftitem_code 
                             ORDER BY a.ftcustomer_type_code,a.ftitem_code");
    $objAxpDB->ViewCaption("ID, Kode Barang, Nama Barang, Group, Harga Lama, Harga Skrg,  Diskon Lama (%), Diskon Skrg (%)"); */

    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftitem_code, CONCAT(b.ftitem_name,CASE WHEN IFNULL(TRIM(b.ftpart_no),'') != '' THEN ' PART NO: ' ELSE '' END,IFNULL(b.ftpart_no,'')) AS ftitem_name,
                                b.ftstock_group_code, a.ftcustomer_type_code,a.fcold_selling_price, a.fcselling_price
                        FROM tlselling_price a inner join tlstock_hdr b
                        INNER JOIN tlcustomer_type c on a.ftcustomer_type_code = c.ftcustomer_type_code 
                        where a.ftitem_code=b.ftitem_code and c.fnstatus = 1 
                        ORDER BY a.ftitem_code,a.ftitem_name");
    $objAxpDB->ViewCaption("ID, Kode Barang, Nama Barang, Group, Tipe Customer,Harga Lama, Harga Terbaru");
    
    $objAxpDB->FormName("Harga Jual");
    $objAxpDB->FormSQL("SELECT ftcustomer_type_code,'' as ftstock_group_code,'' as ftstock_category_code,ftitem_code FROM tlselling_price");
    
    $objAxpDB->FormCaption("Tipe Customer, Group, Kategori, Kode Barang");
    $objAxpDB->FormPosition("HD,HD,HD,HD");
    $objAxpDB->FormLocked("0,0,0,0");
    
    if($_REQUEST['ajax']) {
        if($_REQUEST['code']=="hargajual") {
            if($_REQUEST['ftstock_group_code']) {
                $_REQUEST['code']="hargajual_ftstock_group_code";
            }
            else {
                $_REQUEST['code']="hargajual_ftstock_category_code";
            }
        }

        switch($_REQUEST['code']) {
            case "hargajual_ftstock_group_code":
                $ftstock_group_code=$_REQUEST['ftstock_group_code'];
                $ftcustomer_type_code=$_REQUEST['ftcustomer_type_code'];
                $where =" where 1=1 and b.ftyesno=0 ";
                if($ftstock_group_code) {
                    $where.=" and ftstock_group_code='{$ftstock_group_code}' ";
                }
                if($ftcustomer_type_code) {
                     $where.=" and ftcustomer_type_code='{$ftcustomer_type_code}' ";
                }
                $where.="#";
                break;
            case "hargajual_ftstock_category_code":
                $ftstock_category_code=$_REQUEST['ftstock_category_code'];
                $ftcustomer_type_code=$_REQUEST['ftcustomer_type_code'];
                $where =" where ftstock_category_code='{$ftstock_category_code}' ";              
                if($ftcustomer_type_code) {
                     $where.=" and ftcustomer_type_code='{$ftcustomer_type_code}' ";
                }
                $where.="#";
                break;
            case "ftitem_code":
                $where.=" where a.ftitem_code='{$_REQUEST['ftitem_code']}' ";
                $ftcustomer_type_code=$_REQUEST['ftcustomer_type_code'];
                if($ftcustomer_type_code) {
                     $where.=" and ftcustomer_type_code='{$ftcustomer_type_code}' ";
                }
                $where.="#";
                break;
        }
    }
	
    if($_REQUEST['id']) {
		 
        $where.="and a.fnid='{$_REQUEST['id']}'#";
        
        /*
        $ssql="SELECT  a.ftcustomer_type_code, a.ftitem_code, b.ftstock_group_code 
                    FROM tlselling_price a inner join  tlstock_hdr b inner join tlstock_group c
                    where a.ftitem_code=b.ftitem_code and b.ftstock_group_code=c.ftstock_group_code  $where ";
        $rs=mysql_query($ssql) or die($ssql);
        $r=mysql_fetch_assoc($rs);

        //$_REQUEST['ftitem_code']=$r['ftitem_code'];
        */

    }
      
    $objAxpDB->GridSQL(0, "SELECT a.ftitem_code,a.fcold_selling_price, a.fcselling_price, a.ffold_selling_discount, a.ffselling_discount
                            FROM tlselling_price a inner join  tlstock_hdr b
                            on a.ftitem_code=b.ftitem_code $where");
  
    $objAxpDB->GridCaption(0, "Barang,Harga Lama, Harga Skrg, Diskon Lama (%), Diskon Skrg (%)");
    $objAxpDB->GridLocked(0, "1, 1, 0, 1, 0");
    $objAxpDB->GridHidden(0, "0, 0, 0, 1, 1");
  
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(ftitem_name,CASE WHEN IFNULL(TRIM(ftpart_no),'') != '' THEN ' PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) as ftitem_name FROM tlstock_hdr", "ftitem_code", "ftitem_name", 0,false);
    
    /*
    $objAxpDB->Lookup("ftcustomer_type_code", "SELECT ftcustomer_type_code, ftcustomer_type_name FROM tlcustomer_type", "ftcustomer_type_code", "ftcustomer_type_name", 0,true);
    $objAxpDB->Lookup("ftstock_group_code", "SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group $mm", "ftstock_group_code", "ftstock_group_name", -1,true);
    $objAxpDB->Lookup("ftstock_category_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type WHERE ftstock_type_code='kca' or ftstock_type_code='acc'", "ftstock_type_code", "ftstock_type_name", 0,true);
    $objAxpDB->SetStatus("id");
    
    $ajaxhargajual=" if(this.value!='') ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=hargajual&ftstock_category_code='+ document.getElementById('ftstock_category_code').value+'&ftstock_group_code='+ document.getElementById('ftstock_group_code').value+'&ftcustomer_type_code='+ document.getElementById('ftcustomer_type_code').value);";
    $ajaxhargajual_ftstock_group_code=" if(this.value!='') ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=hargajual_ftstock_group_code&ftstock_category_code='+ document.getElementById('ftstock_category_code').value+'&ftstock_group_code='+ document.getElementById('ftstock_group_code').value+'&ftcustomer_type_code='+ document.getElementById('ftcustomer_type_code').value);";
    $ajaxhargajual_ftstock_category_code=" if(this.value!='') ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=hargajual_ftstock_category_code&ftstock_category_code='+ document.getElementById('ftstock_category_code').value+'&ftstock_group_code='+ document.getElementById('ftstock_group_code').value+'&ftcustomer_type_code='+ document.getElementById('ftcustomer_type_code').value);";
    $ajaxitem_code=" if(this.value!='') ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=ftitem_code&ftitem_code='+ document.getElementById('ftitem_code').value+'&ftstock_category_code='+ document.getElementById('ftstock_category_code').value+'&ftstock_group_code='+ document.getElementById('ftstock_group_code').value+'&ftcustomer_type_code='+ document.getElementById('ftcustomer_type_code').value);";

    $objAxpDB->FormScript(0, "onChange=\"{$ajaxhargajual}\"");
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxhargajual_ftstock_group_code}\"");
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxhargajual_ftstock_category_code}\"");
    $objAxpDB->FormScript(3, "onChange=\"{$ajaxitem_code}\"");
    */
    
    $objAxpDB->SetStatus("id");

    if ($objAxpDB->GetStatus("id") == "") {
            // $objAxpDB->ButtonShow("");
    }
    
    elseif ($objAxpDB->GetStatus("id") != 0) {
            $objAxpDB->ButtonShow("update, tutup"); 
           
    }
    
    if($_REQUEST['ajax']) { 
        switch($_REQUEST['code']) {
            case "ftitem_code":
                $ssql="SELECT fnid FROM tlselling_price
                                 where ftitem_code='{$_REQUEST['ftitem_code']}' ";

                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);
                $_REQUEST['id']=$r['fnid'];
                ob_start();
                $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;

                <?
                break;
            case "hargajual_ftstock_group_code":
                $ssql="SELECT fnid FROM tlselling_price
                                 WHERE ftcustomer_type_code='{$_REQUEST['ftcustomer_type_code']}'";
                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);
                $_REQUEST['id']=$r['fnid'];              
                ob_start();
                $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;                                   
                   
                <?   
                break;
            case "hargajual_ftstock_category_code":
                $ssql="SELECT fnid FROM tlselling_price
                                 WHERE ftcustomer_type_code='{$_REQUEST['ftcustomer_type_code']}'";
                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);
                $_REQUEST['id']=$r['fnid'];
                ob_start();
                $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;

                <?
                break;
        }
        die("");
        exit;
    }
    
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();
