<?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[]=$_REQUEST['site'];;
//$function_filter[]=$_REQUEST['store'];
$data_date=explode('/',$_REQUEST['date']);
					     $date55=$data_date[2].'-'.$data_date[1].'-'.$data_date[0];
					      $date22= strtotime(date("Y-m-d", strtotime($date55)) . " +0 day");
                             $date1= date("Y-m-d",$date22);
$function_filter[]=$date1; 
$function_filter[]=$date1;
//$function_filter[]=$_REQUEST['store'];


                 




if($_REQUEST['show']!='Show')
{
unset($_SESSION['ACGRequisition-ItemList']);
unset($_SESSION['ACGRequisition-ItemList_1']);
}
?>
<script src="js/function-ajax.js"></script>

<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/new_mxerp.css">
<style type="text/css">
	.row1 {
	background-color:#C1E0C0;
	border:#778d77 solid 1px;
	height:30px;
}
</style>
<div class="body_contain">
<div class="tab_div"> 
 <h1>Search ITEMS</h1>
<form action="" method="post" >
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
<td width="30%" align="right"><strong>Item main category</strong></td>
<td>
<?php  
 $item_query="select stockmaincategoryname, stockmaincategoryid from stockmaincategory where stockmaincategoryid != 'FG' ";
 $res=DB_query($item_query ,$db);
  //echo DB_num_rows($res);
?>
<select name="CategoryID"  onchange="ShowDiv(this.value,'hid-subcatitem','disp_subcatitem.php')" style="width:98%;">
	<option value="0">Select</option>
<?php	
		while($item_row1=DB_fetch_array($res))
		{?>
		 <option value="<?php echo $item_row1['stockmaincategoryid'];?>" <?php echo $_REQUEST['CategoryID']==$item_row1['stockmaincategoryid']?'selected':'' ;?>>
		 <?php echo $item_row1['stockmaincategoryname'];?></option>
          <?php 	} ?>
        </select>
		</td>
</tr>
<tr class="row1">
<td align="right"><strong>Item sub category</strong></td>
<td>
<div id='hid-subcatitem'>
<select name="SubCategoryID" style="width:98%;">
<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 $_REQUEST['SubCategoryID']==$item_row2['categoryid']?'selected':'' ;?>><?php echo $item_row2['categorydescription'];?></option>
<?php } ?></select>
</div></td>
</tr>
<tr class="row1">
<td align="right"><strong>Item code</strong></td>
  <td><input name="StockID" type="text" id="StockID" size="25" style="width:98%;" /></td>
  </tr>
<tr class="row1"><td colspan="2" align="center"><input type="submit" value="Show" name="show" class="search" /></td></tr>
</table>

<?php 
if($_REQUEST['Select']!='')
{
	
	 
	$item_array=array();
	$item_array=$_REQUEST['row_id'];
 		$_SESSION['ACGRequisition-ItemList']=$item_array;
 	 if(isset($_SESSION['ACGRequisition-ItemList']))
		$_SESSION['ACGRequisition-ItemList']=array_unique($_SESSION['ACGRequisition-ItemList']);
	
	

	if(!empty($_SESSION['ACGRequisition-ItemList']))
	{
	$itemlist=implode("#",$_SESSION['ACGRequisition-ItemList']);
	$_SESSION['ACGRequisition-ItemList_1']=$itemlist;
	 
	}
	 
	  
if(count($_SESSION['ACGRequisition-ItemList'])==0)
{
	unset($_SESSION['ACGRequisition-ItemList']);
	unset($_SESSION['ACGRequisition-ItemList_1']);
 
}
}

 ?>

<script type="text/javascript">
function ExitWindow()
{
 
opener.window.location="index.php?route=DirectpurchBillBook&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&BillBook=Yes";self.close();
}
</script>


 

  <?php
 //---------------------------Search Starts here-------------------------------
if(isset($_REQUEST['show']))
{
  $_SESSION['ACGRequisition-ItemList']=array();


		$sql_search='';
		$CategoryID=$_REQUEST['CategoryID'];
		$SubCategoryID=$_REQUEST['SubCategoryID'];
		$StockID=$_REQUEST['StockID'];
		$flexo_offset=$_REQUEST['flexo_offset'];
		
		$sql_search = "select * from item_master where 1=1 ";
		
		
		if(!empty($CategoryID))
		{
			$sql_search .=" and  CategoryID = '".$CategoryID."'";
		} 
		if($SubCategoryID!='0')
		{
			$sql_search .=" and SubCategoryID = '".$SubCategoryID."'";
		}
		if(!empty($StockID))
		{
			$sql_search .=" and StockID LIKE '%".$StockID."%'";
		}
		if(!empty($flexo_offset))
		{
			$sql_search .=" and flexo_offset = '".$flexo_offset."'";
		}
		
		 //echo $sql_search ;
			$search_result = DB_query($sql_search, $db);
			//$search_array=DB_fetch_array($search_result);
			//print_r($search_array);
			//echo $sql_search;
	
			
}?>

 <input type="hidden" value="<?php print_r($_SESSION['ACGRequisition-ItemList']);?>" id="arr" />
