<?php
$PageSecurity = 2;
$title = _('View/Search Items') ;
include('includes/SQL_CommonFunctions.inc');
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];
$_SESSION['frmView']=$frmpermission[3];
?>
<script language="javascript" type="text/javascript">
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>
<?php 
if($_REQUEST['id']!='' && $_REQUEST['action']=='delete' && $_REQUEST['item_cat']!='')
{
	$delete_sql="delete from item_master where id='".$_REQUEST['id']."'";
	$d_res=DB_query($delete_sql,$db);
	
	if($_REQUEST['item_cat']=='FG')
	{
		$delete_color="Delete from item_color_master where item_master_id ='".$_REQUEST['id']."'";
		$r_res=DB_query($delete_color,$db);
	}
	$_SESSION['success_msg']="Delete Successfully";
echo "<script>location.href='index.php?route=Inventory/maintenance/item_master_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";

}
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);
	}

	  $sql="select * from item_master where $filter1 ORDER BY StockID ASC";
}
else
{
	$sql="select * from item_master order by item_name ASC";
}
  $_SESSION['item1']=$sql;
 
 
?>
<p><h2 align="center">ITEMS</h2></p>
<form action="" method="post">
<table width="50%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr>
    <th><strong>MAIN GROUP </strong></th>
    <td>
	<select name="main_group" id="main_group" onchange="get_itemsubgroup(this.value)">
	<option value="000">---Select---</option>
	<?php
	$query1=DB_query("select * from stockmaincategory",$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> ITEM CODE </strong></th>
    <td><input type="text" name="item_code" id="item_code" value="<?php echo $_REQUEST['item_code'];?>" /></td>
  </tr>
  <tr>
    <th><strong>SUB GROUP</strong></th>
    <td><div id="sub_id"><select name="sub_group" id="sub_group">
      <option value="000">---Select---</option>
      <?php
	$query2=DB_query("select * from stockcategory  where stockmaincategory='".$_REQUEST['main_group']."' order by id ASC",$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>
    <th><strong>ITEM NAME </strong></th>
    <td><input type="text" name="item_name" id="item_name" value="<?php echo $_REQUEST['item_name'];?>" /></td>
  </tr>
</table>
<p align="center"><input type="submit" name="search" value="Search" /></p>
</form><?php if($_SESSION['frmAdd']=='1'){?>
<p align="center"><a href="index.php?route=Inventory/maintenance/item_master_add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=Add">ADD NEW ITEM</a></p><?php } ?>
<br>
<p align="right"><a href="itemlist_exp.php?mainGroup=<?php echo $_REQUEST['main_group'];?>&subGroup=<?php echo $_REQUEST['sub_group']?>&item_name=<?php echo $_REQUEST['item_name']?>&item_code=<?php echo $_REQUEST['item_code'];?>&sub_group=<?php echo $_REQUEST['sub_group']?>">Export</a></p>
<form action="" method="post">
  <div style="overflow:auto; height:200px"> 
  <table width="100%" border="1" cellspacing="0" cellpadding="3">
  
  <tr>
      <th width="11%"><strong>ITEM CODE </strong></th>
      <th width="34%"><strong>ITEM NAME </strong></th>
      <th width="19%"><strong>MAIN CATEGORY</strong></th>
      <th width="19%"><strong>SUB CATEGORY</strong></th>
      <th width="5%"><div align="center"><strong>VIEW</strong></div></th>
      <th width="5%"><div align="center"><strong>EDIT</strong></div></th>
      <th width="7%"><div align="center"><strong>DELETE</strong></div></th>
    </tr>
  
<?php 
	$result=DB_query($sql,$db);
	$i=1;
	if(DB_num_rows($result)>0)
	{
	while($myrow=DB_fetch_array($result))
	{
		$query1=DB_query("select * from stockcategory where id ='".$myrow['SubCategoryID']."'",$db);	
		$stockcat=Db_fetch_array($query1);	
		
		//$check_data_1=DB_query("SELECT mst_itemid FROM `requisitionorderdetails` WHERE mst_itemid=".$myrow['id']." 
								//UNION
								//SELECT mst_itemid FROM `addmrnqty` WHERE mst_itemid=".$myrow['id']."
								//UNION
								//SELECT mst_itemid FROM `issueqty` WHERE mst_itemid=".$myrow['id']."",$db);
		//$num1=DB_num_rows($check_data_1);
		
		//$check_data_2=DB_query("SELECT itemid FROM `material_return_details` WHERE itemid='".$myrow['id']."' 
								//UNION
								//SELECT itemid FROM `transfer_issue_details` WHERE itemid='".$myrow['id']."'
								//UNION
								//SELECT itemid FROM `transfer_receipt_details` WHERE itemid='".$myrow['id']."'
								// ",$db);
		//$num2=DB_num_rows($check_data_2);
?> 
  <tr bgcolor="#FFFFFF">
    <td width="14%"><?php echo $myrow['StockID'];?></td>
    <td width="18%"><?php echo $myrow['item_name'];?></td>
    <td width="22%">
	
	<?php 
		
 	echo $s->GetFieldValue('stockmaincategory',"id ='".$myrow['CategoryID']."'" ,'stockmaincategoryname'); 
echo	 " - ". $s->GetFieldValue('stockmaincategory',"id='".$myrow['CategoryID']."'" ,'stockmaincategoryid');
 ?></td>
    <td width="20%"><?php echo $stockcat['categorydescription'];  ?></td>
	<td width="8%"><?php if($_SESSION['frmView']=='1'){?><a href="index.php?route=Inventory/maintenance/item_master_view&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id'];?>&action=view">View</a><?php }else{echo "No Permission";}?></td>
	<td width="8%"><?php if($_SESSION['frmEdit']=='1'){?><a href="index.php?route=Inventory/maintenance/item_master_add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&item_cat=<?php echo $myrow['CategoryID'];?>&id=<?php echo $myrow['id'];?>&action=edit">Edit</a><?php }else{echo "No Permission";}?></td>
	<td width="10%"><?php if($_SESSION['frmDelete']=='1'){ if($num1>0 || $num2>0) {echo "Used in transactions";} else {?><a href="index.php?route=Inventory/maintenance/item_master_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&item_cat=<?php echo $myrow['CategoryID'];?>&id=<?php echo $myrow['id'];?>&action=delete" onClick="return confirm('Are you sure you want to delete this Item record?');">Delete</a><?php }} else{echo "No Permission";}?></td>
  </tr>
  <?php $i++;} 
  }
  else
  {?>
  <tr>
  <td colspan="7"><h3 align="center"><font color="#FF0000">No Record Found!</font></h3>
  </td>
  </tr>
  <?php 	
  }?>
</table>
  </div>
</form>
<?php 
 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);
?>