<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Kerala Khadi & Village Industries Board</title>
    <script type="text/javascript" src="scripts/jquery-latest.js"></script>
	<link type="text/css" href="includes/style1.css" rel="stylesheet" />
      <link type="text/css" href="css/form.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>

    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<script type="text/javascript">
$(document).ready(function(){

    $('[id="grp[]"]').change(function(){
        alert('hi');
        var selectedGrp = $(".grp option:selected").val();

        $.ajax({

            type: "POST",

            url: "selectsubgrp.php",

            data: { grp : selectedGrp }

        }).done(function(data){

            $("#response").html(data);

        });

    });

});


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

/***********************************************
* Disable "Enter" key in Form script
* This notice must stay intact for use
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

</script>

<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;

}
table.form{
font-weight: normal;
} 
</style>
<style>
.cont{
      background-color:#fff;
      //background:url(images/bg-inner.jpg) #e9e0c3 repeat-x;
      width: 100%;position: absolute ;
      height:99%;
      /* Optional - just for DIV)*/
      //border: solid 1px #000000;
      -moz-box-shadow: 0px 5px 5px #888;
      -webkit-box-shadow: 0px 5px 5px #888;
      box-shadow: 0px 5px 10px #888;
    
      }


#pg{
       text-decoration:none;
}
#pg button{
       background-color:#3b5998;
}
</style>

<? //include('includes/top.php'); ?>
<table class="container"><tr><td>

<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px;">  &nbsp;&nbsp;Master >> Edit Item Master</font>

<style>
th{background-color:steelblue;color:white}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
<script>
function valid()
{
 
 if(document.f1.pcode.value.length<=0)
 {
   alert("Invalid Partcode.");
   document.f1.pcode.focus();
   return false;
 }

if(document.f1.pname.value.length<=0)
 {
   alert("Invalid Name.");
   document.f1.pname.focus();
   return false;
 }

if ((document.f1.fromyr.value!="") && (isNaN(document.f1.fromyr.value))) 
 {
   alert("Please Enter Numeric Values Only");
   document.f1.fromyr.focus();
   return false;
 }
if (document.f1.fromyr.value.length != 4) 
	{
            alert("FromYear is not proper. Please check");
		document.f1.fromyr.focus();
            return false;
	}

  if ((document.f1.toyear.value!="") && (isNaN(document.f1.toyear.value))) 
	{
    	alert("Please Enter Numeric Values Only");
   	document.f1.toyear.focus();
   	return false;
	}
	if (document.f1.toyear.value.length != 4) 
	{
            alert("ToYear is not proper. Please check");
		document.f1.toyear.focus();
            return false;
	}

if(document.f1.srate.value.length<=0)
 {
   alert("Invalid SaleRate.");
   document.f1.srate.focus();
   return false;
 }
if(document.f1.prate.value.length<=0)
 {
   alert("Invalid Purchase Rate.");
   document.f1.prate.focus();
   return false;
 }



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

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      
function pgnfn(str){
            document.getElementById('pgbtn'+str).style.background = "#ccc";
}
</script>
<link href="css/pagination.css" rel="stylesheet" type="text/css" />
<link href="css/grey.css" rel="stylesheet" type="text/css" />
<style>
.paginate {
	//font-family:Arial, Helvetica, sans-serif;
	padding: 3px;
	margin: 3px;
}

.paginate a {
	padding:2px 5px 2px 5px;
	margin:2px;
	border:1px solid #999;
	text-decoration:none;
	color: #666;
}
.paginate a:hover, .paginate a:active {
	border: 1px solid #999;
	color: #000;
}
.paginate span.current {
    margin: 2px;
	padding: 2px 5px 2px 5px;
		border: 1px solid #999;
		
		font-weight: bold;
		background-color: #999;
		color: #FFF;
	}
	.paginate span.disabled {
		padding:2px 5px 2px 5px;
		margin:2px;
		border:1px solid #eee;
		color:#DDD;
	}
	
		
	
</style>
<table align=center><tr><td id='sea_td'><form name='f2' action='' method='post'><input type='search' name='sea' placeholder='Search By Itemcode'>&nbsp;OR <input type='search' name='seaname' placeholder='Search By Itemname'>&nbsp;<input type='submit' value='Go' id='reg_sea_bttn' class='sub_btn' name='search_bttn'/>
</form></td></tr></table>
<br>&nbsp;
<?

$targetpage = "partmastrepo.php";
$limit = 20; 
$sea= $_REQUEST['sea'];
$seaname= $_REQUEST['seaname'];
if($sea!='')
{
	$query = "SELECT COUNT(*) as num FROM partmaster where partcode like '%$sea%' order by partcode";
	
	$total_pages = mysqli_fetch_array(mysqli_query($db,$query));
	$total_pages = $total_pages[num];
}
else if($_REQUEST['seaname']!='')
{
	$query = "SELECT COUNT(*) as num FROM partmaster where partname like '%$seaname%' order by partcode";
	$total_pages = mysqli_fetch_array(mysqli_query($db,$query));
	$total_pages = $total_pages[num];
}
else
{
$query = "SELECT COUNT(*) as num FROM partmaster";
	$total_pages = mysqli_fetch_array(mysqli_query($db,$query));
	$total_pages = $total_pages[num];
}

	$stages = 3;
	$page = mysqli_escape_string($_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}
	

if($_REQUEST['sea']!='')
{
$query1 = "SELECT * FROM partmaster where partcode like '%$sea%' order by partcode LIMIT $start, $limit";

	//$result = mysqli_query($db,$query1);

if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
		$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?sea=$sea&page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?sea=$sea&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?sea=$sea&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?sea=$sea&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?sea=$sea&page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?sea=$sea&page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}

}
else if($_REQUEST['seaname']!='')
{
$query1 = "SELECT * FROM partmaster where partname like '%$seaname%' order by partcode LIMIT $start, $limit";

	//$result = mysqli_query($db,$query1);

if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?seaname=$seaname&page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?seaname=$seaname&page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?seaname=$seaname&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?seaname=$seaname&page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=1'>1</a>";
				$paginate.= "<a href='$targetpage?seaname=$seaname&page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?seaname=$seaname&page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?seaname=$seaname&page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}

}

