<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');
?>

<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<h2 align="center">SEARCH ITEMS</h2>
<form name="form2" action="" method="post">
<table width="100%" style="border: solid 1px #CCCCCC">
<tr>
<th><strong>Kit Code</strong></th>
<td><input name="kitcode" type="text" id="kitcode" style="width:99%" />
</td>
<th><strong>Kit Name </strong></th>
<td>
<select name="subcat"  id="subcat" style="width:99%"  >
<option value="0">Select </option>
<?php
$ss=DB_query("select * from spare_part_kits",$db);
while($row=DB_fetch_array($ss))
{ 
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['kit_name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr bgcolor="#E7E7E7" style=" border:1px solid #B6B6B6;">
	<td colspan="4" style="text-align:center;"><input type="submit" name="search" value="Search" class="search" /></td>
</tr>
</table>
</form>
<br />
<form name="form1" action="" method="post">
<?php
if($_REQUEST['search']){
$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;						
$filter=array();
if($_REQUEST['kitcode']!='')
$filter[]="kit_name like '%".$_REQUEST['kitcode']."%'";
if($_REQUEST['subcat']!='0')
$filter[]="id = '".$_REQUEST['subcat']."'";
$filter=implode(' and ',$filter);
$sql=DB_query("Select * from spare_part_kits where $filter",$db); 
$i=0;
?>

<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
 <tbody><tr>
   <td colspan="6" class="detailedViewHeader" id="abc" align="center" height="30"><b>Kit Details </b></td>
 </tr>
	
<tr>
	<td colspan="3">&nbsp;</td>
</tr>

  <tr>
  	<td valign="top" width="265">
	
	<fieldset>
	<legend>KIT  NAME </legend>
	
	<table style="margin-top:2px;" align="center" border="0" cellpadding="0" cellspacing="0" height="170" width="250">
	<tbody><tr><td>
	<div style="height:170px; overflow:auto;">
			<table width="100%" border="1" cellspacing="0" cellpadding="0">
     
	  <tr>
        <th>Action</th>
		<th>Kit Code</th>
        <th>Kit Name</th>
	
      </tr>
	  <?php
	  $ID=array(); 
	  $qty=0;
	 while($myrow=DB_fetch_array($sql))
	{ 	
	$ID=$myrow['id'];
	$kitname=$myrow['kit_name'];
	$kitcode=$myrow['kit_code'];
	$kitdesc=$myrow['kit_desc'];
	$sql2=DB_query("select * from spare_part_kits_details where parant_id='".$ID."'",$db);
		   while($row2=DB_fetch_array($sql2))
		   {
		   $qty=$qty+$row2['qty'];	
		   }
		   
	  ?>
	    <tr>
	   <td><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $ID;?>" 
	  onclick="abc('<?php echo $ID;?>','<?php echo $kitcode;?>','<?php echo  $kitname;?>','<?php echo $kitdesc;?>','<?php echo $qty;?>','<?php echo $i;?>')" 
	    /></td>
        <td><?php echo $myrow['kit_code']; ?></td>
        <td><?php echo $myrow['kit_name']; ?></td>
		
      </tr>
	 <?php } ?>
    </table>
			
		</div></td></tr>  
		<tr>
			<td><div align="right"></div></td>
		</tr>    		
    </tbody></table>
	
	</fieldset>
	
	</td>
	
    <td width="681" valign="top">
	<fieldset>
	<legend>Item Details </legend>
    	<table align="center" border="1" cellpadding="0" cellspacing="0" width="100%">
		 <tbody><tr>
		<th width="6%" height="25">SR</th>
		<th class="paddfeildbold" width="27%">Item Name</th>
		<th class="paddfeildbold" width="16%">Item Code</th>
		<th width="17%" class="paddfeildbold">Item Desc</th>
		<th width="11%" class="paddfeildbold">Rate</th>
		 <th width="11%" class="paddfeildbold">Qty</th>
		 <th width="11%" class="paddfeildbold">Stock Qty</th>
		 <th width="12%" class="paddfeildbold">Amount</th>
		 </tr>
	  	<tr>
	  <td colspan="8">
		<div style="height:170px; overflow:auto; ">
		<table align="center" border="0" cellpadding="1" cellspacing="0" width="100%">
			
			 
	  
		<?php 
		
		 
		  $k=1;
		  
		   $sql2=DB_query("select * from spare_part_kits_details where parant_id='".$ID."'",$db);
		   while($row2=DB_fetch_array($sql2))
		   {			
			$itemname=$s->getField('item_master','id',$row2['item_id'],'item_name');
			$stockid=$s->getField('item_master','id',$row2['item_id'],'StockID');
			$item_desc=$s->getField('item_master','id',$row2['item_id'],'item_desc');				
			$parameter=$row2['item_id'];		 
		    $op=openingBalance($function_filter,$row2['item_id']);
              //echo "openingBalance"."<br>";
 		      $in_1=receivedMRN($function_filter,$row2['item_id']);
          //echo "receivedMRN"."<br>";
 		    $out_1=issueSlip($function_filter,$row2['item_id']);
           // echo "issueSlip"."<br>";
		   $out_2=issueSiteToSite($function_filter,$row2['item_id']);		
          // echo "issuesitetosite"."<br>";
		   $in_2=receivedSiteToSite($function_filter,$row2['item_id']);
		// echo " receivesitetosite"."<br>";
		  $out_3=issueMatarialReturn($function_filter,$row2['item_id']);
		 
		   //$out_4=Rgpchallan($function_filter,$row2['item_id']);
		  // $out_5=Nrgp($function_filter,$row2['item_id']);
		  $in_3=receivedReturn($function_filter,$row2['item_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;				
			?>
			<tr bgcolor="#CCCCCC">
		<td width="6%"><?php echo $k; ?></td>
		<td width="27%"><?php echo $itemname;  ?></td>
		<td width="16%"><?php echo $stockid;  ?></td>
		<td width="17%"><?php echo $item_desc;  ?></td>
		<td width="11%"><?php echo $row2['rate'];  ?></td>
		<td width="11%"><?php echo $row2['qty'];  ?></td>
		<td width="11%"><?php echo $total_current;  ?>
		<input type="hidden" name="stockqty[]" id="stockqty_<?php echo $k; ?>" value=" <?php echo $total_current; ?>">
		
		</td>
		<td width="12%"><?php echo $row2['amount'];  ?></td>
		</tr>	
		<tr style="height:5px"><td colspan="7"></td></tr>
			<?php 	
			$k++;}
		
		?>
		<input type="hidden" name="count" id="count" value="<?php echo $k; ?>" />
	  	 
		 </table>
		</div>	</td>
	</tr>
		
</table>
</fieldset>
</td>
  </tr>  
</tbody></table>

<?php } ?>


<script language="javascript" type="text/javascript">

function abc(id,code,shade,desc,netrate,ctr)
{


	var itemcount=parseInt(window.opener.document.getElementById('hiditemcount1').value)+1;
	var edit_cnt=window.opener.document.getElementById('edit_cnt1').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('kitid').value=='')
		{	
					
	window.opener.document.getElementById('kitid').value = id;
	window.opener.document.getElementById('kitcode').value = code;
	window.opener.document.getElementById('kitname').value = shade;
	window.opener.document.getElementById('kitdesc').value = desc;
	window.opener.document.getElementById('kitqty').value = netrate;	
			
		}
	
	else
	{
	for(var i=counter;i<=itemcount; i++)
	{
	
	if(window.opener.document.getElementById('kitid_'+i).value=='')
	{
	
	window.opener.document.getElementById('kitid_'+i).value = id;
	window.opener.document.getElementById('kitcode_'+i).value = code;
	window.opener.document.getElementById('kitname_'+i).value = shade;
	window.opener.document.getElementById('kitdesc_'+i).value = desc;
	window.opener.document.getElementById('kitqty_'+i).value = netrate;	
	
	break;
						
					}
					else
					{
						continue;
					}


}
}
}
else
{
if(window.opener.document.getElementById('kitid').value==document.getElementById("row_id_"+ctr).value)
		{
	window.opener.document.getElementById('kitid').value = '';
	window.opener.document.getElementById('kitcode').value = '';
	window.opener.document.getElementById('kitname').value = '';
	window.opener.document.getElementById('kitdesc').value='';
	window.opener.document.getElementById('kitqty').value = '';
	
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(window.opener.document.getElementById('kitid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
    window.opener.document.getElementById('kitid1_'+i).value = '';
	window.opener.document.getElementById('kitcode1_'+i).value = '';
	window.opener.document.getElementById('kitname_'+i).value = '';
	window.opener.document.getElementById('kitdesc_'+i).value='';
	window.opener.document.getElementById('kitqty_'+i).value = '';	
	
				}
			}
		}


}
}














</script>
















  

</form>
<script language="javascript" type="text/javascript">
function getItemsList(id,code,shade,desc,netrate,mrprate,stock,ctr)
{
	
	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=='')
	{
	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('itemdesc').value = desc;	
		window.opener.document.getElementById('total_stock').value = stock;
	window.opener.document.getElementById('price').value = netrate;
	window.opener.document.getElementById('mrpper').value = mrprate;
	
	}
	else
	{
	for(i=counter; i<=itemcount; i++)
	{
	
	if(window.opener.document.getElementById('itemid_'+i).value=='')
	{
	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('itemdesc_'+i).value = desc;	
	window.opener.document.getElementById('total_stock_'+i).value = stock;	
	window.opener.document.getElementById('price_'+i).value = netrate;
	window.opener.document.getElementById('mrpper_'+i).value = mrprate;	
	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('itemdesc').value = '';
	window.opener.document.getElementById('total_stock').value='';	
	window.opener.document.getElementById('price').value = '';
	window.opener.document.getElementById('mrpper').value = '';
	}
	else
	{
	for(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('itemdesc_'+i).value = '';	
	window.opener.document.getElementById('total_stock_'+i).value='';	
	window.opener.document.getElementById('price_'+i).value = '';
	window.opener.document.getElementById('mrpper_'+i).value = '';
	}
	}
	}
	//alert('false123');
	}
	//var data= addRow('dataTable');
	
	
	
}

function addRow(tableID) 
{

	if(document)
	var table = 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;
	}
	window.opener.document.getElementById("stock_perunit_"+rowCount).value='1';
}



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[0];
	//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);
}	


</script>
