<?php
session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Add Items';
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
include('includes/function_lib.php');
?>
<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<h2 align="center"><?php echo $title; ?></h2>
<div style="width:700px;">

<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
     <th>Main Group</th>
    <td style="width:250px">
	<select name="main_group" id="main_group"
	    onchange="get_itemsubgroup(this.value)" style="width:100%">
	<option value="000">---Select---</option>
	<?php
	$query1=DB_query("select * from stockmaincategory where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db); 
	while($result1=DB_fetch_array($query1)) {
	?>
	<option value="<?php echo $result1['id'];?>"<?php echo ($_REQUEST['main_group']==$result1['id']?'selected':'')?>><?php echo $result1['stockmaincategoryname'];?></option>
	<?php } ?>
	</select>	</td>
	<th><strong>Sub Group</strong></th>
    <td><div id="sub_id">
	<select name="sub_group" id="sub_group"  style="width:100%">
      <option value="000">---Select---</option>
      <?php
	  if($_REQUEST['main_group'])
	  {
	  $query2=DB_query("select * from stockcategory where stockmaincategory='".$_REQUEST['main_group']."'",$db);
	  }
	  else
	  {
	$query2=DB_query("select * from stockcategory",$db); 
	}
	while($result2=DB_fetch_array($query2)) {
	?>
      <option value="<?php echo $result2['id'];?>"<?php echo ($_REQUEST['sub_group']==$result2['id']?'selected':'')?>><?php echo $result2['categorydescription'];?></option>
      <?php } ?>
    </select></div></td>
   
  </tr>
  
  <!--<tr>
    
    <th>Item Name</th>
    <td><input type="text" name="item_name" id="item_name"
    value="<?php //echo $_REQUEST['item_name'];?>"  style="width:100%" /></td>
	 <th> Item Code</th>
    <td><input type="text" name="item_code" id="item_code"
    value="<?php //echo $_REQUEST['item_code'];?>"  style="width:100%" /></td>
  </tr>-->
</table>
<p align="center"><input type="submit" name="search" value="Search" /></p>

</form>
</div>
<form action="" method="post">	
 <div style="overflow:auto; width:700px; height:550px">
 <table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
      <tr>
	     <th><strong>Select</strong></th>
        <th><strong>Item Code</strong></th>
        <th><strong>Item Name </strong></th>
		<th><strong>Description </strong></th>
		<th><strong>UOM</strong></th>
		<th><strong>Main Category</strong></th>
		<th><strong>Sub Category</strong></th>
		
      </tr>
	  <?php   	
	 if($_REQUEST['search'])
     {
	$filter=array();
	
	if($_REQUEST['main_group']!='000')
	$filter[]="CategoryID = '".$_REQUEST['main_group']."'";
	if($_REQUEST['sub_group']!='000')
	$filter[]="SubCategoryID = '".$_REQUEST['sub_group']."'";
	if($_REQUEST['item_code']!='')
	$filter[]="StockID like '%".$_REQUEST['item_code']."%'";
	if($_REQUEST['item_name']!='')
	$filter[]="item_name like '%".$_REQUEST['item_name']."%'";
	
	if($filter!='')
	{
		$filter1=implode(" and ",$filter);
	}

	$itemname="select * from item_master where $filter1 and CompanyId='".$_SESSION['ACGCompanyId']."' order by CategoryID ASC , SubCategoryID ASC, item_name ASC";
}
else
{		
		
	  	$catid=array();
	  $maincat=DB_query("select id from  stockmaincategory ",$db);
	
	 while($row1=DB_fetch_array($maincat))
	 {
	 $catid[]=$row1['id'];
	 }		
     $catid1=implode("','",$catid); 
	$itemname="Select * from item_master WHERE CategoryID in('".$catid1."') order by CategoryID ASC , SubCategoryID ASC, item_name ASC";
		}
		
		$item_res=DB_query($itemname,$db);
		 $i=0;
		 while($myrow =DB_fetch_array($item_res))
	     { 
		 $subcatname=$s->GetFieldValue('stockcategory',"id='".$myrow['SubCategoryID']."'",'categorydescription');  
		  $maincat=$s->GetFieldValue('stockmaincategory',"id='".$myrow['CategoryID']."'",'stockmaincategoryname');
		 
		  $parameter=$myrow['id'];
		
		if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
	//echo "Select unitname from unitsofmeasure where unitid='".$myrow['unit']."'";
		$u_sql=DB_query("Select unitname from unitsofmeasure where unitid='".$myrow['mst_unitid']."'",$db);
		$u_row=DB_fetch_array($u_sql);
		$secondaryunit=$s->GetFieldValue('unitsofmeasure',"unitid='".$myrow['conv_unit']."'",'unitname');  
		
		
		$sql="select * from bill_material where part_id='".$myrow['id']."'";
		$query=DB_query($sql,$db);
		$bill_raw=DB_fetch_array($query);
		if($myrow['conv_factor']!='')
		$confac=$myrow['conv_factor'];
		else
		$confac=0;
		
		$item_name=DB_escape_string($myrow['item_name']);	
		$StockID=DB_escape_string($myrow['StockID']);
	  ?>
	  <tr bgcolor="<?php echo $bg; ?>">
	  <td><input type="checkbox"  name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>"  value="<?php echo  $parameter; ?>"
	  onclick="getItemsListdata('<?php echo $parameter;?>','<?php echo $StockID;?>','<?php echo  $item_name;?>','<?php echo $u_row['unitname'];?>','<?php echo $secondaryunit; ?>','<?php echo $confac; ?>','<?php echo $bill_raw['total_production_rate']; ?>','<?php echo $i;?>','<?php echo $myrow['SubCategoryID'];?>')" /></td>
      <td><?php echo $StockID; ?></td>
        <td><?php echo $myrow['item_name'];?></td>
		<td><?php echo $myrow['item_desc'];?></td>
		<td><?php echo $u_row['unitname'];?></td>
				<td><?php echo $maincat;?></td>
			<td><?php echo $subcatname;?></td>
			
		
		      </tr>
	  <?php $i++;} ?>
</table>
</div>
</form>




<script language="javascript" type="text/javascript">
function getItemsListdata(id,code,shade,desc1,desc2,confac,netrate,ctr,subcat11)
{
	var dup='empty';
	
	var itemcount=parseInt(window.opener.document.getElementById('hiditemcount').value)+1;
	var edit_cnt=window.opener.document.getElementById('edit_cnt').value;
	if(edit_cnt=='')
	{
		var counter=1;
	} 
	else if(edit_cnt>itemcount)
	{
		var counter=edit_cnt;
		var itemcount=parseFloat(counter)+parseFloat(itemcount);
	}else{
		var counter=edit_cnt;
	}
	
	if(document.getElementById("row_id_"+ctr).checked==true)
	{
		if(window.opener.document.getElementById('itemid').value=='')
		{
			if(edit_cnt=='')
			{
				var counter=0;
			}
			for(var j=0; j<counter; j++)
			{
				if(window.opener.document.getElementById('itemid1_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
			}
			if(dup=='empty'){
				window.opener.document.getElementById('itemid').value = id;
				window.opener.document.getElementById('itemcode').value = code;
				window.opener.document.getElementById('desc').value = shade;
				window.opener.document.getElementById('uom').value = desc1;
				window.opener.document.getElementById('uom1').value = desc2;
				window.opener.document.getElementById('confac').value =confac;
				window.opener.document.getElementById('costlandprice').value = netrate;
				if(window.opener.document.getElementById('psize')){
					window.opener.document.getElementById('psize').value = subcat11;
					window.opener.document.getElementById('psize').focus();
				}
			}
		}
		else
		{
			for(var i=counter; i<=itemcount; i++)
			{	
				if(edit_cnt=='')
				{
					var counter=0;
				}
				for(var j=0; j<counter; j++)
				{
					if(window.opener.document.getElementById('itemid1_'+j).value==id)
					{
						alert("Duplicate Value"); 
						document.getElementById("row_id_"+ctr).checked=false;
						var dup = "dup";
						break;
					}
				}
				if(window.opener.document.getElementById('itemid').value==id)
				{
						alert("Duplicate Value");
						document.getElementById("row_id_"+ctr).checked=false;
						break;
				}
				if(window.opener.document.getElementById('itemid_'+i).value==id)
				{
						alert("Duplicate Value"); 
						document.getElementById("row_id_"+ctr).checked=false;
						break;
				}
				if(dup=='empty'){				
					if(window.opener.document.getElementById('itemid_'+i).value=='')
					{
						window.opener.document.getElementById('itemid_'+i).value=id;
						window.opener.document.getElementById('itemcode_'+i).value =code;
						window.opener.document.getElementById('desc_'+i).value = shade;
						window.opener.document.getElementById('uom_'+i).value =desc1;
						window.opener.document.getElementById('uom1_'+i).value =desc2;
						window.opener.document.getElementById('confac_'+i).value =confac;
						window.opener.document.getElementById('costlandprice_'+i).value =netrate;
						if(window.opener.document.getElementById('psize_'+i)){
							window.opener.document.getElementById('psize_'+i).value = subcat11;
							if(i==counter){
								window.opener.document.getElementById('psize_'+i).focus();
								
							}
						}
						break;
						break;
					}
					else
					{
						continue;
					}
				}
			}
		}
		window.opener.document.getElementById('psize').focus();
	}
	else
	{
		if(window.opener.document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
			window.opener.document.getElementById('itemid').value = '';
			window.opener.document.getElementById('itemcode').value = '';
			window.opener.document.getElementById('desc').value = '';
			window.opener.document.getElementById('uom').value = '';
			window.opener.document.getElementById('uom1').value = '';
			window.opener.document.getElementById('confac').value ='';
			window.opener.document.getElementById('costlandprice').value = '';
			if(window.opener.document.getElementById('psize')){
				window.opener.document.getElementById('psize').value = '';
			}
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(window.opener.document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
					window.opener.document.getElementById('itemid_'+i).value='';
					window.opener.document.getElementById('itemcode_'+i).value = '';
					window.opener.document.getElementById('desc_'+i).value = '';
					window.opener.document.getElementById('uom_'+i).value ='';
					window.opener.document.getElementById('uom1_'+i).value ='';
					window.opener.document.getElementById('confac_'+i).value ='';
					window.opener.document.getElementById('costlandprice_'+i).value ='';
					if(window.opener.document.getElementById('psize_'+i)){
						window.opener.document.getElementById('psize_'+i).value = '';
					}
				}
			}
		}
	}
}















/*function loadValue(str,ct)
{  
	var spl = str.split(",");
	var code = spl[0];
	var name = spl[1];
	var uomid = spl[2];	
	var cost=spl[3];
	var itemid=spl[4];
	if(ct)
	{
	window.opener.document.getElementById('itemcode_'+ct).value = code;
	window.opener.document.getElementById('desc_'+ct).value = name;
	window.opener.document.getElementById('uom_'+ct).value = uomid;
	window.opener.document.getElementById('costlandprice_'+ct).value = cost;
	if(cost!='')
	{
	window.opener.document.getElementById("costlandprice_"+ct).setAttribute('readonly','readonly');
	}		
	}		
	else
	{
	window.opener.document.getElementById('itemcode'+ct).value = code;
	window.opener.document.getElementById('desc'+ct).value = name;
	window.opener.document.getElementById('uom'+ct).value = uomid;
	window.opener.document.getElementById('costlandprice'+ct).value = cost;
	if(cost!='')
	{
	window.opener.document.getElementById("costlandprice"+ct).setAttribute('readonly','readonly');
	}
	}
	window.close();
}*/


function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?q="+str,true);
xmlhttp.send();
}
</script>
	