<?php //error_reporting(0);
$PageSecurity = 4;
$PageName = 'Add Items';
global  $db;
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');
$action=$_REQUEST['action'];
echo $_SESSION['crm_newclient'];
if($_REQUEST['Select'] != '')
{
	$item_array=array();
	$item_array=$_REQUEST['row_id'];
	$_SESSION['CRMLeadItem']=$item_array;
	//$_SESSION['crm_newclient']=$NewClient;
	//print_r($_SESSION['manvi']);
	
	$itemlist=implode(",",$_SESSION['CRMLeadItem']);
	$_SESSION['CRMLeadItem']=$itemlist;
	echo $_REQUEST['action'];
if($_REQUEST['action']=='AddNew')	
{
 ?>
<script type="text/javascript">
opener.window.location="index.php?route=CRM/Leads&action=AddNew&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>";self.close();
</script>
<?php 
}else{?>
<script type="text/javascript">
opener.window.location="index.php?route=CRM/crmleadview&action=<?php echo $_REQUEST['action']; ?>&leadid=<?php echo $_REQUEST['leadid'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>";self.close();
</script>

<?php }
}
?>
 
 
<h2 align="center">ADD ITEMS</h2>
<table align="center"><form action="requisition_items.php" method="post">
<tr> <th><strong>Item main category </strong></th>
<td>
 <select name="CategoryID" id="CategoryID ">
	<option value="0">Select</option>
<?php
        $item_query="select stockmaincategoryname, stockmaincategoryid from stockmaincategory where stockmaincategoryid = 'FG' ";
		$res=DB_query($item_query ,$db);
		while($item_row1=DB_fetch_array($res))
		{?>
		 <option value="<?php echo $item_row1['stockmaincategoryid'];?>"><?php echo $item_row1['stockmaincategoryname'];?></option>
          <?php 	} ?>
        </select></td></tr>
<tr><th><strong>Item sub category</strong></th><td>
<select name="SubCategoryID">
<option value="0">Select</option>
<?php $item_query1="select categoryid, categorydescription from stockcategory ";
		$res1=DB_query($item_query1 ,$db);
		while($item_row2=DB_fetch_array($res1))
		{?>
<option value="<?php echo $item_row2['categoryid'];?>"><?php echo $item_row2['categorydescription'];?></option>
<?php } ?></select></td></tr>
<tr><th><strong>Item code</strong></th>
  <td><input name="StockID" type="text" id="StockID" size="25" /></td></tr>
<tr><td><input type="submit" value="Submit" name="submit" /></form></table>

  <?php
 //---------------------------Search Starts here-------------------------------
if(isset($_POST['submit']))
{
		$sql_search='';
		$CategoryID=$_REQUEST['CategoryID'];
		$SubCategoryID=$_REQUEST['SubCategoryID'];
		$StockID=$_REQUEST['StockID'];
		$flexo_offset=$_REQUEST['flexo_offset'];
		if(empty($CategoryID) && empty($SubCategoryID) && empty($StockID) && empty($flexo_offset) )
		{
		$sql_search = "select * from item_master where 1 ";
		}
		else 
		{
			$sql_search = "select * from item_master where 0 ";
		}
		if(!empty($CategoryID))
		{
			$sql_search .=" OR CategoryID = '".$CategoryID."'";
		} 
		if(!empty($SubCategoryID))
		{
			$sql_search .=" OR SubCategoryID = '".$SubCategoryID."'";
		}
		if(!empty($StockID))
		{
			$sql_search .=" OR StockID LIKE '%".$StockID."%'";
		}
		if(!empty($flexo_offset))
		{
			$sql_search .=" OR flexo_offset = '".$flexo_offset."'";
		}
			$search_result = DB_query($sql_search, $db);
		//	 echo $sql_search;
	
			
}?>
<form name="form1" action="" method="post">
<input type="submit" name="Select" id="Select" value="Select">
<input type="hidden" name="date" value="<?php echo $_SESSION['date'];?>" />
<input type="hidden" name="site" value="<?php echo $_SESSION['site'];?>" />
<input type="hidden" name="requisition_no" value="<?php echo $_SESSION['requisition_no'];?>" />
<input type="hidden" name="remarks" value="<?php echo $_SESSION['remarks'];?>" />
<?Php

 if(isset($_SESSION['CRMLeadItem']))
{
 	$x=explode(',',$_SESSION['CRMLeadItem']);
	//print_r($x);
}
?>
  <table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
  <tr>
  	<th width="7%"><strong>SELECT</strong></th>
    <th width="12%"><strong>ITEM CODE</strong></th>
    <th width="23%"><strong>ITEM NAME</strong></th>
	<th width="8%"><strong>ITEM DESCRIPTION </strong></th>
	<th width="10%"><strong>UNIT</strong></th>
	</tr>
<?php
  	$catid= $s->GetFieldValue('stockmaincategory',"stockmaincategoryid='FG'",'id');
	//echo "Select * from item_master where CategoryID='".$catid."' and ( (groupid ='".$_SESSION['ACGRoleId']."' or groupid=NULL) or FIND_IN_SET(users,'".$_SESSION['UserID']."'))";
  	$sql=DB_query("Select * from item_master where CategoryID='".$catid."' and ( (groupid ='".$_SESSION['ACGRoleId']."' or groupid=NULL) or FIND_IN_SET(users,'".$_SESSION['UserID']."'))",$db); 
		if(DB_num_rows($sql)==0)
		{
		// echo  "Select * from item_master where CategoryID='".$catid."' and ( FIND_IN_SET(users,'".$_SESSION['UserID']."'))";
			$sql=DB_query("Select * from item_master where CategoryID='".$catid."'  ",$db); 
		}
	if(DB_num_rows($sql)>0)
	{
		while($myrow=DB_fetch_array($sql))
	  	{ 	
	  $parameter=$myrow['id'];
	  $c='';
	   if(isset($_SESSION['CRMLeadItem']))
	   {
		  if(in_array($myrow['id'],$x)  )
		  $c='checked';
		} 
  ?>
 <tr bgcolor="<?php echo $bg;?>">
      <td><input type="checkbox" name="row_id[<?php echo $i;?>]2" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>"<?php echo $c;?> /></td>
      <td><strong><?php echo $myrow['StockID'];?></strong></td>
      <td><?php echo  $myrow['item_name'];?></td>
      <td><?php echo $myrow['item_desc'];?></td>
      <td><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['unit'],'unitname'); echo $unit;?></td>
    </tr>
<?php $i++;} }?>
 </table>
</form>
  </table></table>
 