<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Add Items';
include('includes/DefinePOClass.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
//include('includes/header.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');
$function_filter[0]=$_REQUEST['site'];
$function_filter[1]=$_REQUEST['store'];
$function_filter[2]=FormatDateForSQL($_REQUEST['issuedate']);
					
?>


<script> 

function ExitWindow()
{
	opener.window.location="index.php?route=POModule/direct_purchase_order2&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&NewRequisition=Yes";self.close();
}

function getItems1()
	{		
     var category=document.getElementById('category').value;
     var subcategory=document.getElementById('subcategory').value;
     var itemName=document.getElementById('part_name').value;
		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("ItemListDiv").innerHTML=xmlhttp.responseText;
    	
        		}
          }
	xmlhttp.open("GET","item_list_ajax.php?catg="+category+"&subctg="+subcategory+"&itemName="+itemName,true);
	xmlhttp.send();
	
	}
</script>
<link href="css/professional/default.css" rel="stylesheet" type="text/css" />


 <h2 align="center">Search ITEMS</h2>
<form action="" method="post" >
<table width="100%" align="center" border="0">
  <tr>
  <td><strong>Select Category</strong></td>
  <td><select name="category" id="category" onchange="getSubGrp(this.value)" >
  	<option value="">Select Group</option>
            <?php 

		 $store_sql ="SELECT * FROM stockmaincategory WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
		$store_result = DB_query($store_sql,$db);
		while($store_row = DB_fetch_array($store_result))
		{
			$p='';
			if($store_row['id'] == $_REQUEST['category'])	
			$p="selected";
		
		?>
            <option value="<?php echo $store_row['id']; ?>" <?php echo $p;?>><?php echo $store_row['stockmaincategoryname']; ?></option>
            <?php } ?>
</select>  	
  </td>
  
   <td><strong>Select Sub Category</strong></td>
     <td ><select name="subcategory" id="subcategory"  >
  	 <option value="">Select Sub Group</option>
  	 <?php $store_sql = "SELECT * FROM stockcategory WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
		$store_result = DB_query($store_sql,$db);
		while($store_row = DB_fetch_array($store_result))
		{
			$p1='';
			if($store_row['categoryid'] == $_REQUEST['subcategory'])
			{	
			$p1="selected";		
		}
			?>
            <option value="<?php echo $store_row['id'] ?>"<?php echo $p1 ?>><?php echo $store_row['categorydescription']; ?></option>
          <?php  } ?>
  	 
           
</select>  	
  </td>
  
  <td><strong>Item Code</strong></td>
  <td><input name="part_code" type="text" id="itemcode" size="25" style="width:129px" /></td>
  <td><strong>Item Name</strong></td>
  <td><input name="part_name" type="text" id="itemname" size="25" style="width:129px" /></td>
  <td><input type="submit" value="Search" name="search" onclick="getdataforSearchItem()"></td>
  
</table></form>
<?php 
if($_REQUEST['Select'] != '')
{
	$item_array=array();
	$item_array=$_REQUEST['row_id'];
	if(empty($_SESSION['ACGRequisition-ItemList']))
	{
		$_SESSION['ACGRequisition-ItemList']=$item_array;
	}
	else if(!empty($_SESSION['ACGRequisition-ItemList']))
	{ if(!empty($_REQUEST['row_id']))
		{ 
		$_SESSION['IntermediateList']=$_REQUEST['row_id'];
		$_SESSION['ACGRequisition-ItemList']=array_unique(array_merge($_SESSION['ACGRequisition-ItemList'],$_SESSION['IntermediateList']));
		}
	}
	if(!empty($_SESSION['ACGRequisition-ItemList']))
	{
	//print_r($_SESSION['ACGRequisition-ItemList']);
	$itemlist=implode("#",$_SESSION['ACGRequisition-ItemList']);
	$_SESSION['ACGRequisition-ItemList_1']=$itemlist;
	}
	print_r($_SESSION['ACGRequisition-ItemList']); 
if($_REQUEST['Reset']!='')
{
	unset($_SESSION['ACGRequisition-ItemList']);
	unset($_SESSION['ACGRequisition-ItemList_1']);
	unset($_SESSION['IntermediateList']);
}
}
?>
<form name="form1" action="" method="post">
	<input type="hidden" value="<?php print_r($_SESSION['ACGRequisition-ItemList']);?>" id="arr" />
  
<input type="hidden" name="prdate" value="<?php echo $_SESSION['prdate'];?>" />
<input type="hidden" name="mst_branchid" value="<?php echo $_SESSION['site'];?>" />
<input type="hidden" name="prdate" value="<?php echo $_SESSION['prdate'];?>" />
<input type="hidden" name="mst_branchid" value="<?php echo $_SESSION['site'];?>" />


  <table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
        <tr>
          <th width="13%"><strong>SELECT</strong></th>
          <th width="30%"><strong>ITEM CODE</strong></th>
          <th width="34%"><strong>ITEM NAME</strong></th>
          <th width="23%"><strong>UNIT</strong></th>
          <th width="23%"><strong>STOCK</strong></th>

        </tr></table>
<?php
$i=0;
  	if($i%2==0)
	{$bg="#CCCCCC";}
	else
	$bg="#C8BECB";
