<?php
        Getsecurity("inventorybb_barang");

        if($_REQUEST['ajax']) {
            switch($_REQUEST['code']) {
                 case "transno":
                  $flag=0;
                  $arr1=explode("|",$_REQUEST['fttrans_no1']);
                   for($j=0; $j<count($arr1); $j++) {
                        if($arr1[$j]!="") {
                            $fttrans_no=$_REQUEST['fttrans_no'].".".$arr1[$j];
                            if(RetRstField("SELECT ftitem_code FROM tlstock_hdr_bahan_baku WHERE ftitem_code='{$fttrans_no}'")) {
                               echo "alert('Kode Barang \"{$fttrans_no}\" , sudah di pakai ...');";
                               $flag=1;
                            }

                        }
                    }

                 if($flag==0) {
                        ?>

                      submitbutton('Simpan','','','','');

                <?php
                  }

                break;
             }
            die("");
        }

        $ssql="SELECT ftitem_code FROM tlstock_hdr_bahan_baku WHERE fnid='{$_REQUEST['id']}'";
        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);

        $m="<table id=\"TransactionDetail0\" name=\"TransactionDetail0\" class=\"ewTable\" width=\"100%\">
            <tr class=\"ewTableHeader\">
                <td colspan=\"1\">Detail Barang / Jasa</td>
            </tr>

            <tr class=\"ewTableHeader\">
                <td valign=\"top\">Kode Gudang</td>
                <td valign=\"top\">Kode Lokasi</td>
                <td valign=\"top\">Exp. Date</td>
                <td valign=\"top\">Qty Available</td>
            </tr>
            ";
        $ssql="SELECT * FROM tlstock_det_bahan_baku WHERE ftitem_code='{$r['ftitem_code']}' and fnqty_available <> 0";

        $rs=mysql_query($ssql);

        while($r=mysql_fetch_assoc($rs)) {

            $m.="<tr class=\"ewTableRow\" id=\"TransactionDetailBlankRow0\" name=\"TransactionDetailBlankRow0\" style=\"display:'';\">
                        <td>
                                <input type=\"text\"  size=\"30\" maxlength=\"20\" value=\"{$r['ftwarehouse_code']}\" readonly style='background-color: #E0E0E0' >
                        </td>
                        <td>
                                <input type=\"text\"  size=\"30\" maxlength=\"20\" value=\"{$r['ftlocation_code']}\" readonly style='background-color: #E0E0E0' >
                        </td>
                        <td>
                                <input type=\"text\"  size=\"30\" maxlength=\"20\" value=\"{$r['fdexp_date']}\" readonly style='background-color: #E0E0E0' >
                        </td>
                        <td>
                                <input  type=\"text\" maxlength=\"22\" size=\"15\" value=\"{$r['fnqty_available']}\" onBlur=\"ElementCurrencyOnBlur(this);\" onFocus=\"ElementCurrencyOnBlur(this); this.select();\" style=\"text-align: right\" readonly style='background-color: #E0E0E0' >
                        </td>
                 </tr>
		 ";
        }

	$m.="</table>"  ;

        // Load default COA Setting utk transaksi baru
        $ftgl_inv_acc = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no='IBB01'");
        $ftgl_cogs_acc = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no='P01'");
        $ftgl_sales_acc = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no='S01'");
        $ftgl_sales_ret_acc = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no='S05'");
        $ftgl_sales_disc_acc = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no='S02'");

        $strLoadGL="document.getElementById('ftgl_inv_acc').value = '{$ftgl_inv_acc}'; ";
        $strLoadGL.="document.getElementById('ftgl_cogs_acc').value = '{$ftgl_cogs_acc}'; ";
        $strLoadGL.="document.getElementById('ftgl_sales_acc').value = '{$ftgl_sales_acc}'; ";
        $strLoadGL.="document.getElementById('ftgl_sales_ret_acc').value = '{$ftgl_sales_ret_acc}'; ";
        $strLoadGL.="document.getElementById('ftgl_sales_disc_acc').value = '{$ftgl_sales_disc_acc}'; ";

        $objAxpDB->LookupMagicQuotes(True);
        $objAxpDB->ViewName("Barang");
        //$objAxpDB->ViewTable("vINBarang");
        $objAxpDB->ViewSQL("SELECT a.fnid,a.ftitem_code, a.ftitem_name,  
                                        c.ftstock_group_code as ftstock_group_code,c.ftstock_group_name as ftstock_group_name,
                                        ifnull(sum(b.fnqty_available),0) as fnqty_available,  a.ftuom_code,
                                        case when a.fnstatus=0 then 'tidak aktif' else 'aktif' end as Status
                                        FROM tlstock_hdr_bahan_baku  a
                                        left join tlstock_det_bahan_baku b on a.ftitem_code=b.ftitem_code
                                        left join tlstock_group_bahan_baku c on a.ftstock_group_code=c.ftstock_group_code
                                        group by a.fnid, a.ftitem_code, a.ftitem_name,  a.ftuom_code,a.fnstatus 
                                        ORDER BY ftitem_code, ftitem_name");

        $objAxpDB->ViewCaption("ID, Kode Barang, Nama Barang,
                                Kode Group, Nama Group,
                                Qty Avail, Satuan, Status");
        $objAxpDB->FormName("Barang Bahan Baku");

        $objAxpDB->FormSQL("SELECT ftitem_code, ftitem_name, ftprint_name, ftstock_category_code,
                                  ftitem_size, ftuom_code, ftstock_group_code,
                                  ffweight, ftweight_code, ffreorder_level,
                                  ffmaximum_order, ftdescription , fnstatus,
                                  ftgl_inv_acc, ftgl_cogs_acc, ftgl_sales_acc, ftgl_sales_ret_acc, ftgl_sales_disc_acc, fdexp_date
                                  FROM tlstock_hdr_bahan_baku");

        $objAxpDB->FormCaption("Kode Barang, Nama Barang, Nama Cetak, Kategori,
                                Keterangan Size, Satuan, Stock Group,
                                Berat, Kode Berat, Minimum Stock, Maksimum Stock, Catatan, Status,
                                 No Perk. Persediaan, No Perk. HPP, No Perk. Penjualan, No Perk. Retur Penjualan, No Perk. Disc Penjualan, Exp. Date Status");

        $objAxpDB->FormPosition(" TL, TL, TL,TL,TL, TL, TL, TL, TL, TL, TL, TR, TR, TR, TR, TR, TR, TR, TR");

        $objAxpDB->GridName(0, "Detail Barang");
        $objAxpDB->GridSQL(0, "SELECT ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_available FROM tlstock_det_bahan_baku ");
        $objAxpDB->GridCaption(0, "Kode Gudang, Kode Lokasi, Exp. Date, Qty Avail");
        $objAxpDB->GridLocked(0,'1,1,1,1,1');
         $objAxpDB->GridAutoAdd(0, False);

        $objAxpDB->GridName(1, "Detail Barang Multi Satuan");
        $objAxpDB->GridSQL(1, "SELECT fnid as fnindex, ftuom_code_multi, ffformula, ftketerangan FROM tlstock_multi_det_bahan_baku");
        $objAxpDB->GridCaption(1, "No.,Multi Satuan, Formula, Keterangan");
        $objAxpDB->GridLocked(1,'1,0,0,0');
        $objAxpDB->GridAllowAddNew(1, TRUE);
        $objAxpDB->GridAllowDelete(1, TRUE);
        $objAxpDB->GridAutoAdd(1, FALSE);
       
	$objAxpDB->Lookup("ftuom_code_multi", "SELECT ftuom_code, ftuom_name FROM tluom order by ftuom_code ", "ftuom_code", "ftuom_name", -1,true);
        $objAxpDB->Lookup("ftstock_group_code", "SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group_bahan_baku ", "ftstock_group_code", "ftstock_group_name", -1,false);
        $objAxpDB->Lookup('fnstatus', 'SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status where fntrans_status_code<>2 and fntrans_group_code=9', 'fntrans_status_code', 'fttrans_status_name', 1,true);
        $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom order by ftuom_code", "ftuom_code", "ftuom_name", -1,true);
        $objAxpDB->Lookup("ftstock_category_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type where ftstock_type_code='invbb' ", "ftstock_type_code", "ftstock_type_name", 0,true);
        $objAxpDB->Lookup("ftweight_code", "SELECT ftweight_code, ftWeight_Name FROM TLWeight", "ftweight_code", "ftWeight_Name", -1,true);
        $objAxpDB->Lookup("ftBrand_Code", "SELECT ftBrand_Code, ftBrand_Name FROM TLBrand", "ftBrand_Code", "ftBrand_Name", -1,true);

        $objAxpDB->Lookup("ftgl_inv_acc", "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 LEFT(a.ftcoa_subgroup_code,1) = '1' AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,true);
        $objAxpDB->Lookup("ftgl_cogs_acc", "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 LEFT(a.ftcoa_subgroup_code,1) = '5' AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,true);
        $objAxpDB->Lookup("ftgl_sales_acc", "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 LEFT(a.ftcoa_subgroup_code,1) = '4' AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,true);
        $objAxpDB->Lookup("ftgl_sales_ret_acc", "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 LEFT(a.ftcoa_subgroup_code,1) = '4' AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,true);
        $objAxpDB->Lookup("ftgl_sales_disc_acc", "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 LEFT(a.ftcoa_subgroup_code,1) = '4' AND RIGHT(a.ftcoa_code, 5) <> '.0000'", "ftcoa_code", "ftcoa_name", -1,true);

        $objAxpDB->FormScript(1, "onChange=\"document.getElementById('ftprint_name').value=document.getElementById('ftitem_name').value;\"");

        $objAxpDB->SetStatus("id");
        if ($objAxpDB->GetStatus("id") == "") {
                $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel , tutup");
        }
        elseif ($objAxpDB->GetStatus("id") == 0) {
                $objAxpDB->FormJS($strLoadGL);
                $objAxpDB->GridReplace(0,$m);
                $objAxpDB->ButtonShow("reset, simpan, tutup");

                $objAxpDB->FormLocked("0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
        }
        elseif ($objAxpDB->GetStatus("id") != 0) {
                $objAxpDB->GridReplace(0,$m);
                $objAxpDB->ButtonShow("update, hapus, tutup");

                $objAxpDB->FormLocked("1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
        }

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