<?php
	if($_REQUEST['ajax']) {
		switch($_REQUEST['code']) {
			case "transno":
				if(RetRstField("SELECT ftitem_code FROM tlstock_hdr WHERE ftitem_code='{$_REQUEST['fttrans_no']}'")) 
				{
					?>
						alert('Kode Barang sudah dipakai... ');
					<?php
				}
				else 
				{
					?>
						submitbutton('Simpan','','','','');
					<?php
				}
			break;

			case "stock_group":
				$ssql="SELECT ftstock_group_code,ftstock_group_name
					FROM  tlstock_group
					WHERE ftstock_group_code='{$_REQUEST['ftstock_group_code']}'";
				$rs=mysql_query($ssql);
				?>
				var ObjSelect=document.getElementById('ftstock_group_code');
					var len = ObjSelect.length;
					for (var idx=0; idx < len; idx++)
					{
						if (ObjSelect.options[ObjSelect.length-1].value!='') {
							ObjSelect.remove(ObjSelect.length-1);
						}
					}
				<?php
				$i=0;
				while($r=mysql_fetch_assoc($rs)) {
					$i++;
					echo "  try {
								ObjSelect.add(new Option('{$r['ftstock_group_code']}--{$r['ftstock_group_name']}','{$r['ftstock_group_code']}'),null,null);
								}
							   catch(ex) {
								ObjSelect.add(new Option('{$r['ftstock_group_code']}--{$r['ftstock_group_name']}','{$r['ftstock_group_code']}'),$i,$i);
						   } ";
				}
                        break;

                        case "noperkiraan":
				?> 
				if (document.getElementById('ftstock_category_code').value == 'inv') {
                                        document.getElementById('divCap_ftgl_inv_acc').style.display = 'block';
                                        document.getElementById('divCap_ftgl_sales_acc').style.display = 'block';
                                        document.getElementById('divCap_ftgl_sales_ret_acc').style.display = 'block';
					document.getElementById('divCap_ftgl_sales_disc_acc').style.display = 'block';
                                        document.getElementById('ftgl_inv_acc').style.display = 'block';
                                        document.getElementById('ftgl_sales_acc').style.display = 'block';
                                        document.getElementById('ftgl_sales_ret_acc').style.display = 'block';
					document.getElementById('ftgl_sales_disc_acc').style.display = 'block';
                                } else {
                                        document.getElementById('divCap_ftgl_inv_acc').style.display = 'none';
                                        document.getElementById('divCap_ftgl_sales_acc').style.display = 'none';
                                        document.getElementById('divCap_ftgl_sales_ret_acc').style.display = 'none';
					document.getElementById('divCap_ftgl_sales_disc_acc').style.display = 'none';
                                        document.getElementById('ftgl_inv_acc').style.display = 'none';
                                        document.getElementById('ftgl_sales_acc').style.display = 'none';
                                        document.getElementById('ftgl_sales_ret_acc').style.display = 'none';
					document.getElementById('ftgl_sales_disc_acc').style.display = 'none';
                                }
				<?php

                        break;
                }
                die("");
	}

	Getsecurity('barang_jasa');
	$ssql="SELECT ftitem_code FROM tlstock_hdr 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\">Qty Available</td>
            </tr>
	   ";
	$ssql="SELECT * FROM tlstock_det 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\" maxlength=\"22\" size=\"15\" value=\"{$r['fnqty_available']}\" 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='I01'");
        $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'");

	$strAutoNumber0="function get_ftstock_group_code() {
                                return  \" WHERE ftstock_group_code='\"+ document.getElementById('ftstock_group_code').value + \"'\";
                         }
                         ";
	// $strAutoNumber0.="document.getElementById('ftstock_group_code').focus(); ";
        $strAutoNumber0.="document.getElementById('ftuom_code').value = 'PCS'; ";
                      
        $strAutoNumber0.="document.getElementById('ftgl_inv_acc').value = '{$ftgl_inv_acc}'; ";
        $strAutoNumber0.="document.getElementById('ftgl_cogs_acc').value = '{$ftgl_cogs_acc}'; ";
        $strAutoNumber0.="document.getElementById('ftgl_sales_acc').value = '{$ftgl_sales_acc}'; ";
        $strAutoNumber0.="document.getElementById('ftgl_sales_ret_acc').value = '{$ftgl_sales_ret_acc}'; ";
        $strAutoNumber0.="document.getElementById('ftgl_sales_disc_acc').value = '{$ftgl_sales_disc_acc}'; ";
        
        $objAxpDB->ViewName("Barang");
	//$objAxpDB->ViewTable("vIBarang");
	$objAxpDB->ViewSQL("SELECT a.fnid,a.ftitem_code, a.ftitem_name,ftpart_no,
                                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  a
                            left join tlstock_det b on a.ftitem_code=b.ftitem_code
                            left join tlstock_group 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_name,ftpart_no");
        $objAxpDB->ViewCaption("ID, Kode Barang, Nama Barang, Part No, Kode Stock Kategory, Nama Stock Kategory, Qty Avail, Satuan, Status");
        
	$objAxpDB->FormName("Barang");
        $objAxpDB->FormSQL("SELECT ftitem_code, ftitem_name, ftprint_name, ftpart_no, ftstock_category_code, ftbrand_code, ftcar_code,
                            ftstock_type_code, ftuom_code, ftstock_group_code,
                            ffweight, ftweight_code, ffreorder_level,
                            ffmaximum_order, ftdescription, fnpo_include, fnsales_include, fnstatus,
                            ftgl_inv_acc, ftgl_cogs_acc, ftgl_sales_acc, ftgl_sales_ret_acc,
                            ftgl_sales_disc_acc
                            FROM tlstock_hdr");
        $objAxpDB->FormCaption("Kode Barang, Nama Barang, Nama Cetak, Part No, Kategori, Merk, Mobil,
                                Supplier Group, Satuan, Stock Kategory,
                                Berat, Kode Berat, Minimum Stock,
                                Maksimum Stock, Catatan, Tampilkan di PO, Tampilkan di SO, Status,
                                No Perk. Persediaan, No Perk. HPP, No Perk. Penjualan, No Perk. Retur Penjualan,
                                No Perk. Disc Penjualan");
        $objAxpDB->FormPosition("TL, TL, TL, TL, TL, TL, TL, 
                                TL, TL, TL, 
                                HD, HD, TL, 
                                HD, TR, TR, TR, TR,
                                TR, TR, TR, TR, 
                                TR");

        $ajaxstock_group="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=stock_group&ftstock_group_code='+ document.getElementById('ftstock_group_code').value);";
	
        if(!$_REQUEST['id']) {
 		$objAxpDB->FormScript(2, "onfocus=\"this.value=document.getElementById('ftitem_name').value;\"");
		$mm=" '+ get_ftstock_group_code() +'";
	}
        
        $strOnChange="function OnChangeMerk() {
                            var m=document.getElementById('ftbrand_code').value;
                            var result='';
                            if (m != '') {
                                return \"SELECT ftcar_code, ftcar_name FROM tlcar\";
                            }
                      }  ";
        $objAxpDB->FormJS($strOnChange);

        $ajaxperkiraan="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=noperkiraan&ftstock_category_code='+ document.getElementById('ftstock_category_code').value);";
	$objAxpDB->FormScript(4, "OnChange=\"{$ajaxperkiraan}\"");
        
	$objAxpDB->GridName(0, "Detail Barang");
	$objAxpDB->GridSQL(0, "SELECT ftwarehouse_code, ftlocation_code, fnqty_available FROM tlstock_det ");
	$objAxpDB->GridCaption(0, "Kode Gudang, Kode Lokasi, Qty Available");
	$objAxpDB->GridLocked(0,'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");
        $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("ftbrand_code", "SELECT ftbrand_code, ftbrand_name FROM tlbrand", "ftbrand_code", "ftbrand_name", -1,true);
        
        $ajaxClear="document.getElementById('ftcar_code').value='';";
        $objAxpDB->FormScript(5, "onChange=\"{$ajaxClear}\"");
        
        $objAxpDB->Lookup("ftcar_code", "SELECT ftcar_code, ftcar_name FROM tlcar", "ftcar_code", "ftcar_name", -1,false);
        
        $objAxpDB->Lookup("ftstock_group_code", "SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group", "ftstock_group_code", "ftstock_group_name", -1,false);
	$objAxpDB->Lookup("fnpo_include", "SELECT '0' AS fnpo_include, 'Non PO' AS ftpo_include UNION SELECT '1' AS fnpo_include, 'Include PO' AS ftpo_include", "fnpo_include", "ftpo_include", 1, true);
	$objAxpDB->Lookup("fnsales_include", "SELECT '0' AS fnsales_include, 'Non SO' AS ftsales_include UNION SELECT '1' AS fnsales_include, 'Include SO' AS ftsales_include", "fnsales_include", "ftsales_include", 1, true);
	$objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom WHERE fnstatus = 1 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='inv' or ftstock_type_code='srv' ", "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('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("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) = '1') OR (LEFT(a.ftcoa_subgroup_code,1) = '5') OR (LEFT(a.ftcoa_subgroup_code,1) = '6')) 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->Lookup("ftuom_code_multi", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);

        $objAxpDB->SetStatus("id,ftstock_category_code");

        if ($objAxpDB->GetStatus("id") == "") {
		$objAxpDB->ButtonShow("baru");
	}
	elseif ($objAxpDB->GetStatus("id") == 0) {
		$objAxpDB->FormScript(1, "onChange=\"document.getElementById('ftprint_name').value=document.getElementById('ftitem_name').value;\"");
                $objAxpDB->FormJS($strAutoNumber0);
		$objAxpDB->GridReplace(0,$m);
		$objAxpDB->ButtonShow("reset, simpan, tutup");

	}
	elseif ($objAxpDB->GetStatus("id") != 0) {
		$objAxpDB->FormScript(1, "onChange=\"document.getElementById('ftprint_name').value=document.getElementById('ftitem_name').value;\"");
                $ajaxperkiraan="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=noperkiraan&ftstock_category_code='+ document.getElementById('ftstock_category_code').value);";
                $objAxpDB->FormJS($ajaxperkiraan);
                $objAxpDB->GridReplace(0,$m);
		$objAxpDB->ButtonShow("update, hapus, tutup");

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

	}
        
        if($_REQUEST['ajax'])
        {
		switch($_REQUEST['code']) 
		{
                    case "mobil":
                        ob_start();
                        $objAxpDB->Lookup("ftcar_code", "SELECT ftcar_code, ftcar_name FROM tlcar WHERE ftbrand_code='{$_REQUEST['ftbrand_code']}' ORDER BY ftcar_code", "ftcar_code", "ftcar_name", -1, False);
                        
                        $objAxpDB->Process();
                        $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;
                            RowBuild0();
                        <?php
                    break;
                }
                die("");
                exit;
        }

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