<table width="100%" border="0" cellpadding="3" cellspacing="0" style="border:0;">
	<tr><td><input style="margin:0px 0px 10px 0px;" type="submit" name="Select" id="Select" value="Add To List" class="search" /></td>
		<td><div align="right"><input style="margin:0px 0px 10px 0px;" type="button" name="Exit" id="Exit" value="Exit" class="search" onclick="ExitWindow()" ></div>
		</td>
	</tr></table>
<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'];?>" />

  <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
        <tr>
          <th width="13%"><strong>SELECT</strong></th>
          <th width="32%"><strong>ITEM CODE</strong></th>
          <th><strong>ITEM NAME</strong></th>
          <th width="21%"><strong>UNIT</strong></th>
		  <th width="21%"><strong>Stock</strong></th>
        </tr>
<?php
$i=0;
  	if($i%2==0)
	{$bg="#fff";}
	else
	$bg="#C8BECB";

if(isset($_REQUEST['show']))
{

	if(DB_num_rows($search_result)>0)
	{
	 	while($myrow = DB_fetch_array($search_result))
	 	{
	 		$c='';
			 
			if( in_array($myrow['id'],$_SESSION['ACGRequisition-ItemList']) && count($_SESSION['ACGRequisition-ItemList'])>0) 
			{
					
			  	$c="checked";
 				$f=0;
			}
			
			
			$parameter=$myrow['id'];
		
		$op=openingBalance($function_filter,$myrow['id']);
		//echo "openingBalance"."<br>";
		$in_1=receivedMRN($function_filter,$myrow['id']);
		//echo "receivedMRN"."<br>";
		$out_1=issueSlip($function_filter,$myrow['id']);
		// echo "issueSlip"."<br>";
		$out_2=issueSiteToSite($function_filter,$myrow['id']);		
		// echo "issuesitetosite"."<br>";
		$in_2=receivedSiteToSite($function_filter,$myrow['id']);
		// echo " receivesitetosite"."<br>";
		$out_3=issueMatarialReturn($function_filter,$myrow['id']);
		
		$out_4=Rgpchallan($function_filter,$myrow['id']);
		$out_5=Nrgp($function_filter,$myrow['id']);
		$in_3=receivedReturn($function_filter,$myrow['id']);		 
		//$in_4=return_rejection($function_filter,$myrow['id']);
		$in_4=Rgpreturn($function_filter,$myrow['id']);
		$invalue=($op+$in_1[0]+$in_2+$in_3+$in_4);
		
		$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5);
		$tot=trim($invalue)-trim($outvalue);		
		
		$total_current=$tot;
   		//$total_current=number_format(($op+$in_1+$in_2+$in_3)-($out_1+$out_2+$out_3),2);
			
	 	?>
        <?php 	
  $parameter=$myrow['id'];
  ?>
        <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;?>"<?php echo $c;?> /></td>
          <td><strong><?php echo $myrow['StockID'];?></strong></td>
          <td><?php echo $myrow['item_name'];?></td>
          <td><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['unit'],'unitname'); echo $unit;?></td>
		  <td><?php echo $total_current; ?></td>
        </tr>
        <?php $i++;
	 } 
  }
	else
	{?>
	 <tr>
      <td colspan="9"><h3 align="center">No Items Found</h3></td>
    </tr>
        <?php 
	}
?>
     
 <?php } //----------------end of Search---------------- 
else 
{
   	$sql=DB_query("Select * from item_master where CategoryID!='FG'",$db); 	
	if(DB_num_rows($sql)>0)
	{
		while($myrow=DB_fetch_array($sql))
	  	{
		$c='';
		  if(isset($_SESSION['ACGRequisition-ItemList']))
		  {
			if( in_array($myrow['id'],$_SESSION['ACGRequisition-ItemList'])) 
			{
					
			  	$c="checked";
 			 
			} 	
		 }	
		 
		     $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']);		 
		   //  $in_4=return_rejection($function_filter,$myrow['id']);
		     $invalue=($op+$in_1+$in_2+$in_3);
		     $outvalue=($out_1+$out_2+$out_3);
		     $tot=trim($invalue)-trim($outvalue);		 
		     $total_current=$tot;	
		
	 	 $parameter=$myrow['id'];
  ?>
 <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 $unit=$s->getField('unitsofmeasure','unitid',$myrow['unit'],'unitname'); echo $unit;?></td>
      <td><?php echo $total_current; ?></td>
    </tr>
<?php $i++;
		} 
}?>
<input type="hidden" name="row_sount" id="row_count" value="<?php echo $i;?>">
</table>
 
<?php } 
 ?>
  </form>
</div>
</div>
