<?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'];
$function_filter[]=date('Y-m-d');
//print_r($function_filter);
?>
<script> 
function getdata1(str)
{  
var xmlhttp;
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)
    {
	 var data=xmlhttp.responseText.split("__");	 
	document.getElementById("ItemListDiv").innerHTML=data;
	//document.getElementById("subcategory1").innerHTML=data[0];
		
    }
  }
  xmlhttp.open("GET","item_list_ajax.php?site="+str,true);
xmlhttp.send();
//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

}
	/* function getItemsNew(str)
	{		
		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?q="+str,true);
xmlhttp.send();
	} */
	
	
	function getItems1()
	{	
		
		var site='<?php echo $_REQUEST["site"]; ?>' ;
		var store='<?php echo $_REQUEST["store"]; ?>';
		var num='<?php echo $_REQUEST["num"]; ?>';
		var catg=document.getElementById('category').value;
		var subctg=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_conversion.php?site="+site+"&store="+store+"&catg="+catg+"&subctg="+subctg+"&itemName="+itemName+"&num="+num,true);
xmlhttp.send();
	}
	
	
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 getItemsList(item_id,id,code,name,uom,stock,ctr,isuerate,s_unit,s_stock,con_factor)
{
	//window.opener.
    if(id=='1')
    {
    window.opener.document.getElementById('convert_stock').value='';
    window.opener.document.getElementById('convert_stock1').value='';	
    window.opener.document.getElementById('first_item').value=item_id;
	window.opener.document.getElementById('item_desc').value=name;
	window.opener.document.getElementById('item_code').value=code;
	window.opener.document.getElementById('item_unit').value=uom;
	//window.opener.document.getElementById('item_stock_hiden').value=stock;
	window.opener.document.getElementById('item_stock_hidden').value=stock;
	window.opener.document.getElementById('item_unit1').value=s_unit;
	window.opener.document.getElementById('item_stock1').value=s_stock;
	window.opener.document.getElementById('con_factor').value=con_factor;
	window.opener.document.getElementById('convert_unit').value=uom;
	window.opener.document.getElementById('convert_unit1').value=s_unit;
   }
  else
  {
  	window.opener.document.getElementById('second_item').value=item_id;
  	window.opener.document.getElementById('new_item_name').value=name;
	window.opener.document.getElementById('new_item_code').value=code;
	window.opener.document.getElementById('new_item_unit').value=uom;
	//window.opener.document.getElementById('new_item_stock').value=stock;
	window.opener.document.getElementById('new_item_stock_hidden').value=stock;
	window.opener.document.getElementById('new_item_unit1').value=s_unit;
	window.opener.document.getElementById('new_item_stock1').value=s_stock;
	window.opener.document.getElementById('second_item_old_stock').value=s_stock;
	window.opener.document.getElementById('con_factor_second').value=con_factor;
	window.opener.document.getElementById('new_item_stock_convert').value='';	
  }


}
</script>
<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/new_mxerp.css">
<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>
    <td width="40%"><strong>Search By Category</strong></td>
    <td><select name="category" id="category" onchange="getItems1()" style="width:98%;">
  		<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>
 </tr>
 <tr>
    <td><strong>Search By Sub Category Category</strong></td>
    <td id="subcategory1"><select name="subcategory" id="subcategory" onchange="getItems1()" style="width:98%;">
  	 	<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>
  </tr>
  <tr>
    <td><strong>Enter Part Name/Part Code</strong></td>
    <td><input name="part_name" type="text" id="part_name" size="25" onkeyup="getItems1()" style="width:98%;" /></td>
  </tr>
</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="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid;" align="center">
        <tr>
          <th width="40">Select</th>
          <th width="100">Item Code</th>
          <th>Item Name</th>
          <th width="110">Unit</th>
        </tr>
    </table>
<?php
$i=0;
  	if($i%2==0)
	{$bg="#fff";}
	else
	$bg="#e4e4e4";