else
{
$query1 = "SELECT * FROM partmaster LIMIT $start, $limit";

// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?page=1'>1</a>";
				$paginate.= "<a href='$targetpage?page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?page=1'>1</a>";
				$paginate.= "<a href='$targetpage?page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}
}
?>








<body>
<center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='white'>


<table cellspacing=1 cellpadding=5 class=report>
<th>No <th>Group <th>Sub Group Name<th>Itemcode<th>ItemName<th>BuyPrice<th>SellPrice<th>Reduction<th>Opening<th>Current<th>Location<th>Partial Rebate<th>Update<th>Del
<?
$k=1;
$i=1;
//$res=mysqli_query($db,"select * from partmaster order by partcode limit 20");
$res= mysqli_query($db,$query1);
while($row=mysqli_fetch_array($res))
{
$query="select ndpcost,lstprice from spareprmast where partcode='$row[partcode]'";
$ex=mysqli_query($db,$query);
$rw=mysqli_fetch_array($ex);
$query1="select opstk,curstk,location from sparestock where itemcode='$row[partcode]'";
$ex1=mysqli_query($db,$query1);
$rw1=mysqli_fetch_array($ex1);
 print "<tr bgcolor='white' onMouseOver=bgColor='b8d7b4'; onMouseOut=bgColor='white'>";
 print "\n<form action='updprice.php' method='post'>";
 print "\n<input type=hidden name='part[$i]' value='$row[partcode]'>";
 print "\n<input type=hidden name='ndp[$i]' value='$rw[0]'>";
 print "\n<input type=hidden name='list[$i]' value='$rw[1]'>";
 print "\n<input type=hidden name='pname[$i]' value='$row[partname]'>";

$qry=mysqli_query($db,"select sum(qty) from invoice where org_item='$row[partname]' and brcode='$brcode'");
$qry1=mysqli_fetch_row($qry);


//$qty=$ror[qty]-$qry1[0];
print "<td>".$k++."</td><td>";


print "<select name='grp[$i]' id='$i' size=1 onchange='selsubgr(this.value,this.id)'>";
$grqry=mysqli_query($db,"select gname from groupmaster");

while($grst=mysqli_fetch_array($grqry))
{
?>
<option value="<? echo $grst[0] ?>"<? if($row[2]==$grst[0]) echo 'selected="selected"';?>><? echo $grst[0] ?></option>
<?
}

print "</select></td><td id='response$i'>";

print "<select name='subgp[$i]' id='subgp$i' size=1>";

$g=mysqli_query($db,"select subname from subgroupmaster where gname='$row[gname]'");

while($grow=mysqli_fetch_array($g))
{
?>
<option value="<? echo $grow[0] ?>"<? if($row[3]==$grow[0]) echo 'selected="selected"';?>><? echo $grow[0] ?></option>
<?
}

print "</select></td>";
 

 
 print "<td align=left>$row[partcode]</td><td align=left><input type=text name='partname[$i]' value='$row[partname]'></td><td align=left><input type=text name='ndp[$i]' value='$rw[0]' size=3></td><td><input type='text' name='list2[$i]' value='$rw[1]' size=3></td><td><input type='text' name='redct[$i]' value='$row[10]' size=3></td><td><input type=text name='op[$i]' value='$rw1[0]' size=3></td>
<td><input type=text name='curstk[$i]' value='$rw1[1]' readonly size=3></td>
<td><input type=text name='locat[$i]' value='$rw1[2]' size=3></td>
<td width=130px>";
print "<select name='rebtvl[]' id='$i' size=1 onchange='showrebtvl(this.value,this,this.id)'>";
?>
<option>-select-</option>
<option value="Yes"<? if($row[8]=='Yes') echo 'selected="selected"';?>>Yes</option>
<option value="No"<? if($row[8]=='No') echo 'selected="selected"';?>>No</option>
</select>
<input type=text id='rbtvl<?echo $i ?>' name='rbtvl[]' value='<? echo $row[9] ?>' <? if($row[8]=='Yes'){ ?>  <? } else { ?> style='visibility:hidden;'<? } ?> size=3>
<?
 print "</td>
<td><input type=submit value='Upd' name='upd' onClick=position_fix(".$i.")></td>
<td><input type=button value='Del' name='del' onClick=delfun('$row[partcode]')></td></tr>";

?>
<div id="strtxt"></div>
<script>
 function delfun(x)
 {
  if (confirm('do you want to delete '+x+' ?'))
	 {
		document.location='delitem.php?code='+x; 
	 }
 }
  
</script>
<?
$i++;
}

 print "<tr><th align='right' colspan=14><input type='hidden' name='pos' value='' id='pos'/>";
 print "<input type='hidden' name='pos1' value='$i' id='pos1'/>";
print "<input type='submit' value='update all' name='upd1'/></form></th></tr>";
?>
</table>
<div align=center>
<?php


 echo $total_pages.' Results';
 // pagination
 echo $paginate;	

?>
</div>
</td></tr></table>
<script>
function position_fix(str){
document.getElementById('pos').value=str;
}
</script>
<script type="text/javascript">
function showrebtvl(str,r,itid)
{	
	if(str=='Yes')
	{
		
 	document.getElementById("rbtvl"+itid).style.visibility = "visible";
	}
    else 
	{	
	document.getElementById("rbtvl"+itid).style.visibility ="hidden";       
	} 
}
</script>

<script type="text/javascript">
function selsubgr(str,suid)
{
   if(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('response'+suid).innerHTML=xmlhttp.responseText;
  //document.f1.batta.value=document.getElementById("bat1").value;


  


    }
  }

xmlhttp.open("GET","selectsubgrp1.php?grp="+str,true);
xmlhttp.send();

}


	
}
</script>
