<?php
    GetSecurity('pembelian_harga_beli');
    
    $objAxpDB->ViewName("Daftar Harga Beli");
    //$objAxpDB->ViewTable("vPUHargaBeli");
    $objAxpDB->ViewSQL("Select * FROM (
                            /* SELECT a.fnid,a.ftitem_code,a.ftitem_name, b.ftstock_group_code,c.ftstock_group_name,d.ftstock_type_name,ifnull(a.ftcurrency_code,'') AS ftcurrency_code, a.fcold_buying_price, a.fcbuying_price
                                FROM tlbuying_price a
                                inner join  tlstock_hdr_bahan_baku b on a.ftitem_code=b.ftitem_code
                                left join tlstock_group_bahan_baku c on  b.ftstock_group_code=c.ftstock_group_code
                                left join tlstock_type d on a.ftstock_type_code = d.ftstock_type_code
                                WHERE b.ftstock_category_code = 'invbb' AND a.ftcurrency_code = 'IDR'
                            UNION ALL
                            SELECT a.fnid,a.ftitem_code,a.ftitem_name, b.ftstock_group_code,c.ftstock_group_name,d.ftstock_type_name,ifnull(a.ftcurrency_code,'') AS ftcurrency_code, a.fcold_buying_price, a.fcbuying_price
                                FROM tlbuying_price a
                                inner join  tlstock_hdr_sparepart b on a.ftitem_code=b.ftitem_code
                                left join tlstock_group_sparepart c on  b.ftstock_group_code=c.ftstock_group_code
                                left join tlstock_type d on a.ftstock_type_code = d.ftstock_type_code
                                WHERE b.ftstock_category_code = 'invsp' AND a.ftcurrency_code = 'IDR'
                            UNION ALL */
                            SELECT a.fnid,a.ftitem_code,a.ftitem_name, b.ftstock_group_code,c.ftstock_group_name,d.ftstock_type_name,ifnull(a.ftcurrency_code,'') AS ftcurrency_code, a.fcold_buying_price, a.fcbuying_price
                                FROM tlbuying_price a
                                inner join  tlstock_hdr b on a.ftitem_code=b.ftitem_code
                                left join tlstock_group c on  b.ftstock_group_code=c.ftstock_group_code
                                left join tlstock_type d on a.ftstock_type_code = d.ftstock_type_code
                                WHERE b.ftstock_category_code = 'inv' -- AND a.ftcurrency_code = 'IDR'
                            ) x ORDER BY x.ftitem_code,x.ftcurrency_code");
    $objAxpDB->ViewCaption("ID, Kode Barang, Nama Barang, group, Nama group, Kategory, Mata Uang, Harga Lama, Harga Skrg");

    $objAxpDB->FormName("Harga Beli");

    if(isset($_REQUEST['id'])) {
        if($_REQUEST['id']!=0) {
            $ftitem_code=RetRstField("SELECT ftitem_code  FROM tlbuying_price WHERE fnid='{$_REQUEST['id']}'");
            $ftstock_type_code=RetRstField("SELECT ftstock_type_code FROM tlbuying_price WHERE fnid='{$_REQUEST['id']}'");
            if ($ftstock_type_code == 'invbb')
                $ftstock_group_code=RetRstField("SELECT ftstock_group_code  FROM tlstock_hdr_bahan_baku WHERE ftitem_code='{$ftitem_code}'");
            elseif ($ftstock_type_code == 'invsp')
                $ftstock_group_code=RetRstField("SELECT ftstock_group_code  FROM tlstock_hdr_sparepart WHERE ftitem_code='{$ftitem_code}'");
            elseif ($ftstock_type_code == 'inv')
                $ftstock_group_code=RetRstField("SELECT ftstock_group_code  FROM tlstock_hdr WHERE ftitem_code='{$ftitem_code}'");
       }
    }

    $objAxpDB->FormSQL("SELECT '$ftstock_group_code' as ftstock_group_code,'$ftstock_type_code' as ftstock_type_code FROM tlbuying_price");

    $objAxpDB->FormCaption("Group Barang,Kategori");
    $objAxpDB->FormPosition("TL,HD");

    $objAxpDB->FormLocked("0,0");
    $strAutoNumber0=" function ValInclude(index) {
                                  if(document.getElementById('chk_includeD0'+index).checked == true) {
                                             document.getElementById('hd_includeD0'+index).value='1';
                                    }
                                    else {
                                            document.getElementById('hd_includeD0'+index).value='0';
                                    }
                             }

                                ";

    $objAxpDB->FormJS($strAutoNumber0);

    if($_REQUEST['id']) {

            // $where.="and a.fnid='{$_REQUEST['id']}'#";
            $where.="and a.ftitem_code='{$ftitem_code}'#";
            if ($ftstock_type_code == 'invbb')
                $ssql="SELECT a.ftitem_code, b.ftstock_group_code
                        FROM tlbuying_price a inner join  tlstock_hdr_bahan_baku b inner join tlstock_group_bahan_baku c
                        where a.ftitem_code=b.ftitem_code and b.ftstock_group_code=c.ftstock_group_code  $where ";
            elseif ($ftstock_type_code == 'invsp')
                $ssql="SELECT a.ftitem_code, b.ftstock_group_code
                        FROM tlbuying_price a inner join  tlstock_hdr_sparepart b inner join tlstock_group_sparepart c
                        where a.ftitem_code=b.ftitem_code and b.ftstock_group_code=c.ftstock_group_code  $where ";
            elseif ($ftstock_type_code == 'inv')
                $ssql="SELECT a.ftitem_code, b.ftstock_group_code
                        FROM tlbuying_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'];


    }
    $where.="#";

    if ($ftstock_type_code == 'invbb')
        $objAxpDB->GridSQL(0, "SELECT a.fnid as fnindex, a.ftitem_code, a.ftstock_type_code, a.ftcurrency_code, a.fcold_buying_price, a.fcbuying_price, a.ffold_buying_discount, a.ffbuying_discount, a.fcbuying_price AS fcbuying_pricex
                            FROM tlbuying_price a inner join  tlstock_hdr_bahan_baku b
                            on a.ftitem_code=b.ftitem_code $where ORDER BY a.ftitem_code,a.ftcurrency_code");
    elseif ($ftstock_type_code == 'invsp')
        $objAxpDB->GridSQL(0, "SELECT a.fnid as fnindex, a.ftitem_code, a.ftstock_type_code, a.ftcurrency_code, a.fcold_buying_price, a.fcbuying_price, a.ffold_buying_discount, a.ffbuying_discount, a.fcbuying_price AS fcbuying_pricex
                            FROM tlbuying_price a inner join  tlstock_hdr_sparepart b
                            on a.ftitem_code=b.ftitem_code $where ORDER BY a.ftitem_code,a.ftcurrency_code");
    elseif ($ftstock_type_code == 'inv')
        $objAxpDB->GridSQL(0, "SELECT a.fnid as fnindex, a.ftitem_code, a.ftstock_type_code, a.ftcurrency_code, a.fcold_buying_price, a.fcbuying_price, a.ffold_buying_discount, a.ffbuying_discount, a.fcbuying_price AS fcbuying_pricex
                            FROM tlbuying_price a inner join  tlstock_hdr b
                            on a.ftitem_code=b.ftitem_code $where ORDER BY a.ftitem_code,a.ftcurrency_code");

    $objAxpDB->GridCaption(0, "No., Barang, Kategori, Mata Uang, Harga Lama, Harga Skrg, Diskon Lama (%), Diskon Skrg (%), Harga");
    $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 0, 1, 0, 0");
    $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 1, 1, 1");
    #$checkbox="<input type='checkbox'  id='chk_include' name='chk_include'>Update Di Semua Supplier";
    //$objAxpDB->GridLayout(0,1,"&nbsp;<input type='hidden' id='hd_includeD0_N_' name='hd_includeD0[]' value='0'> <input type='checkbox'  id='chk_includeD0_N_' name='chk_includeD0[]' onclick='ValInclude(_N_);'> Update Di Semua Supplier");

    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    $objAxpDB->Lookup("ftstock_type_code", " SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type $mm", "ftstock_type_code", "ftstock_type_name", -1,TRUE);

    if ($ftstock_type_code == 'invbb') {
        $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku", "ftitem_code", "ftitem_name", 0,false);
        $objAxpDB->Lookup("ftstock_group_code", " SELECT 'SELURUH' AS ftstock_group_code,'SELURUH' AS ftstock_group_name UNION ALL SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group_bahan_baku $mm", "ftstock_group_code", "ftstock_group_name", -1,TRUE);
    } elseif ($ftstock_type_code == 'invsp') {
        $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_sparepart", "ftitem_code", "ftitem_name", 0,false);
        $objAxpDB->Lookup("ftstock_group_code", " SELECT 'SELURUH' AS ftstock_group_code,'SELURUH' AS ftstock_group_name UNION ALL SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group_sparepart $mm", "ftstock_group_code", "ftstock_group_name", -1,TRUE);
    } elseif ($ftstock_type_code == 'inv') {
        $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr", "ftitem_code", "ftitem_name", 0,false);
        $objAxpDB->Lookup("ftstock_group_code", " SELECT 'SELURUH' AS ftstock_group_code,'SELURUH' AS ftstock_group_name UNION ALL SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group $mm", "ftstock_group_code", "ftstock_group_name", -1,TRUE);
    }

    $objAxpDB->SetStatus("id");
    $ajaxhargabeli="  ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=hargabeli&ftstock_type_code='+ document.getElementById('ftstock_type_code').value + '&ftstock_group_code='+ document.getElementById('ftstock_group_code').value);";
    $ajax1=" OnClick=\"{$ajaxhargabeli}\"";
    $button="<input type='button' id='cmd_employee_list' name='cmd_employee_list' value='LIHAT' {$ajax1}>";
    $objAxpDB->FormLayout(0,"{$button}");


    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow(" cetak_daftar, export_excel ,tutup");

    }

    elseif ($objAxpDB->GetStatus("id") != 0) {
            $objAxpDB->ButtonShow("update, tutup");

    }

          
    if($_REQUEST['ajax']) {
          switch($_REQUEST['code']) {
          case "hargabeli":
               ob_start();
               $_REQUEST['id']=0.5;

               if($_REQUEST['ftstock_group_code']!='SELURUH') {
                   $where=" WHERE b.ftstock_group_code='".$_REQUEST['ftstock_group_code']."' ";
               }

               if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                   $ssql="SELECT a.fnid as fnindex, a.ftitem_code, a.ftstock_type_code, a.ftcurrency_code, a.fcold_buying_price, a.fcbuying_price, a.ffold_buying_discount, a.ffbuying_discount,a.fcbuying_price as fcbuying_pricex
                                           FROM tlbuying_price a inner join  tlstock_hdr_bahan_baku b
                                           on a.ftitem_code=b.ftitem_code
                                           {$where} # ";
               }
               elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                   $ssql="SELECT a.fnid as fnindex, a.ftitem_code, a.ftstock_type_code, a.ftcurrency_code, a.fcold_buying_price, a.fcbuying_price, a.ffold_buying_discount, a.ffbuying_discount,a.fcbuying_price as fcbuying_pricex
                                           FROM tlbuying_price a inner join  tlstock_hdr_sparepart b
                                           on a.ftitem_code=b.ftitem_code
                                           {$where} # ";
               }
               elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                   $ssql="SELECT a.fnid as fnindex, a.ftitem_code, a.ftstock_type_code, a.ftcurrency_code, a.fcold_buying_price, a.fcbuying_price, a.ffold_buying_discount, a.ffbuying_discount,a.fcbuying_price as fcbuying_pricex
                                           FROM tlbuying_price a inner join  tlstock_hdr b
                                           on a.ftitem_code=b.ftitem_code
                                           {$where} # ";
               }

               $objAxpDB->GridSQL(0, $ssql);
               $objAxpDB->GridHidden(0,"0,0,0,0,0,0,1,1,1");

               $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;
                 
               <?php
               break;

           }
           die("");
           exit;
    }

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