//echo "Select * from item_master where CategoryID!='FG'";
 	$sql=DB_query("Select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 	
	?>
	
	<div style="overflow: scroll; height: 300px" id="ItemListDiv">
	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid;" align="center">
	<?php 
	
	if(DB_num_rows($sql)>0)
	{
		$i=0;
		while($myrow=DB_fetch_array($sql))
	  	{ 	
	  		$parameter=$myrow['id'];
			$c='';
			for($x=0;$x<count($_SESSION['ACGRequisition-ItemList']);$x++)
			{
				if($parameter==$_SESSION['ACGRequisition-ItemList'][$x])
				$c="checked";
			}
			$unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname');
			
/*			$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']);
$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_7 + $in_8);
$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5 + $out_6 + $out_7 + $out_8 + $out_9);
$tot=$invalue-$outvalue;
$stockbalance=$tot;
		
		
		  $total_current=$tot;



		    if((float)$myrow['conv_factor']!=0)
		    {
             $other_unit= $total_current * $myrow['conv_factor'];
             $con_factor=$myrow['conv_factor'];
            }
           else
           {
           	$other_unit=$total_current;
           	$con_factor=1;
           }*/

           //echo $_REQUEST['site'];
           //echo $_REQUEST['store'];
           //echo date('Y-m-d');
			$unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname');
			if((float)$myrow['conv_unit']!=0)
				$conv_unit=$s->getField('unitsofmeasure','unitid',$myrow['conv_unit'],'unitname');
			else
		    $conv_unit=$unit;
		
	  $filt=''; 
	  $filt1='';
	if($_REQUEST['site']!='0'){
			$filt[]=" site = '".$_REQUEST['site']."'";
			$filt1[]=" site = '".$_REQUEST['site']."'";
			
			
		}
			if($_REQUEST['store1']!='0'){
	   		$filt[]=" store = '".$_REQUEST['store']."'";
	   		$filt1[]=" store = '".$_REQUEST['store']."'";

	   	}
			if($_REQUEST['item']!='0'){
	   		$filt[]=" mst_itemid  = '".$myrow['id']."'";
	   		$filt1[]=" mst_itemid = '".$myrow['id']."'";
	   	}
			if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!=''){
			$filt[]="date(trasaction) >= '".str_replace('/','-',FormatDateForSQL($_REQUEST['from_date']))."' and  date(trasaction) <='".str_replace('/','-',FormatDateForSQL($_REQUEST['to_date']))."'";
			$filt1[]="date(trasaction) < '".str_replace('/','-',FormatDateForSQL($_REQUEST['from_date']))."'";
		}
			
	
	if($filt!='')
	   		{
				$filt=implode(' and ',$filt);
			}

	    $svsql="SELECT * FROM stock_valuation_tbl where $filt order by trasaction ASC" ;
	    	$recieve = array();
	    	$issue = array();
 			$svsql_result = DB_query($svsql,$db);
 			if(db_num_rows($svsql_result)>0)
 			{
 				while($sv_row = DB_fetch_array($svsql_result))
				{
	   					 $recieve[]=$sv_row['inwardqty_primary'];
				         $issue[]=$sv_row['outwardqty_primary'];
				}

			} 
			//print_r($recieve);

	   	if(array_sum($recieve)){
      				$totalReceive= array_sum($recieve);
      				}else{
      					$totalReceive='0';
      				} 
      						echo " ";	

            			if(array_sum($issue)){
      				$totalIssue= array_sum($issue);
      				}else{
      					$totalIssue='0';
      				}			
      				
	$newTotal=$totalReceive-$totalIssue;

if((float)$myrow['conv_factor']!=0)
		    {
             $other_unit= $newTotal * $myrow['conv_factor'];
             $con_factor=$myrow['conv_factor'];
            }
           else
           {
           	$other_unit=$newTotal;
           	$con_factor=1;
           }



      						// echo "<br>";	
  ?>
 <tr bgcolor="<?php echo $bg;?>">
      <td width="40"><input type="radio" onclick="getItemsList('<?php echo $parameter; ?>','<?php echo $_REQUEST['num'];?>','<?php echo $myrow['StockID'];?>','<?php echo $myrow['item_name'];?>','<?php echo $unit;?>','<?php echo $newTotal;?>','<?php echo $i;?>','<?php echo $myrow['last_po_price'];?>','<?php echo $conv_unit; ?>','<?php echo $other_unit; ?>','<?php echo $con_factor; ?>');" name="row_id[]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>"	<?php echo $c;?> /></td>
      <td width="100"><strong><?php echo $myrow['StockID'];?></strong></td>
      <td><?php echo $myrow['item_name'];?></td>
      <td width="97"><?php  echo $unit;?></td>
 </tr>
<?php $i++;
		} 
}?>
<input type="hidden" name="row_sount" id="row_count" value="<?php echo $i;?>">
</table>
	</div>
  </form>
</div>
</div>
