<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.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");
if($_REQUEST['date']=='')
$date1=date('Y-m-d');
else
$date1= date("Y-m-d",$date22);
$function_filter[]=$date1;
?>
<input type="hidden" name="site2" id="site2" value="<?php echo $_REQUEST['site']; ?>" />
<input type="hidden" name="store2" id="store2" value="<?php echo $_REQUEST['store']; ?>" />
<input type="hidden" name="date2" id="date2" value="<?php echo $_REQUEST['date']; ?>" />

<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<h2 align="center">SEARCH ITEMS</h2>
<table width="50%" style="border: solid 1px #CCCCCC" align="center">
<tr>
<th width="42%"><strong>Exciable/Non Exciable </strong></th>
<td width="58%">
<select style="width:99%" name="CategoryID" id="CategoryID " onchange="getdata1(this.value)">
<option value="0">Select</option>
<option value="exciable">Exciable Items</option>
<option value="non exciable">Non Exciable Items</option>
</select></td>
</tr>
<tr>
<th width="42%"><strong>Item Name </strong></th>
<td width="58%"><input type="text" name="itemname" id="itemname"  style="width:100%;" onkeyup="getdata2(this.value)" /></td>
</tr>
</table>
<br />
<form name="form1" action="" method="post">
<input type="hidden"  name="acgteephonicitems"  id="acgteephonicitems" value=""/>
<div style="overflow: scroll; height: 300px" id="ItemListDiv">
<table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
<tr>
<th width="5%"><strong>SELECT</strong></th>
<th width="20%"><strong>ITEM CODE</strong></th>
<th width="20%"><strong>ITEM NAME</strong></th>
<th width="35%"><strong>UNIT</strong></th>
<th width="20%"><strong>Stock</strong></th>
<th width="20%">Price</th>
</tr>



<?php 
$sql=DB_query("Select id,StockID,item_name,item_desc,mst_unitid,mrp_rate,net_rate,item_exciable from item_master where CompanyId='".$_SESSION['ACGCompanyId']."' and item_exciable='Y'",$db); ?>
<?php
$i=0;
if(DB_num_rows($sql)>0)
{
while($myrow=DB_fetch_array($sql))
{ 	
$parameter=$myrow['id'];

$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']);
$out_6=dispatchsaleorder($function_filter,$myrow['id']);
$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5);
$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5+$out_6);
$tot=trim($invalue)-trim($outvalue);
$total_current=$tot;	


$excise=$myrow['item_exciable'];








/*$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);
$outvalue=($out_1+$out_2+$out_3);
$tot=trim($invalue)-trim($outvalue);
$total_current=$tot;*/
$excise=$myrow['item_exciable'];
$unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname');

?>

<tr bgcolor="#CCCCCC">
<td width="9%"><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>" onclick="getItemsList('<?php echo $parameter;?>','<?php echo $myrow['StockID'];?>','<?php echo  $myrow['item_name'];?>','<?php echo $myrow['item_desc'];?>','<?php echo $myrow['net_rate'];?>','<?php echo $unit;?>','<?php echo $excise; ?>','<?php echo $myrow['net_rate']; ?>',<?php echo $total_current; ?>,'<?php echo $excise; ?>','<?php echo $i;?>')"/></td>
<td width="20%"><strong><?php echo $myrow['StockID'];?></strong></td>
<td width="20%"><?php echo  $myrow['item_name'];?></td>
<td width="35%"><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname'); echo $unit;?></td>
<td width="20%"><?php echo $total_current; ?></td>
<td width="22"><?php echo $myrow['net_rate']; ?></td>
</tr>
<?php $i++;} }?>
</table>
</div>
</form>