//echo "Select * from item_master where CategoryID!='FG'";
$filter='';
if($_REQUEST['forfilter']!='')
{
	if($_REQUEST['mainCategoryid']!=0)  
		$filter[]="CategoryID='".$_REQUEST['mainCategoryid']."'";
        //$sqlSub.=" and CategoryID='".$_REQUEST['mainCategoryid']."'";

       if($_REQUEST['subCategoryid']!=0)
       	$filter[]="SubCategoryID='".$_REQUEST['subCategoryid']."'";
      
      // 	$sqlSub.=" and SubCategoryID='".$_REQUEST['subCategoryid']."'";

         if($_REQUEST['itemName']!='')
       		$filter[]="item_name like '%".trim($_REQUEST['itemName'])."%'";
      
         	//$sqlSub.=" and item_name like '%".trim($_REQUEST['itemName'])."%'";
         	
         if($_REQUEST['itemScode']!='')
         	$filter[]="StockID like '%".trim($_REQUEST['itemScode'])."%'";
      
         	//$sqlSub.=" and StockID like '%".trim($_REQUEST['itemScode'])."%'";
         if($filter!='')
         {
         	$filter1=implode(" and ", $filter);
         	$query="Select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."' and  $filter1";
         	
         }
 }
 else
 {
 	$query="Select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."' limit 10";
 }
   //echo $query;

 $sql=DB_query($query,$db); 	
	?>
	
	<div style="height: 300px" id="ItemListDiv"><table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
	<?php 
	
	if(DB_num_rows($sql)>0)
	{
		$i=0;
		while($myrow=DB_fetch_array($sql))
	  	{ 	
	  		//echo $myrow['id'];
	  		$parameter=$myrow['id'];
			$c='';
			for($x=0;$x<count($_SESSION['ACGRequisition-ItemList']);$x++)
			{
				if($parameter==$_SESSION['ACGRequisition-ItemList'][$x])
				$c="checked";
			}
			 $data1='';
			 $data2='';

			$unit=DB_query("select * from  unitsofmeasure 
		where unitid ='".$myrow['mst_unitid']."' ",$db);
		$numrow=DB_num_rows($unit);
		if($numrow>0)
		{                  
						   while($unit_row=DB_fetch_array($unit))
						   {
						   $data1.="<option value=".$unit_row['unitid'].">".$unit_row['unitname']."</option>";
						   }
		 
		 }

		 $unit1=DB_query("select * from  unitsofmeasure where unitid ='".$myrow['conv_unit']."' ",$db);
		$numrow1=DB_num_rows($unit1);
		if($numrow1>0)
		{                  
						   while($unit_row1=DB_fetch_array($unit1))
						   {
						   $data2.="<option value=".$unit_row1['unitid'].">".$unit_row1['unitname']."</option>";
						   }
		 	
		 	
		 }

		 
		 $confac=$myrow['conv_factor'];
			

                $op=openingBalance($function_filter,$myrow['id']);
				$in_1=receivedMRN($function_filter,$myrow['id']);
				$out_1=issueSlip($function_filter,$myrow['id']);
				$out_2=issueSiteToSite($function_filter,$myrow['id']);    
				$in_2=receivedSiteToSite($function_filter,$myrow['id']);
				$out_3=issueMatarialReturn($function_filter,$myrow['id']);
				$in_3=receivedReturn($function_filter,$myrow['id']);  
				$out_4=Rgpchallan($function_filter,$myrow['id']);
				$out_5=Nrgp($function_filter,$myrow['id']);
				$in_4=Rgpreturn($function_filter,$myrow['id']);  
				$in_5=FinishedMR($function_filter,$myrow['id']);
				$in_6=MaterialConReceived($function_filter,$myrow['id']);
				$out_6=MaterialConIssue($function_filter,$myrow['id']);
				$in_7=PhysicalStockPlus($function_filter,$myrow['id']);
				$out_7=PhysicalStockMinus($function_filter,$myrow['id']);
				$out_8=dispatchIssue($function_filter,$myrow['id']);
				$out_9=jobWorkChallan57Issue($function_filter,$myrow['id']);
				$in_8=jobWorkChallan57Received($function_filter,$myrow['id']);
				$in_9=directPurchase($function_filter,$myrow['id']);
				$in_10=saleReturn($function_filter,$myrow['id']);

				$out_10=invoice($function_filter,$myrow['id']);
				$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_7 + $in_8+$in_9+$in_10);
				$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5 + $out_6 + $out_7 + $out_8 + $out_9+$out_10);
				$tot=$invalue-$outvalue;
				$stockallitems=$tot;

	 			$stockbalance=$tot;

	 			if($stockbalance>0)
	 			{?>
 					<tr bgcolor="<?php echo $bg;?>">
      				<td width="13%"><input type="checkbox" onclick="getItemsList('<?php echo $parameter;?>','<?php echo $myrow['StockID'];?>','<?php echo $myrow['item_name'];?>','<?php echo $data1;?>','<?php echo $data2; ?>','<?php echo $confac; ?>','<?php echo $stockbalance;?>','<?php echo $i;?>','<?php echo $myrow['last_po_price'];?>','<?php echo $stockbalance; ?>');" name="row_id[]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>"	<?php echo $c;?> /></td>
      				<td width="31%"><strong><?php echo $myrow['StockID'];?></strong></td>
      				<td width="34%"><?php echo $myrow['item_name'];?></td>
      				<td><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname'); echo $unit;?></td>
      				<td><?php echo $stockbalance; ?></td>
    			</tr>
				<?php 
				}
				$i++;
		} 
}


?>
</table>
<input type="hidden" name="row_sount" id="row_count" value="<?php echo $i;?>">
</table>
	</div>
  </form>
  

