<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../../opendb.php");
include("../../test.php");
?>
<link rel="stylesheet" href="../../style.css" type="text/css" />
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<h3>TELCON STOCK</H3>
<?
$tbl="serv".$user;
mysqli_query($db,"delete from $tbl");
$sql="create table $tbl (partcode varchar(100),partname varchar(100),qty double(14,2))";
mysqli_query($db,$sql);
$sql="select partcode,partname,sum(recqty) from sparetrinwartel group by partcode";
$re=mysqli_query($db,$sql) or die('err1'.mysqli_error($db));
while($row=mysqli_fetch_array($re))
{
 $sq="insert into $tbl values('$row[0]','$row[1]','$row[2]')";
 mysqli_query($db,$sq) or die('err2'.mysqli_error($db));
}

$sql="select partcode,partname,sum(recqty) from sparetroutwartel group by partcode";
$re1=mysqli_query($db,$sql) or die('err3'.mysqli_error($db));
while($row=mysqli_fetch_array($re1))
{
 $sq="update $tbl set qty=qty-'".($row[2])."' where partcode='$row[partcode]'";
 mysqli_query($db,$sq) or die('err4'.mysqli_error($db));
}

$re2=mysqli_query($db,"select * from $tbl order by partcode");
print "<table border=1 CELLPADDING=4><TH>Part Code <th>Part Name <th> Quantity";
while($row=mysqli_fetch_array($re2))
{
 print "<tr><td>$row[0]</td><td>$row[1]</td><td align=right>$row[2]</td></tr>";
}
?>
</table>