<script language="javascript" type="text/javascript">
function getItemsList(id,code,shade,desc,netrate,mrprate,excise,price,stock,type,ctr)
{
var dup='empty';
	var itemcount=parseInt(window.opener.document.getElementById('hiditemcount').value)+1;
	var edit_cnt=window.opener.document.getElementById('edit_cnt').value;
	if(edit_cnt=='')
		{var counter=1;} 
	else if(edit_cnt>itemcount)
		{var counter=edit_cnt;
		var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} else  {var counter=edit_cnt;}
	if(document.getElementById("row_id_"+ctr).checked==true){
		if(window.opener.document.getElementById('itemid').value=='')
		{
			if(edit_cnt==''){var counter=0;}
			for(var j=0; j<counter; j++)
			{
				if(window.opener.document.getElementById('itemid1_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
			}
			
			if(dup=='empty'){
			var data= addRow('dataTable');	
			
	window.opener.document.getElementById('itemid').value = id;
	window.opener.document.getElementById('itemcode').value = code;
	window.opener.document.getElementById('itemname').value = shade;
	window.opener.document.getElementById("uom").value=mrprate;
	window.opener.document.getElementById('total_stock').value = stock;
	window.opener.document.getElementById('rate').value = price;
	window.opener.document.getElementById('type1').value = type;
	
	//window.opener.document.getElementById('mrpper').value = mrprate;
			}
		}
		else
		{
			for(var i=counter; i<=itemcount; i++)
			{	if(edit_cnt==''){var counter=0;}
				for(var j=0; j<counter; j++)
				{
					if(window.opener.document.getElementById('itemid1_'+j).value==id)
					{
						alert("Duplicate Value"); 
						document.getElementById("row_id_"+ctr).checked=false;
						var dup = "dup";
						break;
					}
				}
				if(window.opener.document.getElementById('itemid').value==id)
					{
						alert("Duplicate Value");
						document.getElementById("row_id_"+ctr).checked=false;
						break;
					}
				if(window.opener.document.getElementById('itemid_'+i).value==id)
					{
						alert("Duplicate Value"); 
						document.getElementById("row_id_"+ctr).checked=false;
						break;
					}
				if(dup=='empty'){				
					if(window.opener.document.getElementById('itemid_'+i).value=='')
					{
					var data= addRow('dataTable');	
			window.opener.document.getElementById('itemid_'+i).value = id;
	window.opener.document.getElementById('itemcode_'+i).value = code;
	window.opener.document.getElementById('itemname_'+i).value = shade;
	window.opener.document.getElementById("uom_"+i).value=mrprate;
	window.opener.document.getElementById('total_stock_'+i).value = stock;
	window.opener.document.getElementById('rate_'+i).value = price;
	window.opener.document.getElementById('type1_'+i).value = type;

	//window.opener.document.getElementById('mrpper_'+i).value = mrprate;	
	break;
						break;
					}
					else
					{
						continue;
					}
				}
			}
		}
	}
	else
	{
		if(window.opener.document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
	window.opener.document.getElementById('itemid').value = '';
	window.opener.document.getElementById('itemcode').value = '';
	window.opener.document.getElementById('itemname').value = '';
	window.opener.document.getElementById("type1").value='';
	window.opener.document.getElementById("uom").value='';
	window.opener.document.getElementById('total_stock').value='';
	window.opener.document.getElementById('rate').value = '';
	window.opener.document.getElementById('type1').value = '';

	//window.opener.document.getElementById('mrpper').value = '';
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(window.opener.document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
     window.opener.document.getElementById('itemid_'+i).value = '';
	window.opener.document.getElementById('itemcode_'+i).value = '';
	window.opener.document.getElementById('itemname_'+i).value = '';
	window.opener.document.getElementById("type1_"+i).value='';
	window.opener.document.getElementById("uom_"+i).value='';
	window.opener.document.getElementById('total_stock_'+i).value='';
	window.opener.document.getElementById('itemdesc_'+i).value = '';
	window.opener.document.getElementById('rate_'+i).value = '';
	window.opener.document.getElementById('type1_'+i).value = '';	

				}
			}
		}
	}
	
	
	
}

function addRow(tableID) 
{

	
	var table = window.opener.document.getElementById(tableID);
	var rowCount = table.rows.length;	
	var row = table.insertRow(rowCount);
	window.opener.document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	
}



function getdata1(str)
{  
    if(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[0];
	//document.getElementById("subcategory1").innerHTML=data[0];
	}
	}
	var site1=document.getElementById("site2").value;
	
	var store1=document.getElementById("store2").value;
	var date1=document.getElementById("date2").value;
	xmlhttp.open("GET","saleenquiryitem_list_ajax.php?site="+str+"&site1="+site1+"&store1="+store1+"&date1="+date1,true);
	xmlhttp.send();
	}
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
}	
function getdata2(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[0];
	//document.getElementById("subcategory1").innerHTML=data[0];
	}
	}
	var site1=document.getElementById("site2").value;
	var store1=document.getElementById("store2").value;
	var date1=document.getElementById("date2").value;
	xmlhttp.open("GET","enquiryitem_list_ajax.php?site2="+str+"&site1="+site1+"&store1="+store1+"&date1="+date1,true);
	xmlhttp.send();
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
}	


</script>
